⬆️ ⬇️

lodash (underscore) - know your standard library

image



Knowledgeable people recommend that developers periodically run over their standard library in order to better present its capabilities. Why once again reinvent the wheel, when we have already done so much, and, most importantly, are well tested, and sometimes even normally documented. It is said that the full use of the standard library greatly improves performance.



Well, lately, in my work I will use lodash in all projects (someone might like underscore more ). For me, this is actually a standard. Once again, running through the eyes of the API, I decided to compose a cheat sheet for myself in the form: “function name” - “short description”. It is so convenient to refresh the API in memory. Maybe someone will come in handy.



Arrays


  compact 
Remove from the array all empty elements (0, "", null , undefined )
  difference 
We create a new array, as the difference, where all values ​​of the second ( A - B ) were excluded from the first array
  findIndex 
Returns the index of the first element by which callback returns true
  findLastIndex 
Returns the index of the first since the end of the element, on which callback returned true
  first 
Returns the first or several first elements of an array
  flatten 
Extracts, in the form of an array, elements from the objects contained in the transmitted array
  indexOf 
Returns the index of the first matching (===) element, if the array is sorted, you can speed up the search
  initial 
Returns the initial part of the array (except the last n)
  intersection 
Returns the intersection of arrays (several)
  last 
Returns the last or several last elements of an array
  lastIndexOf 
Returns the index of the last matching (===) element. (you can not search from the very end)
  pull 
Removes the passed elements from the array.
  range 
Creates an array with numbers from start to end (you can select a step)
  remove 
Removes elements from an array according to the specified rule and returns an array of deleted elements.
  rest 
Returns everything except the first (first few) array elements.
  sortedIndex 
Returns the insert index of the current value in a sorted array.
  union 
Returns an array of unique values ​​- the result of combining several arrays
  uniq 
Creates a copy of the array without duplicates.
  without 
Creates a new array from the existing one, excluding some values.
  xor 
Creates a new array - “semantic difference” between the transferred arrays
  zip 
Creates an array of elements grouped into subarrays. At the input accepts several arrays. Assembles as follows: the first elements of all input arrays fall into the first group, etc.
  zipObject 
Collects an object from two arrays (with keys and with values)


Collections


  at 
Creates a new collection, only from the listed items.
  contains 
Checks whether a particular item is contained in the collection.
  countBy 
Creates an object whose keys are the values ​​returned by the callback function, and the values ​​are the number of corresponding returns.
  every 
Check that all elements of the collection are true (or satisfy the condition).
  filter 
Runs through all the elements of the collection and returns the MASSIF of the elements that satisfy the condition
  find 
Returns the first element of the collection that meets the specified criteria.
  findLast 
Same as find , only from the end
  forEach 
Runs through all elements of the collection by invoking a callback function for each element.
  forEachRight 
The same as forEach, only from the end
  groupBy 
Creates a collection whose keys are the values ​​returned by the callback function, and the values ​​are arrays from the original elements of the collection
  indexBy 
Creates a collection, the keys of which are the values ​​returned by the callback function, and the values ​​are the last elements of the original collection with the corresponding key
  invoke 
Executes a specific method for each item in the collection and returns an array of the results of this execution.
  map 
Creates an array of elements, driving each element of the collection through a callback function.
  max 
Returns the maximum value of the collection.
  min 
Returns the minimum value of the collection.
  pluck 
Returns all the values ​​of a specific collection property.
  reduce 
Reduce the collection to the value received by calling the callback functions for each value. In addition to the value itself, the result of the previous call is also transferred to this function.
  reduceRight 
Same as reduce , only from the end
  reject 
In contrast to filter , this function returns an array of collection elements that do not satisfy the condition.
  sample 
Returns a random collection item.
  shuffle 
Returns an array of randomly shuffled elements of the collection.
  size 
Returns the size of the collection.
  some 
Check that at least one element of the collection is true (or satisfies the condition).
  sortBy 
Returns an array of sorted in ascending collection items. For sorting, the values ​​obtained by the callback function for each element are used.
  toArray 
Converts a collection to an array. Useful for working with arguments.
  where 
Conducts a deep comparison of each element with a specific object. Returns an array that satisfies the comparison.


Functions


  after 
Returns the function that will call the transferred callback only after the nth call
  bind 
Returns the function that, when invoked, will be bound to the current this , to the bound arguments and arguments that are passed to the function itself.
  bindAll 
Binds all object methods to this object itself ( this object will always contain the object itself)
  bindKey 
Binds an object method to the object itself (at the time of binding, the method may not yet exist)
  compose 
Returns a function - a composition of the passed functions. Pass f, g, h - returns the function = f (g (h ()))
  curry 
Accepts a function with n parameters as input, and returns a function that, if the parameter is sufficient, will call the input function, if not, return another function, passing the remaining parameters of which will call the original one.
  debounce 
Returns a function that will start the input function only after a pause after its last run. (elimination of rattling)
  defer 
Calls the input function when the current call stack is released (at the beginning of the next event cycle)
  delay 
Call the input function after n milliseconds.
  memoize 
Returns a function that caches the results of its execution, and is not executed if the result is in the cache.
  once 
Returns a function that will call the input only once. In subsequent times, the result will be returned.
  partial 
Same as bind , but does not bind this
  partialRight 
Same as partial , but binds to parameters on the right.
  throttle 
Returns a function that calls the original no more than once every n milliseconds
  wrap 
Creates a function in the first parameter of which the first parameter of the wrapper will be passed.


Objects


  assign 
Supplements an object with missing (not just undefined ) properties from another object.
  clone 
Makes a copy of an object (nested objects are copied by reference)
  cloneDeep 
Deep object copying (nested objects are copied by content)
  create 
Creates an object by its prototype and properties.
  defaults 
Supplements an object with missing (=== undefined ) properties from another object — sets defaults
  findKey 
Searches for the first object that satisfies the conditions - returns the key
  findLastKey 
Searches for the last object satisfying the conditions - returns the key
  forIn 
Bypasses all object properties (including internal) by calling a callback function for each of them.
  forInRight 
Same as forIn only from the end
  forOwn 
Wraps all the object's own properties by calling a callback function for each of them.
  forOwnRight 
Same as forOwn only from the end.
  functions 
Returns a sorted array of names of all the properties of an object whose values ​​are functions.
  has 
Checks whether the specified property is an object's own property.
  invert 
Creates an object whose keys and values ​​are swapped
  isArguments 
Checks whether a value is an object of arguments
  isArray 
Checks if a value is an array
  isBoolean 
Checks whether the value is a boolean variable
  isDate 
Checks if a value is an array
  isElement 
Checks if a DOM value is an element
  isEmpty 
Checks if a value is empty. Arrays, strings, arguments - objects with zero length are considered empty
  isEqual 
Conducts a deep comparison of two values.
  isFinite 
Checks whether a given value is a finite number, or it can be converted to it.
  isFunction 
Checks if a given value is a function
  isNaN 
Checks the value for === NaN (this is not the same as the standard isNuN , which returns true for undefined and not numeric values)
  isNull 
Checks value for === null
  isNumber 
Checks whether a value is a number ( NaN is also considered a number)
  isObject 
Checks if a value is an object
  isPlainObject 
Checks whether a value is a pure object (created by the Object constructor)
  isRegExp 
Checks if the value is a regular expression
  isString 
Checks if a value is a string
  isUndefined 
Checks value to === undefined
  keys 
Returns an array of object keys
  mapValues 
Creates a new object with the same keys as the original, the values ​​are obtained by calling the callback function to each element.
  merge 
Recursively adds transferred objects to the destination.
  omit 
Returns an object that has some properties removed.
  pairs 
Creates a two-dimensional array from the object, of type [[key1, value1], [key2, value2]] .
  pick 
Creates another object from an object with properties from the list
  transform 
A simpler alternative to reduce - allows you to transform an incoming object into another, through a callback function, to which, in addition to the elements, the resulting object is also passed
  values 
Returns an array of object values.


Utilities


  now 
Returns the current Unix-time in milliseconds
  constant 
Creates a function that returns the passed value.
  createCallback 
Creates a function, callback - used for internal purposes. Lodash
  escape 
Escapes the characters &, <,>, ", and 'corresponding html entities
  identity 
The function returns the first argument passed to it.
  mixin 
Adds to the object (or lodash itself) the elements of a devotional object. If the receiving object is a function, it adds properties to the prototype.
  noConflict 
Makes _ equal to the old value (before running lodash ), and returns a pointer to lodash
  noop 
Empty function. Returns undefined
  parseInt 
Extracts a number from a string, by default it always works with the 10th number system (as opposed to the standard one)
  property 
Returns a function in the pluck style, which, when called with an object as a parameter, returns the value of a specific property.
  random 
Returns a random number from the range. Maybe fractional.
  result 
Returns the value of the property in the object (if the value is a function, it will be called and its result returned)
  runInContext 
Will return a new lodash function bound to the specified context.
  template 
Micromastering machine
  times 
Performs the specified callback n-time, returning an array of results
  unescape 
Inverse escape function
  uniqueId 
Returns a unique numeric ID (a number, each time one more. You can send a prefix)


PS I, of course, did not use all 100% of the functions in my work, so inaccuracies are possible - write in a personal, I will correct everything. About spelling - there too.


')

Source: https://habr.com/ru/post/217515/



All Articles