internalReference

Wrapper to organize all internal variables and functions inside

internalReference
Static Members
root
previousUnderscore

internalDependencies

Wrapper for prototypes and methods underscoreJS depends on

internalDependencies
Static Members
ArrayProto
ObjProto
SymbolProto
push
slice
toString
hasOwnProperty

internalFunctions

Wrapper to include all internal functions for creating underscore public API

internalFunctions
Static Members
Ctor
_(obj)
optimizeCb(func, context, argCount)
cb(value, context, argCount)

iteratee

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.

iteratee(value: any, context: any)
Parameters
value (any)
context (any)

each

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.

each

each

run callback on each element of an array, arrayLike, or object, return obj

each
Parameters
obj (any) array, array-like object, object in general
iteratee (any) callback function
context (any) thisArg
Returns
any: obj

map

OFFICIAL: https://underscorejs.org/#map Return the results of applying the iteratee to each element.

map

map

run callback on each element of array, arrayLike or other object, return an array with each result in it

map
Parameters
obj (any) array, arrayLike or other object
iteratee (any) callback function
context (any) thisArg, usually an object
Returns
any: obj

reduce

what's behind _.reduce = createReduce(1)?

  1. createReduce(1)
  2. created reducer function to do real reduction work
  3. passed 1 to set reducer to work from left to right
  4. returned a wrapper function of reducer
  5. this wrapper function
  6. ask users for 4 arguments (obj, iteratee, memo, context)
  7. further prepares initial or memo, and iteratee for running reducer internally
reduce

find

_.find(obj, predicate, context) => the first element that matches predicate

find
Parameters
obj (any) arrayLike or object
predicate (any) a callback or an element of obj
context (any) an object, thisArg
Returns
any: an element of obj

groupBy

.groupBy(obj,iteratee,context): behavior for `.groupByis the following 1. ifresultdoes not havekey`, result[ key ] = [ value ]; 2. if it does, result[ key ].push(value)

groupBy
Parameters
obj (arrayLikeOrObject)
iteratee (dynamicValue) value which triggers callbackTransformer in cb
context (object) thisArg
Example
// 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});

intersection

tests // debugger; // _.union([1,2],[3,4]);

intersection(array: any)
Parameters
array (any)

keys

OFFICIAL Retrieve the names of an object's own properties. Delegates to ECMAScript 5's native Object.keys.

keys(obj: any)
Parameters
obj (any)

keys

_.keys(obj) => [] or [key1, key2, ...] of direct properties not properties on prototype

keys(obj: any)
Parameters
obj (any) arrayLike or objects

isArray

_.isArray(obj): check whether an array or not use Array.isArray or toString.call(obj)

isArray

isObject

_.isObject(obj): check whether an obj is of type 'function' or 'object' obj: must not be undefined, null, NaN

isObject(obj: any)
Parameters
obj (any)

property

_.property(path)(obj) => get a value from shallow object or nested object

property(path: any, obj: any): any
Parameters
path (any) a key or an array of keys
obj (any) an array, arrayLike, other object or their nested version
Returns
any: a single value or undefined

times

tests // debugger; _.matcher({num: 1})({num:1}) => true

times(n: any, iteratee: any, context: any)
Parameters
n (any)
iteratee (any)
context (any)

restArguments

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')

restArguments

baseCreate

baseCreate({num: 1}) => {}; whose {}.proto === {num:1}

baseCreate
Parameters
prototype (any) an object
Returns
any: an object with prototype to be its proto

shallowProperty

shallowProperty(key)(obj) => obj[key] or undefined

shallowProperty
Parameters
key (any) key of an object or key of an array
obj (any) an object or an array
Returns
any: a value or undefined

has

has(obj, path) ===> obj.hasOwnProperty(path) considering undefined and null too

has
Parameters
obj (any) any object (array, arrayLike, other object)
path (any) key to the obj above
Returns
any: a bool

deepGet

deepGet(obj, path) => a value at many level deep of a nested array, or arrayLike or other object

deepGet
Parameters
obj (any) array, arrayLike or ohter object
path (any) an array of keys
Returns
any: single value or undefined

isArrayLike

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

isArrayLike

isArrayLike(collection): check array, or arguments, or NodeList

isArrayLike
Parameters
collection (any) an array, arguments, or NodeList
Returns
any: a bool

createReduce

createReduce(dir)(obj, iteratee, memo, context)

createReduce
Parameters
dir (any) 1 or -1, for starting left or right
obj (any) array, arrayLike, or other object
iteratee (any) a function, callback(memo, value, idx, obj)
memo (any) a number, variable to accumulate values
context (any) an object, thisArg
Returns
any: a value, memo

reducer

reducer(obj, iteratee, memo, initial) => memo

reducer(obj: any, iteratee: any, memo: any, initial: any)
Parameters
obj (any) array, or arrayLike, or object
iteratee (any) a function, callback(accumulator, value, idx, obj)
memo (any) a number, accumulator or initial value
initial (any) a bool, whether the initial memo is provided by the user

group

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

group(behavior: function, partition: collection, obj: any, iteratee: any, context: any, key: any)
Parameters
behavior (function) a func
partition (collection) act as a bool
obj (any) arrayLike or object
iteratee (any) value which triggers callbackTransformer in cb
context (any) thisArg
key (any) result of element invocation on callback

createPredicateIndexFinder

createPredicateIndexFinder(1)(array, predicate, context) => _.findIndex(...)

createPredicateIndexFinder
Parameters
dir (any) 1: get first matched index, -1: get last matched index
array (any) array of numbers or even objects
predicate (any) a callback or an object
context (any) an object, thisArg
Returns
any: index or -1