You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # package-utils [![Build Status](https://travis-ci.com/jonkemp/package-utils.svg?branch=master)](https://travis-ci.com/jonkemp/package-utils)
  2. > Helper utility modules for collections, arrays, objects and more.
  3. Inspired by `_`. 😄
  4. ## Install
  5. Install with [npm](https://npmjs.org/package/@jonkemp/package-utils)
  6. ```
  7. $ npm install @jonkemp/package-utils
  8. ```
  9. ## Usage
  10. ### forEach
  11. Iterates over a list of elements, yielding each in turn to an iteratee function. The iteratee is bound to the context object, if one is passed. Each invocation of iteratee is called with three arguments: (element, index, list). If list is a JavaScript object, iteratee's arguments will be (value, key, list). Returns the list for chaining.
  12. ```js
  13. forEach([1, 2, 3], alert);
  14. //=> alerts each number in turn...
  15. forEach({one: 1, two: 2, three: 3}, alert);
  16. //=> alerts each number value in turn...
  17. ```
  18. ### map
  19. Produces a new array of values by mapping each value in list through a transformation function (iteratee). The iteratee is passed three arguments: the value, then the index (or key) of the iteration, and finally a reference to the entire list.
  20. ```js
  21. map([1, 2, 3], num => num * 3);
  22. //=> [3, 6, 9]
  23. map({one: 1, two: 2, three: 3}, (num, key) => num * 3);
  24. //=> [3, 6, 9]
  25. map([[1, 2], [3, 4]], first);
  26. //=> [1, 3]
  27. ```
  28. ### flatten
  29. Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.
  30. ```js
  31. flatten([1, [2], [3, [[4]]]]);
  32. //=> [1, 2, 3, 4];
  33. flatten([1, [2], [3, [[4]]]], true);
  34. //=> [1, 2, 3, [[4]]];
  35. ```
  36. ### allKeys
  37. Retrieve all the names of object's own and inherited properties.
  38. ```js
  39. function Stooge(name) {
  40. this.name = name;
  41. }
  42. Stooge.prototype.silly = true;
  43. allKeys(new Stooge("Moe"));
  44. //=> ["name", "silly"]
  45. ```
  46. ### property
  47. Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
  48. ```js
  49. const stooge = {name: 'moe'};
  50. 'moe' === property('name')(stooge);
  51. //=> true
  52. const stooges = {moe: {fears: {worst: 'Spiders'}}, curly: {fears: {worst: 'Moe'}}};
  53. const curlysWorstFear = property(['curly', 'fears', 'worst']);
  54. curlysWorstFear(stooges);
  55. //=> 'Moe'
  56. ```
  57. ### matcher
  58. Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.
  59. ```js
  60. const ready = matcher({selected: true, visible: true});
  61. const readyToGoList = filter(list, ready);
  62. ```
  63. ### isFunction
  64. Returns true if object is a Function.
  65. ```js
  66. isFunction(alert);
  67. // => true
  68. ```
  69. ### isNumber
  70. Returns true if object is a Number (including NaN).
  71. ```js
  72. isNumber(8.4 * 5);
  73. //=> true
  74. ```
  75. ### isUndefined
  76. Returns true if value is undefined.
  77. ```js
  78. isUndefined(window.missingVariable);
  79. //=> true
  80. ```
  81. ### identity
  82. Returns the same value that is used as the argument. In math: f(x) = x
  83. This function looks useless, but is used throughout Underscore as a default iteratee.
  84. ```js
  85. const stooge = { name: 'moe' };
  86. stooge === identity(stooge);
  87. //=> true
  88. ```
  89. ### constant
  90. Creates a function that returns the same value that is used as the argument of constant.
  91. ```js
  92. const stooge = { name: 'moe' };
  93. stooge === constant(stooge)();
  94. //=> true
  95. ```
  96. ---
  97. | **Like us a lot?** Help others know why you like us! **Review this package on [pkgreview.dev](https://pkgreview.dev/npm/package-utils)** | ➡ | [![Review us on pkgreview.dev](https://i.ibb.co/McjVMfb/pkgreview-dev.jpg)](https://pkgreview.dev/npm/package-utils) |
  98. | ----------------------------------------------------------------------------------------------------------------------------------------- | --- | --------------------------------------------------------------------------------------------------------------------- |
  99. ## License
  100. MIT