UNPKG

8.17 kBJavaScriptView Raw
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
10/** Used as references for various `Number` constants. */
11var INFINITY = 1 / 0,
12 MAX_INTEGER = 1.7976931348623157e+308,
13 NAN = 0 / 0;
14
15/** `Object#toString` result references. */
16var symbolTag = '[object Symbol]';
17
18/** Used to match leading and trailing whitespace. */
19var reTrim = /^\s+|\s+$/g;
20
21/** Used to detect bad signed hexadecimal string values. */
22var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
23
24/** Used to detect binary string values. */
25var reIsBinary = /^0b[01]+$/i;
26
27/** Used to detect octal string values. */
28var reIsOctal = /^0o[0-7]+$/i;
29
30/** Built-in method references without a dependency on `root`. */
31var freeParseInt = parseInt;
32
33/**
34 * The base implementation of `_.findIndex` and `_.findLastIndex` without
35 * support for iteratee shorthands.
36 *
37 * @private
38 * @param {Array} array The array to inspect.
39 * @param {Function} predicate The function invoked per iteration.
40 * @param {number} fromIndex The index to search from.
41 * @param {boolean} [fromRight] Specify iterating from right to left.
42 * @returns {number} Returns the index of the matched value, else `-1`.
43 */
44function baseFindIndex(array, predicate, fromIndex, fromRight) {
45 var length = array.length,
46 index = fromIndex + (fromRight ? 1 : -1);
47
48 while ((fromRight ? index-- : ++index < length)) {
49 if (predicate(array[index], index, array)) {
50 return index;
51 }
52 }
53 return -1;
54}
55
56/**
57 * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
58 *
59 * @private
60 * @param {Array} array The array to inspect.
61 * @param {*} value The value to search for.
62 * @param {number} fromIndex The index to search from.
63 * @returns {number} Returns the index of the matched value, else `-1`.
64 */
65function baseIndexOf(array, value, fromIndex) {
66 if (value !== value) {
67 return baseFindIndex(array, baseIsNaN, fromIndex);
68 }
69 var index = fromIndex - 1,
70 length = array.length;
71
72 while (++index < length) {
73 if (array[index] === value) {
74 return index;
75 }
76 }
77 return -1;
78}
79
80/**
81 * The base implementation of `_.isNaN` without support for number objects.
82 *
83 * @private
84 * @param {*} value The value to check.
85 * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
86 */
87function baseIsNaN(value) {
88 return value !== value;
89}
90
91/** Used for built-in method references. */
92var objectProto = Object.prototype;
93
94/**
95 * Used to resolve the
96 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
97 * of values.
98 */
99var objectToString = objectProto.toString;
100
101/* Built-in method references for those with the same name as other `lodash` methods. */
102var nativeMax = Math.max;
103
104/**
105 * Gets the index at which the first occurrence of `value` is found in `array`
106 * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
107 * for equality comparisons. If `fromIndex` is negative, it's used as the
108 * offset from the end of `array`.
109 *
110 * @static
111 * @memberOf _
112 * @since 0.1.0
113 * @category Array
114 * @param {Array} array The array to inspect.
115 * @param {*} value The value to search for.
116 * @param {number} [fromIndex=0] The index to search from.
117 * @returns {number} Returns the index of the matched value, else `-1`.
118 * @example
119 *
120 * _.indexOf([1, 2, 1, 2], 2);
121 * // => 1
122 *
123 * // Search from the `fromIndex`.
124 * _.indexOf([1, 2, 1, 2], 2, 2);
125 * // => 3
126 */
127function indexOf(array, value, fromIndex) {
128 var length = array ? array.length : 0;
129 if (!length) {
130 return -1;
131 }
132 var index = fromIndex == null ? 0 : toInteger(fromIndex);
133 if (index < 0) {
134 index = nativeMax(length + index, 0);
135 }
136 return baseIndexOf(array, value, index);
137}
138
139/**
140 * Checks if `value` is the
141 * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
142 * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
143 *
144 * @static
145 * @memberOf _
146 * @since 0.1.0
147 * @category Lang
148 * @param {*} value The value to check.
149 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
150 * @example
151 *
152 * _.isObject({});
153 * // => true
154 *
155 * _.isObject([1, 2, 3]);
156 * // => true
157 *
158 * _.isObject(_.noop);
159 * // => true
160 *
161 * _.isObject(null);
162 * // => false
163 */
164function isObject(value) {
165 var type = typeof value;
166 return !!value && (type == 'object' || type == 'function');
167}
168
169/**
170 * Checks if `value` is object-like. A value is object-like if it's not `null`
171 * and has a `typeof` result of "object".
172 *
173 * @static
174 * @memberOf _
175 * @since 4.0.0
176 * @category Lang
177 * @param {*} value The value to check.
178 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
179 * @example
180 *
181 * _.isObjectLike({});
182 * // => true
183 *
184 * _.isObjectLike([1, 2, 3]);
185 * // => true
186 *
187 * _.isObjectLike(_.noop);
188 * // => false
189 *
190 * _.isObjectLike(null);
191 * // => false
192 */
193function isObjectLike(value) {
194 return !!value && typeof value == 'object';
195}
196
197/**
198 * Checks if `value` is classified as a `Symbol` primitive or object.
199 *
200 * @static
201 * @memberOf _
202 * @since 4.0.0
203 * @category Lang
204 * @param {*} value The value to check.
205 * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
206 * @example
207 *
208 * _.isSymbol(Symbol.iterator);
209 * // => true
210 *
211 * _.isSymbol('abc');
212 * // => false
213 */
214function isSymbol(value) {
215 return typeof value == 'symbol' ||
216 (isObjectLike(value) && objectToString.call(value) == symbolTag);
217}
218
219/**
220 * Converts `value` to a finite number.
221 *
222 * @static
223 * @memberOf _
224 * @since 4.12.0
225 * @category Lang
226 * @param {*} value The value to convert.
227 * @returns {number} Returns the converted number.
228 * @example
229 *
230 * _.toFinite(3.2);
231 * // => 3.2
232 *
233 * _.toFinite(Number.MIN_VALUE);
234 * // => 5e-324
235 *
236 * _.toFinite(Infinity);
237 * // => 1.7976931348623157e+308
238 *
239 * _.toFinite('3.2');
240 * // => 3.2
241 */
242function toFinite(value) {
243 if (!value) {
244 return value === 0 ? value : 0;
245 }
246 value = toNumber(value);
247 if (value === INFINITY || value === -INFINITY) {
248 var sign = (value < 0 ? -1 : 1);
249 return sign * MAX_INTEGER;
250 }
251 return value === value ? value : 0;
252}
253
254/**
255 * Converts `value` to an integer.
256 *
257 * **Note:** This method is loosely based on
258 * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
259 *
260 * @static
261 * @memberOf _
262 * @since 4.0.0
263 * @category Lang
264 * @param {*} value The value to convert.
265 * @returns {number} Returns the converted integer.
266 * @example
267 *
268 * _.toInteger(3.2);
269 * // => 3
270 *
271 * _.toInteger(Number.MIN_VALUE);
272 * // => 0
273 *
274 * _.toInteger(Infinity);
275 * // => 1.7976931348623157e+308
276 *
277 * _.toInteger('3.2');
278 * // => 3
279 */
280function toInteger(value) {
281 var result = toFinite(value),
282 remainder = result % 1;
283
284 return result === result ? (remainder ? result - remainder : result) : 0;
285}
286
287/**
288 * Converts `value` to a number.
289 *
290 * @static
291 * @memberOf _
292 * @since 4.0.0
293 * @category Lang
294 * @param {*} value The value to process.
295 * @returns {number} Returns the number.
296 * @example
297 *
298 * _.toNumber(3.2);
299 * // => 3.2
300 *
301 * _.toNumber(Number.MIN_VALUE);
302 * // => 5e-324
303 *
304 * _.toNumber(Infinity);
305 * // => Infinity
306 *
307 * _.toNumber('3.2');
308 * // => 3.2
309 */
310function toNumber(value) {
311 if (typeof value == 'number') {
312 return value;
313 }
314 if (isSymbol(value)) {
315 return NAN;
316 }
317 if (isObject(value)) {
318 var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
319 value = isObject(other) ? (other + '') : other;
320 }
321 if (typeof value != 'string') {
322 return value === 0 ? value : +value;
323 }
324 value = value.replace(reTrim, '');
325 var isBinary = reIsBinary.test(value);
326 return (isBinary || reIsOctal.test(value))
327 ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
328 : (reIsBadHex.test(value) ? NAN : +value);
329}
330
331module.exports = indexOf;