_.cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy. The predicate-function pairs are invoked with the this binding and arguments of the created function.
여러 쌍을 차례로 실행하는 함수를 만들고, 첫 번째로 true를 반환하는 함수를 호출한다. 조건을 검사하는지 검사하는 함수(predicate-function)는 만들어지는 함수의 binding과 arguments와 함께 호출 된다.
Since
4.0.0
Arguments
pairs (Array): The predicate-function pairs.
Returns
(Function): Returns the new composite function.
Example
var func = _.cond([
[_.matches({ 'a': 1 }), _.constant('matches A')],
[_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
[_.stubTrue, _.constant('no match')]
]);
func({ 'a': 1, 'b': 2 });
// => 'matches A'
func({ 'a': 0, 'b': 1 });
// => 'matches B'
func({ 'a': '1', 'b': '2' });
// => 'no match'
Memo
if ... else, 또는 switch 문 대신에 사용할 수 있을 것 같다. 다만 파라미터로 넘길 수 있는 갯수가 하나로 제한되어 있음
cond<R>(pairs: Array<CondPairNullary<R>>): () => R;
cond<T, R>(pairs: Array<CondPairUnary<T, R>>): (Target: T) => R;
_.constant(value)
Creates a function that returns value.
Since
2.4.0
Arguments
value (*): The value to return from the new function.
Returns
(Function): Returns the new constant function.
Example
var objects = _.times(2, _.constant({ 'a': 1 }));
console.log(objects);
// => [{ 'a': 1 }, { 'a': 1 }]
console.log(objects[0] === objects[1]);
// => true
Memo
참조 데이터 타입(object, array)의 상수를 만들어 사용할 때 쓰면 좋을 듯
_.defaultTo(value, defaultValue)
Checks value to determine whether a default value should be returned in its place. The defaultValue is returned if value is NaN, null, or undefined.
Since
4.14.0
Arguments
value (*): The value to check. defaultValue (*): The default value.
Returns
(*): Returns the resolved value.
Example
_.defaultTo(1, 10);
// => 1
_.defaultTo(undefined, 10);
// => 10
_.flow([funcs])
Creates a function that returns the result of invoking the given functions with the this binding of the created function, where each successive invocation is supplied the return value of the previous.
Since
3.0.0
Arguments
[funcs] (...(Function|Function[])): The functions to invoke.
Returns
(Function): Returns the new composite function.
Example
function square(n) {
return n * n;
}
var addSquare = _.flow([_.add, square]);
addSquare(1, 2);
// => 9
_.identity(value)
This method returns the first argument it receives.
Since
0.1.0
Arguments
value (*): Any value.
Returns
(*): Returns value.
Example
var object = { 'a': 1 };
console.log(_.identity(object) === object);
// => true
_.matches(source)
Creates a function that performs a partial deep comparison between a given object and source, returning true if the given object has equivalent property values, else false.
Note: The created function is equivalent to _.isMatch with source partially applied.
Partial comparisons will match empty array and empty object source values against any array or object value, respectively. See _.isEqual for a list of supported value comparisons.
Since
3.0.0
Arguments
source (Object): The object of property values to match.
Returns
(Function): Returns the new spec function.
Example
var objects = [
{ 'a': 1, 'b': 2, 'c': 3 },
{ 'a': 4, 'b': 5, 'c': 6 }
];
_.filter(objects, _.matches({ 'a': 4, 'c': 6 }));
// => [{ 'a': 4, 'b': 5, 'c': 6 }]
_.matchesProperty(path, srcValue)
Creates a function that performs a partial deep comparison between the value at path of a given object to srcValue, returning true if the object value is equivalent, else false.
Note: Partial comparisons will match empty array and empty object srcValue values against any array or object value, respectively. See _.isEqual for a list of supported value comparisons.
Since
3.2.0
Arguments
path (Array|string): The path of the property to get. srcValue (*): The value to match.
Returns
(Function): Returns the new spec function.
Example
var objects = [
{ 'a': 1, 'b': 2, 'c': 3 },
{ 'a': 4, 'b': 5, 'c': 6 }
];
_.find(objects, _.matchesProperty('a', 4));
// => { 'a': 4, 'b': 5, 'c': 6 }
_.method(path, [args])
Creates a function that invokes the method at path of a given object. Any additional arguments are provided to the invoked method.
Since
3.7.0
Arguments
path (Array|string): The path of the method to invoke. [args] (...*): The arguments to invoke the method with.
Returns
(Function): Returns the new invoker function.
Example
var objects = [
{ 'a': { 'b': _.constant(2) } },
{ 'a': { 'b': _.constant(1) } }
];
_.map(objects, _.method('a.b'));
// => [2, 1]
_.map(objects, _.method(['a', 'b']));
// => [2, 1]