-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
73 lines (65 loc) · 1.94 KB
/
mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const Numesis = require('numesis');
/**
* @type {string} DEFAULT_CHARSET The default charset for USID
*/
const DEFAULT_CHARSET = Numesis.DEFAULT;
/**
* @param min The minimum number
* @param max The maximum number
* @returns A number between min and max
*/
const randInt = (min, max) => parseInt(Math.random() * (max - min + 1) + min + "");
/**
* @param len The length of the string
* @param charset The charset for the string
* @returns A random string with len length
*/
const rand = (len = 3, charset = null) => {
charset = charset || DEFAULT_CHARSET;
const n = new Numesis(charset);
const c = n.charset.length;
const min = c ** (len - 1) - (len > 1 ? 0 : 1);
const max = c ** len - 1;
return n.e(randInt(min, max));
};
/**
* @param out_len The length of the output string
* @param real_len The length of the initial int value to be encoded
* @param charset The charset for the string
* @returns A random string with out_len length
*/
const uid = (out_len = 24, real_len = 18, charset = DEFAULT_CHARSET) => {
charset = charset || DEFAULT_CHARSET;
const n = new Numesis(charset);
let y = performance.now().toString();
y = (y.length < real_len ? y + "0".repeat(real_len - y.length - 1) + "1" : y).replace(".", "0");
let r = n.e(y);
return r.length < out_len ? r + rand(out_len - r.length) : r;
};
/**
*
* @param opts The options for the USID
* @returns A callback function that generates an index based ID
*/
const createIndex = (opts) => {
/**
* @return encoded latest index
*/
return () => {
let { get, set, charset, min } = opts;
min = min || 0;
charset = charset || DEFAULT_CHARSET;
const n = new Numesis(opts.charset);
const lastIndex = get();
const index = lastIndex + 1;
set(index);
return n.t(min, index);
};
};
module.exports = {
DEFAULT_CHARSET,
randInt,
rand,
uid,
createIndex
};