utily
A set of useful functions for node.js
Installation
Use npm to install this module:
npm install --save utily
Now you can import it into your project:
var utily = ;
API
Common methods
utily.delay(time, fn)
This is just setTimeout
but with the arguments reverted (first the delay time
, then the callback fn
function).
utily;
utily.each(items, fn)
Iterate over an array
or an object
.
items
:array
orobject
you want to iterate.fn
: function that will be called with each item of theitems
array or object with the following arguments:key
: the property name ifitems
is an object, or the index ifitems
is an array.value
: the property value ifitems
is an object, or the value ifitems
is an array.
//Iterate over an array utily;// 0 -> 1// 1 -> 2// 2 -> 3 //Iterate over an object utily;// key1 -> value1// key2 -> value2
You can break the loop at a particular item if you return a false
boolean in your iterator function.
utily;// 0 -> 1// 1 -> 2// 2 -> 3
utily.eachAsync(items, fn, callback)
Asynchronous version of utily.each
. Iterate over an array
or an object
and execute the callback
when the iteration is finished.
- The
fn
function will be called with the same arguments as theutily.each
method and with anext
function, that indicates that the iteration is done and can continue with the next item. - The
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 filesutily;
Array functions
utily.array.clone(array)
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 = utilyarray;
utily.array.has(array, item)
Returns true
if item
exists in array
, false
if does not.
utilyarray; // -> trueutilyarray; // -> false
utily.array.max(array)
Returns the maximum value in array
.
utilyarray; // -> 5
utily.array.min(array)
Returns the minimum value in array
.
utilyarray; // -> 1
utily.array.range(start, end[, step])
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
.
utilyarray; // -> [0, 1, 2, 3, 4, 5]utilyarray; // -> [0, 2, 4]
utily.array.remove(array, item)
Removes a specific item
of the array array
. This method also modifies the original array.
var list = 'bananas' 'oranges' 'apples' ; //Remove an element utilyarray; // -> list = [ 'bananas', 'apples' ];
File System functions
utily.fs.checksum(file[, options], cb)
Generate the checksum of file
. options
can be an object with the following options:
algorithm
: astring
with the algorithm to generate the checksum. Default ismd5
.encoding
: astring
with the encoding. Default ishex
.
If options
is a non-object, it will be treated as the options.algorithm
option.
//Generate the md5 of the file utilyfs;
utily.fs.copy(source, destination, cb)
Copy a source
file to destination
.
utilyfs;
utily.fs.exists(path, cb)
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).
utilyfs;
utily.fs.isDir(path, cb)
Check if the provided path exists and is a directory or not.
utilyfs;
utily.fs.isFile(path, cb)
Check if the provided path exists and is a file or not.
utilyfs;
utily.fs.mkdir(path, cb)
Create a folder and all the parent folders of path
.
//Create the folder and all the parent folders (if does not exists)utilyfs;
utily.fs.size(file, cb)
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 utilyfssize'/path/to/file.txt' { //Check the error iferror //Something went wrong... console;};
utily.fs.readdir(path, cb)
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 utilyfs;
utily.fs.unlink(files, cb)
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 filesutilyfs; //Remove a single file utilyfs;
Is functions
A set of functions to check the type of a given value.
utily.is.undefined(value)
Return true if value
is undefined.
var a;var b = 'Hello';utilyisundefineda; // -> true, `a` is not definedutilyisundefinedb; // -> false, `b` is a string
utily.is.string(value)
Return true if value
is a string.
utilyis; // -> true utilyis; // -> trueutilyis; // -> false
utily.is.object(value)
Return true if value
is an object.
utilyisobject{}; // -> trueutilyisobjectnull; // -> false
utily.is.array(value)
Return true if value
is an array.
utilyis; // -> true utilyis; // -> false
utily.is.stream(value)
Return true if value
is a stream.
utilyis; // -> trueutilyis; // -> false
utily.is.null(value)
Return true if value
is null
.
utilyis; // -> trueutilyis; // -> false
utily.is.integer(value)
Return true if value
is an integer number.
utilyis; // -> trueutilyis; // -> false
utily.is.number(value)
Return true if value
is a number;
utilyis; // --> trueutilyis; // -> false
utily.is.boolean(value)
Return true if value
is a boolean.
utilyis; // -> true utilyis; // -> false
utily.is.function(value)
Return true if value
is a function.
utilyis; // -> true
utily.is.buffer(value)
Return true if value
is a buffer.
utilyis; // -> true
utily.is.regexp(value)
Return true if value
is a regular expression.
utilyis; // -> true
JSON functions
utily.json.read(file[, options], callback)
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 utilyjson;
utily.json.write(file, object[, opt], callback)
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 utilyjson;
Object functions
utily.object.each(obj, fn)
Execute fn
with each pair key
- value
in obj
.
var obj = key1: 'value1' key2: 'value2' key3: 'value3' ;utilyobject; //Output in console:// key1 -> value1// key2 -> value2// key3 -> value3
utily.object.keys(obj)
This is just Object.keys
.
var keys = utilyobject; // --> keys = [ 'a', 'b', 'c' ]
utily.object.sort(array, keys, order)
Sort an array with objects by the provided keys
and with the provided order
(default is ASC
order).
var list = ;list;list;list;list; //Sort the list utilyobject; //Print the array console;// [ { name: 'Andy', age: 24 }, // { name: 'Boby', age: 28 }, // { name: 'Sarah', age: 29 }, // { name: 'Susan', age: 35 } ]
utily.object.values(obj)
Returns an array of a given object's own enumerable property values. It's a ponyfill of the Object.values
method.
var values = utilyobject; // -> values = [ 1, 2, 'hello' ]
String functions
utily.string.format(str, obj)
Replace all handlebars expressions from str
with values of obj
.
utilystring; // --> "My car is blue!"
utily.string.is_empty(str)
Return true if str
is an empty string.
utilystring; // -> trueutilystring; // -> false
utily.string.is_lowercase(str)
Return true if str
is a string in lowercase format.
utilystring; // -> trueutilystring; // -> false
utily.string.is_uppercase(str)
Return true if str
is a string in uppercase format;
utilystring; // -> trueutilystring; // -> false
utily.string.camel_case(str)
Return the camel-case format of str
.
utilystring; // -> 'helloWorld'
utily.string.capitalize(str)
Return the capitalized format of str
.
utilystring; // -> 'Hello world'
utily.string.unique()
Generate a unique random string of 15 characters.
var str = utilystring; // -> str = 'wv1ufiqj5e6xd3k'
License
Under the MIT LICENSE.