0.1.0
Wrapper to organize all internal variables and functions inside
Wrapper for prototypes and methods underscoreJS depends on
ArrayProto = Array.prototype;
ObjProto = Object.prototype;
SymbolProto = Symbol.prototype or null;
push = ArrayProto.push;
slice = ArrayProto.slice;
toString = ObjProto.toString;
hasOwnProperty = ObjProto.hasOwnProperty;
Wrapper to include all internal functions for creating underscore public API
Empty function for surrogate-prototype-swapping
create an instance of object _
and store obj
as a property _wrapped: obj
(any)
any
:
obj _ {_wrapped: obj}
CallbackTransformer : Because we want a method/callback to apply to any type, array, arrayLike, object and even nested version of them, the callback has to adjust itself accordingly.
(dynamics)
the value's typeof determines which callback to return
(object)
the object this inside the callback will refer to
(number)
number of args the callback has
any
:
a callback or a callback execution
External wrapper for our callback generator cb
. Users may customize
_.iteratee
if they want additional predicate/iteratee shorthand styles.
This abstraction hides the internal-only argCount argument.
(any)
(any)
OFFICIAL: https://underscorejs.org/#each
// The cornerstone, an each
implementation, aka forEach
.
// Handles raw objects in addition to array-likes. Treats all
// sparse array-likes as if they were dense.
run callback on each element of an array, arrayLike, or object, return obj
(any)
array, array-like object, object in general
(any)
callback function
(any)
thisArg
any
:
obj
OFFICIAL: https://underscorejs.org/#map Return the results of applying the iteratee to each element.
run callback on each element of array, arrayLike or other object, return an array with each result in it
(any)
array, arrayLike or other object
(any)
callback function
(any)
thisArg, usually an object
any
:
obj
what's behind _.reduce = createReduce(1)?
reducer
to work from left to right_.find(obj, predicate, context) => the first element that matches predicate
(any)
arrayLike or object
(any)
a callback or an element of obj
(any)
an object, thisArg
any
:
an element of obj
.groupBy(obj,iteratee,context): behavior
for `.groupByis the following 1. if
resultdoes not have
key`, result[ key ] = [ value ];
2. if it does, result[ key ].push(value)
(arrayLikeOrObject)
(dynamicValue)
value which triggers callbackTransformer in
cb
(object)
thisArg
// use1: _.groupBy(array, callback) group elements by callback(el,idx,array) result
_.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; })
// => {"0": [2, 4, 6], "1": [1, 3, 5]}
// use2: _.groupBy(array, 'length') => group elements by _.property('length')(...) result
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
var grouped = _.groupBy(list, 'length');
arrayEq(grouped['3'], ['one', 'two', 'six', 'ten']);
arrayEq(grouped['4'], ['four', 'five', 'nine']);
arrayEq(grouped['5'], ['three', 'seven', 'eight']);
// use3 _.groupBy(array) => group elements by _.identity(el) result
var array = [1, 2, 1, 2, 3];
var grouped = _.groupBy(array);
eq(grouped['1'].length, 2);
eq(grouped['3'].length, 1);
// use4 _.groupBy(arrayOfArrays, number) => group element(a sub-array) by _.property(number)(el-subarray)
var matrix = [
[1, 2],
[1, 3],
[2, 3]
];
_.isEqual(_.groupBy(matrix, 0), {1: [[1, 2], [1, 3]], 2: [[2, 3]]});
_.isEqual(_.groupBy(matrix, 1), {2: [[1, 2]], 3: [[1, 3], [2, 3]]});
// use5 _.groupBy(arrayOfObjects, arrayOfKeys) => group element(an object) by _.property(arrayOfKeys)(el-object)
var liz = {name: 'Liz', stats: {power: 10}};
var chelsea = {name: 'Chelsea', stats: {power: 10}};
var jordan = {name: 'Jordan', stats: {power: 6}};
var collection = [liz, chelsea, jordan];
var expected = {
10: [liz, chelsea],
6: [jordan]
};
_.isEqual(_.groupBy(collection, ['stats', 'power']), expected);
// use6 _.groupBy(arrayOfObjects, key) => group element(an object) by _.property(key)(el-object)
var foos = [{foo: [1, 2]}, {foo: [1, 2]}]
_.isEqual(_.groupBy(foos, 'foo'), {"1,2": foos});
tests // debugger; // _.union([1,2],[3,4]);
(any)
OFFICIAL
Retrieve the names of an object's own properties.
Delegates to ECMAScript 5's native Object.keys
.
(any)
_.keys(obj) => [] or [key1, key2, ...] of direct properties not properties on prototype
(any)
arrayLike or objects
_.isArray(obj): check whether an array or not use Array.isArray or toString.call(obj)
_.isObject(obj): check whether an obj is of type 'function' or 'object' obj: must not be undefined, null, NaN
(any)
_.property(path)(obj) => get a value from shallow object or nested object
(any)
a key or an array of keys
(any)
an array, arrayLike, other object or their nested version
any
:
a single value or undefined
tests // debugger; _.matcher({num: 1})({num:1}) => true
(any)
(any)
(any)
func(args) => restArguments(func, startIndex)(arg1, arg2, ...) => no array format is needed func([el1, el2, el3, ... ]) => restArguments(func, 0)(el1, el2, el3, ...) func(arg1, [el1, el2, ...]) => restArguments(func, 1)(arg1, el1, el2, ...) func(arg1, arg2, [el1, el2, ...]) => restArguments(func, 2)(arg1, arg2, el1, el2, ...) .union(arrays) => restArgument(.union, 0)([1,2], [3,4], ...) .without(array, otherArrays) => restArgument(.without, 1)([1,2,3,4], 2,4,...) var log = .bind(console.log, console); .delay(func, wait, args) => restArgument(_.delay, 2)(log, 1000, 'logged later', 'and more', 'and less')
baseCreate({num: 1}) => {}; whose {}.proto === {num:1}
(any)
an object
any
:
an object with prototype to be its
proto
shallowProperty(key)(obj) => obj[key] or undefined
(any)
key of an object or key of an array
(any)
an object or an array
any
:
a value or undefined
has(obj, path) ===> obj.hasOwnProperty(path) considering undefined and null too
(any)
any object (array, arrayLike, other object)
(any)
key to the obj above
any
:
a bool
deepGet(obj, path) => a value at many level deep of a nested array, or arrayLike or other object
(any)
array, arrayLike or ohter object
(any)
an array of keys
any
:
single value or undefined
OFFICIAL // Helper for collection methods to determine whether a collection // should be iterated as an array or as an object. // Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength // Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
isArrayLike(collection): check array, or arguments, or NodeList
(any)
an array, arguments, or NodeList
any
:
a bool
createReduce(dir)(obj, iteratee, memo, context)
(any)
1 or -1, for starting left or right
(any)
array, arrayLike, or other object
(any)
a function, callback(memo, value, idx, obj)
(any)
a number, variable to accumulate values
(any)
an object, thisArg
any
:
a value, memo
reducer(obj, iteratee, memo, initial) => memo
(any)
array, or arrayLike, or object
(any)
a function, callback(accumulator, value, idx, obj)
(any)
a number, accumulator or initial value
(any)
a bool, whether the initial memo is provided by the user
group(behavior, partition) => [[...],[...]] or {...}
1. create the returned container [[ ],[ ]], or { }, result
2. callbackTransformer based on value of iteratee
3. invoke callback on each element and get result key
4. run behavior
which sets rules for how result
use key
to store value
5. return result
(function)
a func
(collection)
act as a bool
(any)
arrayLike or object
(any)
value which triggers callbackTransformer in
cb
(any)
thisArg
(any)
result of element invocation on callback
createPredicateIndexFinder(1)(array, predicate, context) => _.findIndex(...)
(any)
1: get first matched index, -1: get last matched index
(any)
array of numbers or even objects
(any)
a callback or an object
(any)
an object, thisArg
any
:
index or -1