UNPKG

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