• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
9var getNative = require('lodash._getnative');
10
11/** Native method references. */
12var Set = getNative(global, 'Set');
13
14/* Native method references for those with the same name as other `lodash` methods. */
15var nativeCreate = getNative(Object, 'create');
16
17/**
18 *
19 * Creates a cache object to store unique values.
20 *
21 * @private
22 * @param {Array} [values] The values to cache.
23 */
24function SetCache(values) {
25  var length = values ? values.length : 0;
26
27  this.data = { 'hash': nativeCreate(null), 'set': new Set };
28  while (length--) {
29    this.push(values[length]);
30  }
31}
32
33/**
34 * Adds `value` to the cache.
35 *
36 * @private
37 * @name push
38 * @memberOf SetCache
39 * @param {*} value The value to cache.
40 */
41function cachePush(value) {
42  var data = this.data;
43  if (typeof value == 'string' || isObject(value)) {
44    data.set.add(value);
45  } else {
46    data.hash[value] = true;
47  }
48}
49
50/**
51 * Creates a `Set` cache object to optimize linear searches of large arrays.
52 *
53 * @private
54 * @param {Array} [values] The values to cache.
55 * @returns {null|Object} Returns the new cache object if `Set` is supported, else `null`.
56 */
57function createCache(values) {
58  return (nativeCreate && Set) ? new SetCache(values) : null;
59}
60
61/**
62 * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
63 * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
64 *
65 * @static
66 * @memberOf _
67 * @category Lang
68 * @param {*} value The value to check.
69 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
70 * @example
71 *
72 * _.isObject({});
73 * // => true
74 *
75 * _.isObject([1, 2, 3]);
76 * // => true
77 *
78 * _.isObject(1);
79 * // => false
80 */
81function isObject(value) {
82  // Avoid a V8 JIT bug in Chrome 19-20.
83  // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
84  var type = typeof value;
85  return !!value && (type == 'object' || type == 'function');
86}
87
88// Add functions to the `Set` cache.
89SetCache.prototype.push = cachePush;
90
91module.exports = createCache;
92