UNPKG

3.55 kBJavaScriptView Raw
1
2exports.lineEmitter = lineEmitter
3exports.uuid4 = uuid4
4exports.rand = rand
5exports.round = round
6exports.wait = wait
7exports.Storage = Storage
8
9exports.urlRe = /^([-.\da-z]+:)?\/\/(([\da-z.]*)(?::(\d+))?)(\/.*?)(\?.*?)?(#.*)?$/
10exports.domainRe = /^(?:(?:xn-|[a-z\d]+)(?:-[a-z\d]+)*(?:\.(?=.)|$)){2,}/i
11exports.ipv4Re = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?=.)|$)){4}$/
12exports.ipv6Re = /^((?=.*::)(([1-9a-f][\da-f]{0,3}|0|)(:(?=[^:])|::(?!.*::)|$)){1,8}|([\da-f]{1,4}(:(?=[^:])|$)){8})$/i
13exports.ip2int = ip2int
14exports.int2ip = int2ip
15exports.ipInNet = ipInNet
16
17// Usage:
18// var client = net.connect(soc)
19// .on("line", function(line) {})
20// lineEmitter(client)
21
22function lineEmitter(emitter, opts) {
23 opts = opts || {}
24
25 var leftover = ""
26 , separator = opts.separator || /\r?\n/
27 , emit = opts.emit || "line"
28 , listen = opts.listen || "data"
29 , end = opts.end || "end"
30
31 emitter
32 .on(listen, lineEmitterData)
33 .on(end, lineEmitterEnd)
34
35 function lineEmitterData(data){
36 var lines = (leftover + data).split(separator)
37
38 // keep the last partial line buffered
39 leftover = lines.pop()
40
41 for (var i = 0, len = lines.length; i < len; ) {
42 this.emit(emit, lines[i++], len)
43 }
44 }
45 function lineEmitterEnd() {
46 if (leftover) {
47 this.emit(emit, leftover)
48 }
49 leftover = ""
50 emitter
51 .removeListener(listen, lineEmitterData)
52 .removeListener(end, lineEmitterEnd)
53 }
54 return emitter
55}
56
57function uuid4(a, b) {
58 for (a = b = ""; a++ < 36; ) {
59 b += a*51&52 ? ( a^15 ? 8 ^ Math.random() * (a^20?16:4) : 4 ).toString(16) : "-";
60 }
61 return b
62}
63
64function rand(len) {
65 for (var out = ""; out.length < len; ) {
66 out += (Date.now() * Math.random()).toString(36).split(".")[0]
67 }
68 return out.slice(-len)
69}
70
71// Rounding Errors
72// Number((1.005).toFixed(2)); // 1 instead of 1.01
73// Math.round(1.005*100)/100; // 1 instead of 1.01
74// http://www.jacklmoore.com/notes/rounding-in-javascript/
75// The rounding problem can be avoided by using numbers represented in exponential notation:
76
77function round(value, decimals) {
78 return +(Math.round(value + "e" + decimals) + "e-" + decimals)
79}
80
81
82function wait(fn, _pending) {
83 var pending = _pending || 0
84 , result = [null]
85
86 if (typeof fn !== "function") throw TypeError("Not a function")
87
88 function resume() {
89 if (--pending === 0) {
90 fn.apply(this, result)
91 }
92 }
93 resume.wait = function(pos) {
94 ++pending
95 if (pos === void 0) pos = pending
96 return function(err, res) {
97 if (err) {
98 pending = 0
99 fn.call(this, err)
100 } else {
101 result[pos] = res
102 resume()
103 }
104 }
105 }
106 return resume
107}
108
109function ip2int(str) {
110 var t = str.split(".")
111 return ((t[0] << 24) | (t[1] << 16) | (t[2] << 8 ) | (t[3]))>>>0
112}
113
114function int2ip(i) {
115 return [i>>>24, (i>>>16)&255, (i>>>8)&255, i&255].join(".")
116}
117
118// var re = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(?=.)|$)){4}$/
119function ipInNet(ip, cidr) {
120 var junks = cidr.split("/")
121 , bits = junks[1] || 8 * junks[0].split(".").length
122 , mask = bits < 31 ? (-1 << (32 - bits)) >>> 0 : ip2int(bits)
123 //, netInt = (ip2int(cidr) & mask) >>> 0
124 //, size = 1 << (32 - bits)
125 //, last = netInt + size - 1
126
127 return (ip2int(ip) & mask) >>> 0 === (ip2int(junks[0]) & mask) >>> 0
128}
129
130function Storage() {
131 this.data = {}
132}
133
134Storage.prototype = {
135 get: function(key, next) {
136 var obj = this.data[key]
137 , err = obj ? null : "Item not found"
138 next(err, obj)
139 },
140 set: function(key, val, next) {
141 this.data[key] = val
142 next(null)
143 },
144 rename: function(key, newKey, next) {
145 this.data[newKey] = this.data[key]
146 delete this.data[key]
147 next(null)
148 }
149}
150
151
\No newline at end of file