lodash.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. const assign = require('lodash/assign');
  3. const clone = require('lodash/clone');
  4. const cloneDeepWith = require('lodash/cloneDeepWith');
  5. const isArray = require('lodash/isArray');
  6. const isEmpty = require('lodash/isEmpty');
  7. const isFunction = require('lodash/isFunction');
  8. const isNaN = require('lodash/isNaN');
  9. const isNil = require('lodash/isNil');
  10. const isNumber = require('lodash/isNumber');
  11. const isNull = require('lodash/isNull');
  12. const isObject = require('lodash/isObject');
  13. const isString = require('lodash/isString');
  14. const isUndefined = require('lodash/isUndefined');
  15. const capitalize = require('lodash/capitalize');
  16. const reduce = require('lodash/reduce');
  17. const filter = require('lodash/filter');
  18. const reject = require('lodash/reject');
  19. const has = require('lodash/has');
  20. const map = require('lodash/map');
  21. const forEach = require('lodash/forEach');
  22. const includes = require('lodash/includes');
  23. const size = require('lodash/size');
  24. const join = require('lodash/join');
  25. const trimEnd = require('lodash/trimEnd');
  26. const findIndex = require('lodash/findIndex');
  27. const get = require('lodash/get');
  28. const every = require('lodash/every');
  29. const keys = require('lodash/keys');
  30. const mapKeys = require('lodash/mapKeys');
  31. const pickBy = require('lodash/pickBy');
  32. const value = require('lodash/value');
  33. const unset = require('lodash/unset');
  34. const transform = require('lodash/transform');
  35. const startsWith = require('lodash/startsWith');
  36. const endsWith = require('lodash/endsWith');
  37. /* istanbul ignore next */
  38. module.exports = {
  39. /**
  40. * Creates an inheritance relation between the child and the parent, adding a 'super_' attribute to the
  41. * child, and setting up the child prototype.
  42. *
  43. * @param {Function} child - The target object to create parent references for,.
  44. * @param {Function} base - The parent association to assign to the provided child definition.
  45. * @returns {*}
  46. */
  47. inherit: function (child, base) {
  48. Object.defineProperty(child, 'super_', {
  49. value: isFunction(base) ? base : _.noop,
  50. configurable: false,
  51. enumerable: false,
  52. writable: false
  53. });
  54. child.prototype = Object.create((isFunction(base) ? base.prototype : base), {
  55. constructor: {
  56. value: child,
  57. enumerable: false,
  58. writable: true,
  59. configurable: true
  60. }
  61. });
  62. return child;
  63. },
  64. /**
  65. * Creates a hidden property on an object, which can be changed, but is not enumerable.
  66. *
  67. * @param {Object} obj
  68. * @param {String} name
  69. * @param {*} prop
  70. * @returns {*}
  71. */
  72. assignHidden: function (obj, name, prop) {
  73. Object.defineProperty(obj, name, {
  74. value: prop,
  75. configurable: true,
  76. enumerable: false,
  77. writable: true
  78. });
  79. return obj;
  80. },
  81. /**
  82. * Creates a property on an object, with the given type.
  83. *
  84. * @param {Object} obj
  85. * @param {String} name
  86. * @param {Property} Prop
  87. * @param {*} [fallback]
  88. * @returns {Prop|undefined}
  89. */
  90. createDefined: function (obj, name, Prop, fallback) {
  91. return has(obj, name) ? (new Prop(obj[name])) : fallback;
  92. },
  93. /**
  94. * Merges defined keys from the target object onto the source object.
  95. *
  96. * @param {Object} target
  97. * @param {Object} source
  98. * @returns {Object}
  99. */
  100. mergeDefined: function (target, source) {
  101. var key;
  102. for (key in source) {
  103. if (source.hasOwnProperty(key) && !isUndefined(source[key])) {
  104. target[key] = source[key];
  105. }
  106. }
  107. return target;
  108. },
  109. /**
  110. * Returns the value of a property if defined in object, else the default
  111. *
  112. * @param {Object} obj
  113. * @param {String} prop
  114. * @param {*=} def
  115. *
  116. * @returns {*}
  117. */
  118. getOwn: function (obj, prop, def) {
  119. return has(obj, prop) ? obj[prop] : def;
  120. },
  121. /**
  122. * Creates a clone of an object, but uses the toJSON method if available.
  123. *
  124. * @param {Object} obj
  125. * @returns {*}
  126. */
  127. cloneElement: function (obj) {
  128. return cloneDeepWith(obj, function (value) {
  129. // falls back to default deepclone if object does not have explicit toJSON().
  130. if (value && isFunction(value.toJSON)) {
  131. return value.toJSON();
  132. }
  133. });
  134. },
  135. isNumeric: function (n) {
  136. return !isNaN(n) && isFinite(n);
  137. },
  138. assign,
  139. clone,
  140. isArray,
  141. isEmpty,
  142. isFunction,
  143. isNaN,
  144. isNil,
  145. isNumber,
  146. isNull,
  147. isObject,
  148. isString,
  149. isUndefined,
  150. capitalize,
  151. reduce,
  152. filter,
  153. reject,
  154. has,
  155. map,
  156. forEach,
  157. includes,
  158. size,
  159. join,
  160. trimEnd,
  161. findIndex,
  162. get,
  163. every,
  164. keys,
  165. mapKeys,
  166. pickBy,
  167. value,
  168. unset,
  169. transform,
  170. startsWith,
  171. endsWith,
  172. };