UNPKG

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