index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /**
  2. * lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. /** Used as references for various `Number` constants. */
  10. var MAX_SAFE_INTEGER = 9007199254740991;
  11. /** `Object#toString` result references. */
  12. var argsTag = '[object Arguments]',
  13. funcTag = '[object Function]',
  14. genTag = '[object GeneratorFunction]',
  15. mapTag = '[object Map]',
  16. objectTag = '[object Object]',
  17. promiseTag = '[object Promise]',
  18. setTag = '[object Set]',
  19. weakMapTag = '[object WeakMap]';
  20. var dataViewTag = '[object DataView]';
  21. /**
  22. * Used to match `RegExp`
  23. * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
  24. */
  25. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  26. /** Used to detect host constructors (Safari). */
  27. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  28. /** Detect free variable `global` from Node.js. */
  29. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  30. /** Detect free variable `self`. */
  31. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  32. /** Used as a reference to the global object. */
  33. var root = freeGlobal || freeSelf || Function('return this')();
  34. /** Detect free variable `exports`. */
  35. var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;
  36. /** Detect free variable `module`. */
  37. var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;
  38. /** Detect the popular CommonJS extension `module.exports`. */
  39. var moduleExports = freeModule && freeModule.exports === freeExports;
  40. /**
  41. * Gets the value at `key` of `object`.
  42. *
  43. * @private
  44. * @param {Object} [object] The object to query.
  45. * @param {string} key The key of the property to get.
  46. * @returns {*} Returns the property value.
  47. */
  48. function getValue(object, key) {
  49. return object == null ? undefined : object[key];
  50. }
  51. /**
  52. * Checks if `value` is a host object in IE < 9.
  53. *
  54. * @private
  55. * @param {*} value The value to check.
  56. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  57. */
  58. function isHostObject(value) {
  59. // Many host objects are `Object` objects that can coerce to strings
  60. // despite having improperly defined `toString` methods.
  61. var result = false;
  62. if (value != null && typeof value.toString != 'function') {
  63. try {
  64. result = !!(value + '');
  65. } catch (e) {}
  66. }
  67. return result;
  68. }
  69. /**
  70. * Creates a unary function that invokes `func` with its argument transformed.
  71. *
  72. * @private
  73. * @param {Function} func The function to wrap.
  74. * @param {Function} transform The argument transform.
  75. * @returns {Function} Returns the new function.
  76. */
  77. function overArg(func, transform) {
  78. return function(arg) {
  79. return func(transform(arg));
  80. };
  81. }
  82. /** Used for built-in method references. */
  83. var funcProto = Function.prototype,
  84. objectProto = Object.prototype;
  85. /** Used to detect overreaching core-js shims. */
  86. var coreJsData = root['__core-js_shared__'];
  87. /** Used to detect methods masquerading as native. */
  88. var maskSrcKey = (function() {
  89. var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  90. return uid ? ('Symbol(src)_1.' + uid) : '';
  91. }());
  92. /** Used to resolve the decompiled source of functions. */
  93. var funcToString = funcProto.toString;
  94. /** Used to check objects for own properties. */
  95. var hasOwnProperty = objectProto.hasOwnProperty;
  96. /**
  97. * Used to resolve the
  98. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  99. * of values.
  100. */
  101. var objectToString = objectProto.toString;
  102. /** Used to detect if a method is native. */
  103. var reIsNative = RegExp('^' +
  104. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  105. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  106. );
  107. /** Built-in value references. */
  108. var Buffer = moduleExports ? root.Buffer : undefined,
  109. propertyIsEnumerable = objectProto.propertyIsEnumerable;
  110. /* Built-in method references for those with the same name as other `lodash` methods. */
  111. var nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined,
  112. nativeKeys = overArg(Object.keys, Object);
  113. /* Built-in method references that are verified to be native. */
  114. var DataView = getNative(root, 'DataView'),
  115. Map = getNative(root, 'Map'),
  116. Promise = getNative(root, 'Promise'),
  117. Set = getNative(root, 'Set'),
  118. WeakMap = getNative(root, 'WeakMap');
  119. /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
  120. var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
  121. /** Used to detect maps, sets, and weakmaps. */
  122. var dataViewCtorString = toSource(DataView),
  123. mapCtorString = toSource(Map),
  124. promiseCtorString = toSource(Promise),
  125. setCtorString = toSource(Set),
  126. weakMapCtorString = toSource(WeakMap);
  127. /**
  128. * The base implementation of `getTag`.
  129. *
  130. * @private
  131. * @param {*} value The value to query.
  132. * @returns {string} Returns the `toStringTag`.
  133. */
  134. function baseGetTag(value) {
  135. return objectToString.call(value);
  136. }
  137. /**
  138. * The base implementation of `_.isNative` without bad shim checks.
  139. *
  140. * @private
  141. * @param {*} value The value to check.
  142. * @returns {boolean} Returns `true` if `value` is a native function,
  143. * else `false`.
  144. */
  145. function baseIsNative(value) {
  146. if (!isObject(value) || isMasked(value)) {
  147. return false;
  148. }
  149. var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
  150. return pattern.test(toSource(value));
  151. }
  152. /**
  153. * Gets the native function at `key` of `object`.
  154. *
  155. * @private
  156. * @param {Object} object The object to query.
  157. * @param {string} key The key of the method to get.
  158. * @returns {*} Returns the function if it's native, else `undefined`.
  159. */
  160. function getNative(object, key) {
  161. var value = getValue(object, key);
  162. return baseIsNative(value) ? value : undefined;
  163. }
  164. /**
  165. * Gets the `toStringTag` of `value`.
  166. *
  167. * @private
  168. * @param {*} value The value to query.
  169. * @returns {string} Returns the `toStringTag`.
  170. */
  171. var getTag = baseGetTag;
  172. // Fallback for data views, maps, sets, and weak maps in IE 11,
  173. // for data views in Edge < 14, and promises in Node.js.
  174. if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||
  175. (Map && getTag(new Map) != mapTag) ||
  176. (Promise && getTag(Promise.resolve()) != promiseTag) ||
  177. (Set && getTag(new Set) != setTag) ||
  178. (WeakMap && getTag(new WeakMap) != weakMapTag)) {
  179. getTag = function(value) {
  180. var result = objectToString.call(value),
  181. Ctor = result == objectTag ? value.constructor : undefined,
  182. ctorString = Ctor ? toSource(Ctor) : undefined;
  183. if (ctorString) {
  184. switch (ctorString) {
  185. case dataViewCtorString: return dataViewTag;
  186. case mapCtorString: return mapTag;
  187. case promiseCtorString: return promiseTag;
  188. case setCtorString: return setTag;
  189. case weakMapCtorString: return weakMapTag;
  190. }
  191. }
  192. return result;
  193. };
  194. }
  195. /**
  196. * Checks if `func` has its source masked.
  197. *
  198. * @private
  199. * @param {Function} func The function to check.
  200. * @returns {boolean} Returns `true` if `func` is masked, else `false`.
  201. */
  202. function isMasked(func) {
  203. return !!maskSrcKey && (maskSrcKey in func);
  204. }
  205. /**
  206. * Checks if `value` is likely a prototype object.
  207. *
  208. * @private
  209. * @param {*} value The value to check.
  210. * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
  211. */
  212. function isPrototype(value) {
  213. var Ctor = value && value.constructor,
  214. proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
  215. return value === proto;
  216. }
  217. /**
  218. * Converts `func` to its source code.
  219. *
  220. * @private
  221. * @param {Function} func The function to process.
  222. * @returns {string} Returns the source code.
  223. */
  224. function toSource(func) {
  225. if (func != null) {
  226. try {
  227. return funcToString.call(func);
  228. } catch (e) {}
  229. try {
  230. return (func + '');
  231. } catch (e) {}
  232. }
  233. return '';
  234. }
  235. /**
  236. * Checks if `value` is likely an `arguments` object.
  237. *
  238. * @static
  239. * @memberOf _
  240. * @since 0.1.0
  241. * @category Lang
  242. * @param {*} value The value to check.
  243. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  244. * else `false`.
  245. * @example
  246. *
  247. * _.isArguments(function() { return arguments; }());
  248. * // => true
  249. *
  250. * _.isArguments([1, 2, 3]);
  251. * // => false
  252. */
  253. function isArguments(value) {
  254. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  255. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  256. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  257. }
  258. /**
  259. * Checks if `value` is classified as an `Array` object.
  260. *
  261. * @static
  262. * @memberOf _
  263. * @since 0.1.0
  264. * @category Lang
  265. * @param {*} value The value to check.
  266. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  267. * @example
  268. *
  269. * _.isArray([1, 2, 3]);
  270. * // => true
  271. *
  272. * _.isArray(document.body.children);
  273. * // => false
  274. *
  275. * _.isArray('abc');
  276. * // => false
  277. *
  278. * _.isArray(_.noop);
  279. * // => false
  280. */
  281. var isArray = Array.isArray;
  282. /**
  283. * Checks if `value` is array-like. A value is considered array-like if it's
  284. * not a function and has a `value.length` that's an integer greater than or
  285. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  286. *
  287. * @static
  288. * @memberOf _
  289. * @since 4.0.0
  290. * @category Lang
  291. * @param {*} value The value to check.
  292. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  293. * @example
  294. *
  295. * _.isArrayLike([1, 2, 3]);
  296. * // => true
  297. *
  298. * _.isArrayLike(document.body.children);
  299. * // => true
  300. *
  301. * _.isArrayLike('abc');
  302. * // => true
  303. *
  304. * _.isArrayLike(_.noop);
  305. * // => false
  306. */
  307. function isArrayLike(value) {
  308. return value != null && isLength(value.length) && !isFunction(value);
  309. }
  310. /**
  311. * This method is like `_.isArrayLike` except that it also checks if `value`
  312. * is an object.
  313. *
  314. * @static
  315. * @memberOf _
  316. * @since 4.0.0
  317. * @category Lang
  318. * @param {*} value The value to check.
  319. * @returns {boolean} Returns `true` if `value` is an array-like object,
  320. * else `false`.
  321. * @example
  322. *
  323. * _.isArrayLikeObject([1, 2, 3]);
  324. * // => true
  325. *
  326. * _.isArrayLikeObject(document.body.children);
  327. * // => true
  328. *
  329. * _.isArrayLikeObject('abc');
  330. * // => false
  331. *
  332. * _.isArrayLikeObject(_.noop);
  333. * // => false
  334. */
  335. function isArrayLikeObject(value) {
  336. return isObjectLike(value) && isArrayLike(value);
  337. }
  338. /**
  339. * Checks if `value` is a buffer.
  340. *
  341. * @static
  342. * @memberOf _
  343. * @since 4.3.0
  344. * @category Lang
  345. * @param {*} value The value to check.
  346. * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
  347. * @example
  348. *
  349. * _.isBuffer(new Buffer(2));
  350. * // => true
  351. *
  352. * _.isBuffer(new Uint8Array(2));
  353. * // => false
  354. */
  355. var isBuffer = nativeIsBuffer || stubFalse;
  356. /**
  357. * Checks if `value` is an empty object, collection, map, or set.
  358. *
  359. * Objects are considered empty if they have no own enumerable string keyed
  360. * properties.
  361. *
  362. * Array-like values such as `arguments` objects, arrays, buffers, strings, or
  363. * jQuery-like collections are considered empty if they have a `length` of `0`.
  364. * Similarly, maps and sets are considered empty if they have a `size` of `0`.
  365. *
  366. * @static
  367. * @memberOf _
  368. * @since 0.1.0
  369. * @category Lang
  370. * @param {*} value The value to check.
  371. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  372. * @example
  373. *
  374. * _.isEmpty(null);
  375. * // => true
  376. *
  377. * _.isEmpty(true);
  378. * // => true
  379. *
  380. * _.isEmpty(1);
  381. * // => true
  382. *
  383. * _.isEmpty([1, 2, 3]);
  384. * // => false
  385. *
  386. * _.isEmpty({ 'a': 1 });
  387. * // => false
  388. */
  389. function isEmpty(value) {
  390. if (isArrayLike(value) &&
  391. (isArray(value) || typeof value == 'string' ||
  392. typeof value.splice == 'function' || isBuffer(value) || isArguments(value))) {
  393. return !value.length;
  394. }
  395. var tag = getTag(value);
  396. if (tag == mapTag || tag == setTag) {
  397. return !value.size;
  398. }
  399. if (nonEnumShadows || isPrototype(value)) {
  400. return !nativeKeys(value).length;
  401. }
  402. for (var key in value) {
  403. if (hasOwnProperty.call(value, key)) {
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409. /**
  410. * Checks if `value` is classified as a `Function` object.
  411. *
  412. * @static
  413. * @memberOf _
  414. * @since 0.1.0
  415. * @category Lang
  416. * @param {*} value The value to check.
  417. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  418. * @example
  419. *
  420. * _.isFunction(_);
  421. * // => true
  422. *
  423. * _.isFunction(/abc/);
  424. * // => false
  425. */
  426. function isFunction(value) {
  427. // The use of `Object#toString` avoids issues with the `typeof` operator
  428. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  429. var tag = isObject(value) ? objectToString.call(value) : '';
  430. return tag == funcTag || tag == genTag;
  431. }
  432. /**
  433. * Checks if `value` is a valid array-like length.
  434. *
  435. * **Note:** This method is loosely based on
  436. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  437. *
  438. * @static
  439. * @memberOf _
  440. * @since 4.0.0
  441. * @category Lang
  442. * @param {*} value The value to check.
  443. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  444. * @example
  445. *
  446. * _.isLength(3);
  447. * // => true
  448. *
  449. * _.isLength(Number.MIN_VALUE);
  450. * // => false
  451. *
  452. * _.isLength(Infinity);
  453. * // => false
  454. *
  455. * _.isLength('3');
  456. * // => false
  457. */
  458. function isLength(value) {
  459. return typeof value == 'number' &&
  460. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  461. }
  462. /**
  463. * Checks if `value` is the
  464. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  465. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  466. *
  467. * @static
  468. * @memberOf _
  469. * @since 0.1.0
  470. * @category Lang
  471. * @param {*} value The value to check.
  472. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  473. * @example
  474. *
  475. * _.isObject({});
  476. * // => true
  477. *
  478. * _.isObject([1, 2, 3]);
  479. * // => true
  480. *
  481. * _.isObject(_.noop);
  482. * // => true
  483. *
  484. * _.isObject(null);
  485. * // => false
  486. */
  487. function isObject(value) {
  488. var type = typeof value;
  489. return !!value && (type == 'object' || type == 'function');
  490. }
  491. /**
  492. * Checks if `value` is object-like. A value is object-like if it's not `null`
  493. * and has a `typeof` result of "object".
  494. *
  495. * @static
  496. * @memberOf _
  497. * @since 4.0.0
  498. * @category Lang
  499. * @param {*} value The value to check.
  500. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  501. * @example
  502. *
  503. * _.isObjectLike({});
  504. * // => true
  505. *
  506. * _.isObjectLike([1, 2, 3]);
  507. * // => true
  508. *
  509. * _.isObjectLike(_.noop);
  510. * // => false
  511. *
  512. * _.isObjectLike(null);
  513. * // => false
  514. */
  515. function isObjectLike(value) {
  516. return !!value && typeof value == 'object';
  517. }
  518. /**
  519. * This method returns `false`.
  520. *
  521. * @static
  522. * @memberOf _
  523. * @since 4.13.0
  524. * @category Util
  525. * @returns {boolean} Returns `false`.
  526. * @example
  527. *
  528. * _.times(2, _.stubFalse);
  529. * // => [false, false]
  530. */
  531. function stubFalse() {
  532. return false;
  533. }
  534. module.exports = isEmpty;