Skip to content

Commit 20dac69

Browse files
committed
Add alphanum case and symbol encoders
1 parent 38b49f8 commit 20dac69

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/lib/encoder/alphaNumCase.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Knoxen
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
export default (uppercase = false): PuidEncoder => {
24+
const decimal = '0'.charCodeAt(0)
25+
const alpha = (uppercase ? 'A' : 'a').charCodeAt(0) - 10
26+
27+
return (n: number) => {
28+
if (n < 10) return n + decimal
29+
return n + alpha
30+
}
31+
}

src/lib/encoder/symbol.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Knoxen
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
export default (): PuidEncoder => {
24+
const s1 = '!'.charCodeAt(0)
25+
const s2 = '#'.charCodeAt(0) - 1
26+
const s3 = '('.charCodeAt(0) - 5
27+
const s4 = ':'.charCodeAt(0) - 13
28+
const s5 = '['.charCodeAt(0)
29+
const s6 = ']'.charCodeAt(0) - 21
30+
const s7 = '{'.charCodeAt(0) - 24
31+
32+
return (n: number) => {
33+
if (n === 0) return s1
34+
if (n < 5) return n + s2
35+
if (n < 13) return n + s3
36+
if (n < 20) return n + s4
37+
if (n === 20) return s5
38+
if (n < 24) return n + s6
39+
return n + s7
40+
}
41+
}

0 commit comments

Comments
 (0)