This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uid.mjs
78 lines (76 loc) · 2.11 KB
/
uid.mjs
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
74
75
76
77
78
import { toString62, toStringN } from 'num2str';
let date = new Date();
let count = 0;
let uCount = -1;
function uid() {
return `${toString62(random())}${toString62(udate())}${toString62(udatecount())}`;
}
function udate() {
const now = new Date();
if (+date == +now) {
count++;
}
else {
count = 0;
date = now;
}
return new Date().getTime();
}
function udatecount() {
return count;
}
function random() {
return +Math.random().toString().substr(2);
}
function ucount() {
uCount++;
return uCount;
}
function chain() {
return {
random: () => local_chain([], random),
time: () => local_chain([], udate),
timecount: () => local_chain([], udatecount),
count: () => local_chain([], ucount),
add: fn => local_chain([], fn)
};
}
function local_chain(list, fn) {
function getNewList() {
return list.concat(fn);
}
return get_chain(getNewList, () => list);
}
function map_chain(list, cb) {
function getNewList() {
return list.map(cb);
}
return get_chain(getNewList, () => list);
}
function get_chain(getNewList, getList) {
return {
random: () => local_chain(getNewList(), random),
time: () => local_chain(getNewList(), udate),
timecount: () => local_chain(getNewList(), udatecount),
count: () => local_chain(getNewList(), ucount),
add: nfn => local_chain(getNewList(), nfn),
get: () => chain_get(getList()),
getJoin: c => chain_getJoin(getList(), c),
mapX62: () => map_chain(getNewList(), f => () => {
const sn = f();
return typeof sn === 'number' ? toString62(sn) : sn;
}),
mapXn: (r) => (map_chain(getNewList(), f => () => {
const sn = f();
return typeof sn === 'number' ? toStringN(sn, r) : sn;
})),
map: cb => map_chain(getNewList(), cb)
};
}
function chain_get(list) {
return () => list.map(fn => fn()).join('');
}
function chain_getJoin(list, c) {
return () => list.map(fn => fn()).join(c);
}
export { chain, random, ucount, udate, udatecount, uid };