Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
/*
2
* index.js: Top-level include for the `utile` module.
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var fs = require('fs'),
10
path = require('path'),
11
util = require('util');
12
13
var utile = module.exports;
14
15
//
16
// Extend the `utile` object with all methods from the
17
// core node `util` methods.
18
//
19
// Remark: Somehow copying `util.inspect` makes the `utile`
20
// object `2`. See: https://github.com/joyent/node/issues/2225
21
//
22
Object.keys(util).forEach(function (key) {
23
if (key !== 'inspect') {
24
utile[key] = util[key];
25
}
26
});
27
28
//
29
// @async {Object}
30
// Simple wrapper to `require('async')`.
31
//
32
utile.__defineGetter__('async', function () {
33
return require('async');
34
});
35
36
//
37
// ### function mkdirp
38
// Simple wrapper to `require('mkdirp')`
39
//
40
utile.__defineGetter__('mkdirp', function () {
41
return require('mkdirp');
42
});
43
44
//
45
// ### function rimraf
46
// Simple wrapper to `require('rimraf')`
47
//
48
utile.__defineGetter__('rimraf', function () {
49
return require('rimraf');
50
});
51
52
//
53
// ### function cpr
54
// Simple wrapper to `require('ncp').ncp`
55
//
56
utile.__defineGetter__('cpr', function () {
57
return require('ncp').ncp;
58
});
59
60
//
61
// ### @file {Object}
62
// Lazy-loaded `file` module
63
//
64
utile.__defineGetter__('file', function () {
65
return require('./file');
66
});
67
68
//
69
// ### @base64 {Object}
70
// Lazy-loaded `base64` object
71
//
72
utile.__defineGetter__('base64', function () {
73
return require('./base64');
74
});
75
76
//
77
// ### function each (obj, iterator)
78
// #### @obj {Object} Object to iterate over
79
// #### @iterator {function} Continuation to use on each key. `function (value, key, object)`
80
// Iterate over the keys of an object.
81
//
82
utile.each = function (obj, iterator) {
83
Object.keys(obj).forEach(function (key) {
84
iterator(obj[key], key, obj);
85
});
86
};
87
88
//
89
// ### function find (o)
90
//
91
//
92
utile.find = function (obj, pred) {
93
var value, key;
94
95
for (key in obj) {
96
value = obj[key];
97
if (pred(value, key)) {
98
return value;
99
}
100
}
101
};
102
103
//
104
// ### function createPath (obj, path, value)
105
// ### @obj {Object} Object to insert value into
106
// ### @path {Array} List of nested keys to insert value at
107
// Retreives a value from given Object, `obj`, located at the
108
// nested keys, `path`.
109
//
110
utile.path = function (obj, path) {
111
var key, i;
112
113
for (i in path) {
114
if (typeof obj === 'undefined') {
115
return undefined;
116
}
117
118
key = path[i];
119
obj = obj[key];
120
}
121
122
return obj;
123
};
124
125
//
126
// ### function createPath (obj, path, value)
127
// ### @obj {Object} Object to insert value into
128
// ### @path {Array} List of nested keys to insert value at
129
// ### @value {*} Value to insert into the object.
130
// Inserts the `value` into the given Object, `obj`, creating
131
// any keys in `path` along the way if necessary.
132
//
133
utile.createPath = function (obj, path, value) {
134
var key, i;
135
136
for (i in path) {
137
key = path[i];
138
if (!obj[key]) {
139
obj[key] = ((+i + 1 === path.length) ? value : {});
140
}
141
142
obj = obj[key];
143
}
144
};
145
146
//
147
// ### function mixin (target [source0, source1, ...])
148
// Copies enumerable properties from `source0 ... sourceN`
149
// onto `target` and returns the resulting object.
150
//
151
utile.mixin = function (target) {
152
var objs = Array.prototype.slice.call(arguments, 1);
153
objs.forEach(function (o) {
154
Object.keys(o).forEach(function (attr) {
155
var getter = o.__lookupGetter__(attr);
156
if (!getter) {
157
target[attr] = o[attr];
158
}
159
else {
160
target.__defineGetter__(attr, getter);
161
}
162
});
163
});
164
165
return target;
166
};
167
168
//
169
// ### function capitalize (str)
170
// #### @str {string} String to capitalize
171
// Capitalizes the specified `str`.
172
//
173
utile.capitalize = function (str) {
174
return str && str[0].toUpperCase() + str.slice(1);
175
};
176
177
//
178
// ### function randomString (length)
179
// #### @length {integer} The number of bits for the random base64 string returned to contain
180
// randomString returns a pseude-random ASCII string (subset)
181
// the return value is a string of length ⌈bits/6⌉ of characters
182
// from the base64 alphabet.
183
//
184
utile.randomString = function (length) {
185
var chars, rand, i, ret, mod, bits;
186
187
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
188
ret = '';
189
// standard 4
190
mod = 4;
191
// default is 16
192
bits = length * mod || 64;
193
194
// in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
195
while (bits > 0) {
196
// 32-bit integer
197
rand = Math.floor(Math.random() * 0x100000000);
198
//we use the top bits
199
for (i = 26; i > 0 && bits > 0; i -= mod, bits -= mod) {
200
ret += chars[0x3F & rand >>> i];
201
}
202
}
203
204
return ret;
205
};
206
207
//
208
// ### function filter (object, test)
209
// #### @obj {Object} Object to iterate over
210
// #### @pred {function} Predicate applied to each property. `function (value, key, object)`
211
// Returns an object with properties from `obj` which satisfy
212
// the predicate `pred`
213
//
214
utile.filter = function (obj, pred) {
215
var copy = Array.isArray(obj) ? [] : {};
216
utile.each(obj, function (val, key) {
217
if (pred(val, key, obj)) {
218
copy[key] = val;
219
}
220
});
221
222
return copy;
223
};
224
225
//
226
// ### function requireDir (directory)
227
// #### @directory {string} Directory to require
228
// Requires all files and directories from `directory`, returning an object
229
// with keys being filenames (without trailing `.js`) and respective values
230
// being return values of `require(filename)`.
231
//
232
utile.requireDir = function (directory) {
233
var result = {},
234
files = fs.readdirSync(directory);
235
236
files.forEach(function (file) {
237
if (file.substr(-3) == '.js') {
238
file = file.substr(0, file.length - 3);
239
}
240
result[file] = require(path.resolve(directory, file));
241
});
242
return result;
243
};
244
245
//
246
// ### function requireDirLazy (directory)
247
// #### @directory {string} Directory to require
248
// Lazily requires all files and directories from `directory`, returning an
249
// object with keys being filenames (without trailing `.js`) and respective
250
// values (getters) being return values of `require(filename)`.
251
//
252
utile.requireDirLazy = function (directory) {
253
var result = {},
254
files = fs.readdirSync(directory);
255
256
files.forEach(function (file) {
257
if (file.substr(-3) == '.js') {
258
file = file.substr(0, file.length - 3);
259
}
260
result.__defineGetter__(file, function () {
261
return require(path.resolve(directory, file));
262
});
263
});
264
return result;
265
};
266
267
//
268
// ### function clone (object)
269
// #### @object {Object} Object to clone
270
// Shallow clones the specified object.
271
//
272
utile.clone = function (object) {
273
return Object.keys(object).reduce(function (obj, k) {
274
obj[k] = object[k];
275
return obj;
276
}, {});
277
};
278
279