neopg 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/makeId.js ADDED
@@ -0,0 +1,202 @@
1
+ 'use strict'
2
+
3
+ const randstring = require('./randstring.js');
4
+
5
+ //2017-2-25 2050
6
+ let start_time = 1490390182066
7
+
8
+ let start_year = 2023
9
+
10
+ if ((new Date()).getFullYear() > 2085) {
11
+ //start_time = t.setFullYear(2045, 1, 25)
12
+ start_year = 2086
13
+ }
14
+
15
+ let loopch = [
16
+ "0","1","2","3","4","5","6","7","8","9",
17
+ "a","b","c","d","e","f","g","h","i","j",
18
+ "k","l","m","n","o","p","q","r","s","t",
19
+ "u","v","w","x","y","z"
20
+ ]
21
+
22
+ //不要在意一点点内存,用空间换时间
23
+ let msloopch = []
24
+ for (let x of loopch) {
25
+ for (let y of loopch) {
26
+ msloopch.push(x+y)
27
+ }
28
+ }
29
+
30
+ let sloopch = msloopch.slice(0, 100)
31
+ let yloopch = msloopch.slice(36, 500)
32
+
33
+ msloopch = msloopch.slice(parseInt(Math.random() * 100))
34
+
35
+ let loopLength = loopch.length
36
+ let sloopLength = sloopch.length
37
+ let yloopLength = yloopch.length
38
+
39
+ class Clocks {
40
+ constructor() {
41
+ this.clocks = {
42
+ y: 0,
43
+ m: 0,
44
+ d: 0
45
+ }
46
+
47
+ this.startYear = 2023
48
+ }
49
+
50
+ rand() {
51
+ for (let k in this.clocks) {
52
+ this.clocks[k] = ((loopLength * Math.random()) | 0)
53
+ }
54
+ }
55
+
56
+ getFullTime() {
57
+ return this.getTime() + this.getCharTime()
58
+ }
59
+
60
+ getTime() {
61
+ let t = new Date()
62
+ let year = t.getFullYear()
63
+ let month = t.getMonth()
64
+ let dat = t.getDate()
65
+ let hour = t.getHours()
66
+ let minute = t.getMinutes()
67
+ let seconds = t.getSeconds()
68
+ let ms = t.getMilliseconds()
69
+
70
+ let yind = year - this.startYear
71
+
72
+ if (yind < 0 || yind >= yloopLength) yind = 0
73
+
74
+ //bits: 2 + 1 + 1 + 1 + 2 + 2 + 2 = 11
75
+ return yloopch[yind] + loopch[month] + loopch[dat] + loopch[hour] + sloopch[minute] + sloopch[seconds] + msloopch[ms]
76
+ }
77
+
78
+ getCharTime() {
79
+ let str = loopch[this.clocks.y] + loopch[this.clocks.m] + loopch[this.clocks.d]
80
+
81
+ this.clocks.d++
82
+ if (this.clocks.d >= loopLength) {
83
+ this.clocks.d = 0
84
+ this.clocks.m++
85
+ if (this.clocks.m >= loopLength) {
86
+ this.clocks.m = 0
87
+ this.clocks.y++
88
+ if (this.clocks.y >= loopLength) {
89
+ this.clocks.y = 0
90
+ }
91
+ }
92
+ }
93
+
94
+ return str
95
+ }
96
+
97
+ }
98
+
99
+ function longId(idLen=18, idPre = '') {
100
+ let pstr = (Date.now() - start_time).toString(16)
101
+ let leng = pstr.length
102
+ if (idLen < 18) idLen = 18
103
+ return idPre + pstr + randstring(idLen - leng)
104
+ }
105
+
106
+ function makeId (idLen = 12, idPre = '') {
107
+ if (idLen > 17) {
108
+ return longId(idLen, idPre);
109
+ }
110
+
111
+ let tmstr = Math.random().toString(16).substring(2);
112
+
113
+ if (tmstr.length < idLen) {
114
+ tmstr = `${tmstr}${randstring(idLen - tmstr.length)}`;
115
+ }
116
+
117
+ if (tmstr.length > idLen) {
118
+ tmstr = tmstr.substring(tmstr.length - idLen);
119
+ }
120
+
121
+ if (idPre) {
122
+ return `${idPre}${tmstr}`;
123
+ }
124
+
125
+ return tmstr;
126
+ }
127
+
128
+ let b_year = 2**47
129
+ let b_month = 2**43
130
+ let b_date = 2**38
131
+ let b_hour = 2**33
132
+ let b_min = 2**27
133
+ let b_sec = 2**21
134
+ let b_msec = 2**11
135
+
136
+ let end_max = 4096
137
+
138
+ function numId (obj) {
139
+ let t = new Date()
140
+
141
+ let first_num = (t.getFullYear() - start_year) * b_year + (t.getMonth()+1) * b_month
142
+ + t.getDate() * b_date + t.getHours() * b_hour + t.getMinutes() * b_min
143
+ + t.getSeconds() * b_sec + t.getMilliseconds() * b_msec
144
+
145
+ let fnum = first_num + obj.endnum
146
+
147
+ obj.endnum++
148
+
149
+ if (obj.endnum >= end_max) {
150
+ obj.endnum = 0
151
+ }
152
+
153
+ return fnum
154
+ }
155
+
156
+ function bigId(obj, a='', b='') {
157
+ let fnum = numId(obj)
158
+ return (BigInt(fnum) * 1000n + BigInt(((Math.random() * 1000) | 0))).toString()
159
+ }
160
+
161
+ Object.defineProperty(makeId, 'numId', {
162
+ enumerable: false,
163
+ configurable: false,
164
+ get: function () {
165
+ let oo = {
166
+ endnum: (Math.random() * 2000) | 0
167
+ }
168
+
169
+ return numId.bind(null, oo)
170
+ }
171
+ })
172
+
173
+ Object.defineProperty(makeId, 'bigId', {
174
+ enumerable: false,
175
+ configurable: false,
176
+ get: function () {
177
+ let oo = {
178
+ endnum: (Math.random() * 2000) | 0
179
+ }
180
+
181
+ return bigId.bind(null, oo)
182
+ }
183
+ })
184
+
185
+ makeId.longId = longId
186
+
187
+ Object.defineProperty(makeId, 'serialId', {
188
+ enumerable: false,
189
+ configurable: false,
190
+ get: function () {
191
+ let _next = new Clocks()
192
+ _next.rand()
193
+
194
+ return function sid (idLen=16, idPre='') {
195
+ if (idLen < 14) return makeId(idLen, idPre)
196
+
197
+ return idPre + _next.getFullTime() + randstring(idLen - 14)
198
+ }
199
+ }
200
+ })
201
+
202
+ module.exports = makeId
@@ -0,0 +1,28 @@
1
+ 'use strict'
2
+
3
+ module.exports = function makeTimestamp(data, t) {
4
+ if (!t || !Array.isArray(t)) return false;
5
+ if (data[t[0]] !== undefined) return true;
6
+ if (!t[1]) return false;
7
+
8
+ if (typeof t[1] === 'function') {
9
+ data[t[0]] = t[1]()
10
+ return true
11
+ }
12
+
13
+ switch (t[1]) {
14
+ case 'bigint':
15
+ data[t[0]] = Date.now()
16
+ break
17
+
18
+ case 'int':
19
+ data[t[0]] = parseInt(Date.now() / 1000)
20
+ break
21
+
22
+ case 'timestamp':
23
+ data[t[0]] = (new Date()).toLocaleString().replaceAll('/', '-')
24
+ break
25
+ }
26
+
27
+ return true
28
+ }
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ let saltArr = [
4
+ 'a','b','c','d','e','f','g',
5
+ 'h','i','j','k','l','m','n',
6
+ 'o','p','q','r','s','t','u',
7
+ 'v','w','x','y','z','1','2',
8
+ '3','4','5','6','7','8','9'
9
+ ]
10
+
11
+ module.exports = (length = 8, sarr = null) => {
12
+ let saltstr = ''
13
+ let ind = 0
14
+
15
+ let arr = sarr || saltArr
16
+ let alen = arr.length
17
+
18
+ for(let i = 0; i < length; i++) {
19
+ saltstr += arr[(Math.random() * alen) | 0]
20
+ }
21
+
22
+ return saltstr
23
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "neopg",
3
+ "version": "0.0.0",
4
+ "description": "orm for postgres",
5
+ "keywords": [
6
+ "postgres",
7
+ "orm",
8
+ "database"
9
+ ],
10
+ "homepage": "https://github.com/master-genius/neopg#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/master-genius/neopg/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/master-genius/neopg.git"
17
+ },
18
+ "license": "ISC",
19
+ "author": "BraveWang",
20
+ "type": "commonjs",
21
+ "main": "index.js",
22
+ "directories": {
23
+ "lib": "lib"
24
+ },
25
+ "scripts": {
26
+ "test": "echo ok"
27
+ }
28
+ }
@@ -0,0 +1,78 @@
1
+ const size = 256
2
+ let buffer = Buffer.allocUnsafe(size)
3
+
4
+ const messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {
5
+ const v = x.charCodeAt(0)
6
+ acc[x] = () => {
7
+ buffer[0] = v
8
+ b.i = 5
9
+ return b
10
+ }
11
+ return acc
12
+ }, {})
13
+
14
+ const b = Object.assign(reset, messages, {
15
+ N: String.fromCharCode(0),
16
+ i: 0,
17
+ inc(x) {
18
+ b.i += x
19
+ return b
20
+ },
21
+ str(x) {
22
+ const length = Buffer.byteLength(x)
23
+ fit(length)
24
+ b.i += buffer.write(x, b.i, length, 'utf8')
25
+ return b
26
+ },
27
+ i16(x) {
28
+ fit(2)
29
+ buffer.writeUInt16BE(x, b.i)
30
+ b.i += 2
31
+ return b
32
+ },
33
+ i32(x, i) {
34
+ if (i || i === 0) {
35
+ buffer.writeUInt32BE(x, i)
36
+ return b
37
+ }
38
+ fit(4)
39
+ buffer.writeUInt32BE(x, b.i)
40
+ b.i += 4
41
+ return b
42
+ },
43
+ z(x) {
44
+ fit(x)
45
+ buffer.fill(0, b.i, b.i + x)
46
+ b.i += x
47
+ return b
48
+ },
49
+ raw(x) {
50
+ buffer = Buffer.concat([buffer.subarray(0, b.i), x])
51
+ b.i = buffer.length
52
+ return b
53
+ },
54
+ end(at = 1) {
55
+ buffer.writeUInt32BE(b.i - at, at)
56
+ const out = buffer.subarray(0, b.i)
57
+ b.i = 0
58
+ buffer = Buffer.allocUnsafe(size)
59
+ return out
60
+ }
61
+ })
62
+
63
+ module.exports = b
64
+
65
+ function fit(x) {
66
+ if (buffer.length - b.i < x) {
67
+ const prev = buffer
68
+ , length = prev.length
69
+
70
+ buffer = Buffer.allocUnsafe(length + (length >> 1) + x)
71
+ prev.copy(buffer)
72
+ }
73
+ }
74
+
75
+ function reset() {
76
+ b.i = 0
77
+ return b
78
+ }