+2
-2
src/lib/constants.ts
+2
-2
src/lib/constants.ts
+9
-9
src/lib/utils/encoding.ts
+9
-9
src/lib/utils/encoding.ts
···
5
import { SHORTCODE } from '$lib/constants';
6
7
/**
8
-
* Base70 characters used for encoding (0-9, a-z, A-Z, and special chars: +=_-?<>)
9
*/
10
-
const BASE_CHARS = SHORTCODE.BASE62_CHARS;
11
const BASE = BASE_CHARS.length;
12
13
/**
···
29
* Encodes a number to base string
30
* @param num - Number to encode
31
* @param length - Target length of the encoded string
32
-
* @returns Base70 encoded string
33
*/
34
function toBase(num: number, length: number): string {
35
let encoded = '';
···
41
}
42
43
/**
44
-
* Encodes a URL to a short base70 string
45
-
* Uses a deterministic hash-to-base70 encoding
46
*
47
* @param url - URL to encode
48
* @param length - Target length of the shortcode (default: 6)
49
-
* @returns Short base70 encoded string
50
*
51
* @example
52
* encodeUrl('https://github.com/user') // Returns something like 'a3k9zx'
···
57
}
58
59
/**
60
-
* Validates if a string is a valid base70 shortcode
61
* @param code - String to validate
62
* @returns True if the code contains only valid characters
63
*/
64
export function isValidShortcode(code: string): boolean {
65
-
return /^[0-9a-zA-Z+=_\-?<>]+$/.test(code);
66
}
67
68
/**
···
71
* @returns Number of possible combinations
72
*
73
* @example
74
-
* getMaxCombinations(6) // Returns 117649000000 (70^6)
75
*/
76
export function getMaxCombinations(length: number): number {
77
return Math.pow(BASE, length);
···
5
import { SHORTCODE } from '$lib/constants';
6
7
/**
8
+
* characters used for encoding (0-9, a-z, A-Z)
9
*/
10
+
const BASE_CHARS = SHORTCODE.CHARS;
11
const BASE = BASE_CHARS.length;
12
13
/**
···
29
* Encodes a number to base string
30
* @param num - Number to encode
31
* @param length - Target length of the encoded string
32
+
* @returns encoded string
33
*/
34
function toBase(num: number, length: number): string {
35
let encoded = '';
···
41
}
42
43
/**
44
+
* Encodes a URL to a short string
45
+
* Uses a deterministic hash encoding
46
*
47
* @param url - URL to encode
48
* @param length - Target length of the shortcode (default: 6)
49
+
* @returns Short encoded string
50
*
51
* @example
52
* encodeUrl('https://github.com/user') // Returns something like 'a3k9zx'
···
57
}
58
59
/**
60
+
* Validates if a string is a valid shortcode
61
* @param code - String to validate
62
* @returns True if the code contains only valid characters
63
*/
64
export function isValidShortcode(code: string): boolean {
65
+
return /^[0-9a-zA-Z]+$/.test(code);
66
}
67
68
/**
···
71
* @returns Number of possible combinations
72
*
73
* @example
74
+
* getMaxCombinations(6)
75
*/
76
export function getMaxCombinations(length: number): number {
77
return Math.pow(BASE, length);