-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.js
210 lines (192 loc) · 5.34 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright Stanislas Polu
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* base.js
*
* This is a repository of helper functions available directly from
* the pipes library object. These functions help adding methods to
* Crockford style objects, and adds a few functionalities to the
* Function, Array and String objects
*
* WARNING: never add methods to the Object type as they would appear
* in enumerations. The pipes fwk rely on the NON-EXISTENCE of such
* extended method on the Object type.
*
*/
var crypto = require('crypto');
/**
* method(that, name, method, _super)
* Adds a method to the current object denoted by that and preserves
* _super implementation (see Crockford)
*/
exports.method = function(that, name, method, _super) {
if(_super) {
var m = that[name];
_super[name] = function() {
return m.apply(that, arguments);
};
}
that[name] = method;
};
/**
* getter(that, name, obj, prop)
* Generates a getter on the current object denoted by that
*/
exports.getter = function(that, name, obj, prop) {
var getter = function() {
return obj[prop];
};
that[name] = getter;
};
/**
* setter(that, name, obj, prop)
* Generates a setter on the current object denoted by that
*/
exports.setter = function(that, name, obj, prop) {
var setter = function (arg) {
obj[prop] = arg;
return that;
};
that['set' + name.substring(0, 1).toUpperCase() + name.substring(1)] = setter;
};
/**
* responds(that, name)
* Tests whether the object responds to a given function name
*/
exports.responds = function(that, name) {
return (that[name] && typeof that[name] === 'function');
};
/**
* once()
* Returns a function that will call the underlying function only once
* whether it is called once or multiple times
*/
Function.prototype.once = function() {
var fn = this;
var done = false;
return function() {
if(!done) {
args = Array.prototype.slice.call(arguments);
done = true;
fn.apply(null, args);
}
};
};
/**
* bind()
* The .bind method from Prototype.js
*/
Function.prototype.bind = function() {
var fn = this,
args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(
object,
args.concat(Array.prototype.slice.call(arguments))
);
};
};
/**
* remove(e)
* Removes the element e from the Array, using the JS '===' equality
*/
Array.prototype.remove = function(e) {
for(var i = 0; i < this.length; i++)
if(e === this[i]) this.splice(i, 1);
};
/**
* shallow(that)
* Shallow copy of the object that
*/
exports.shallow = function(that) {
if(that == null || typeof(that) != 'object')
return that;
var temp = new that.constructor();
for(var key in that) {
if(that.hasOwnProperty(key))
temp[key] = that[key];
}
return temp;
};
/**
* clone(that)
* Deep copy of the object that
*/
exports.clone = function(that) {
if(that == null || typeof(that) != 'object')
return this;
var temp = new that.constructor();
for(var key in that) {
if(that.hasOwnProperty(key))
temp[key] = clone(that[key]);
}
return temp;
};
/**
* makehash(that)
* Generates a 'deep' hash that ignores properties starting with a '_'
*/
exports.makehash = function(that) {
var hash = crypto.createHash('sha1');
for(var i in that) {
if(i.charAt(0) !== '_' && that.hasOwnProperty(i)) {
var str = JSON.stringify(that[i]);
if(str)
hash.update(str);
}
}
/** add args to update */
for(var j = 1; j < arguments.length; j++)
hash.update(arguments[j]);
return hash.digest(encoding='hex');
};
/**
* forEach(that, fun)
* Applies Array-like forEach to an object
*/
exports.forEach = function(that, fun /*, thisp */) {
"use strict";
if(that === void 0 || that === null)
throw new TypeError();
var t = Object(that);
if(typeof fun !== "function")
throw new TypeError();
var thisp = arguments[2];
for(var i in t) {
if(t.hasOwnProperty(i)) {
fun.call(thisp, t[i], i, t);
}
}
};
/**
* trim() ltrim() rtrim()
* String trim functions
*/
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
};