busyman
A lodash-like JavaScript utility library


Table of Contents
- Overiew
- Installation
- Usage
- APIs
1. Overview
busyman is a lodash-like JS utility library that provides commonly used functions for your convenience.
2. Installation
$ npm install busyman --save
3. Usage
var _ = require('busyman');
console.log(_.isArray('hello'));
4. APIs
- Type checking
- isNull, isNil, isUndefined, isNaN
- isArray, isObject, isPlainObject, isBuffer
- isBoolean, isNumber, isString, isInteger, isFunction
- String
- Object/Collection
- has, includes, every
- keys, values, size
- assign, merge, omit, pick
- get, set, unset, find, filter
- forOwn
- Array
- some
- findIndex, indexOf
- concat, join
- slice, last, pull, take, drop, dropRight
- forEach, map, reject, remove
- Function
- Utility
_.isNull(value)
Checks if a value is null.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is null, otherwise false
.
Examples:
var aUndef;
_.isNull(null);
_.isNull(aUndef);
_.isNull(3);
_.isNil(value)
Checks if a value is null or undefined.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is null or undefined, otherwise false
.
Examples:
var aUndef;
_.isNil(null);
_.isNil(aUndef);
_.isNil(3);
_.isNil([]);
_.isUndefined(value)
Checks if a value is undefined.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is undefined, otherwise false
.
Examples:
var aUndef;
_.isUndefined();
_.isUndefined(aUndef);
_.isUndefined(6);
_.isNaN(value)
Checks if a value is NaN. This method is based on the global isNaN.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is NaN, otherwise false
.
Examples:
_.isNaN(NaN);
_.isNaN(undefined);
_.isNaN({});
_.
_.isNaN(true);
_.isNaN(null);
_.isNaN(37);
_.isNaN("37");
_.isNaN("37.37");
_.isNaN("123ABC");
_.isNaN("");
_.isNaN(" ");
_.isNaN(new Date());
_.isNaN(new Date().toString());
_.isNaN("blabla");
_.isArray(value)
Checks if a value is an array. This method is based on Array.isArray.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is, otherwise false
.
Examples:
_.isArray([ 1 ]);
_.isArray({ x: 1 });
_.isObject(value)
Checks if a value is an object. A null
is considered not an object. An array is considered as an object as usual.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is an object, otherwise false
.
Examples:
var anObject = { x: 1 },
anArray = [ 1, 2 ],
aNull = null,
notAnObject = 8;
_.isObject({ x: 1 });
_.isObject([ 1, 2 ]);
_.isObject(null);
_.isObject(8);
_.isPlainObject(value)
Checks if a value is a plain object. A null
is not an plain object. An array is not an plain object. An object created from a constructo other than Object constructor is not an plain object.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a plain object, otherwise false
.
Examples:
function Foo() {}
_.isPlainObject({ x: 1 });
_.isPlainObject([ 1, 2 ]);
_.isPlainObject(null);
_.isPlainObject(8);
_.isPlainObject(new Foo());
_.isBuffer(value)
Checks if a value is a Buffer. This method is based on node.js Buffer.isBuffer.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a buffer, otherwise false
.
Examples:
_.isBuffer(new Buffer([ 1, 2, 3 ]));
_.isBuffer([ 1, 2 ]);
_.isBoolean(value)
Checks if a value is a bool.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a bool, otherwise false
.
Examples:
_.isBoolean(false);
_.isBoolean(18);
_.isNumber(value)
Checks if a value is a number.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a number, otherwise false
.
Examples:
_.isNumber(3);
_.isNumber('18');
_.isNumber('hello');
_.isString(value)
Checks if a value is a string.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a string, otherwise false
.
Examples:
_.isString('hello');
_.isString(6);
_.isInteger(value)
Checks if a value is an integer. This method is based on Number.isInteger.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is an interger, otherwise false
.
Examples:
_.isInteger(12);
_.isInteger(1.36);
_.isInteger('hi');
_.isInteger('18');
_.isInteger({});
_.isFunction(value)
Checks if a value is a function.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is a function, otherwise false
.
Examples:
_.isFunction(function () {});
_.isFunction(6);
_.parseInt(string[, radix])
Parses a string to an integer with the specified radix. This method is based on the global parseInt.
Arguments:
- (String): The string to parse.
Returns:
- (Number): The parsed numeric value, or NaN if parsing fails.
Examples:
_.parseInt(" 0xF", 16);
_.parseInt("17", 8));
_.parseInt("-F", 16));
_.parseInt(-15.1, 10));
_.parseInt("-1111", 2));
_.parseInt("hello", 16);
_.parseInt("546", 2);
_.parseInt([], 16);
_.parseInt({}, 16);
_.parseInt(true, 16);
_.startsWith(string, target[, position])
Checks if a string
starts with the given target
string.
Arguments:
string
(String): The string to search.target
(String | RegExp): The string to search for.position
(Number): The position to search from.
Returns:
- (Boolean): Returns
true
if string starts with target, else false
.
Examples:
_.startsWith('abc', 'a');
_.startsWith('abc', 'b');
_.startsWith('abc', 'b', 1);
_.endsWith(string, target[, position])
Checks if the string
ends with the given target
string.
Arguments:
string
(String): The string to search.target
(String): The string to search for.position
(Number): The position to search up to.
Returns:
- (Boolean): Returns
true
if string ends with target, else false
.
Examples:
_.endsWith('abc', 'c');
_.endsWith('abc', 'b');
_.endsWith('abc', 'b', 2);
_.split(string, separator[, limit])
Splits a string
, according to the separator
, into an array of the split strings.
Arguments:
string
(String): The string to split.separator
(String | RegExp): The separator pattern to split by.limit
(Number): The length to truncate results to.
Returns:
- (Array): Returns the string segments.
Examples:
_.split('abcde', 'c');
_.split('abcde', /[bd]/);
_.split('abcde', '', 3);
_.replace(string, pattern, replacement)
Replaces matches for pattern
in string
with replacement
.
Arguments:
string
(String): The string to modify.pattern
(String | RegExp): The pattern to replace.replacement
(String | Function): The match replacement.
Returns:
- (String): Returns the modified string.
Examples:
_.replace('abcde', 'c', ' ');
_.replace('abcde', 'cde', '123');
_.replace('abcde', /[bd]/g, '-');
_.camelCase(string)
Converts a string
into camel case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the camel cased string.
Examples:
_.camelCase('24 hour');
_.camelCase('HELLO-WORLD');
_.camelCase('__FOO_BAR__');
_.toLower(string)
Converts a string
into lower case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the lower cased string.
Examples:
_.toLower('ABCDE');
_.toLower('24_HOUR');
_.toLower('--FOO-BAR--');
_.toUpper(string)
Converts a string
into upper case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the upper cased string.
Examples:
_.toUpper('abcde');
_.toUpper('24_hour');
_.toUpper('--foo-bar--');
_.lowerCase(string)
Converts a string
, as space separated words, to lower case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the lower cased string.
Examples:
_.lowerCase('HELLO');
_.lowerCase('HELLO-WORLD');
_.upperCase(string)
Converts a string
, as space separated words, to upper case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the upper cased string.
Examples:
_.upperCase('hello');
_.upperCase('hello-world');
_.lowerFirst(string)
Converts the first character of a string
to lower case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the converted string.
Examples:
_.lowerFirst('HELLO');
_.lowerFirst('HELLO-WORLD');
_.upperFirst(string)
Converts the first character of a string
to upper case.
Arguments:
string
(String): The string to convert.
Returns:
- (String): Returns the converted string.
Examples:
_.upperFirst('hello');
_.upperFirst('hello-world');
###_.has(object, path)
Checks if the path
is the direct property in the object
.
Arguments:
object
(Object): The object to query.path
(String | Array): Path to be checked.
Returns:
- (Boolean): Returns
true
if path
exists, otherwise false
.
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.has(object, 'x.y[0].z');
_.has(object, ['x', 'y', '0', 'z']);
_.has(object, 'x.y.z');
###_.includes(collection, value)
Checks if the value
is in collection
.
Arguments:
collection
(Object | Array | String): The collection to query.value
(Depends): The value to search for.
Returns:
- (Boolean): Returns
true
if value is found, else false
.
Examples:
var obj = { 'user': 'fred', 'age': 40 },
arr = [ 1, 2, 3 ],
str = 'pebbles';
_.includes(obj, 'fred');
_.includes(obj, 'freddy');
_.includes(arr, 2);
_.includes(arr, 4);
_.includes(str, 'eb');
_.includes(str, 'ese');
###_.every(collection, predicate)
Checks if every value in the collection pass the predicate
test.
Arguments:
collection
(Object | Array): The collection to query.predicate
(Function): function (value, key|index, collection) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key|index
: The key or index of the current property being processed in the collection
.collection
: The original collection
.
Returns:
- (Boolean): Returns
true
if every value passes the test, else returns false
.
Examples:
var obj = { 'user': 'fred', 'age': 40 },
arr = [ 1, 2, 3 ];
_.every(obj, function (v) {
return v === 'fred';
});
_.every(arr, function (v) {
return v === 2;
});
_.every(arr, _.isNumber);
###_.keys(object)
Creates an array contains all own enumerable property names of a given object
. This method is based on Object.keys.
Arguments:
object
(Object): The object to query.
Returns:
- (Array): Returns the array of all property names.
Examples:
function Foo() {
this.x = 0;
this.y = 1;
}
Foo.prototype.z = 2;
_.keys(new Foo);
_.keys([ 1, 2, 3, 4, 5 ]);
_.keys('hey');
###_.values(object)
Creates an array contains all own enumerable property values of a given object
.
Arguments:
object
(Object): The object to query.
Returns:
- (Array): Returns the array of all property values.
Examples:
function Foo() {
this.x = 0;
this.y = 1;
}
Foo.prototype.z = 2;
_.values(new Foo);
_.values([ 1, 2, 3, 4, 5 ]);
_.values('hey');
###_.size(collection)
Obtains size of the collection
.
Arguments:
collection
(Object | Array | String): The collection to query.
Returns:
- (Number): Returns the collection size.
Examples:
var obj = { 'user': 'fred', 'age': 40 },
arr = [ 1, 2, 3 ],
str = 'pebbles';
_.size(obj);
_.size(arr);
_.size(str);
###_.assign(object[, sources1[, sources2[, ...[, sourcesN]]]])
Copy the values of all enumerable own properties from one or more source objects to a destination object. It will return the destination object. Source objects in the arguments are applied from left to right. This method is based on Object.assign.
Arguments:
object
(Object): The destination object.sourcesN
(Object): The source objects.
Returns:
- (Object): Destination object
Examples:
var result;
var function A() {
this.b = 1;
}
var function B() {
this.d = 3;
}
A.prototype.c = 2;
B.prototype.e = 4;
result = _.assign({ a: 0 }, new A(), new B());
###_.merge(object[, sources1[, sources2[, ...[, sourcesN]]]])
Merges all source objects into the destination object.
Arguments:
object
(Object): The destination object.sourceN
(Object): The source objects.
Returns:
- (Object): The destination object.
Examples:
var originObj = {
data: [ { user: 'arya' }, { user: 'robb' } ]
},
sourceObj1 = {
data: [ { age: 19 }, { age: 25 } ]
};
_.merge(originObj, sourceObj1);
###_.omit(object, props)
Create an object with properties that are not omitted in object
.
Arguments:
object
(Object): The source object.props
(String | Array): Properties to be omitted.
Returns:
- (Object): New object with properities not omitted.
Examples:
var object = {
x: 0,
y: '1',
z: 2
};
_.omit(object, 'x');
_.omit(object, ['y', 'z']);
###_.pick(object, props)
Create an object with properties that are picked from the object
.
Arguments:
object
(Object): The source object.props
(String | Array): Properties to be picked.
Returns:
- (Object): New object with picked properities.
Examples:
var object = {
x: 0,
y: '1',
z: 2
};
_.pick(object, 'x');
_.pick(object, [ 'y', 'z' ]);
###_.get(object, path[, defaultValue])
Gets the value at the specified path
from an object
.
Arguments:
object
(Object): The object to query.path
(Array | String): The path refers to an object property.defaultValue
(Depends): If value of specified path not found, return defaultValue
.
Returns:
- (Depends): The value at the specified path.
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.get(object, 'x.y[0].z');
_.get(object, ['x', 'y', '0', 'z']);
_.get(object, 'x.y.z', 'foo');
###_.set(object, path, value)
Set property to the new value
at the given path
.
Arguments:
object
(object): The object to be set.path
(String | Array): The path to the property.value
(Depends): The value to set.
Returns:
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.set(object, 'x.y[0].z'), 'hello');
###_.unset(object, path)
Delete property from the object
at the given path
.
Arguments:
object
(object): The object to be unset.path
(String | Array): The path to the property.
Returns:
- (Boolean):
true
of unset, else false
.
Examples:
var object = {
x: {
y: [ { z: 5, m: 'hi' } ]
}
};
_.unset(object, 'x.y[0].z'));
console.log(object);
###_.find(collection, predicate)
Iterates over elements in collection
. This method returns the first element of predicate
returns truthy for.
Arguments:
collection
(Array | Object): The collection to be iterated.predicate
(Function): function (value, key|index, collection) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key|index
: The key or index of the current property being processed in the collection
.collection
: The original collection
.
Returns:
- (Depends): The first matched element.
Examples:
var users = [
{ 'user': 'arya', 'age': 19, 'active': true },
{ 'user': 'robb', 'age': 25, 'active': false },
{ 'user': 'sansa', 'age': 22, 'active': true }
],
result;
result = _.find(users, function (user) {
return user.age > 20;
});
console.log(result);
###_.filter(collection, predicate)
Iterates over elements in collection
. The method returns an array which contains all elements of predicate
returns truthy for.
Arguments:
collection
(Array | Object): The collection to be iterated.predicate
(Function): function (value, key|index, collection) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key|index
: The key or index of the current property being processed in the collection
.collection
: The original collection
.
Returns:
- (Array): Returns the new filtered array.
Examples:
var users = [
{ 'user': 'arya', 'age': 19, 'active': true },
{ 'user': 'robb', 'age': 25, 'active': false },
{ 'user': 'sansa', 'age': 22, 'active': true }
],
result;
result = _.filter(users, function (user) {
return user.active === true;
});
console.log(result);
###_.forOwn(object, iteratee)
Interates all own enumerable properties of the object
, and invokes iteratee
with each property. Iteratee function may exit iteration early by explicitly returning false.
Arguments:
object
(Object): The object to be iterated.iteratee
(Function): function (value, key|index, object) { }
, the function invoked per iteration.
value
: The current value being processed in the object
.key|index
: The key of the current property being processed in the object
.object
: The original object
.
Returns:
Examples:
var keys = [],
vals = [];
function Foo() {
this.x = 0;
this.y = 1;
}
Foo.prototype.z = 2;
_.forOwn(new Foo, function(value, key) {
keys.push(key);
vals.push(val);
});
console.log(keys);
console.log(vals);
_.some(collection, predicate)
Checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy.
Arguments:
collection
(Array|Object): The collection to iterate over.predicate
(Function): function (value, key, collection) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key
: The key or index of the current element being processed in the collection
.collection
: The original collection
.
Returns:
- (Boolean): Returns
true
if any element passes the predicate check, else false
.
Examples:
_.some([ 1, 2, 3, 4, 5 ], function (val) {
return val % 2 === 0;
});
_.some([ 1, 3, 5 ], function (val) {
return val % 2 === 0;
});
_.findIndex(array, predicate[, thisArg])
This method returns the index of the first element that makes predicate
return truthy. This method is based on Array.prototype.findIndex.
Arguments:
array
(Array): The array to search.predicate
(Function): function (value, index, array) { }
, the function invoked per iteration.
value
: The current value being processed in the array
.index
: The index of the current value being processed in the array
.array
: The original array
.
thisArg
(Depends): Optional. Object to use as this when executing callback.
Returns:
- (Number): Returns the index of the found element, else -1.
Examples:
var array = [ 1, 2, 3, 1, 2, 3 ];
_.findIndex(array, function (value, index, array) {
return value > 2;
});
_.findIndex(array, function (value, index, array) {
return value > 4;
});
_.indexOf(array, value[, fromIndex])
Gets the index at which a given value
been firstly found in the array
. This method is based on Array.prototype.find.
Arguments:
array
(Array): The array to search.value
(String | Number): The value to search for.fromIndex
(Number): The index to search from. Default is 0.
Returns:
- (Number): Returns the index of the matched element, else -1.
Examples:
var array = [ 1, 2, 3, 1, 2, 3 ];
_.indexOf(array, 3);
_.indexOf(array, 3, 2);
_.concat(array, value1[, value2[, ...[, valueN]]])
Creates a new array concatenating array
with any additional arrays and/or values. This method is based on Array.prototype.concat.
Arguments:
array
(Array): The array to concatenate.valueN
(Depends): The values to concatenate.
Returns:
- (Array): Returns the new concatenated array.
Examples:
var array = [ 1 ];
var other = _.concat(array, 2, [ 3 ], [ [ 4 ] ]);
console.log(other);
console.log(array);
_.join(array[, separator])
Converts all elements in array into a string separated by separator
. This method is based on Array.prototype.join.
Arguments:
array
(Array): The array to convert.separator
(String): Specifies a string to separate each element of the array. Default is ','.
Returns:
- (String): Returns the joined string.
Examples:
_.join([ 'a', 'b', 'c' ]));
_.join([ 'a', 'b', 'c' ], '-'));
_.slice(array[, start[, end]])
Creates a slice of shallow copied array from parts of the given array
. This method is based on Array.prototype.slice.
Arguments:
array
(Array): The array to slice.start
(Number): The start position. Default is 0.end
(Number): The end position. Default is array.length
.
Returns:
- (Array): Returns the slice of
array
.
Examples:
var array = [ 1, 2, 3 ];
_.slice(array, 1);
_.slice(array, 1, 2);
_.last(array)
Gets the last element of array.
Arguments:
array
(Array): The array to query.
Returns:
- (Depends): Returns the last element of
array
.
Examples:
_.last([ 'a', 'b', 'c' ]);
_.pull(array, value1[, value2[, ...[, valueN]]])
Removes all given values
from array
.
Arguments:
array
(Array): The array to modify.valueN
(String | Number): The values to remove.
Returns:
- (Array): Returns the pulled
array
.
Examples:
var array = [ 'a', 'b', 'c', 'a', 'b' ];
_.pull(array, 'a', 'c');
_.take(array)
Creates a slice of array
with n
elements taken from the beginning.
Arguments:
array
(Array): The array to query.n
(Number): The number of elements to take.
Returns:
- (Array): Returns the slice of
array
.
Examples:
var array = [ 1, 2, 3 ];
_.take(array, 2);
_.drop(array, n)
Creates a slice of array
with n
elements dropped from the beginning.
Arguments:
array
(Array): The array to query.n
(Number): The number of elements to drop. Default is 0.
Returns:
- (Array): Returns the slice of
array
.
Examples:
var array = [ 1, 2, 3, 4, 5, 6 ];
_.drop(array);
_.drop(array, 3);
_.drop(array, 6);
_.dropRight(array, n)
Creates a slice of array
with n
elements dropped from the end.
Arguments:
array
(Array): The array to query.n
(Number): The number of elements to drop. Default is 0.
Returns:
- (Array): Returns the slice of
array
.
Examples:
var array = [ 1, 2, 3, 4, 5, 6 ];
_.dropRight(array);
_.dropRight(array, 3);
_.dropRight(array, 6);
###_.forEach(collection, iteratee)
Iterates over each element of collection, and invokes iteratee
with each element. Iteratee function may exit iteration early by explicitly returning false.
Arguments:
collection
(Array | Object): The collection to be iterated.iteratee
(Function): function (value, key|index, collection) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key|index
: The key or index of the current property being processed in the collection
.collection
: The original collection
.
Returns:
Examples:
var obj = { a: 0, b: 1, c: 'x', d: '2' },
arr = [ 'x', 'y', 3, '0' ],
vals1 = [],
keys1 = [],
vals2 = [],
keys2 = [];
_.forEach(obj, function (val, key) {
vals1.push(val);
keys1.push(key);
});
console.log(vals1);
console.log(keys1);
_.forEach(arr, function (val, key) {
vals2.push(val);
keys2.push(key);
});
console.log(vals2);
console.log(keys2);
_.map(array, iteratee)
Creates an array of values with each element returned from iteratee
.
Arguments:
array
(Array): The array to iterate over.iterate
(Function): function (value, index, array) { }
, the function invoked per iteration.
value
: The current value being processed in the array
.index
: The index of the current value being processed in the array
.array
: The original array
.
Returns:
- (Array): Returns the new mapped array.
Examples:
var array = [ 1, 2, 3 ];
_.map(array, String);
_.map(array, function (val) {
return val * 2;
});
_.reject(collection, predicate)
Returns the elements of collection
that predicate
does not return truthy for.
Arguments:
collection
(Array|Object): The collection to iterate over.predicate
(Function): function (value, key, array) { }
, the function invoked per iteration.
value
: The current value being processed in the collection
.key
: The key or index of the current value being processed in the collection
.collection
: The original collection
.
Returns:
- (Array): Returns the new filtered array.
Examples:
_.reject([ 1, 2, 3, 4 ], function (val) {
return val % 2 === 0;
});
_.remove(array, predicate)
Removes all elements from array
that predicate returns truthy for and returns an array of the removed elements.
Arguments:
array
(Array): The array to modify.predicate
(Function | String | Number): function (value, index, array) { }
, the function invoked per iteration.
value
: The current value being processed in the array
.index
: The index of the current value being processed in the array
.array
: The original array
.
Returns:
- (Array): Returns the new array of removed elements.
Examples:
var array = [ 1, 2, 3, 4 ];
_.remove(array, function (val) {
return val % 2 === 0;
});
console.log(array);
_.remove(array, 3);
console.log(array);
_.bind(func, thisArg[, partial1[, partial2[, ... [, partialN]]]])
Creates a function that invokes func
with the this
binding of thisArg
and partialN
prepended to the arguments it receives.
Arguments:
func
(Function): The function to bind.thisArg
(): The thi
s binding of func
.partialN
(Depends): The arguments to be partially applied.
Returns:
- (Function): Returns the new bound function.
Examples:
var greet = function (greeting, punctuation) {
return greeting + ' ' + this.name + punctuation;
};
var person = {
name: 'Peter',
age: 24
};
var bound = _.bind(greet, person, 'Hello');
bound('!');
_.delay(func, wait, [, arg1[, arg2[, ...[, argN]]]])
Invokes func
after wait
milliseconds. Any additional arguments are provided to func
when it is invoked.
Arguments:
func
(Function): The function to delay.wait
(Number): The number of milliseconds to delay invocation.argN
(Depends): The arguments to invoke func
with.
Returns:
- (Object): Returns the timer id.
Examples:
_.delay(function(text) {
console.log(text);
}, 1000, 'Hello');
_.isEmpty(value)
Check if the vlaue is an empty object. An object is empty if it has no own enumerable properties.
null
is empty. Array-like values such as arguments objects, arrays, buffers, or strings are considered empty if they have a length of 0.
Arguments:
value
(*): The value to check.
Returns:
- (Boolean):
true
if it is empty, otherwise false
.
Examples:
function X () {}
X.prototype.someProp = 0;
function Y (name) {
this.name = name;
}
_.isEmpty({});
_.isEmpty(new X());
_.isEmpty([]);
_.isEmpty('');
function foo() {
console.log(_.isEmpty(arguments));
}
foo();
foo('a', 2, 'hello');
_.isEmpty({ x: 1 });
_.isEmpty(new Y('busyman'));
_.isEmpty([ 2 ]);
_.isEmpty('hello');
_.isEqual(value, other)
Check if the tow given values are deeply equal to each other.
Arguments:
value
(*): The value to compare.other
(*): The other value to compare.
Returns:
- (Boolean):
true
if the values are deeply equal, otherwise false
.
Examples:
var obj1 = {
x: 1,
y: [ 1, 2, 3 ],
z: {
m: 'hello',
n: { p: 2, q: [ 'foo', 'bar' ] }
}
},
obj2 = {
x: 1,
y: [ 1, 2, 3 ],
z: {
m: 'hello',
n: { p: 2, q: [ 'foo', 'bar' ] }
}
},
_.isEqual(obj1, 'hi' );
_.isEqual(obj1, {
x: 1,
y: [ 1, 2, 3 ]
});
_.isEqual(obj1, obj2 );
console.log(obj1 === obj2);
_.clone(value)
Creates a shallow copy of the value.
Arguments:
value
(*): The value to clone.
Returns:
- (*): The shallow cloned value.
Examples:
var objects = [ { x: 1 }, { y: 2 } ];
var shallow = _.clone(objects);
console.log(shallow[0] === objects[0]);
_.cloneDeep(value)
Creates a deep clone of the value.
Arguments:
value
(*): The value to clone.
Returns:
- (*): The deeply cloned value.
Examples:
var objects = [ { x: 1 }, { y: 2 } ];
var deep = _.clone(objects);
console.log(deep[0] === objects[0]);
_.now()
This method is based on Date.now. It returns the numeric value corresponding to the current time - the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
Arguments:
Returns:
- (Number): Milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now.
Examples:
_.now();