UNPKG

4.85 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(len) {
109 for (var out = ""; out.length < len; ) {
110 out += (Date.now() * Math.random()).toString(36).split(".")[0]
111 }
112 return out.slice(-len)
113}
114
115// Rounding Errors
116// Number((1.005).toFixed(2)); // 1 instead of 1.01
117// Math.round(1.005*100)/100; // 1 instead of 1.01
118// http://www.jacklmoore.com/notes/rounding-in-javascript/
119// The rounding problem can be avoided by using numbers represented in exponential notation:
120
121function round(value, decimals) {
122 return +(Math.round(value + "e" + decimals) + "e-" + decimals)
123}
124
125
126function wait(fn, _pending) {
127 var pending = _pending || 0
128 , result = [null]
129
130 if (typeof fn !== "function") throw TypeError("Not a function")
131
132 function resume() {
133 if (--pending === 0) {
134 fn.apply(this, result)
135 }
136 }
137 resume.wait = function(pos) {
138 ++pending
139 if (pos === void 0) pos = pending
140 return function(err, res) {
141 if (err) {
142 pending = 0
143 fn.call(this, err)
144 } else {
145 result[pos] = res
146 resume()
147 }
148 }
149 }
150 return resume
151}
152
153function ip2int(str) {
154 var t = str.split(".")
155 return ((t[0] << 24) | (t[1] << 16) | (t[2] << 8 ) | (t[3]))>>>0
156}
157
158function int2ip(i) {
159 return [i>>>24, (i>>>16)&255, (i>>>8)&255, i&255].join(".")
160}
161
162// var re = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(?=.)|$)){4}$/
163function ipInNet(ip, cidr) {
164 var junks = cidr.split("/")
165 , bits = junks[1] || 8 * junks[0].split(".").length
166 , mask = bits < 31 ? (-1 << (32 - bits)) >>> 0 : ip2int(bits)
167 //, netInt = (ip2int(cidr) & mask) >>> 0
168 //, size = 1 << (32 - bits)
169 //, last = netInt + size - 1
170
171 return (ip2int(ip) & mask) >>> 0 === (ip2int(junks[0]) & mask) >>> 0
172}
173
174function ip2buf(str) {
175 var arr = str.split(":", 8)
176 , len = arr.length
177 if (arr[len - 1].length > 4) {
178 return Buffer.from(arr[len - 1].split(".", 4))
179 }
180 for (; len--; ) {
181 arr[len] = (
182 arr[len] !== "" ?
183 ("0000" + arr[len]).slice(-4) :
184 "00000000000000000000000000000000".slice(-4*(9 - arr.length))
185 )
186 }
187 return Buffer.from(arr.join(""), "hex")
188}
189
190function buf2ip(buf) {
191 return buf.length === 4 ? buf.join(".") : buf.toString("hex").replace(/.{4}(?=.)/g, "$&:")
192}
193
194
\No newline at end of file