-
Notifications
You must be signed in to change notification settings - Fork 0
Home
busyman is a lightweight and lodash-like JS utility library that provides commonly used functions for your convenience.
$ npm install busyman --save
var _ = require('busyman');
console.log(_.isArray('hello')); // falseChecks if a value is null.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is null, otherwisefalse.
Examples:
var aUndef;
_.isNull(null); // true
_.isNull(aUndef); // false
_.isNull(3); // falseChecks if a value is null or undefined.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is null or undefined, otherwisefalse.
Examples:
var aUndef;
_.isNil(null); // true
_.isNil(aUndef); // true
_.isNil(3); // false
_.isNil([]); // falseChecks if a value is undefined.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is undefined, otherwisefalse.
Examples:
var aUndef;
_.isUndefined(); // true
_.isUndefined(aUndef); // true
_.isUndefined(6); // falseChecks if a value is NaN. This method is based on the global isNaN.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is NaN, otherwisefalse.
Examples:
// These examples are from MDN document
_.isNaN(NaN); // true
_.isNaN(undefined); // true
_.isNaN({}); // true
_.
_.isNaN(true); // false
_.isNaN(null); // false
_.isNaN(37); // false
// strings
_.isNaN("37"); // false
_.isNaN("37.37"); // false
_.isNaN("123ABC"); // true
_.isNaN(""); // false
_.isNaN(" "); // false
// dates
_.isNaN(new Date()); // false
_.isNaN(new Date().toString()); // true
// This is a false positive and the reason why isNaN is not entirely reliable
_.isNaN("blabla"); // true: "blabla" is converted to a number.
// Parsing this as a number fails and returns NaNChecks if a value is an array. This method is based on Array.isArray.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is, otherwisefalse.
Examples:
_.isArray([ 1 ]); // true
_.isArray({ x: 1 }); // falseChecks 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):
trueif it is an object, otherwisefalse.
Examples:
var anObject = { x: 1 },
anArray = [ 1, 2 ],
aNull = null,
notAnObject = 8;
_.isObject({ x: 1 }); // true
_.isObject([ 1, 2 ]); // true
_.isObject(null); // false
_.isObject(8); // falseChecks 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):
trueif it is a plain object, otherwisefalse.
Examples:
function Foo() {} // Foo constructor
_.isPlainObject({ x: 1 }); // true
_.isPlainObject([ 1, 2 ]); // false
_.isPlainObject(null); // false
_.isPlainObject(8); // false
_.isPlainObject(new Foo()); // falseChecks if a value is a Buffer. This method is based on node.js Buffer.isBuffer.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is a buffer, otherwisefalse.
Examples:
_.isBuffer(new Buffer([ 1, 2, 3 ])); // true
_.isBuffer([ 1, 2 ]); // falseChecks if a value is a bool.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is a bool, otherwisefalse.
Examples:
_.isBoolean(false); // true
_.isBoolean(18); // falseChecks if a value is a number.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is a number, otherwisefalse.
Examples:
_.isNumber(3); // true
_.isNumber('18'); // false
_.isNumber('hello'); // falseChecks if a value is a string.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is a string, otherwisefalse.
Examples:
_.isString('hello'); // true
_.isString(6); // falseChecks if a value is an integer. This method is based on Number.isInteger.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is an interger, otherwisefalse.
Examples:
_.isInteger(12); // true
_.isInteger(1.36); // false
_.isInteger('hi'); // false
_.isInteger('18'); // false
_.isInteger({}); // falseChecks if a value is a function.
Arguments:
-
value(*): The value to check.
Returns:
- (Boolean):
trueif it is a function, otherwisefalse.
Examples:
_.isFunction(function () {}); // true
_.isFunction(6); // falseParses 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); // 15
_.parseInt("17", 8)); // 15
_.parseInt("-F", 16)); // -15
_.parseInt(-15.1, 10)); // -15
_.parseInt("-1111", 2)); // -15
_.parseInt("hello", 16); // NaN
_.parseInt("546", 2); // NaN
_.parseInt([], 16); // NaN
_.parseInt({}, 16); // NaN
_.parseInt(true, 16); // NaNChecks 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
trueif string starts with target, elsefalse.
Examples:
_.startsWith('abc', 'a'); // true
_.startsWith('abc', 'b'); // false
_.startsWith('abc', 'b', 1); // trueChecks 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
trueif string ends with target, elsefalse.
Examples:
_.endsWith('abc', 'c'); // true
_.endsWith('abc', 'b'); // false
_.endsWith('abc', 'b', 2); // trueSplits 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'); // [ 'ab', 'de' ]
_.split('abcde', /[bd]/); // [ 'a', 'c', 'e' ]
_.split('abcde', '', 3); // [ 'a', 'b', 'c' ]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', ' '); // 'ab de'
_.replace('abcde', 'cde', '123'); // 'ab123'
_.replace('abcde', /[bd]/g, '-'); // 'a-c-e'Converts a string into camel case.
Arguments:
-
string(String): The string to convert.
Returns:
- (String): Returns the camel cased string.
Examples:
_.camelCase('24 hour'); // '24Hour'
_.camelCase('HELLO-WORLD'); // 'helloWorld'
_.camelCase('__FOO_BAR__'); // 'fooBar'Converts a string into lower case.
Arguments:
-
string(String): The string to convert.
Returns:
- (String): Returns the lower cased string.
Examples:
_.toLower('ABCDE'); // 'abcde'
_.toLower('24_HOUR'); // '24_hour'
_.toLower('--FOO-BAR--'); // '--foo-bar--'Converts a string into upper case.
Arguments:
-
string(String): The string to convert.
Returns:
- (String): Returns the upper cased string.
Examples:
_.toUpper('abcde'); // 'ABCDE'
_.toUpper('24_hour'); // '24_HOUR'
_.toUpper('--foo-bar--'); // '--FOO-BAR--'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'); // 'hello'
_.lowerCase('HELLO-WORLD'); // 'hello world'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'); // 'HELLO'
_.upperCase('hello-world'); // 'HELLO WORLD'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'); // 'hELLO'
_.lowerFirst('HELLO-WORLD'); // 'hELLO-WORLD'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'); // 'Hello'
_.upperFirst('hello-world'); // 'Hello-world'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
trueifpathexists, otherwisefalse.
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.has(object, 'x.y[0].z'); // true
_.has(object, ['x', 'y', '0', 'z']); // true
_.has(object, 'x.y.z'); // 'falseChecks 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
trueif value is found, elsefalse.
Examples:
var obj = { 'user': 'fred', 'age': 40 },
arr = [ 1, 2, 3 ],
str = 'pebbles';
_.includes(obj, 'fred'); // true
_.includes(obj, 'freddy'); // false
_.includes(arr, 2); // true
_.includes(arr, 4); // false
_.includes(str, 'eb'); // true
_.includes(str, 'ese'); // falseChecks 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 thecollection. -
key|index: The key or index of the current property being processed in thecollection. -
collection: The originalcollection.
-
Returns:
- (Boolean): Returns
trueif every value passes the test, else returnsfalse.
Examples:
var obj = { 'user': 'fred', 'age': 40 },
arr = [ 1, 2, 3 ];
_.every(obj, function (v) {
return v === 'fred';
}); // false
_.every(arr, function (v) {
return v === 2;
}); // false
_.every(arr, _.isNumber); // trueCreates 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);
// [ 'x', 'y' ] (iteration order is not guaranteed)
_.keys([ 1, 2, 3, 4, 5 ]);
// [ '0', '1', '2', '3', '4' ]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);
// [ 0, 1 ] (iteration order is not guaranteed)
_.values([ 1, 2, 3, 4, 5 ]);
// [ 1, 2, 3, 4, 5 ]
_.values('hey');
// [ 'h', 'e', 'y' ]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); // 2
_.size(arr); // 3
_.size(str); // 7Copy 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());
// { a: 0, b: 1, d: 3 }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);
// [ { user: 'arya', age: 19 }, { user: 'robb', age: 25 } ]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'); // { y: '1', z: 2 }
_.omit(object, ['y', 'z']); // { x: 0 }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'); // { x: 0 }
_.pick(object, [ 'y', 'z' ]); // { y: '1', z: 2 }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, returndefaultValue.
Returns:
- (Depends): The value at the specified path.
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.get(object, 'x.y[0].z'); // 5
_.get(object, ['x', 'y', '0', 'z']); // 5
_.get(object, 'x.y.z', 'foo'); // returns default value 'foo' when target not foundSet 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:
- (Object):
object
Examples:
var object = {
x: {
y: [ { z: 5 } ]
}
};
_.set(object, 'x.y[0].z'), 'hello');
// {
// x: {
// y: [ { z: 'hello' } ]
// }
// }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):
trueof unset, elsefalse.
Examples:
var object = {
x: {
y: [ { z: 5, m: 'hi' } ]
}
};
_.unset(object, 'x.y[0].z')); // true
console.log(object);
// {
// x: {
// y: [ { m: 'hi' } ]
// }
// }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 thecollection. -
key|index: The key or index of the current property being processed in thecollection. -
collection: The originalcollection.
-
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); // { 'user': 'robb', 'age': 25, 'active': false }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 thecollection. -
key|index: The key or index of the current property being processed in thecollection. -
collection: The originalcollection.
-
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);
// [
// { 'user': 'arya', 'age': 19, 'active': true },
// { 'user': 'sansa', 'age': 22, 'active': true }
// ]Iterates 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 theobject. -
key|index: The key of the current property being processed in theobject. -
object: The originalobject.
-
Returns:
- (none)
Examples:
var keys = [],
vals = [];
function Foo() {
this.x = 0;
this.y = 1;
}
Foo.prototype.z = 2;
_.forOwn(new Foo, function(val, key) {
keys.push(key);
vals.push(val);
});
console.log(keys);
// [ 'x', 'y' ]
console.log(vals);
// [ 0, 1 ]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 thecollection. -
key: The key or index of the current element being processed in thecollection. -
collection: The originalcollection.
-
Returns:
- (Boolean): Returns
trueif any element passes the predicate check, elsefalse.
Examples:
_.some([ 1, 2, 3, 4, 5 ], function (val) {
return val % 2 === 0;
}); // true
_.some([ 1, 3, 5 ], function (val) {
return val % 2 === 0;
}); // falseThis 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 thearray. -
index: The index of the current value being processed in thearray. -
array: The originalarray.
-
-
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;
}); // 2
_.findIndex(array, function (value, index, array) {
return value > 4;
}); // -1Gets 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); // 2
_.indexOf(array, 3, 2); // 5Creates 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); // [ 1, 2, 3, [ 4 ] ]
console.log(array); // [ 1 ]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' ]); // 'a,b,c'
_.join([ 'a', 'b', 'c' ], '-'); // 'a-b-c'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 isarray.length.
Returns:
- (Array): Returns the slice of
array.
Examples:
var array = [ 1, 2, 3 ];
_.slice(array, 1); // [ 2, 3 ]
_.slice(array, 1, 2); // [ 2 ]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' ]); // 'c'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'); // [ 'b', 'b' ]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); // [ 1, 2 ]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); // [ 1, 2, 3, 4, 5, 6 ]
_.drop(array, 3); // [ 4, 5, 6 ]
_.drop(array, 6); // []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); // [ 1, 2, 3, 4, 5, 6 ]
_.dropRight(array, 3); // [ 1, 2, 3 ]
_.dropRight(array, 6); // []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 thecollection. -
key|index: The key or index of the current property being processed in thecollection. -
collection: The originalcollection.
-
Returns:
- (none)
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); // [ 0, 1, 'x', '2' ]
console.log(keys1); // [ 'a', 'b', 'c', 'd' ]
_.forEach(arr, function (val, key) {
vals2.push(val);
keys2.push(key);
});
console.log(vals2); // [ 'x', 'y', 3, '0' ]
console.log(keys2); // [ 0, 1, 2, 3 ]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 thearray. -
index: The index of the current value being processed in thearray. -
array: The originalarray.
-
Returns:
- (Array): Returns the new mapped array.
Examples:
var array = [ 1, 2, 3 ];
_.map(array, String); // [ '1', '2', '3' ]
_.map(array, function (val) {
return val * 2;
}); // [ 2, 4, 6 ]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 thecollection. -
key: The key or index of the current value being processed in thecollection. -
collection: The originalcollection.
-
Returns:
- (Array): Returns the new filtered array.
Examples:
_.reject([ 1, 2, 3, 4 ], function (val) {
return val % 2 === 0;
}); // [ 1, 3 ]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 thearray. -
index: The index of the current value being processed in thearray. -
array: The originalarray.
-
Returns:
- (Array): Returns the new array of removed elements.
Examples:
var array = [ 1, 2, 3, 4 ];
_.remove(array, function (val) {
return val % 2 === 0;
}); // [ 2, 4 ]
console.log(array); // [ 1, 3 ]
_.remove(array, 3); // [ 3 ]
console.log(array); // [ 1 ]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(): Thethis binding offunc. -
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('!'); // 'Hello Peter!'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 invokefuncwith.
Returns:
- (Object): Returns the timer id.
Examples:
_.delay(function(text) {
console.log(text);
}, 1000, 'Hello');
// Logs 'Hello' after one second.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):
trueif it is empty, otherwisefalse.
Examples:
function X () {}
X.prototype.someProp = 0;
function Y (name) {
this.name = name;
}
_.isEmpty({}); // true
_.isEmpty(new X()); // true
_.isEmpty([]); // true
_.isEmpty(''); // true
function foo() {
console.log(_.isEmpty(arguments));
}
foo(); // true
foo('a', 2, 'hello'); // false
_.isEmpty({ x: 1 }); // false
_.isEmpty(new Y('busyman')); // false
_.isEmpty([ 2 ]); // false
_.isEmpty('hello'); // falseCheck 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):
trueif the values are deeply equal, otherwisefalse.
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' ); // false
_.isEqual(obj1, {
x: 1,
y: [ 1, 2, 3 ]
}); // false
_.isEqual(obj1, obj2 ); // true
console.log(obj1 === obj2); // falseCreates 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]); // trueCreates 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 = _.cloneDeep(objects);
console.log(deep[0] === objects[0]); // falseThis 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:
- (none)
Returns:
- (Number): Milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now.
Examples:
_.now(); // 1466047513349Type checking
- isNull
- isNil
- isUndefined
- isNaN
- isArray
- isObject
- isPlainObject
- isBuffer
- isBoolean
- isNumber
- isString
- isInteger
- isFunction
String
- parseInt
- startsWith
- endsWith
- split
- replace
- camelCase
- toLower
- toUpper
- lowerCase
- upperCase
- lowerFirst
- upperFirst
Object/Collection
Array
Function
Utility