PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs//0.7.0/lodash.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 1516 lines | 677 code | 132 blank | 707 comment | 229 complexity | 148dda143a5f4a3ee12bc28f4fe52ff2 MD5 | raw file
  1. /*!
  2. * Lo-Dash v0.7.0 <http://lodash.com>
  3. * Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
  4. * Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
  5. * <http://documentcloud.github.com/underscore>
  6. * Available under MIT license <http://lodash.com/license>
  7. */
  8. ;(function(window, undefined) {
  9. 'use strict';
  10. /**
  11. * Used to cache the last `_.templateSettings.evaluate` delimiter to avoid
  12. * unnecessarily assigning `reEvaluateDelimiter` a new generated regexp.
  13. * Assigned in `_.template`.
  14. */
  15. var lastEvaluateDelimiter;
  16. /**
  17. * Used to cache the last template `options.variable` to avoid unnecessarily
  18. * assigning `reDoubleVariable` a new generated regexp. Assigned in `_.template`.
  19. */
  20. var lastVariable;
  21. /**
  22. * Used to match potentially incorrect data object references, like `obj.obj`,
  23. * in compiled templates. Assigned in `_.template`.
  24. */
  25. var reDoubleVariable;
  26. /**
  27. * Used to match "evaluate" delimiters, including internal delimiters,
  28. * in template text. Assigned in `_.template`.
  29. */
  30. var reEvaluateDelimiter;
  31. /** Detect free variable `exports` */
  32. var freeExports = typeof exports == 'object' && exports &&
  33. (typeof global == 'object' && global && global == global.global && (window = global), exports);
  34. /** Native prototype shortcuts */
  35. var ArrayProto = Array.prototype,
  36. BoolProto = Boolean.prototype,
  37. ObjectProto = Object.prototype,
  38. NumberProto = Number.prototype,
  39. StringProto = String.prototype;
  40. /** Used to generate unique IDs */
  41. var idCounter = 0;
  42. /** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
  43. var largeArraySize = 30;
  44. /** Used to restore the original `_` reference in `noConflict` */
  45. var oldDash = window._;
  46. /** Used to detect delimiter values that should be processed by `tokenizeEvaluate` */
  47. var reComplexDelimiter = /[-?+=!~*%&^<>|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/;
  48. /** Used to match HTML entities */
  49. var reEscapedHtml = /&(?:amp|lt|gt|quot|#x27);/g;
  50. /** Used to match empty string literals in compiled template source */
  51. var reEmptyStringLeading = /\b__p \+= '';/g,
  52. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  53. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  54. /** Used to match regexp flags from their coerced string values */
  55. var reFlags = /\w*$/;
  56. /** Used to insert the data object variable into compiled template source */
  57. var reInsertVariable = /(?:__e|__t = )\(\s*(?![\d\s"']|this\.)/g;
  58. /** Used to detect if a method is native */
  59. var reNative = RegExp('^' +
  60. (ObjectProto.valueOf + '')
  61. .replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
  62. .replace(/valueOf|for [^\]]+/g, '.+?') + '$'
  63. );
  64. /** Used to match internally used tokens in template text */
  65. var reToken = /__token(\d+)__/g;
  66. /** Used to match HTML characters */
  67. var reUnescapedHtml = /[&<>"']/g;
  68. /** Used to match unescaped characters in compiled string literals */
  69. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  70. /** Used to fix the JScript [[DontEnum]] bug */
  71. var shadowed = [
  72. 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
  73. 'toLocaleString', 'toString', 'valueOf'
  74. ];
  75. /** Used to make template sourceURLs easier to identify */
  76. var templateCounter = 0;
  77. /** Used to replace template delimiters */
  78. var tokenHead = '__token',
  79. tokenFoot = '__';
  80. /** Used to store tokenized template text snippets */
  81. var tokenized = [];
  82. /** Native method shortcuts */
  83. var concat = ArrayProto.concat,
  84. hasOwnProperty = ObjectProto.hasOwnProperty,
  85. push = ArrayProto.push,
  86. propertyIsEnumerable = ObjectProto.propertyIsEnumerable,
  87. slice = ArrayProto.slice,
  88. toString = ObjectProto.toString;
  89. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  90. var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
  91. nativeFloor = Math.floor,
  92. nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
  93. nativeIsFinite = window.isFinite,
  94. nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
  95. nativeMax = Math.max,
  96. nativeMin = Math.min,
  97. nativeRandom = Math.random;
  98. /** `Object#toString` result shortcuts */
  99. var argsClass = '[object Arguments]',
  100. arrayClass = '[object Array]',
  101. boolClass = '[object Boolean]',
  102. dateClass = '[object Date]',
  103. funcClass = '[object Function]',
  104. numberClass = '[object Number]',
  105. objectClass = '[object Object]',
  106. regexpClass = '[object RegExp]',
  107. stringClass = '[object String]';
  108. /** Timer shortcuts */
  109. var clearTimeout = window.clearTimeout,
  110. setTimeout = window.setTimeout;
  111. /**
  112. * Detect the JScript [[DontEnum]] bug:
  113. *
  114. * In IE < 9 an objects own properties, shadowing non-enumerable ones, are
  115. * made non-enumerable as well.
  116. */
  117. var hasDontEnumBug;
  118. /**
  119. * Detect if `Array#shift` and `Array#splice` augment array-like objects
  120. * incorrectly:
  121. *
  122. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  123. * and `splice()` functions that fail to remove the last element, `value[0]`,
  124. * of array-like objects even though the `length` property is set to `0`.
  125. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  126. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  127. */
  128. var hasObjectSpliceBug;
  129. /** Detect if own properties are iterated after inherited properties (IE < 9) */
  130. var iteratesOwnLast;
  131. /** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
  132. var noArgsEnum = true;
  133. (function() {
  134. var object = { '0': 1, 'length': 1 },
  135. props = [];
  136. function ctor() { this.x = 1; }
  137. ctor.prototype = { 'valueOf': 1, 'y': 1 };
  138. for (var prop in new ctor) { props.push(prop); }
  139. for (prop in arguments) { noArgsEnum = !prop; }
  140. hasDontEnumBug = (props + '').length < 4;
  141. iteratesOwnLast = props[0] != 'x';
  142. hasObjectSpliceBug = (props.splice.call(object, 0, 1), object[0]);
  143. }(1));
  144. /** Detect if an `arguments` object's [[Class]] is unresolvable (Firefox < 4, IE < 9) */
  145. var noArgsClass = !isArguments(arguments);
  146. /** Detect if `Array#slice` cannot be used to convert strings to arrays (Opera < 10.52) */
  147. var noArraySliceOnStrings = slice.call('x')[0] != 'x';
  148. /**
  149. * Detect lack of support for accessing string characters by index:
  150. *
  151. * IE < 8 can't access characters by index and IE 8 can only access
  152. * characters by index on string literals.
  153. */
  154. var noCharByIndex = ('x'[0] + Object('x')[0]) != 'xx';
  155. /**
  156. * Detect if a node's [[Class]] is unresolvable (IE < 9)
  157. * and that the JS engine won't error when attempting to coerce an object to
  158. * a string without a `toString` property value of `typeof` "function".
  159. */
  160. try {
  161. var noNodeClass = ({ 'toString': 0 } + '', toString.call(window.document || 0) == objectClass);
  162. } catch(e) { }
  163. /* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
  164. var isBindFast = nativeBind && /\n|Opera/.test(nativeBind + toString.call(window.opera));
  165. /* Detect if `Object.keys` exists and is inferred to be fast (IE, Opera, V8) */
  166. var isKeysFast = nativeKeys && /^.+$|true/.test(nativeKeys + !!window.attachEvent);
  167. /* Detect if strict mode, "use strict", is inferred to be fast (V8) */
  168. var isStrictFast = !isBindFast;
  169. /**
  170. * Detect if sourceURL syntax is usable without erroring:
  171. *
  172. * The JS engine in Adobe products, like InDesign, will throw a syntax error
  173. * when it encounters a single line comment beginning with the `@` symbol.
  174. *
  175. * The JS engine in Narwhal will generate the function `function anonymous(){//}`
  176. * and throw a syntax error.
  177. *
  178. * Avoid comments beginning `@` symbols in IE because they are part of its
  179. * non-standard conditional compilation support.
  180. * http://msdn.microsoft.com/en-us/library/121hztk3(v=vs.94).aspx
  181. */
  182. try {
  183. var useSourceURL = (Function('//@')(), !window.attachEvent);
  184. } catch(e) { }
  185. /** Used to identify object classifications that are array-like */
  186. var arrayLikeClasses = {};
  187. arrayLikeClasses[boolClass] = arrayLikeClasses[dateClass] = arrayLikeClasses[funcClass] =
  188. arrayLikeClasses[numberClass] = arrayLikeClasses[objectClass] = arrayLikeClasses[regexpClass] = false;
  189. arrayLikeClasses[argsClass] = arrayLikeClasses[arrayClass] = arrayLikeClasses[stringClass] = true;
  190. /** Used to identify object classifications that `_.clone` supports */
  191. var cloneableClasses = {};
  192. cloneableClasses[argsClass] = cloneableClasses[funcClass] = false;
  193. cloneableClasses[arrayClass] = cloneableClasses[boolClass] = cloneableClasses[dateClass] =
  194. cloneableClasses[numberClass] = cloneableClasses[objectClass] = cloneableClasses[regexpClass] =
  195. cloneableClasses[stringClass] = true;
  196. /**
  197. * Used to convert characters to HTML entities:
  198. *
  199. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  200. * don't require escaping in HTML and have no special meaning unless they're part
  201. * of a tag or an unquoted attribute value.
  202. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  203. */
  204. var htmlEscapes = {
  205. '&': '&amp;',
  206. '<': '&lt;',
  207. '>': '&gt;',
  208. '"': '&quot;',
  209. "'": '&#x27;'
  210. };
  211. /** Used to convert HTML entities to characters */
  212. var htmlUnescapes = {
  213. '&amp;': '&',
  214. '&lt;': '<',
  215. '&gt;': '>',
  216. '&quot;': '"',
  217. '&#x27;': "'"
  218. };
  219. /** Used to determine if values are of the language type Object */
  220. var objectTypes = {
  221. 'boolean': false,
  222. 'function': true,
  223. 'object': true,
  224. 'number': false,
  225. 'string': false,
  226. 'undefined': false,
  227. 'unknown': true
  228. };
  229. /** Used to escape characters for inclusion in compiled string literals */
  230. var stringEscapes = {
  231. '\\': '\\',
  232. "'": "'",
  233. '\n': 'n',
  234. '\r': 'r',
  235. '\t': 't',
  236. '\u2028': 'u2028',
  237. '\u2029': 'u2029'
  238. };
  239. /*--------------------------------------------------------------------------*/
  240. /**
  241. * The `lodash` function.
  242. *
  243. * @name _
  244. * @constructor
  245. * @param {Mixed} value The value to wrap in a `LoDash` instance.
  246. * @returns {Object} Returns a `LoDash` instance.
  247. */
  248. function lodash(value) {
  249. // allow invoking `lodash` without the `new` operator
  250. return new LoDash(value);
  251. }
  252. /**
  253. * Creates a `LoDash` instance that wraps a value to allow chaining.
  254. *
  255. * @private
  256. * @constructor
  257. * @param {Mixed} value The value to wrap.
  258. */
  259. function LoDash(value) {
  260. // exit early if already wrapped
  261. if (value && value.__wrapped__) {
  262. return value;
  263. }
  264. this.__wrapped__ = value;
  265. }
  266. /**
  267. * By default, the template delimiters used by Lo-Dash are similar to those in
  268. * embedded Ruby (ERB). Change the following template settings to use alternative
  269. * delimiters.
  270. *
  271. * @static
  272. * @memberOf _
  273. * @type Object
  274. */
  275. lodash.templateSettings = {
  276. /**
  277. * Used to detect `data` property values to be HTML-escaped.
  278. *
  279. * @static
  280. * @memberOf _.templateSettings
  281. * @type RegExp
  282. */
  283. 'escape': /<%-([\s\S]+?)%>/g,
  284. /**
  285. * Used to detect code to be evaluated.
  286. *
  287. * @static
  288. * @memberOf _.templateSettings
  289. * @type RegExp
  290. */
  291. 'evaluate': /<%([\s\S]+?)%>/g,
  292. /**
  293. * Used to detect `data` property values to inject.
  294. *
  295. * @static
  296. * @memberOf _.templateSettings
  297. * @type RegExp
  298. */
  299. 'interpolate': /<%=([\s\S]+?)%>/g,
  300. /**
  301. * Used to reference the data object in the template text.
  302. *
  303. * @static
  304. * @memberOf _.templateSettings
  305. * @type String
  306. */
  307. 'variable': ''
  308. };
  309. /*--------------------------------------------------------------------------*/
  310. /**
  311. * The template used to create iterator functions.
  312. *
  313. * @private
  314. * @param {Obect} data The data object used to populate the text.
  315. * @returns {String} Returns the interpolated text.
  316. */
  317. var iteratorTemplate = template(
  318. // conditional strict mode
  319. '<% if (useStrict) { %>\'use strict\';\n<% } %>' +
  320. // the `iteratee` may be reassigned by the `top` snippet
  321. 'var index, value, iteratee = <%= firstArg %>, ' +
  322. // assign the `result` variable an initial value
  323. 'result<% if (init) { %> = <%= init %><% } %>;\n' +
  324. // add code to exit early or do so if the first argument is falsey
  325. '<%= exit %>;\n' +
  326. // add code after the exit snippet but before the iteration branches
  327. '<%= top %>;\n' +
  328. // the following branch is for iterating arrays and array-like objects
  329. '<% if (arrayBranch) { %>' +
  330. 'var length = iteratee.length; index = -1;' +
  331. ' <% if (objectBranch) { %>\nif (length > -1 && length === length >>> 0) {<% } %>' +
  332. // add support for accessing string characters by index if needed
  333. ' <% if (noCharByIndex) { %>\n' +
  334. ' if (toString.call(iteratee) == stringClass) {\n' +
  335. ' iteratee = iteratee.split(\'\')\n' +
  336. ' }' +
  337. ' <% } %>\n' +
  338. ' <%= arrayBranch.beforeLoop %>;\n' +
  339. ' while (++index < length) {\n' +
  340. ' value = iteratee[index];\n' +
  341. ' <%= arrayBranch.inLoop %>\n' +
  342. ' }' +
  343. ' <% if (objectBranch) { %>\n}<% } %>' +
  344. '<% } %>' +
  345. // the following branch is for iterating an object's own/inherited properties
  346. '<% if (objectBranch) { %>' +
  347. ' <% if (arrayBranch) { %>\nelse {' +
  348. // add support for iterating over `arguments` objects if needed
  349. ' <% } else if (noArgsEnum) { %>\n' +
  350. ' var length = iteratee.length; index = -1;\n' +
  351. ' if (length && isArguments(iteratee)) {\n' +
  352. ' while (++index < length) {\n' +
  353. ' value = iteratee[index += \'\'];\n' +
  354. ' <%= objectBranch.inLoop %>\n' +
  355. ' }\n' +
  356. ' } else {' +
  357. ' <% } %>' +
  358. // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
  359. // (if the prototype or a property on the prototype has been set)
  360. // incorrectly sets a function's `prototype` property [[Enumerable]]
  361. // value to `true`. Because of this Lo-Dash standardizes on skipping
  362. // the the `prototype` property of functions regardless of its
  363. // [[Enumerable]] value.
  364. ' <% if (!hasDontEnumBug) { %>\n' +
  365. ' var skipProto = typeof iteratee == \'function\' && \n' +
  366. ' propertyIsEnumerable.call(iteratee, \'prototype\');\n' +
  367. ' <% } %>' +
  368. // iterate own properties using `Object.keys` if it's fast
  369. ' <% if (isKeysFast && useHas) { %>\n' +
  370. ' var ownIndex = -1,\n' +
  371. ' ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [],\n' +
  372. ' length = ownProps.length;\n\n' +
  373. ' <%= objectBranch.beforeLoop %>;\n' +
  374. ' while (++ownIndex < length) {\n' +
  375. ' index = ownProps[ownIndex];\n' +
  376. ' <% if (!hasDontEnumBug) { %>if (!(skipProto && index == \'prototype\')) {\n <% } %>' +
  377. ' value = iteratee[index];\n' +
  378. ' <%= objectBranch.inLoop %>\n' +
  379. ' <% if (!hasDontEnumBug) { %>}\n<% } %>' +
  380. ' }' +
  381. // else using a for-in loop
  382. ' <% } else { %>\n' +
  383. ' <%= objectBranch.beforeLoop %>;\n' +
  384. ' for (index in iteratee) {<%' +
  385. ' if (!hasDontEnumBug || useHas) { %>\n if (<%' +
  386. ' if (!hasDontEnumBug) { %>!(skipProto && index == \'prototype\')<% }' +
  387. ' if (!hasDontEnumBug && useHas) { %> && <% }' +
  388. ' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' +
  389. ' %>) {' +
  390. ' <% } %>\n' +
  391. ' value = iteratee[index];\n' +
  392. ' <%= objectBranch.inLoop %>;\n' +
  393. ' <% if (!hasDontEnumBug || useHas) { %>}\n<% } %>' +
  394. ' }' +
  395. ' <% } %>' +
  396. // Because IE < 9 can't set the `[[Enumerable]]` attribute of an
  397. // existing property and the `constructor` property of a prototype
  398. // defaults to non-enumerable, Lo-Dash skips the `constructor`
  399. // property when it infers it's iterating over a `prototype` object.
  400. ' <% if (hasDontEnumBug) { %>\n\n' +
  401. ' var ctor = iteratee.constructor;\n' +
  402. ' <% for (var k = 0; k < 7; k++) { %>\n' +
  403. ' index = \'<%= shadowed[k] %>\';\n' +
  404. ' if (<%' +
  405. ' if (shadowed[k] == \'constructor\') {' +
  406. ' %>!(ctor && ctor.prototype === iteratee) && <%' +
  407. ' } %>hasOwnProperty.call(iteratee, index)) {\n' +
  408. ' value = iteratee[index];\n' +
  409. ' <%= objectBranch.inLoop %>\n' +
  410. ' }' +
  411. ' <% } %>' +
  412. ' <% } %>' +
  413. ' <% if (arrayBranch || noArgsEnum) { %>\n}<% } %>' +
  414. '<% } %>\n' +
  415. // add code to the bottom of the iteration function
  416. '<%= bottom %>;\n' +
  417. // finally, return the `result`
  418. 'return result'
  419. );
  420. /**
  421. * Reusable iterator options shared by
  422. * `every`, `filter`, `find`, `forEach`, `forIn`, `forOwn`, `groupBy`, `map`,
  423. * `reject`, `some`, and `sortBy`.
  424. */
  425. var baseIteratorOptions = {
  426. 'args': 'collection, callback, thisArg',
  427. 'init': 'collection',
  428. 'top':
  429. 'if (!callback) {\n' +
  430. ' callback = identity\n' +
  431. '}\n' +
  432. 'else if (thisArg) {\n' +
  433. ' callback = iteratorBind(callback, thisArg)\n' +
  434. '}',
  435. 'inLoop': 'if (callback(value, index, collection) === false) return result'
  436. };
  437. /** Reusable iterator options for `countBy`, `groupBy`, and `sortBy` */
  438. var countByIteratorOptions = {
  439. 'init': '{}',
  440. 'top':
  441. 'var prop;\n' +
  442. 'if (typeof callback != \'function\') {\n' +
  443. ' var valueProp = callback;\n' +
  444. ' callback = function(value) { return value[valueProp] }\n' +
  445. '}\n' +
  446. 'else if (thisArg) {\n' +
  447. ' callback = iteratorBind(callback, thisArg)\n' +
  448. '}',
  449. 'inLoop':
  450. 'prop = callback(value, index, collection);\n' +
  451. '(hasOwnProperty.call(result, prop) ? result[prop]++ : result[prop] = 1)'
  452. };
  453. /** Reusable iterator options for `every` and `some` */
  454. var everyIteratorOptions = {
  455. 'init': 'true',
  456. 'inLoop': 'if (!callback(value, index, collection)) return !result'
  457. };
  458. /** Reusable iterator options for `defaults` and `extend` */
  459. var extendIteratorOptions = {
  460. 'useHas': false,
  461. 'useStrict': false,
  462. 'args': 'object',
  463. 'init': 'object',
  464. 'top':
  465. 'for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {\n' +
  466. ' if (iteratee = arguments[argsIndex]) {',
  467. 'inLoop': 'result[index] = value',
  468. 'bottom': ' }\n}'
  469. };
  470. /** Reusable iterator options for `filter`, `reject`, and `where` */
  471. var filterIteratorOptions = {
  472. 'init': '[]',
  473. 'inLoop': 'callback(value, index, collection) && result.push(value)'
  474. };
  475. /** Reusable iterator options for `find`, `forEach`, `forIn`, and `forOwn` */
  476. var forEachIteratorOptions = {
  477. 'top': 'if (thisArg) callback = iteratorBind(callback, thisArg)'
  478. };
  479. /** Reusable iterator options for `forIn` and `forOwn` */
  480. var forOwnIteratorOptions = {
  481. 'inLoop': {
  482. 'object': baseIteratorOptions.inLoop
  483. }
  484. };
  485. /** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */
  486. var mapIteratorOptions = {
  487. 'init': '',
  488. 'exit': 'if (!collection) return []',
  489. 'beforeLoop': {
  490. 'array': 'result = Array(length)',
  491. 'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
  492. },
  493. 'inLoop': {
  494. 'array': 'result[index] = callback(value, index, collection)',
  495. 'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(callback(value, index, collection))'
  496. }
  497. };
  498. /** Reusable iterator options for `omit` and `pick` */
  499. var omitIteratorOptions = {
  500. 'useHas': false,
  501. 'args': 'object, callback, thisArg',
  502. 'init': '{}',
  503. 'top':
  504. 'var isFunc = typeof callback == \'function\';\n' +
  505. 'if (!isFunc) {\n' +
  506. ' var props = concat.apply(ArrayProto, arguments)\n' +
  507. '} else if (thisArg) {\n' +
  508. ' callback = iteratorBind(callback, thisArg)\n' +
  509. '}',
  510. 'inLoop':
  511. 'if (isFunc\n' +
  512. ' ? !callback(value, index, object)\n' +
  513. ' : indexOf(props, index) < 0\n' +
  514. ') result[index] = value'
  515. };
  516. /*--------------------------------------------------------------------------*/
  517. /**
  518. * Creates a new function optimized for searching large arrays for a given `value`,
  519. * starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
  520. *
  521. * @private
  522. * @param {Array} array The array to search.
  523. * @param {Mixed} value The value to search for.
  524. * @param {Number} [fromIndex=0] The index to start searching from.
  525. * @param {Number} [largeSize=30] The length at which an array is considered large.
  526. * @returns {Boolean} Returns `true` if `value` is found, else `false`.
  527. */
  528. function cachedContains(array, fromIndex, largeSize) {
  529. fromIndex || (fromIndex = 0);
  530. var length = array.length,
  531. isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
  532. cache = isLarge ? {} : array;
  533. if (isLarge) {
  534. // init value cache
  535. var key,
  536. index = fromIndex - 1;
  537. while (++index < length) {
  538. // manually coerce `value` to string because `hasOwnProperty`, in some
  539. // older versions of Firefox, coerces objects incorrectly
  540. key = array[index] + '';
  541. (hasOwnProperty.call(cache, key) ? cache[key] : (cache[key] = [])).push(array[index]);
  542. }
  543. }
  544. return function(value) {
  545. if (isLarge) {
  546. var key = value + '';
  547. return hasOwnProperty.call(cache, key) && indexOf(cache[key], value) > -1;
  548. }
  549. return indexOf(cache, value, fromIndex) > -1;
  550. }
  551. }
  552. /**
  553. * Creates compiled iteration functions. The iteration function will be created
  554. * to iterate over only objects if the first argument of `options.args` is
  555. * "object" or `options.inLoop.array` is falsey.
  556. *
  557. * @private
  558. * @param {Object} [options1, options2, ...] The compile options objects.
  559. *
  560. * useHas - A boolean to specify whether or not to use `hasOwnProperty` checks
  561. * in the object loop.
  562. *
  563. * useStrict - A boolean to specify whether or not to include the ES5
  564. * "use strict" directive.
  565. *
  566. * args - A string of comma separated arguments the iteration function will
  567. * accept.
  568. *
  569. * init - A string to specify the initial value of the `result` variable.
  570. *
  571. * exit - A string of code to use in place of the default exit-early check
  572. * of `if (!arguments[0]) return result`.
  573. *
  574. * top - A string of code to execute after the exit-early check but before
  575. * the iteration branches.
  576. *
  577. * beforeLoop - A string or object containing an "array" or "object" property
  578. * of code to execute before the array or object loops.
  579. *
  580. * inLoop - A string or object containing an "array" or "object" property
  581. * of code to execute in the array or object loops.
  582. *
  583. * bottom - A string of code to execute after the iteration branches but
  584. * before the `result` is returned.
  585. *
  586. * @returns {Function} Returns the compiled function.
  587. */
  588. function createIterator() {
  589. var object,
  590. prop,
  591. value,
  592. index = -1,
  593. length = arguments.length;
  594. // merge options into a template data object
  595. var data = {
  596. 'bottom': '',
  597. 'exit': '',
  598. 'init': '',
  599. 'top': '',
  600. 'arrayBranch': { 'beforeLoop': '' },
  601. 'objectBranch': { 'beforeLoop': '' }
  602. };
  603. while (++index < length) {
  604. object = arguments[index];
  605. for (prop in object) {
  606. value = (value = object[prop]) == null ? '' : value;
  607. // keep this regexp explicit for the build pre-process
  608. if (/beforeLoop|inLoop/.test(prop)) {
  609. if (typeof value == 'string') {
  610. value = { 'array': value, 'object': value };
  611. }
  612. data.arrayBranch[prop] = value.array || '';
  613. data.objectBranch[prop] = value.object || '';
  614. } else {
  615. data[prop] = value;
  616. }
  617. }
  618. }
  619. // set additional template `data` values
  620. var args = data.args,
  621. firstArg = /^[^,]+/.exec(args)[0],
  622. useStrict = data.useStrict;
  623. data.firstArg = firstArg;
  624. data.hasDontEnumBug = hasDontEnumBug;
  625. data.isKeysFast = isKeysFast;
  626. data.noArgsEnum = noArgsEnum;
  627. data.shadowed = shadowed;
  628. data.useHas = data.useHas !== false;
  629. data.useStrict = useStrict == null ? isStrictFast : useStrict;
  630. if (data.noCharByIndex == null) {
  631. data.noCharByIndex = noCharByIndex;
  632. }
  633. if (!data.exit) {
  634. data.exit = 'if (!' + firstArg + ') return result';
  635. }
  636. if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
  637. data.arrayBranch = null;
  638. }
  639. // create the function factory
  640. var factory = Function(
  641. 'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn, ' +
  642. 'hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction, ' +
  643. 'isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys, ' +
  644. 'propertyIsEnumerable, slice, stringClass, toString',
  645. 'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
  646. 'return callee'
  647. );
  648. // return the compiled function
  649. return factory(
  650. arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn,
  651. hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction,
  652. isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys,
  653. propertyIsEnumerable, slice, stringClass, toString
  654. );
  655. }
  656. /**
  657. * Used by `sortBy` to compare transformed `collection` values, stable sorting
  658. * them in ascending order.
  659. *
  660. * @private
  661. * @param {Object} a The object to compare to `b`.
  662. * @param {Object} b The object to compare to `a`.
  663. * @returns {Number} Returns the sort order indicator of `1` or `-1`.
  664. */
  665. function compareAscending(a, b) {
  666. var ai = a.index,
  667. bi = b.index;
  668. a = a.criteria;
  669. b = b.criteria;
  670. // ensure a stable sort in V8 and other engines
  671. // http://code.google.com/p/v8/issues/detail?id=90
  672. if (a !== b) {
  673. if (a > b || a === undefined) {
  674. return 1;
  675. }
  676. if (a < b || b === undefined) {
  677. return -1;
  678. }
  679. }
  680. return ai < bi ? -1 : 1;
  681. }
  682. /**
  683. * Used by `template` to replace tokens with their corresponding code snippets.
  684. *
  685. * @private
  686. * @param {String} match The matched token.
  687. * @param {String} index The `tokenized` index of the code snippet.
  688. * @returns {String} Returns the code snippet.
  689. */
  690. function detokenize(match, index) {
  691. return tokenized[index];
  692. }
  693. /**
  694. * Used by `template` to escape characters for inclusion in compiled
  695. * string literals.
  696. *
  697. * @private
  698. * @param {String} match The matched character to escape.
  699. * @returns {String} Returns the escaped character.
  700. */
  701. function escapeStringChar(match) {
  702. return '\\' + stringEscapes[match];
  703. }
  704. /**
  705. * Used by `escape` to convert characters to HTML entities.
  706. *
  707. * @private
  708. * @param {String} match The matched character to escape.
  709. * @returns {String} Returns the escaped character.
  710. */
  711. function escapeHtmlChar(match) {
  712. return htmlEscapes[match];
  713. }
  714. /**
  715. * Creates a new function that, when called, invokes `func` with the `this`
  716. * binding of `thisArg` and the arguments (value, index, object).
  717. *
  718. * @private
  719. * @param {Function} func The function to bind.
  720. * @param {Mixed} [thisArg] The `this` binding of `func`.
  721. * @returns {Function} Returns the new bound function.
  722. */
  723. function iteratorBind(func, thisArg) {
  724. return function(value, index, object) {
  725. return func.call(thisArg, value, index, object);
  726. };
  727. }
  728. /**
  729. * A no-operation function.
  730. *
  731. * @private
  732. */
  733. function noop() {
  734. // no operation performed
  735. }
  736. /**
  737. * Used by `template` to replace "escape" template delimiters with tokens.
  738. *
  739. * @private
  740. * @param {String} match The matched template delimiter.
  741. * @param {String} value The delimiter value.
  742. * @returns {String} Returns a token.
  743. */
  744. function tokenizeEscape(match, value) {
  745. if (match && reComplexDelimiter.test(value)) {
  746. return '<e%-' + value + '%>';
  747. }
  748. var index = tokenized.length;
  749. tokenized[index] = "' +\n__e(" + value + ") +\n'";
  750. return tokenHead + index + tokenFoot;
  751. }
  752. /**
  753. * Used by `template` to replace "evaluate" template delimiters, or complex
  754. * "escape" and "interpolate" delimiters, with tokens.
  755. *
  756. * @private
  757. * @param {String} match The matched template delimiter.
  758. * @param {String} escapeValue The complex "escape" delimiter value.
  759. * @param {String} interpolateValue The complex "interpolate" delimiter value.
  760. * @param {String} [evaluateValue] The "evaluate" delimiter value.
  761. * @returns {String} Returns a token.
  762. */
  763. function tokenizeEvaluate(match, escapeValue, interpolateValue, evaluateValue) {
  764. if (evaluateValue) {
  765. var index = tokenized.length;
  766. tokenized[index] = "';\n" + evaluateValue + ";\n__p += '";
  767. return tokenHead + index + tokenFoot;
  768. }
  769. return escapeValue
  770. ? tokenizeEscape(null, escapeValue)
  771. : tokenizeInterpolate(null, interpolateValue);
  772. }
  773. /**
  774. * Used by `template` to replace "interpolate" template delimiters with tokens.
  775. *
  776. * @private
  777. * @param {String} match The matched template delimiter.
  778. * @param {String} value The delimiter value.
  779. * @returns {String} Returns a token.
  780. */
  781. function tokenizeInterpolate(match, value) {
  782. if (match && reComplexDelimiter.test(value)) {
  783. return '<e%=' + value + '%>';
  784. }
  785. var index = tokenized.length;
  786. tokenized[index] = "' +\n((__t = (" + value + ")) == null ? '' : __t) +\n'";
  787. return tokenHead + index + tokenFoot;
  788. }
  789. /**
  790. * Used by `unescape` to convert HTML entities to characters.
  791. *
  792. * @private
  793. * @param {String} match The matched character to unescape.
  794. * @returns {String} Returns the unescaped character.
  795. */
  796. function unescapeHtmlChar(match) {
  797. return htmlUnescapes[match];
  798. }
  799. /*--------------------------------------------------------------------------*/
  800. /**
  801. * Checks if `value` is an `arguments` object.
  802. *
  803. * @static
  804. * @memberOf _
  805. * @category Objects
  806. * @param {Mixed} value The value to check.
  807. * @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  808. * @example
  809. *
  810. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  811. * // => true
  812. *
  813. * _.isArguments([1, 2, 3]);
  814. * // => false
  815. */
  816. function isArguments(value) {
  817. return toString.call(value) == argsClass;
  818. }
  819. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  820. if (noArgsClass) {
  821. isArguments = function(value) {
  822. return !!(value && hasOwnProperty.call(value, 'callee'));
  823. };
  824. }
  825. /**
  826. * Checks if `value` is an array.
  827. *
  828. * @static
  829. * @memberOf _
  830. * @category Objects
  831. * @param {Mixed} value The value to check.
  832. * @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
  833. * @example
  834. *
  835. * (function() { return _.isArray(arguments); })();
  836. * // => false
  837. *
  838. * _.isArray([1, 2, 3]);
  839. * // => true
  840. */
  841. var isArray = nativeIsArray || function(value) {
  842. return toString.call(value) == arrayClass;
  843. };
  844. /**
  845. * Checks if `value` is a function.
  846. *
  847. * @static
  848. * @memberOf _
  849. * @category Objects
  850. * @param {Mixed} value The value to check.
  851. * @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
  852. * @example
  853. *
  854. * _.isFunction(''.concat);
  855. * // => true
  856. */
  857. function isFunction(value) {
  858. return typeof value == 'function';
  859. }
  860. // fallback for older versions of Chrome and Safari
  861. if (isFunction(/x/)) {
  862. isFunction = function(value) {
  863. return toString.call(value) == funcClass;
  864. };
  865. }
  866. /**
  867. * A fallback implementation of `isPlainObject` that checks if a given `value`
  868. * is an object created by the `Object` constructor, assuming objects created
  869. * by the `Object` constructor have no inherited enumerable properties and that
  870. * there are no `Object.prototype` extensions.
  871. *
  872. * @private
  873. * @param {Mixed} value The value to check.
  874. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  875. * `arguments` objects.
  876. * @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
  877. */
  878. function isPlainFallback(value, skipArgsCheck) {
  879. // avoid non-objects and false positives for `arguments` objects
  880. var result = false;
  881. if (!(value && typeof value == 'object') || (!skipArgsCheck && isArguments(value))) {
  882. return result;
  883. }
  884. // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
  885. // methods that are `typeof` "string" and still can coerce nodes to strings.
  886. // Also check that the constructor is `Object` (i.e. `Object instanceof Object`)
  887. var ctor = value.constructor;
  888. if ((!noNodeClass || !(typeof value.toString != 'function' && typeof (value + '') == 'string')) &&
  889. (!isFunction(ctor) || ctor instanceof ctor)) {
  890. // IE < 9 iterates inherited properties before own properties. If the first
  891. // iterated property is an object's own property then there are no inherited
  892. // enumerable properties.
  893. if (iteratesOwnLast) {
  894. forIn(value, function(value, key, object) {
  895. result = !hasOwnProperty.call(object, key);
  896. return false;
  897. });
  898. return result === false;
  899. }
  900. // In most environments an object's own properties are iterated before
  901. // its inherited properties. If the last iterated property is an object's
  902. // own property then there are no inherited enumerable properties.
  903. forIn(value, function(value, key) {
  904. result = key;
  905. });
  906. return result === false || hasOwnProperty.call(value, result);
  907. }
  908. return result;
  909. }
  910. /**
  911. * Checks if a given `value` is an object created by the `Object` constructor.
  912. *
  913. * @private
  914. * @param {Mixed} value The value to check.
  915. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  916. * `arguments` objects.
  917. * @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
  918. */
  919. var isPlainObject = objectTypes.__proto__ != ObjectProto ? isPlainFallback : function(value, skipArgsCheck) {
  920. if (!value) {
  921. return false;
  922. }
  923. var valueOf = value.valueOf,
  924. objProto = typeof valueOf == 'function' && (objProto = valueOf.__proto__) && objProto.__proto__;
  925. return objProto
  926. ? value == objProto || (value.__proto__ == objProto && (skipArgsCheck || !isArguments(value)))
  927. : isPlainFallback(value);
  928. };
  929. /**
  930. * A shim implementation of `Object.keys` that produces an array of the given
  931. * object's own enumerable property names.
  932. *
  933. * @private
  934. * @param {Object} object The object to inspect.
  935. * @returns {Array} Returns a new array of property names.
  936. */
  937. var shimKeys = createIterator({
  938. 'args': 'object',
  939. 'init': '[]',
  940. 'inLoop': 'result.push(index)'
  941. });
  942. /*--------------------------------------------------------------------------*/
  943. /**
  944. * Creates a clone of `value`. If `deep` is `true`, all nested objects will
  945. * also be cloned otherwise they will be assigned by reference. If a value has
  946. * a `clone` method it will be used to perform the clone. Functions, DOM nodes,
  947. * `arguments` objects, and objects created by constructors other than `Object`
  948. * are **not** cloned unless they have a custom `clone` method.
  949. *
  950. * @static
  951. * @memberOf _
  952. * @category Objects
  953. * @param {Mixed} value The value to clone.
  954. * @param {Boolean} deep A flag to indicate a deep clone.
  955. * @param {Object} [guard] Internally used to allow this method to work with
  956. * others like `_.map` without using their callback `index` argument for `deep`.
  957. * @param {Object} [data={}] Internally used to track traversed objects to avoid
  958. * circular references and indicate whether to perform a more thorough clone
  959. * of non-object values.
  960. * @returns {Mixed} Returns the cloned `value`.
  961. * @example
  962. *
  963. * var stooges = [
  964. * { 'name': 'moe', 'age': 40 },
  965. * { 'name': 'larry', 'age': 50 },
  966. * { 'name': 'curly', 'age': 60 }
  967. * ];
  968. *
  969. * _.clone({ 'name': 'moe' });
  970. * // => { 'name': 'moe' }
  971. *
  972. * var shallow = _.clone(stooges);
  973. * shallow[0] === stooges[0];
  974. * // => true
  975. *
  976. * var deep = _.clone(stooges, true);
  977. * shallow[0] === stooges[0];
  978. * // => false
  979. */
  980. function clone(value, deep, guard, data) {
  981. if (value == null) {
  982. return value;
  983. }
  984. if (guard) {
  985. deep = false;
  986. }
  987. // init internal data
  988. data || (data = { 'thorough': null });
  989. // avoid slower checks on primitives
  990. if (data.thorough == null) {
  991. // primitives passed from iframes use the primary document's native prototypes
  992. data.thorough = !!(BoolProto.clone || NumberProto.clone || StringProto.clone);
  993. }
  994. // use custom `clone` method if available
  995. var isObj = objectTypes[typeof value];
  996. if ((isObj || data.thorough) && value.clone && isFunction(value.clone)) {
  997. data.thorough = null;
  998. return value.clone(deep);
  999. }
  1000. // inspect [[Class]]
  1001. if (isObj) {
  1002. // don't clone `arguments` objects, functions, or non-object Objects
  1003. var className = toString.call(value);
  1004. if (!cloneableClasses[className] || (noArgsClass && isArguments(value))) {
  1005. return value;
  1006. }
  1007. var isArr = className == arrayClass;
  1008. isObj = isArr || (className == objectClass ? isPlainObject(value, true) : isObj);
  1009. }
  1010. // shallow clone
  1011. if (!isObj || !deep) {
  1012. // don't clone functions
  1013. return isObj
  1014. ? (isArr ? slice.call(value) : extend({}, value))
  1015. : value;
  1016. }
  1017. var ctor = value.constructor;
  1018. switch (className) {
  1019. case boolClass:
  1020. return new ctor(value == true);
  1021. case dateClass:
  1022. return new ctor(+value);
  1023. case numberClass:
  1024. case stringClass:
  1025. return new ctor(value);
  1026. case regexpClass:
  1027. return ctor(value.source, reFlags.exec(value));
  1028. }
  1029. var clones = data.clones || (data.clones = []),
  1030. sources = data.sources || (data.sources = []),
  1031. length = clones.length;
  1032. // check for circular references and return corresponding clone
  1033. while (length--) {
  1034. if (sources[length] == value) {
  1035. return clones[length];
  1036. }
  1037. }
  1038. // init cloned object
  1039. var result = isArr ? ctor(length = value.length) : {};
  1040. // add current clone and original source value to the stack of traversed objects
  1041. clones.push(result);
  1042. sources.push(value);
  1043. // recursively populate clone (susceptible to call stack limits)
  1044. if (isArr) {
  1045. var index = -1;
  1046. while (++index < length) {
  1047. result[index] = clone(value[index], deep, null, data);
  1048. }
  1049. } else {
  1050. forOwn(value, function(objValue, key) {
  1051. result[key] = clone(objValue, deep, null, data);
  1052. });
  1053. }
  1054. return result;
  1055. }
  1056. /**
  1057. * Assigns enumerable properties of the default object(s) to the `destination`
  1058. * object for all `destination` properties that resolve to `null`/`undefined`.
  1059. * Once a property is set, additional defaults of the same property will be
  1060. * ignored.
  1061. *
  1062. * @static
  1063. * @memberOf _
  1064. * @category Objects
  1065. * @param {Object} object The destination object.
  1066. * @param {Object} [default1, default2, ...] The default objects.
  1067. * @returns {Object} Returns the destination object.
  1068. * @example
  1069. *
  1070. * var iceCream = { 'flavor': 'chocolate' };
  1071. * _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
  1072. * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
  1073. */
  1074. var defaults = createIterator(extendIteratorOptions, {
  1075. 'inLoop': 'if (result[index] == null) ' + extendIteratorOptions.inLoop
  1076. });
  1077. /**
  1078. * Assigns enumerable properties of the source object(s) to the `destination`
  1079. * object. Subsequent sources will overwrite propery assignments of previous
  1080. * sources.
  1081. *
  1082. * @static
  1083. * @memberOf _
  1084. * @category Objects
  1085. * @param {Object} object The destination object.
  1086. * @param {Object} [source1, source2, ...] The source objects.
  1087. * @returns {Object} Returns the destination object.
  1088. * @example
  1089. *
  1090. * _.extend({ 'name': 'moe' }, { 'age': 40 });
  1091. * // => { 'name': 'moe', 'age': 40 }
  1092. */
  1093. var extend = createIterator(extendIteratorOptions);
  1094. /**
  1095. * Iterates over `object`'s own and inherited enumerable properties, executing
  1096. * the `callback` for each property. The `callback` is bound to `thisArg` and
  1097. * invoked with 3 arguments; (value, key, object). Callbacks may exit iteration
  1098. * early by explicitly returning `false`.
  1099. *
  1100. * @static
  1101. * @memberOf _
  1102. * @category Objects
  1103. * @param {Object} object The object to iterate over.
  1104. * @param {Function} callback The function called per iteration.
  1105. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1106. * @returns {Object} Returns `object`.
  1107. * @example
  1108. *
  1109. * function Dog(name) {
  1110. * this.name = name;
  1111. * }
  1112. *
  1113. * Dog.prototype.bark = function() {
  1114. * alert('Woof, woof!');
  1115. * };
  1116. *
  1117. * _.forIn(new Dog('Dagny'), function(value, key) {
  1118. * alert(key);
  1119. * });
  1120. * // => alerts 'name' and 'bark' (order is not guaranteed)
  1121. */
  1122. var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
  1123. 'useHas': false
  1124. });
  1125. /**
  1126. * Iterates over `object`'s own enumerable properties, executing the `callback`
  1127. * for each property. The `callback` is bound to `thisArg` and invoked with 3
  1128. * arguments; (value, key, object). Callbacks may exit iteration early by
  1129. * explicitly returning `false`.
  1130. *
  1131. * @static
  1132. * @memberOf _
  1133. * @category Objects
  1134. * @param {Object} object The object to iterate over.
  1135. * @param {Function} callback The function called per iteration.
  1136. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1137. * @returns {Object} Returns `object`.
  1138. * @example
  1139. *
  1140. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  1141. * alert(key);
  1142. * });
  1143. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  1144. */
  1145. var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
  1146. /**
  1147. * Creates a sorted array of all enumerable properties, own and inherited,
  1148. * of `object` that have function values.
  1149. *
  1150. * @static
  1151. * @memberOf _
  1152. * @alias methods
  1153. * @category Objects
  1154. * @param {Object} object The object to inspect.
  1155. * @returns {Array} Returns a new array of property names that have function values.
  1156. * @example
  1157. *
  1158. * _.functions(_);
  1159. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1160. */
  1161. var functions = createIterator({
  1162. 'useHas': false,
  1163. 'args': 'object',
  1164. 'init': '[]',
  1165. 'inLoop': 'if (isFunction(value)) result.push(index)',
  1166. 'bottom': 'result.sort()'
  1167. });
  1168. /**
  1169. * Checks if the specified object `property` exists and is a direct property,
  1170. * instead of an inherited property.
  1171. *
  1172. * @static
  1173. * @memberOf _
  1174. * @category Objects
  1175. * @param {Object} object The object to check.
  1176. * @param {String} property The property to check for.
  1177. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  1178. * @example
  1179. *
  1180. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1181. * // => true
  1182. */
  1183. function has(object, property) {
  1184. return object ? hasOwnProperty.call(object, property) : false;
  1185. }
  1186. /**
  1187. * Creates an object composed of the inverted keys and values of the given `object`.
  1188. *
  1189. * @static
  1190. * @memberOf _
  1191. * @category Objects
  1192. * @param {Object} object The object to invert.
  1193. * @returns {Object} Returns the created inverted object.
  1194. * @example
  1195. *
  1196. * _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' });
  1197. * // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
  1198. */
  1199. var invert = createIterator({
  1200. 'args': 'object',
  1201. 'init': '{}',
  1202. 'inLoop': 'result[value] = index'
  1203. });
  1204. /**
  1205. * Checks if `value` is a boolean (`true` or `false`) value.
  1206. *
  1207. * @static
  1208. * @memberOf _
  1209. * @category Objects
  1210. * @param {Mixed} value The value to check.
  1211. * @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1212. * @example
  1213. *
  1214. * _.isBoolean(null);
  1215. * // => false
  1216. */
  1217. function isBoolean(value) {
  1218. return value === true || value === false || toString.call(value) == boolClass;
  1219. }
  1220. /**
  1221. * Checks if `value` is a date.
  1222. *
  1223. * @static
  1224. * @memberOf _
  1225. * @category Objects
  1226. * @param {Mixed} value The value to check.
  1227. * @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
  1228. * @example
  1229. *
  1230. * _.isDate(new Date);
  1231. * // => true
  1232. */
  1233. function isDate(value) {
  1234. return toString.call(value) == dateClass;
  1235. }
  1236. /**
  1237. * Checks if `value` is a DOM element.
  1238. *
  1239. * @static
  1240. * @memberOf _
  1241. * @category Objects
  1242. * @param {Mixed} value The value to check.
  1243. * @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1244. * @example
  1245. *
  1246. * _.isElement(document.body);
  1247. * // => true
  1248. */
  1249. function isElement(value) {
  1250. return value ? value.nodeType === 1 : false;
  1251. }
  1252. /**
  1253. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1254. * length of `0` and objects with no own enumerable properties are considered
  1255. * "empty".
  1256. *
  1257. * @static
  1258. * @memberOf _
  1259. * @category Objects
  1260. * @param {Array|Object|String} value The value to inspect.
  1261. * @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
  1262. * @example
  1263. *
  1264. * _.isEmpty([1, 2, 3]);
  1265. * // => false
  1266. *
  1267. * _.isEmpty({});
  1268. * // => true
  1269. *
  1270. * _.isEmpty('');
  1271. * // => true
  1272. */
  1273. var isEmpty = createIterator({
  1274. 'args': 'value',
  1275. 'init': 'true',
  1276. 'top':
  1277. 'var className = toString.call(value),\n' +
  1278. ' length = value.length;\n' +
  1279. 'if (arrayLikeClasses[className]' +
  1280. (noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
  1281. ' (className == objectClass && length > -1 && length === length >>> 0 &&\n' +
  1282. ' isFunction(value.splice))' +
  1283. ') return !length',
  1284. 'inLoop': {
  1285. 'object': 'return false'
  1286. }
  1287. });
  1288. /**
  1289. * Performs a deep comparison between two values to determine if they are
  1290. * equivalent to each other. If a value has an `isEqual` method it will be
  1291. * used to perform the comparison.
  1292. *
  1293. * @static
  1294. * @memberOf _
  1295. * @category Objects
  1296. * @param {Mixed} a The value to compare.
  1297. * @param {Mixed} b The other value to compare.
  1298. * @param {Object} [data={}] Internally used track traversed objects to avoid
  1299. * circular references and indicate whether to perform a more thorough comparison
  1300. * of non-object values.
  1301. * @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
  1302. * @example
  1303. *
  1304. * var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1305. * var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1306. *
  1307. * moe == clone;
  1308. * // => false
  1309. *
  1310. * _.isEqual(moe, clone);
  1311. * // => true
  1312. */
  1313. function isEqual(a, b, data) {
  1314. // a strict comparison is necessary because `null == undefined`
  1315. if (a == null || b == null) {
  1316. return a === b;
  1317. }
  1318. // init internal data
  1319. data || (data = { 'thorough': null });
  1320. // avoid slower checks on non-objects
  1321. if (data.thorough == null) {
  1322. // primitives passed from iframes use the primary document's native prototypes
  1323. data.thorough = !!(BoolProto.isEqual || NumberProto.isEqual || StringProto.isEqual);
  1324. }
  1325. if (objectTypes[typeof a] || objectTypes[typeof b] || data.thorough) {
  1326. // unwrap any LoDash wrapped values
  1327. a = a.__wrapped__ || a;
  1328. b = b.__wrapped__ || b;
  1329. // use custom `isEqual` method if available
  1330. if (a.isEqual && isFunction(a.isEqual)) {
  1331. data.thorough = null;
  1332. return a.isEqual(b);
  1333. }
  1334. if (b.isEqual && isFunction(b.isEqual)) {
  1335. data.thorough = null;
  1336. return b.isEqual(a);
  1337. }
  1338. }
  1339. // exit early for identical values
  1340. if (a === b) {
  1341. // treat `+0` vs. `-0` as not equal
  1342. return a !== 0 || (1 / a == 1 / b);
  1343. }
  1344. // compare [[Class]] names
  1345. var className = toString.call(a);
  1346. if (className != toString.call(b)) {
  1347. return false;
  1348. }
  1349. switch (className) {
  1350. case boolClass:
  1351. case dateClass:
  1352. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  1353. // to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
  1354. return +a == +b;
  1355. case numberClass:
  1356. // treat `NaN` vs. `NaN` as equal
  1357. return a != +a
  1358. ? b != +b
  1359. // but treat `+0` vs. `-0` as not equal
  1360. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  1361. case regexpClass:
  1362. case stringClass:
  1363. // coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
  1364. // treat string primitives and their corresponding object instances as equal
  1365. return a == b + '';
  1366. }
  1367. // exit early, in older browsers, if `a` is array-like but not `b`
  1368. var isArr = arrayLikeClasses[className];
  1369. if (noArgsClass && !isArr && (isArr = isArguments(a)) && !isArguments(b)) {
  1370. return false;
  1371. }
  1372. // exit for functions and DOM nodes
  1373. if (!isArr && (className != objectClass || (noNodeClass && (
  1374. (typeof a.toString != 'function' && typeof (a + '') == 'string') ||
  1375. (typeof b.toString != 'function' && typeof (b + '') == 'string'))))) {
  1376. return false;
  1377. }
  1378. // assume cyclic structures are equal
  1379. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  1380. // section 15.12.3, abstract operation