
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
A set of useful functions for node.js
Use npm to install this module:
npm install --save utily
Now you can import it into your project:
var utily = require('utily');
This is just setTimeout but with the arguments reverted (first the delay time, then the callback fn function).
utily.delay(1000, function()
{
console.log('Hello after 1 second!!');
});
Iterate over an array or an object.
items: array or object you want to iterate.fn: function that will be called with each item of the items array or object with the following arguments:
key: the property name if items is an object, or the index if items is an array.value: the property value if items is an object, or the value if items is an array.//Iterate over an array
utily.each([1, 2, 3], function(index, value)
{
//Display in console
console.log(index + ' -> ' + value);
//Continue with the next item in the array
return true;
});
// 0 -> 1
// 1 -> 2
// 2 -> 3
//Iterate over an object
utily.each({ 'key1': 'value1', 'key2': 'value2' }, function(key, value)
{
//Display in console
console.log(key + ' -> ' + value);
//Continue with the next item in the array
return true;
});
// key1 -> value1
// key2 -> value2
You can break the loop at a particular item if you return a false boolean in your iterator function.
utily.each([1, 2, 3, 4, 5], function(index, value)
{
//Display in console
console.log(index + ' -> ' + value);
//Check the value
if(value >= 3){ return false; }
});
// 0 -> 1
// 1 -> 2
// 2 -> 3
Asynchronous version of utily.each. Iterate over an array or an object and execute the callback when the iteration is finished.
fn function will be called with the same arguments as the utily.each method and with a next function, that indicates that the iteration is done and can continue with the next item.callback function will be called when the iteration is finished.You can break the iteration by calling the next function with a value or an error object.
//List of files
var files = ['./file1.txt', './file2.txt', './file3.txt'];
//Read all the files
utily.eachAsync(file, function(index, value, next)
{
//Read the file content
return fs.readFile(value, function(error, data)
{
//If something went wrong -> stop the iteration
if(error){ return next(error); }
//Display the file content in console
console.log('Content of file ' + index);
console.log(data);
//Next file in the list
return next();
});
}, function(error)
{
//Check the error
if(error){ return console.log(error.message); }
//Display done
console.log('All files processed!');
});
Clone an array. Note that if objects exist in the array this method does not do perform a deep clone of the content.
var array_cloned = utily.array.clone([1, 2, 3, 4]);
Returns true if item exists in array, false if does not.
utily.array.has([1, 2, 3, 4], 2); // -> true
utily.array.has([1, 2, 3, 4], 5); // -> false
Returns the maximum value in array.
utily.array.max([1, 2, 3, 4, 5]); // -> 5
Returns the minimum value in array.
utily.array.min([1, 2, 3, 4, 5]); // -> 1
Returns a new array with values starting in start to end (included). You can specify the distance between each number in the sequence by providing a step value. Default step value is 1.
utily.array.range(0, 5); // -> [0, 1, 2, 3, 4, 5]
utily.array.range(0, 4, 2); // -> [0, 2, 4]
Removes a specific item of the array array. This method also modifies the original array.
var list = [ 'bananas', 'oranges', 'apples' ];
//Remove an element
utily.array.remove(list, 'oranges'); // -> list = [ 'bananas', 'apples' ];
Generate the checksum of file. options can be an object with the following options:
algorithm: a string with the algorithm to generate the checksum. Default is md5.encoding: a string with the encoding. Default is hex.If options is a non-object, it will be treated as the options.algorithm option.
//Generate the md5 of the file
utily.fs.checksum('/path/to/file.txt', function(error, sum)
{
//Check the error
if(error){ /* Something went wrong */ }
console.log('Checksum --> ' + sum);
});
Copy a source file to destination.
utily.fs.copy('/my/source/file.txt', '/my/destination/file.txt', function(error)
{
//Check the error
if(error){ /* Something went wrong */ }
});
Check if the provided path exists, and then the cb method will be executed with two arguments (error and a boolean exists that indicates if the path exists).
utily.fs.exists('/path/to/my/file.txt', function(error, exists)
{
//Check the error
if(error)
{
//Something went wrong...
}
//Check if the file exists
if(exists === true)
{
//File exists
}
else
{
//File does not exists...
}
});
Check if the provided path exists and is a directory or not.
utily.fs.isDir('/path/to/my/directory', function(error, is_dir)
{
//Check the error
if(error){ /* Something went wrong */ }
//Check if is a directory
if(is_dir === true)
{
//Path exists and is a directory
}
else
{
//Path does not exists ot is not a directory
}
});
Check if the provided path exists and is a file or not.
utily.fs.isDir('/path/to/my/file.txt', function(error, is_file)
{
//Check the error
if(error){ /* Something went wrong */ }
//Check if is a file
if(is_file === true)
{
//Path exists and is a file
}
else
{
//Path does not exists or is not a file
}
});
Create a folder and all the parent folders of path.
//Create the folder and all the parent folders (if does not exists)
utily.fs.mkdir('/path/to/my/directory', function(error)
{
//Check the error
if(error){ /* Something went wrong */ }
console.log('Directory created!');
});
Returns the size of the file. The callback function will be executed with an error object and the size of the file.
//Get the size of the file
utily.fs.size('/path/to/file.txt', function(error, size)
{
//Check the error
if(error)
{
//Something went wrong...
}
console.log('The size of the file is: ' + size);
});
Reads the content of a directory. The callback method gets two arguments, error and files, where files is an array with the real path of each file in the provided directory.
//Content of directory /test/fake/directory:
// - index.js
// - package.json
// - license.txt
//Read the content of a directory
return utily.fs.readdit('/test/fake/directory', function(error, files)
{
//Check the error
if(error){ /* display error */ }
//Work with the list of files
console.log(files);
// files = [
// '/test/fake/directory/index.js',
// '/test/fake/directory/package.json', '
// '/test/fake/directory/license.txt']
});
Remove a file or list of files. The files argument must be a string for a single file, or an array with the file paths to remove.
Note: this method does not throw an error if the path does not exists.
//Remove multiple files
utily.fs.unlink([ './file1.txt', './file2.txt' ], function(error)
{
//Check if there is an error
if(error)
{
//Something went wrong...
}
});
//Remove a single file
utily.fs.unlink('./another-file.txt', function(error)
{
//Check if there is an error
if(error)
{
//Something went wrong...
}
});
A set of functions to check the type of a given value.
Return true if value is undefined.
var a;
var b = 'Hello';
utily.is.undefined(a); // -> true, `a` is not defined
utily.is.undefined(b); // -> false, `b` is a string
Return true if value is a string.
utily.is.string('Hello world'); // -> true
utily.is.string(''); // -> true
utily.is.string({ a: 'Hello' }); // -> false
Return true if value is an object.
utily.is.object({}); // -> true
utily.is.object(null); // -> false
Return true if value is an array.
utily.is.array([ 1, 2, 3 ]); // -> true
utily.is.array({ a: true }); // -> false
Return true if value is a stream.
utily.is.stream(fs.createReadStream('file.txt')); // -> true
utily.is.stream({ }); // -> false
Return true if value is null.
utily.is.null(null); // -> true
utily.is.null({ }); // -> false
Return true if value is an integer number.
utily.is.integer(45); // -> true
utily.is.integer(2.5); // -> false
Return true if value is a number;
utily.is.number(1235.2); // --> true
utily.is.number('1234'); // -> false
Return true if value is a boolean.
utily.is.boolean(true); // -> true
utily.is.boolean(0); // -> false
Return true if value is a function.
utily.is.function(function(){ return 0; }); // -> true
Return true if value is a buffer.
utily.is.buffer(new Buffer(10)); // -> true
Return true if value is a regular expression.
utily.is.regexp(/\s/g); // -> true
Read a JSON file and convert it's content to a JSON object using JSON.parse method.
The options object will be passed to fs.readFile.
//Read a JSON file
utily.json.read('/my/file.json', 'utf8', function(error, data)
{
//Check the error
if(error)
{
//Something went wrong
}
//Print the JSON object in console
console.log(data);
});
Converts a JSON object to string using the JSON.stringify and then it will be written to the provided file path.
The options object will be passed to fs.writeFile.
//Initialize my object
var obj = { key1: 'value1', key2: 'value2' };
//Write to a file
utily.json.write('/my/file.json', obj, 'utf8', function(error)
{
//Check the error
if(error)
{
//Something went wrong
}
});
Execute fn with each pair key - value in obj.
var obj = { key1: 'value1', key2: 'value2', key3: 'value3' };
utily.object.each(obj, function(key, value)
{
//Display in console
console.log(key + ' -> ' + value);
});
//Output in console:
// key1 -> value1
// key2 -> value2
// key3 -> value3
This is just Object.keys.
var keys = utily.object.keys({ a: 1, b: 2, c: 'hello' }); // --> keys = [ 'a', 'b', 'c' ]
Sort an array with objects by the provided keys and with the provided order (default is ASC order).
var list = [];
list.push({ name: 'Susan', age: 35 });
list.push({ name: 'Boby', age: 28 });
list.push({ name: 'Andy', age: 24 });
list.push({ name: 'Sarah', age: 29 });
//Sort the list
utily.object.sort(list, 'name', 'ASC');
//Print the array
console.log(list);
// [ { name: 'Andy', age: 24 },
// { name: 'Boby', age: 28 },
// { name: 'Sarah', age: 29 },
// { name: 'Susan', age: 35 } ]
Returns an array of a given object's own enumerable property values. It's a ponyfill of the Object.values method.
var values = utily.object.values({ a: 1, b: 2, c: 'hello' }); // -> values = [ 1, 2, 'hello' ]
Replace all handlebars expressions from str with values of obj.
utily.string.format('My car is {{ color }}!', { color: 'blue' }); // --> "My car is blue!"
Return true if str is an empty string.
utily.string.is_empty(''); // -> true
utily.string.is_empty(' '); // -> false
Return true if str is a string in lowercase format.
utily.string.is_lowercase('hello world'); // -> true
utily.string.is_lowercase('Hello World'); // -> false
Return true if str is a string in uppercase format;
utily.string.is_uppercase('HELLO WORLD'); // -> true
utily.string.is_uppercase('Hello World'); // -> false
Return the camel-case format of str.
utily.string.camel_case('hello world'); // -> 'helloWorld'
Return the capitalized format of str.
utily.string.capitalize('hello world'); // -> 'Hello world'
Generate a unique random string of 15 characters.
var str = utily.string.unique(); // -> str = 'wv1ufiqj5e6xd3k'
Under the MIT LICENSE.
FAQs
A set of useful functions for node.js
We found that utily demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.