@leyyo/mask-id
v1.0.2
Published
ID mask generator
Readme
Mask ID
ID masker or ID converter component for JavaScript/TypeScript
- If you have sequenced/incremental id (ie: auto-incremented DB ids), it is very useful converter
- It generates domain based id converter to hide them
Install
npm i @leyyo/mask-id
Samples
// file: id-generators.ts
import {maskId} from "@leyyo/mask-id";
// encoded user id should be min length of 10
export const userIdMask = maskId.integer('0246813579', 10);
// encoded product id does not have any padding
export const productIdMask = maskId.integer('1357902468', 1);// file: everywhere
import {userIdMask, productIdMask} from "./id-generators";
userIdMask.encode(567) // 'IkXaMogElw'
productIdMask.encode(567) // 'Ps'
// Samples with different, see samples below
[0, 99, 567, 1209, 45678, 903421, 6753410, 34509123].forEach(id => {
// look samples code
}); 1-1 Integer to Integer Masker
Create integer id generator, it encodes given integer to another integer
- Pattern should be shuffled numeric alphabet,
[ 0...9 ]
export const userIdMask = maskId.integer('5026417839', 10); // with padding 10
export const productIdMask = maskId.integer('2135964870', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ '5067510371' │ 0 │ '2' │ 0 │
│ 1 │ 99 │ '9904369746' │ 99 │ '00' │ 99 │
│ 2 │ 567 │ '8404369746' │ 567 │ '891' │ 567 │
│ 3 │ 1209 │ '9543286538' │ 1209 │ '0297' │ 1209 │
│ 4 │ 45678 │ '3742510371' │ 45678 │ '74932' │ 45678 │
│ 5 │ 903421 │ '0457545814' │ 903421 │ '192429' │ 903421 │
│ 6 │ 6753410 │ '5234618420' │ 6753410 │ '2379568' │ 6753410 │
│ 7 │ 34509123 │ '6790111162' │ 34509123 │ '54016666' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Hex Masker
Create hex id generator, it encodes given integer to a hex value
- Pattern should be shuffled hex alphabet,
[ 0...9 ] + [ a...f ]
export const userIdMask = maskId.hex('4f90a72365bd1e8c', 10); // with padding 10
export const productIdMask = maskId.hex('8cb01fea364d9752', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ '4f02bc71ae' │ 0 │ '8' │ 0 │
│ 1 │ 99 │ '0b1c0687e2' │ 99 │ '04' │ 99 │
│ 2 │ 567 │ '3dc92df645' │ 567 │ 'ad2' │ 567 │
│ 3 │ 1209 │ '57d893ea17' │ 1209 │ '6fd' │ 1209 │
│ 4 │ 45678 │ '8753d42e78' │ 45678 │ '5f6a' │ 45678 │
│ 5 │ 903421 │ 'ee323195fb' │ 903421 │ '77aea' │ 903421 │
│ 6 │ 6753410 │ '9d51396c34' │ 6753410 │ 'bd69ab' │ 6753410 │
│ 7 │ 34509123 │ '06d3582e78' │ 34509123 │ '03da65e' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to UUID Masker
Create integer id generator, it encodes given integer to another integer
- Pattern should be shuffled numeric alphabet,
[ 0...9 ]
export const userIdMask = maskId.uuid('1f389dc46705a2eb'); // noHypen: false, with hypen
export const productIdMask = maskId.uuid('d7b9c45a8e612f30', true); // noHypen: true, without hypen┌─────────┬──────────┬────────────────────────────────────────┬──────────┬────────────────────────────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼────────────────────────────────────────┼──────────┼────────────────────────────────────┼────────────┤
│ 0 │ 0 │ '00000000-0000-0000-0000-000000000000' │ 0 │ '00000000000000000000000000000000' │ 0 │
│ 1 │ 99 │ '80ab86ed-2c15-493f-f394-51c2de68ba07' │ 99 │ '96209834f5d1acb77bca1d5f4389026e' │ 99 │
│ 2 │ 567 │ '45b3c5f6-178e-04d9-9d40-e8716f5c3b2a' │ 567 │ 'a10b5178de936a4cc4a639ed8715b0f2' │ 567 │
│ 3 │ 1209 │ '7d5e3429-adb0-c8f1-1f8c-0bda9243e576' │ 1209 │ 'e413bafc2406597dd7956042cfab31e8' │ 1209 │
│ 4 │ 45678 │ 'ed7451c2-de68-ba07-70ab-86ed2c15493f' │ 45678 │ '34ea1d5f4389026ee6209834f5d1acb7' │ 45678 │
│ 5 │ 903421 │ '224c4a37-f09b-56cd-dc65-b90f73a481e2' │ 903421 │ 'ffa5a2be76c0185445810c67eb2a9d3f' │ 903421 │
│ 6 │ 6753410 │ '357a436b-410d-fea5-5aef-d014b6327c98' │ 6753410 │ 'b1e2ab80ad647321123746da08bfe5c9' │ 6753410 │
│ 7 │ 34509123 │ '86547ec2-de68-ba07-70ab-86ed2c15493f' │ 34509123 │ '981ae35f4389026ee6209834f5d1acb7' │ 34509123 │
└─────────┴──────────┴────────────────────────────────────────┴──────────┴────────────────────────────────────┴────────────┘1-1 Integer to Lower-Case Letters Masker
Create lower case latter id generator, it encodes given integer to a word value which contains only lower case letters
- Pattern should be shuffled lower case alphabet,
[ a...z ]
export const userIdMask = maskId.lower('rcmjagsipdbuhonvqxzywletfk', 10); // with padding 10
export const productIdMask = maskId.lower('agcxmlnedhzkotusbpvyiwrjqf', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ 'rcjsbvlmby' │ 0 │ 'a' │ 0 │
│ 1 │ 99 │ 'lkcapoyrpx' │ 99 │ 'wf' │ 99 │
│ 2 │ 567 │ 'lxyerguzrd' │ 567 │ 'wp' │ 567 │
│ 3 │ 1209 │ 'opunztjbzc' │ 1209 │ 'tdk' │ 1209 │
│ 4 │ 45678 │ 'eumiuqejuw' │ 45678 │ 'rkce' │ 45678 │
│ 5 │ 903421 │ 'kbefjpnljh' │ 903421 │ 'fzrqx' │ 903421 │
│ 6 │ 6753410 │ 'nljrztjbzc' │ 6753410 │ 'uwxav' │ 6753410 │
│ 7 │ 34509123 │ 'kkucmdveao' │ 34509123 │ 'ffkgch' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Upper-Case Letters Masker
Create upper case latter id generator, it encodes given integer to a word value which contains only upper case letters
- Pattern should be shuffled upper case alphabet,
[ A...Z ]
export const userIdMask = maskId.upper('HMTYKQFCEAWGXLNDPBOZURVSJI', 10); // with padding 10
export const productIdMask = maskId.upper('OZLRFUXWVNJSDIGEYBMTCKHPQA', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ 'HMYFWDRTWZ' │ 0 │ 'O' │ 0 │
│ 1 │ 99 │ 'RIMKELZHEB' │ 99 │ 'KA' │ 99 │
│ 2 │ 567 │ 'RBZVHQGOHA' │ 567 │ 'KB' │ 567 │
│ 3 │ 1209 │ 'LEGNOSYWOM' │ 1209 │ 'IVS' │ 1209 │
│ 4 │ 45678 │ 'VGTCGPVYGU' │ 45678 │ 'HSLW' │ 45678 │
│ 5 │ 903421 │ 'IWVJYENRYX' │ 903421 │ 'AJHQR' │ 903421 │
│ 6 │ 6753410 │ 'NRYHOSYWOM' │ 6753410 │ 'GKROM' │ 6753410 │
│ 7 │ 34509123 │ 'IIGMTADVKL' │ 34509123 │ 'AASZLN' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Letters Masker
Create letter id generator, it encodes given integer to a word value which contains only letters
- Pattern should be shuffled letter alphabet,
[ a...z ] + [ A...Z ]
export const userIdMask = maskId.letter('YjqpZPkGXuBaVszMKHQJovRniWgyrcAdeENhFfOLClbUtxTImDwS', 10); // with padding 10
export const productIdMask = maskId.letter('tbFIXNsfmcEMaHkAnvhBURzTerpKyYVQGJoOiSxqwuLdlWZPDCgj', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ 'YjpkBMvrFx' │ 0 │ 't' │ 0 │
│ 1 │ 99 │ 'IDSqkaHiel' │ 99 │ 'PC' │ 99 │
│ 2 │ 567 │ 'IkXaMogElw' │ 567 │ 'Ps' │ 567 │
│ 3 │ 1209 │ 'sfLbTSPVoc' │ 1209 │ 'HS' │ 1209 │
│ 4 │ 45678 │ 'RHhObIjXKW' │ 45678 │ 'zvO' │ 45678 │
│ 5 │ 903421 │ 'WdpVKvyNbS' │ 903421 │ 'rQIa' │ 903421 │
│ 6 │ 6753410 │ 'ztITwpuKiE' │ 6753410 │ 'klPZ' │ 6753410 │
│ 7 │ 34509123 │ 'SVFiefUwkM' │ 34509123 │ 'jaieG' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Base-36 Characters Masker
Create base 36 id generator, it encodes given integer to a word value which contains only numbers and lower case letters
- Pattern should be shuffled base 36 alphabet,
[ 0...9 ] + [ a...z ]
export const userIdMask = maskId.base36('z7iho2jl1g3wum4tder9xya5fvsb60kpnqc8', 10); // with padding 10
export const productIdMask = maskId.base36('845kchwbnjfspdiz30mo6qevu9yxg7ra1t2l', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ 'z7hj3ty6zg' │ 0 │ '8' │ 0 │
│ 1 │ 99 │ 'bkn8h14y0i' │ 99 │ 'xr' │ 99 │
│ 2 │ 567 │ 'blgudybcjt' │ 567 │ 'xb' │ 567 │
│ 3 │ 1209 │ 'y9yf6qh3rb' │ 1209 │ 'qo' │ 1209 │
│ 4 │ 45678 │ 'kholwda073' │ 45678 │ 'rkc' │ 45678 │
│ 5 │ 903421 │ '72xj3ty6zg' │ 903421 │ '4h6w' │ 903421 │
│ 6 │ 6753410 │ 'svex6qh3rb' │ 6753410 │ 'y906g' │ 6753410 │
│ 7 │ 34509123 │ 'bjpe23d5po' │ 34509123 │ 'xwa0h' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Base-36 Characters Masker
Create base 62 id generator, it encodes given integer to a word value which contains only numbers and letters
- Pattern should be shuffled base 62 alphabet,
[ 0...9 ] + [ a...z ] + [ A...Z ]
export const userIdMask = maskId.base62('FrEJBMHLo90QGuzv7RsCYf2qSWkOex4yw8Uita1PhgX3K6d5ZDIVcpAlmTnjNb', 10); // with padding 10
export const productIdMask = maskId.base62('Atu1pkgz2FSqTGbhJf47mMvPxcesQ9nNZjLOUWaXKr6BHR3IdV0DYCyoEi5lw8', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ 'FrJH0vfet6' │ 0 │ 'A' │ 0 │
│ 1 │ 99 │ 'aPgKZpjBGf' │ 99 │ 'WX' │ 99 │
│ 2 │ 567 │ '9CfSe8PdAr' │ 567 │ 'F7' │ 567 │
│ 3 │ 1209 │ 'yVpmNJ97S8' │ 1209 │ 'ND' │ 1209 │
│ 4 │ 45678 │ 'dPcljEovqw' │ 45678 │ '3XY' │ 45678 │
│ 5 │ 903421 │ 'Cf07YWy1dl' │ 903421 │ '7MSJ' │ 903421 │
│ 6 │ 6753410 │ 'NcG35cnJQY' │ 6753410 │ 'wYTB' │ 6753410 │
│ 7 │ 34509123 │ '50b2e8PdAr' │ 34509123 │ 'IS8vQ' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘1-1 Integer to Custom Alphabet Masker
Create custom id generator, it encodes given integer to a word value which contains only characters between from and len in base 62 pattern
- Pattern should be shuffled alphabet,
[ 0...9 ] + [ a...z ] + [ A...Z ]- Sample below indicates that Consonants letters and odd numbers as a use case, nobody may need it :)
export const userIdMask = maskId.custom('20R6SMNB8VTLFZCWYGDHJ4KXPQ', 10); // with padding 10
export const productIdMask = maskId.custom('Z0NVFM68YD4TLKRQJPH2SGXWCB', 1); // without padding┌─────────┬──────────┬──────────────┬──────────┬────────────┬────────────┐
│ (index) │ id │ encUser │ decUser │ encProduct │ decProduct │
├─────────┼──────────┼──────────────┼──────────┼────────────┼────────────┤
│ 0 │ 0 │ '206NTW4RTH' │ 0 │ 'Z' │ 0 │
│ 1 │ 99 │ '4Q0S8ZH28G' │ 99 │ 'GB' │ 99 │
│ 2 │ 567 │ '4GHK2MLD2V' │ 567 │ 'GP' │ 567 │
│ 3 │ 1209 │ 'Z8LCDX6TD0' │ 1209 │ 'KYT' │ 1209 │
│ 4 │ 45678 │ 'KLRBLYK6LJ' │ 45678 │ 'XTN8' │ 45678 │
│ 5 │ 903421 │ 'QTKP68C46F' │ 903421 │ 'B4XCV' │ 903421 │
│ 6 │ 6753410 │ 'C462DX6TD0' │ 6753410 │ 'RGVZH' │ 6753410 │
│ 7 │ 34509123 │ 'QQL0RVWKSZ' │ 34509123 │ 'BBT0ND' │ 34509123 │
└─────────┴──────────┴──────────────┴──────────┴────────────┴────────────┘Exceptions
- MaskIdError
Possible error codes and messages
| Code | Message |
|--------------------|-------------------------------------------------------------|
| empty-pattern | Empty pattern is not acceptable |
| invalid-pattern | Pattern has invalid type: {type}, it should be string |
| duplicated-chars | Pattern has duplicated characters: {characters} |
| unwanted-chars | Pattern has unwanted characters: {characters} |
| pattern-length | Pattern length should be {expected}, but it's {current} |
| min-length | Minimum length should be between [ 1, {max} ] |
| invalid-length | Minimum has invalid type {type}, it should be number |
| length-range | Length should be an integer between 1 and 62 |
| ------------------ | --------------------------------------------------------- |
Standards
- [x] Language:
TS - [x] Eslint:
Yes - [x] Static Code Analysis:
YesIntelliJ Code Inspections - [x] DDD - Document Driven:
NoNo required - [x] EDD - Exception Driven:
Yes - [x] TDD - Test Driven:
Yes
Commands
npm run clear// clears "dist" foldernpm run lint// runs eslint for static code analysisnpm run build// builds JS files at "dist" foldernpm run test// runs test files in "test" foldernpm run test:watch*// runs test with watch optionnpm run test:coverage*// runs test with coverage
Author
Date2024-12-10NameMustafa YelmerRepogithub.com/leyyonet/mask-id
