Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / utile / lib / index.js
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 Object.keys(util).forEach(function (key) {
20   utile[key] = util[key];
21 });
22
23 Object.defineProperties(utile, {
24
25   //
26   // ### function async
27   // Simple wrapper to `require('async')`.
28   //
29   'async': {
30     get: function() {
31       return utile.async = require('async');
32     }
33   },
34
35   //
36   // ### function inflect
37   // Simple wrapper to `require('i')`.
38   //
39   'inflect': {
40     get: function() {
41       return utile.inflect = require('i')();
42     }
43   },
44
45   //
46   // ### function mkdirp
47   // Simple wrapper to `require('mkdirp')`
48   //
49   'mkdirp': {
50     get: function() {
51       return utile.mkdirp = require('mkdirp');
52     }
53   },
54
55   //
56   // ### function deepEqual
57   // Simple wrapper to `require('deep-equal')`
58   // Remark: deepEqual is 4x faster then using assert.deepEqual
59   //         see: https://gist.github.com/2790507
60   //
61   'deepEqual': {
62     get: function() {
63       return utile.deepEqual = require('deep-equal');
64     }
65   },
66
67   //
68   // ### function rimraf
69   // Simple wrapper to `require('rimraf')`
70   //
71   'rimraf': {
72     get: function() {
73       return utile.rimraf = require('rimraf');
74     }
75   },
76
77   //
78   // ### function cpr
79   // Simple wrapper to `require('ncp').ncp`
80   //
81   'cpr': {
82     get: function() {
83       return utile.cpr = require('ncp').ncp;
84     }
85   },
86
87   //
88   // ### @file {Object}
89   // Lazy-loaded `file` module
90   //
91   'file': {
92     get: function() {
93       return utile.file = require('./file');
94     }
95   },
96
97   //
98   // ### @args {Object}
99   // Lazy-loaded `args` module
100   //
101   'args': {
102     get: function() {
103       return utile.args = require('./args');
104     }
105   },
106
107   //
108   // ### @base64 {Object}
109   // Lazy-loaded `base64` object
110   //
111   'base64': {
112     get: function() {
113       return utile.base64 = require('./base64');
114     }
115   },
116
117   //
118   // ### @format {Object}
119   // Lazy-loaded `format` object
120   //
121   'format': {
122     get: function() {
123       return utile.format = require('./format');
124     }
125   }
126
127 });
128
129
130 //
131 // ### function rargs(_args)
132 // #### _args {Arguments} Original function arguments
133 //
134 // Top-level method will accept a javascript "arguments" object
135 // (the actual keyword "arguments" inside any scope) and return
136 // back an Array.
137 //
138 utile.rargs = function (_args, slice) {
139   if (!slice) {
140     slice = 0;
141   }
142
143   var len = (_args || []).length,
144       args = new Array(len - slice),
145       i;
146
147   //
148   // Convert the raw `_args` to a proper Array.
149   //
150   for (i = slice; i < len; i++) {
151     args[i - slice] = _args[i];
152   }
153
154   return args;
155 };
156
157 //
158 // ### function each (obj, iterator)
159 // #### @obj {Object} Object to iterate over
160 // #### @iterator {function} Continuation to use on each key. `function (value, key, object)`
161 // Iterate over the keys of an object.
162 //
163 utile.each = function (obj, iterator) {
164   Object.keys(obj).forEach(function (key) {
165     iterator(obj[key], key, obj);
166   });
167 };
168
169 //
170 // ### function find (o)
171 //
172 //
173 utile.find = function (obj, pred) {
174   var value, key;
175
176   for (key in obj) {
177     value = obj[key];
178     if (pred(value, key)) {
179       return value;
180     }
181   }
182 };
183
184 //
185 // ### function pad (str, len, chr)
186 // ### @str {String} String to pad
187 // ### @len {Number} Number of chars to pad str with
188 // ### @chr {String} Optional replacement character, defaults to empty space
189 // Appends chr to str until it reaches a length of len
190 //
191 utile.pad = function pad(str, len, chr) {
192   var s;
193   if (!chr) {
194     chr = ' ';
195   }
196   str = str || '';
197   s = str;
198   if (str.length < len) {
199     for (var i = 0; i < (len - str.length); i++) {
200       s += chr;
201     }
202   }
203   return s;
204 }
205
206 //
207 // ### function path (obj, path, value)
208 // ### @obj {Object} Object to insert value into
209 // ### @path {Array} List of nested keys to insert value at
210 // Retreives a value from given Object, `obj`, located at the
211 // nested keys, `path`.
212 //
213 utile.path = function (obj, path) {
214   var key, i;
215
216   for (i in path) {
217     if (typeof obj === 'undefined') {
218       return undefined;
219     }
220
221     key = path[i];
222     obj = obj[key];
223   }
224
225   return obj;
226 };
227
228 //
229 // ### function createPath (obj, path, value)
230 // ### @obj {Object} Object to insert value into
231 // ### @path {Array} List of nested keys to insert value at
232 // ### @value {*} Value to insert into the object.
233 // Inserts the `value` into the given Object, `obj`, creating
234 // any keys in `path` along the way if necessary.
235 //
236 utile.createPath = function (obj, path, value) {
237   var key, i;
238
239   for (i in path) {
240     key = path[i];
241     if (!obj[key]) {
242       obj[key] = ((+i + 1 === path.length) ? value : {});
243     }
244
245     obj = obj[key];
246   }
247 };
248
249 //
250 // ### function mixin (target [source0, source1, ...])
251 // Copies enumerable properties from `source0 ... sourceN`
252 // onto `target` and returns the resulting object.
253 //
254 utile.mixin = function (target) {
255   utile.rargs(arguments, 1).forEach(function (o) {
256     Object.getOwnPropertyNames(o).forEach(function(attr) {
257       var getter = Object.getOwnPropertyDescriptor(o, attr).get,
258           setter = Object.getOwnPropertyDescriptor(o, attr).set;
259
260       if (!getter && !setter) {
261         target[attr] = o[attr];
262       }
263       else {
264         Object.defineProperty(target, attr, {
265           get: getter,
266           set: setter
267         });
268       }
269     });
270   });
271
272   return target;
273 };
274
275
276 //
277 // ### function capitalize (str)
278 // #### @str {string} String to capitalize
279 // Capitalizes the specified `str`.
280 //
281 utile.capitalize = utile.inflect.camelize;
282
283 //
284 // ### function escapeRegExp (str)
285 // #### @str {string} String to be escaped
286 // Escape string for use in Javascript regex
287 //
288 utile.escapeRegExp = function (str) {
289   return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
290 };
291
292 //
293 // ### function randomString (length)
294 // #### @length {integer} The number of bits for the random base64 string returned to contain
295 // randomString returns a pseude-random ASCII string (subset)
296 // the return value is a string of length ⌈bits/6⌉ of characters
297 // from the base64 alphabet.
298 //
299 utile.randomString = function (length) {
300   var chars, rand, i, ret, mod, bits;
301
302   chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
303   ret = '';
304   // standard 4
305   mod = 4;
306   // default is 16
307   bits = length * mod || 64;
308
309   // in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
310   while (bits > 0) {
311     // 32-bit integer
312     rand = Math.floor(Math.random() * 0x100000000);
313     //we use the top bits
314     for (i = 26; i > 0 && bits > 0; i -= mod, bits -= mod) {
315       ret += chars[0x3F & rand >>> i];
316     }
317   }
318
319   return ret;
320 };
321
322 //
323 // ### function filter (object, test)
324 // #### @obj {Object} Object to iterate over
325 // #### @pred {function} Predicate applied to each property. `function (value, key, object)`
326 // Returns an object with properties from `obj` which satisfy
327 // the predicate `pred`
328 //
329 utile.filter = function (obj, pred) {
330   var copy;
331   if (Array.isArray(obj)) {
332     copy = [];
333     utile.each(obj, function (val, key) {
334       if (pred(val, key, obj)) {
335         copy.push(val);
336       }
337     });
338   }
339   else {
340     copy = {};
341     utile.each(obj, function (val, key) {
342       if (pred(val, key, obj)) {
343         copy[key] = val;
344       }
345     });
346   }
347   return copy;
348 };
349
350 //
351 // ### function requireDir (directory)
352 // #### @directory {string} Directory to require
353 // Requires all files and directories from `directory`, returning an object
354 // with keys being filenames (without trailing `.js`) and respective values
355 // being return values of `require(filename)`.
356 //
357 utile.requireDir = function (directory) {
358   var result = {},
359       files = fs.readdirSync(directory);
360
361   files.forEach(function (file) {
362     if (file.substr(-3) === '.js') {
363       file = file.substr(0, file.length - 3);
364     }
365     result[file] = require(path.resolve(directory, file));
366   });
367   return result;
368 };
369
370 //
371 // ### function requireDirLazy (directory)
372 // #### @directory {string} Directory to require
373 // Lazily requires all files and directories from `directory`, returning an
374 // object with keys being filenames (without trailing `.js`) and respective
375 // values (getters) being return values of `require(filename)`.
376 //
377 utile.requireDirLazy = function (directory) {
378   var result = {},
379       files = fs.readdirSync(directory);
380
381   files.forEach(function (file) {
382     if (file.substr(-3) === '.js') {
383       file = file.substr(0, file.length - 3);
384     }
385     Object.defineProperty(result, file, {
386       get: function() {
387         return result[file] = require(path.resolve(directory, file));
388       }
389     });
390   });
391   
392   return result;
393 };
394
395 //
396 // ### function clone (object, filter)
397 // #### @object {Object} Object to clone
398 // #### @filter {Function} Filter to be used
399 // Shallow clones the specified object.
400 //
401 utile.clone = function (object, filter) {
402   return Object.keys(object).reduce(filter ? function (obj, k) {
403     if (filter(k)) obj[k] = object[k];
404     return obj;
405   } : function (obj, k) {
406     obj[k] = object[k];
407     return obj;
408   }, {});
409 };
410
411 //
412 // ### function camelToUnderscore (obj)
413 // #### @obj {Object} Object to convert keys on.
414 // Converts all keys of the type `keyName` to `key_name` on the
415 // specified `obj`.
416 //
417 utile.camelToUnderscore = function (obj) {
418   if (typeof obj !== 'object' || obj === null) {
419     return obj;
420   }
421
422   if (Array.isArray(obj)) {
423     obj.forEach(utile.camelToUnderscore);
424     return obj;
425   }
426
427   Object.keys(obj).forEach(function (key) {
428     var k = utile.inflect.underscore(key);
429     if (k !== key) {
430       obj[k] = obj[key];
431       delete obj[key];
432       key = k;
433     }
434     utile.camelToUnderscore(obj[key]);
435   });
436
437   return obj;
438 };
439
440 //
441 // ### function underscoreToCamel (obj)
442 // #### @obj {Object} Object to convert keys on.
443 // Converts all keys of the type `key_name` to `keyName` on the
444 // specified `obj`.
445 //
446 utile.underscoreToCamel = function (obj) {
447   if (typeof obj !== 'object' || obj === null) {
448     return obj;
449   }
450
451   if (Array.isArray(obj)) {
452     obj.forEach(utile.underscoreToCamel);
453     return obj;
454   }
455
456   Object.keys(obj).forEach(function (key) {
457     var k = utile.inflect.camelize(key, false);
458     if (k !== key) {
459       obj[k] = obj[key];
460       delete obj[key];
461       key = k;
462     }
463     utile.underscoreToCamel(obj[key]);
464   });
465
466   return obj;
467 };