Skip to content

Commit 8fb1c77

Browse files
Merge pull request #67 from wakidurrahman/feat/string-05
encode decode
2 parents 974a1d2 + 26925ed commit 8fb1c77

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const DICTIONARY01 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');
2+
3+
function decodeId(id: string) {
4+
const base: number = DICTIONARY01.length;
5+
let decoded: string | number = 0;
6+
7+
for (let i = 0; i < id.split('').length; i++) {
8+
decoded = decoded * base + DICTIONARY01.indexOf(id.charAt(i));
9+
}
10+
11+
return decoded;
12+
}
13+
14+
console.log(encodeId(11231230)); // prints 'VhU2'
15+
console.log(decodeId('VhU2')); // prints '11231230'

0 commit comments

Comments
 (0)