Comparing version 0.0.6 to 0.0.8
157
lib/index.js
@@ -9,2 +9,5 @@ /* | ||
var fs = require('fs'), | ||
path = require('path'); | ||
var utile = exports; | ||
@@ -45,10 +48,18 @@ | ||
// | ||
// ### @read {Object} | ||
// Lazy-loaded `read` module | ||
// ### @file {Object} | ||
// Lazy-loaded `file` module | ||
// | ||
utile.__defineGetter__('read', function () { | ||
return require('./read'); | ||
utile.__defineGetter__('file', function () { | ||
return require('./file'); | ||
}); | ||
// | ||
// ### @base64 {Object} | ||
// Lazy-loaded `base64` object | ||
// | ||
utile.__defineGetter__('base64', function () { | ||
return require('./base64'); | ||
}); | ||
// | ||
// ### function each (obj, iterator) | ||
@@ -65,35 +76,59 @@ // #### @obj {Object} Object to iterate over | ||
exports.find = function (object, iterator) { | ||
for( var key in object) { | ||
var value = object[key]; | ||
if(iterator(value, key)) | ||
// | ||
// ### function find (o) | ||
// | ||
// | ||
exports.find = function (obj, pred) { | ||
var value, key; | ||
for (key in obj) { | ||
value = obj[key]; | ||
if (pred(value, key)) { | ||
return value; | ||
} | ||
} | ||
}; | ||
// get an item from deep down inside an object | ||
exports.path = function (object, path) { | ||
for (var i in path) { | ||
//== is intended here. do not change to === | ||
if(object == null) return undefined; | ||
var key = path[i]; | ||
object = object[key]; | ||
// | ||
// ### function createPath (obj, path, value) | ||
// ### @obj {Object} Object to insert value into | ||
// ### @path {Array} List of nested keys to insert value at | ||
// Retreives a value from given Object, `obj`, located at the | ||
// nested keys, `path`. | ||
// | ||
utile.path = function (obj, path) { | ||
var key, i; | ||
for (i in path) { | ||
if (typeof obj === 'undefined') { | ||
return undefined; | ||
} | ||
key = path[i]; | ||
obj = obj[key]; | ||
} | ||
return object; | ||
return obj; | ||
}; | ||
// | ||
// given an object, set value at path, creating objects if necessary. | ||
// ### function createPath (obj, path, value) | ||
// ### @obj {Object} Object to insert value into | ||
// ### @path {Array} List of nested keys to insert value at | ||
// ### @value {*} Value to insert into the object. | ||
// Inserts the `value` into the given Object, `obj`, creating | ||
// any keys in `path` along the way if necessary. | ||
// | ||
exports.createPath = function (object, path, value) { | ||
utile.createPath = function (obj, path, value) { | ||
var key, i; | ||
for (var i in path) { | ||
var key = path[i]; | ||
if(!object[key]) object[key] = ((+i + 1 == path.length) ? value : {}); | ||
object = object[key]; | ||
for (i in path) { | ||
key = path[i]; | ||
if (!obj[key]) { | ||
obj[key] = ((+i + 1 === path.length) ? value : {}); | ||
} | ||
obj = obj[key]; | ||
} | ||
} | ||
}; | ||
@@ -171,38 +206,59 @@ // | ||
// #### @obj {Object} Object to iterate over | ||
// #### @test {function} test applied to each property. `function (value, key, object)` | ||
// filters the properties that `test` returns true on. | ||
// #### @pred {function} Predicate applied to each property. `function (value, key, object)` | ||
// Returns an object with properties from `obj` which satisfy | ||
// the predicate `pred` | ||
// | ||
exports.filter = function (obj, test) { | ||
var n = Array.isArray(obj) ? [] : {}; | ||
exports.each(obj, function (v, k) { | ||
if (test(v, k, obj)) { | ||
n[k] = v; | ||
exports.filter = function (obj, pred) { | ||
var copy = Array.isArray(obj) ? [] : {}; | ||
exports.each(obj, function (val, key) { | ||
if (pred(val, key, obj)) { | ||
copy[key] = val; | ||
} | ||
}); | ||
return n; | ||
return copy; | ||
}; | ||
// | ||
// readJSONFile | ||
// ### function requireDir (directory) | ||
// #### @directory {string} Directory to require | ||
// Requires all files and directories from `directory`, returning an object | ||
// with keys being filenames (without trailing `.js`) and respective values | ||
// being return values of `require(filename)`. | ||
// | ||
exports.requireDir = function (directory) { | ||
var result = {}, | ||
files = fs.readdirSync(directory); | ||
exports.readJSONFile = function (file, callback) { | ||
if ('function' != typeof callback) | ||
throw new Error('readJSONFile needs a callback'); | ||
fs.readFile(file, 'utf-8', function (err, json) { | ||
if (err) | ||
return callback (err); | ||
var obj; | ||
try { | ||
obj = JSON.parse(json); | ||
} catch (err) { | ||
return callback(err); | ||
files.forEach(function (file) { | ||
if (file.substr(-3) == '.js') { | ||
file = file.substr(0, file.length - 3); | ||
} | ||
callback(null, obj); | ||
result[file] = require(path.resolve(directory, file)); | ||
}); | ||
return result; | ||
}; | ||
// | ||
// ### function requireDirLazy (directory) | ||
// #### @directory {string} Directory to require | ||
// Lazily requires all files and directories from `directory`, returning an | ||
// object with keys being filenames (without trailing `.js`) and respective | ||
// values (getters) being return values of `require(filename)`. | ||
// | ||
exports.requireDirLazy = function (directory) { | ||
var result = {}, | ||
files = fs.readdirSync(directory); | ||
files.forEach(function (file) { | ||
if (file.substr(-3) == '.js') { | ||
file = file.substr(0, file.length - 3); | ||
} | ||
result.__defineGetter__(file, function () { | ||
return require(path.resolve(directory, file)); | ||
}); | ||
}); | ||
return result; | ||
}; | ||
// | ||
@@ -212,2 +268,3 @@ // Extend the `utile` object with all methods from the | ||
// | ||
utile.mixin(utile, require('util')); | ||
utile.mixin(utile, require('util')); | ||
{ | ||
"name": "utile", | ||
"description": "A drop-in replacement for `util` with some additional advantageous functions", | ||
"version": "0.0.6", | ||
"version": "0.0.8", | ||
"author": "Nodejitsu Inc <info@nodejitsu.com>", | ||
"contributors": [ | ||
{ "name": "Charlie Robbins", "email": "charlie@nodejitsu.com" }, | ||
{ "name": "Dominic Tarr", "email": "dominic@nodejitsu.com" } | ||
{ "name": "Dominic Tarr", "email": "dominic@nodejitsu.com" }, | ||
{ "name": "Maciej Malecki", "email": "maciej@nodejitsu.com" } | ||
], | ||
@@ -23,4 +24,7 @@ "repository": { | ||
}, | ||
"scripts": { | ||
"test": "vows --spec --isolate" | ||
}, | ||
"main": "./lib/index", | ||
"engines": { "node": ">= 0.4.0" } | ||
} |
@@ -1,2 +0,2 @@ | ||
# utile | ||
# utile [![Build Status](https://secure.travis-ci.org/flatiron/utile.png)](http://travis-ci.org/flatiron/utile) | ||
@@ -26,6 +26,6 @@ A drop-in replacement for `util` with some additional advantageous functions | ||
utile.async utile.capitalize utile.clone utile.cpr utile.debug | ||
utile.each utile.error utile.exec utile.inherits utile.inspect | ||
utile.log utile.mixin utile.mkdirp utile.p utile.print | ||
utile.pump utile.puts utile.randomString utile.read utile.rimraf | ||
utile.async utile.capitalize utile.clone utile.cpr utile.createPath utile.debug | ||
utile.each utile.error utile.exec utile.file utile.filter utile.find | ||
utile.inherits utile.inspect utile.log utile.mixin utile.mkdirp utile.p | ||
utile.path utile.print utile.pump utile.puts utile.randomString utile.rimraf | ||
``` | ||
@@ -81,2 +81,2 @@ | ||
[4]: https://vowsjs.org | ||
[5]: https://npmjs.or | ||
[5]: https://npmjs.or |
@@ -20,3 +20,3 @@ /* | ||
assert.isString(random); | ||
assert.length(random, 16); | ||
assert.lengthOf(random, 16); | ||
assert.notEqual(random, utile.randomString()); | ||
@@ -30,7 +30,7 @@ }, | ||
assert.isArray(strings); | ||
assert.length(strings,2); | ||
assert.lengthOf(strings,2); | ||
assert.isString(strings[0]); | ||
assert.isString(strings[1]); | ||
assert.length(strings[0], 4); | ||
assert.length(strings[1], 128); | ||
assert.lengthOf(strings[0], 4); | ||
assert.lengthOf(strings[1], 128); | ||
assert.notEqual(strings[0], strings[1].substr(0,4)); | ||
@@ -37,0 +37,0 @@ } |
/* | ||
* common-test.js: Tests for common utility functions in broadway. | ||
* utile-test.js: Tests for `utile` module. | ||
* | ||
@@ -56,4 +56,5 @@ * (C) 2011, Nodejitsu Inc. | ||
"the createPath() method": function () { | ||
var x = {} | ||
, r = Math.random() | ||
var x = {}, | ||
r = Math.random(); | ||
utile.createPath(x, ['a','b','c'], r) | ||
@@ -63,2 +64,3 @@ assert.equal(x.a.b.c, r) | ||
} | ||
}).export(module); | ||
}).export(module); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18775
16
489
81
4