UNPKG

4.91 kBJavaScriptView Raw
1
2exports.deepAssign = deepAssign
3exports.lineEmitter = lineEmitter
4exports.nop = nop
5exports.num = num
6exports.rand = rand
7exports.round = round
8exports.uuid4 = uuid4
9exports.wait = wait
10
11exports.urlRe = /^([-.\da-z]+:)?\/\/(([\da-z.]*)(?::(\d+))?)(\/.*?)(\?.*?)?(#.*)?$/
12exports.domainRe = /^(?:(?:xn-|[a-z\d]+)(?:-[a-z\d]+)*(?:\.(?=.)|$)){2,}/i
13exports.ipv4Re = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?=.)|$)){4}$/
14exports.ipv6Re = /^((?=.*::)(([1-9a-f][\da-f]{0,3}|0|)(:(?=[^:])|::(?!.*::)|$)){1,8}|([\da-f]{1,4}(:(?=[^:])|$)){8})$/i
15exports.buf2ip = buf2ip
16exports.int2ip = int2ip
17exports.ip2buf = ip2buf
18exports.ip2int = ip2int
19exports.ipInNet = ipInNet
20
21var numRe = /^(-?\d+\.?\d*) *([kMGTP]i?|sec|min|hr|day|week|month|year|).?$/
22, numMap = {
23 "": 1,
24 sec: 1e3, min: 6e4, hr: 36e5, day: 864e5, week: 6048e5, month: 2629742400, year: 31556908800,
25 k: 1e3, M: 1e6, G: 1e9, T: 1e12, P: 1e15,
26 ki: 1024,
27 Mi: 1048576,
28 Gi: 1073741824,
29 Ti: 1099511627776,
30 Pi: 1125899906842624
31}
32, hasOwn = numMap.hasOwnProperty
33
34function deepAssign(to) {
35 if (to !== Object.prototype) for (var key, from, a = arguments, i = 1, len = a.length; i < len; ) {
36 if (from = a[i++]) for (key in from) if (hasOwn.call(from, key)) {
37 if (from[key] === null) delete to[key]
38 else to[key] = (
39 from[key] && from[key].constructor === Object ?
40 deepAssign(to[key] && to[key].constructor === Object ? to[key] : {}, from[key]) :
41 from[key]
42 )
43 }
44 }
45 return to
46}
47
48// Usage:
49// var client = net.connect(soc)
50// .on("line", function(line) {})
51// lineEmitter(client)
52
53function lineEmitter(emitter, opts) {
54 opts = opts || {}
55
56 var leftover = ""
57 , separator = opts.separator || /\r?\n/
58 , emit = opts.emit || "line"
59 , listen = opts.listen || "data"
60 , end = opts.end || "end"
61
62 emitter
63 .on(listen, lineEmitterData)
64 .on(end, lineEmitterEnd)
65
66 function lineEmitterData(data){
67 var lines = (leftover + data).split(separator)
68
69 // keep the last partial line buffered
70 leftover = lines.pop()
71
72 for (var i = 0, len = lines.length; i < len; ) {
73 this.emit(emit, lines[i++], len)
74 }
75 }
76 function lineEmitterEnd() {
77 if (leftover) {
78 this.emit(emit, leftover)
79 }
80 leftover = ""
81 emitter
82 .removeListener(listen, lineEmitterData)
83 .removeListener(end, lineEmitterEnd)
84 }
85 return emitter
86}
87
88function nop() {}
89
90function num(a, b, c) {
91 var tmp
92 return (
93 typeof a === "number" && a === a ? a :
94 typeof a === "string" && (tmp = numRe.exec(a)) ? tmp[1] * numMap[tmp[2]] :
95 typeof b === "number" && b === b ? b :
96 typeof b === "string" && (tmp = numRe.exec(b)) ? tmp[1] * numMap[tmp[2]] :
97 c
98 )
99}
100
101function uuid4(a, b) {
102 for (a = b = ""; a++ < 36; ) {
103 b += a*51&52 ? ( a^15 ? 8 ^ Math.random() * (a^20?16:4) : 4 ).toString(16) : "-";
104 }
105 return b
106}
107
108function rand(min, max) {
109 if (typeof max === "number") return Math.random() * (max - min) + min
110 for (var out = ""; out.length < min; ) {
111 out += Math.random().toString(36).slice(2)
112 }
113 return out.slice(-min)
114}
115
116// Rounding Errors
117// Number((1.005).toFixed(2)); // 1 instead of 1.01
118// Math.round(1.005*100)/100; // 1 instead of 1.01
119// http://www.jacklmoore.com/notes/rounding-in-javascript/
120// The rounding problem can be avoided by using numbers represented in exponential notation:
121
122function round(value, decimals) {
123 return +(Math.round(value + "e" + decimals) + "e-" + decimals)
124}
125
126
127function wait(fn, _pending) {
128 var pending = _pending || 0
129 , result = [null]
130
131 if (typeof fn !== "function") throw TypeError("Not a function")
132
133 function resume() {
134 if (--pending === 0) {
135 fn.apply(this, result)
136 }
137 }
138 resume.wait = function(pos) {
139 ++pending
140 if (pos === void 0) pos = pending
141 return function(err, res) {
142 if (err) {
143 pending = 0
144 fn.call(this, err)
145 } else {
146 result[pos] = res
147 resume()
148 }
149 }
150 }
151 return resume
152}
153
154function ip2int(str) {
155 var t = str.split(".")
156 return ((t[0] << 24) | (t[1] << 16) | (t[2] << 8 ) | (t[3]))>>>0
157}
158
159function int2ip(i) {
160 return [i>>>24, (i>>>16)&255, (i>>>8)&255, i&255].join(".")
161}
162
163// var re = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(?=.)|$)){4}$/
164function ipInNet(ip, cidr) {
165 var junks = cidr.split("/")
166 , bits = junks[1] || 8 * junks[0].split(".").length
167 , mask = bits < 31 ? (-1 << (32 - bits)) >>> 0 : ip2int(bits)
168 //, netInt = (ip2int(cidr) & mask) >>> 0
169 //, size = 1 << (32 - bits)
170 //, last = netInt + size - 1
171
172 return (ip2int(ip) & mask) >>> 0 === (ip2int(junks[0]) & mask) >>> 0
173}
174
175function ip2buf(str) {
176 var arr = str.split(":", 8)
177 , len = arr.length
178 if (arr[len - 1].length > 4) {
179 return Buffer.from(arr[len - 1].split(".", 4))
180 }
181 for (; len--; ) {
182 arr[len] = (
183 arr[len] !== "" ?
184 ("0000" + arr[len]).slice(-4) :
185 "00000000000000000000000000000000".slice(-4*(9 - arr.length))
186 )
187 }
188 return Buffer.from(arr.join(""), "hex")
189}
190
191function buf2ip(buf) {
192 return buf.length === 4 ? buf.join(".") : buf.toString("hex").replace(/.{4}(?=.)/g, "$&:")
193}
194
195
\No newline at end of file