暂无描述

index.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * lodash 3.1.2 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modern modularize exports="npm" -o ./`
  4. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. var getNative = require('lodash._getnative'),
  10. isArguments = require('lodash.isarguments'),
  11. isArray = require('lodash.isarray');
  12. /** Used to detect unsigned integer values. */
  13. var reIsUint = /^\d+$/;
  14. /** Used for native method references. */
  15. var objectProto = Object.prototype;
  16. /** Used to check objects for own properties. */
  17. var hasOwnProperty = objectProto.hasOwnProperty;
  18. /* Native method references for those with the same name as other `lodash` methods. */
  19. var nativeKeys = getNative(Object, 'keys');
  20. /**
  21. * Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
  22. * of an array-like value.
  23. */
  24. var MAX_SAFE_INTEGER = 9007199254740991;
  25. /**
  26. * The base implementation of `_.property` without support for deep paths.
  27. *
  28. * @private
  29. * @param {string} key The key of the property to get.
  30. * @returns {Function} Returns the new function.
  31. */
  32. function baseProperty(key) {
  33. return function(object) {
  34. return object == null ? undefined : object[key];
  35. };
  36. }
  37. /**
  38. * Gets the "length" property value of `object`.
  39. *
  40. * **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
  41. * that affects Safari on at least iOS 8.1-8.3 ARM64.
  42. *
  43. * @private
  44. * @param {Object} object The object to query.
  45. * @returns {*} Returns the "length" value.
  46. */
  47. var getLength = baseProperty('length');
  48. /**
  49. * Checks if `value` is array-like.
  50. *
  51. * @private
  52. * @param {*} value The value to check.
  53. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  54. */
  55. function isArrayLike(value) {
  56. return value != null && isLength(getLength(value));
  57. }
  58. /**
  59. * Checks if `value` is a valid array-like index.
  60. *
  61. * @private
  62. * @param {*} value The value to check.
  63. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  64. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  65. */
  66. function isIndex(value, length) {
  67. value = (typeof value == 'number' || reIsUint.test(value)) ? +value : -1;
  68. length = length == null ? MAX_SAFE_INTEGER : length;
  69. return value > -1 && value % 1 == 0 && value < length;
  70. }
  71. /**
  72. * Checks if `value` is a valid array-like length.
  73. *
  74. * **Note:** This function is based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  75. *
  76. * @private
  77. * @param {*} value The value to check.
  78. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  79. */
  80. function isLength(value) {
  81. return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  82. }
  83. /**
  84. * A fallback implementation of `Object.keys` which creates an array of the
  85. * own enumerable property names of `object`.
  86. *
  87. * @private
  88. * @param {Object} object The object to query.
  89. * @returns {Array} Returns the array of property names.
  90. */
  91. function shimKeys(object) {
  92. var props = keysIn(object),
  93. propsLength = props.length,
  94. length = propsLength && object.length;
  95. var allowIndexes = !!length && isLength(length) &&
  96. (isArray(object) || isArguments(object));
  97. var index = -1,
  98. result = [];
  99. while (++index < propsLength) {
  100. var key = props[index];
  101. if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
  102. result.push(key);
  103. }
  104. }
  105. return result;
  106. }
  107. /**
  108. * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
  109. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  110. *
  111. * @static
  112. * @memberOf _
  113. * @category Lang
  114. * @param {*} value The value to check.
  115. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  116. * @example
  117. *
  118. * _.isObject({});
  119. * // => true
  120. *
  121. * _.isObject([1, 2, 3]);
  122. * // => true
  123. *
  124. * _.isObject(1);
  125. * // => false
  126. */
  127. function isObject(value) {
  128. // Avoid a V8 JIT bug in Chrome 19-20.
  129. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
  130. var type = typeof value;
  131. return !!value && (type == 'object' || type == 'function');
  132. }
  133. /**
  134. * Creates an array of the own enumerable property names of `object`.
  135. *
  136. * **Note:** Non-object values are coerced to objects. See the
  137. * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
  138. * for more details.
  139. *
  140. * @static
  141. * @memberOf _
  142. * @category Object
  143. * @param {Object} object The object to query.
  144. * @returns {Array} Returns the array of property names.
  145. * @example
  146. *
  147. * function Foo() {
  148. * this.a = 1;
  149. * this.b = 2;
  150. * }
  151. *
  152. * Foo.prototype.c = 3;
  153. *
  154. * _.keys(new Foo);
  155. * // => ['a', 'b'] (iteration order is not guaranteed)
  156. *
  157. * _.keys('hi');
  158. * // => ['0', '1']
  159. */
  160. var keys = !nativeKeys ? shimKeys : function(object) {
  161. var Ctor = object == null ? undefined : object.constructor;
  162. if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
  163. (typeof object != 'function' && isArrayLike(object))) {
  164. return shimKeys(object);
  165. }
  166. return isObject(object) ? nativeKeys(object) : [];
  167. };
  168. /**
  169. * Creates an array of the own and inherited enumerable property names of `object`.
  170. *
  171. * **Note:** Non-object values are coerced to objects.
  172. *
  173. * @static
  174. * @memberOf _
  175. * @category Object
  176. * @param {Object} object The object to query.
  177. * @returns {Array} Returns the array of property names.
  178. * @example
  179. *
  180. * function Foo() {
  181. * this.a = 1;
  182. * this.b = 2;
  183. * }
  184. *
  185. * Foo.prototype.c = 3;
  186. *
  187. * _.keysIn(new Foo);
  188. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  189. */
  190. function keysIn(object) {
  191. if (object == null) {
  192. return [];
  193. }
  194. if (!isObject(object)) {
  195. object = Object(object);
  196. }
  197. var length = object.length;
  198. length = (length && isLength(length) &&
  199. (isArray(object) || isArguments(object)) && length) || 0;
  200. var Ctor = object.constructor,
  201. index = -1,
  202. isProto = typeof Ctor == 'function' && Ctor.prototype === object,
  203. result = Array(length),
  204. skipIndexes = length > 0;
  205. while (++index < length) {
  206. result[index] = (index + '');
  207. }
  208. for (var key in object) {
  209. if (!(skipIndexes && isIndex(key, length)) &&
  210. !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  211. result.push(key);
  212. }
  213. }
  214. return result;
  215. }
  216. module.exports = keys;