UNPKG

11.7 kBJavaScriptView Raw
1/**
2 * lodash 4.1.0 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modularize exports="npm" -o ./`
4 * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9var baseIsEqual = require('lodash._baseisequal'),
10 baseIsMatch = require('lodash._baseismatch'),
11 basePullAt = require('lodash._basepullat'),
12 get = require('lodash.get'),
13 hasIn = require('lodash.hasin'),
14 root = require('lodash._root'),
15 toPairs = require('lodash.topairs');
16
17/** Used to compose bitmasks for comparison styles. */
18var UNORDERED_COMPARE_FLAG = 1,
19 PARTIAL_COMPARE_FLAG = 2;
20
21/** Used as references for various `Number` constants. */
22var INFINITY = 1 / 0;
23
24/** `Object#toString` result references. */
25var symbolTag = '[object Symbol]';
26
27/** Used to match property names within property paths. */
28var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
29 reIsPlainProp = /^\w*$/,
30 rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/g;
31
32/** Used to match backslashes in property paths. */
33var reEscapeChar = /\\(\\)?/g;
34
35/** Used for built-in method references. */
36var objectProto = Object.prototype;
37
38/**
39 * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
40 * of values.
41 */
42var objectToString = objectProto.toString;
43
44/** Built-in value references. */
45var Symbol = root.Symbol;
46
47/** Used to convert symbols to primitives and strings. */
48var symbolProto = Symbol ? Symbol.prototype : undefined,
49 symbolToString = Symbol ? symbolProto.toString : undefined;
50
51/**
52 * The base implementation of `_.get` without support for default values.
53 *
54 * @private
55 * @param {Object} object The object to query.
56 * @param {Array|string} path The path of the property to get.
57 * @returns {*} Returns the resolved value.
58 */
59function baseGet(object, path) {
60 path = isKey(path, object) ? [path + ''] : baseToPath(path);
61
62 var index = 0,
63 length = path.length;
64
65 while (object != null && index < length) {
66 object = object[path[index++]];
67 }
68 return (index && index == length) ? object : undefined;
69}
70
71/**
72 * The base implementation of `_.iteratee`.
73 *
74 * @private
75 * @param {*} [value=_.identity] The value to convert to an iteratee.
76 * @returns {Function} Returns the iteratee.
77 */
78function baseIteratee(value) {
79 var type = typeof value;
80 if (type == 'function') {
81 return value;
82 }
83 if (value == null) {
84 return identity;
85 }
86 if (type == 'object') {
87 return isArray(value)
88 ? baseMatchesProperty(value[0], value[1])
89 : baseMatches(value);
90 }
91 return property(value);
92}
93
94/**
95 * The base implementation of `_.matches` which doesn't clone `source`.
96 *
97 * @private
98 * @param {Object} source The object of property values to match.
99 * @returns {Function} Returns the new function.
100 */
101function baseMatches(source) {
102 var matchData = getMatchData(source);
103 if (matchData.length == 1 && matchData[0][2]) {
104 var key = matchData[0][0],
105 value = matchData[0][1];
106
107 return function(object) {
108 if (object == null) {
109 return false;
110 }
111 return object[key] === value &&
112 (value !== undefined || (key in Object(object)));
113 };
114 }
115 return function(object) {
116 return object === source || baseIsMatch(object, source, matchData);
117 };
118}
119
120/**
121 * The base implementation of `_.matchesProperty` which doesn't clone `srcValue`.
122 *
123 * @private
124 * @param {string} path The path of the property to get.
125 * @param {*} srcValue The value to match.
126 * @returns {Function} Returns the new function.
127 */
128function baseMatchesProperty(path, srcValue) {
129 return function(object) {
130 var objValue = get(object, path);
131 return (objValue === undefined && objValue === srcValue)
132 ? hasIn(object, path)
133 : baseIsEqual(srcValue, objValue, undefined, UNORDERED_COMPARE_FLAG | PARTIAL_COMPARE_FLAG);
134 };
135}
136
137/**
138 * The base implementation of `_.property` without support for deep paths.
139 *
140 * @private
141 * @param {string} key The key of the property to get.
142 * @returns {Function} Returns the new function.
143 */
144function baseProperty(key) {
145 return function(object) {
146 return object == null ? undefined : object[key];
147 };
148}
149
150/**
151 * A specialized version of `baseProperty` which supports deep paths.
152 *
153 * @private
154 * @param {Array|string} path The path of the property to get.
155 * @returns {Function} Returns the new function.
156 */
157function basePropertyDeep(path) {
158 return function(object) {
159 return baseGet(object, path);
160 };
161}
162
163/**
164 * The base implementation of `_.toPath` which only converts `value` to a
165 * path if it's not one.
166 *
167 * @private
168 * @param {*} value The value to process.
169 * @returns {Array} Returns the property path array.
170 */
171function baseToPath(value) {
172 return isArray(value) ? value : stringToPath(value);
173}
174
175/**
176 * Gets the property names, values, and compare flags of `object`.
177 *
178 * @private
179 * @param {Object} object The object to query.
180 * @returns {Array} Returns the match data of `object`.
181 */
182function getMatchData(object) {
183 var result = toPairs(object),
184 length = result.length;
185
186 while (length--) {
187 result[length][2] = isStrictComparable(result[length][1]);
188 }
189 return result;
190}
191
192/**
193 * Checks if `value` is a property name and not a property path.
194 *
195 * @private
196 * @param {*} value The value to check.
197 * @param {Object} [object] The object to query keys on.
198 * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
199 */
200function isKey(value, object) {
201 if (typeof value == 'number') {
202 return true;
203 }
204 return !isArray(value) &&
205 (reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
206 (object != null && value in Object(object)));
207}
208
209/**
210 * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
211 *
212 * @private
213 * @param {*} value The value to check.
214 * @returns {boolean} Returns `true` if `value` if suitable for strict
215 * equality comparisons, else `false`.
216 */
217function isStrictComparable(value) {
218 return value === value && !isObject(value);
219}
220
221/**
222 * Converts `string` to a property path array.
223 *
224 * @private
225 * @param {string} string The string to convert.
226 * @returns {Array} Returns the property path array.
227 */
228function stringToPath(string) {
229 var result = [];
230 toString(string).replace(rePropName, function(match, number, quote, string) {
231 result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
232 });
233 return result;
234}
235
236/**
237 * Removes all elements from `array` that `predicate` returns truthy for
238 * and returns an array of the removed elements. The predicate is invoked with
239 * three arguments: (value, index, array).
240 *
241 * **Note:** Unlike `_.filter`, this method mutates `array`.
242 *
243 * @static
244 * @memberOf _
245 * @category Array
246 * @param {Array} array The array to modify.
247 * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
248 * @returns {Array} Returns the new array of removed elements.
249 * @example
250 *
251 * var array = [1, 2, 3, 4];
252 * var evens = _.remove(array, function(n) {
253 * return n % 2 == 0;
254 * });
255 *
256 * console.log(array);
257 * // => [1, 3]
258 *
259 * console.log(evens);
260 * // => [2, 4]
261 */
262function remove(array, predicate) {
263 var result = [];
264 if (!(array && array.length)) {
265 return result;
266 }
267 var index = -1,
268 indexes = [],
269 length = array.length;
270
271 predicate = baseIteratee(predicate, 3);
272 while (++index < length) {
273 var value = array[index];
274 if (predicate(value, index, array)) {
275 result.push(value);
276 indexes.push(index);
277 }
278 }
279 basePullAt(array, indexes);
280 return result;
281}
282
283/**
284 * Checks if `value` is classified as an `Array` object.
285 *
286 * @static
287 * @memberOf _
288 * @type Function
289 * @category Lang
290 * @param {*} value The value to check.
291 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
292 * @example
293 *
294 * _.isArray([1, 2, 3]);
295 * // => true
296 *
297 * _.isArray(document.body.children);
298 * // => false
299 *
300 * _.isArray('abc');
301 * // => false
302 *
303 * _.isArray(_.noop);
304 * // => false
305 */
306var isArray = Array.isArray;
307
308/**
309 * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
310 * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
311 *
312 * @static
313 * @memberOf _
314 * @category Lang
315 * @param {*} value The value to check.
316 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
317 * @example
318 *
319 * _.isObject({});
320 * // => true
321 *
322 * _.isObject([1, 2, 3]);
323 * // => true
324 *
325 * _.isObject(_.noop);
326 * // => true
327 *
328 * _.isObject(null);
329 * // => false
330 */
331function isObject(value) {
332 var type = typeof value;
333 return !!value && (type == 'object' || type == 'function');
334}
335
336/**
337 * Checks if `value` is object-like. A value is object-like if it's not `null`
338 * and has a `typeof` result of "object".
339 *
340 * @static
341 * @memberOf _
342 * @category Lang
343 * @param {*} value The value to check.
344 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
345 * @example
346 *
347 * _.isObjectLike({});
348 * // => true
349 *
350 * _.isObjectLike([1, 2, 3]);
351 * // => true
352 *
353 * _.isObjectLike(_.noop);
354 * // => false
355 *
356 * _.isObjectLike(null);
357 * // => false
358 */
359function isObjectLike(value) {
360 return !!value && typeof value == 'object';
361}
362
363/**
364 * Checks if `value` is classified as a `Symbol` primitive or object.
365 *
366 * @static
367 * @memberOf _
368 * @category Lang
369 * @param {*} value The value to check.
370 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
371 * @example
372 *
373 * _.isSymbol(Symbol.iterator);
374 * // => true
375 *
376 * _.isSymbol('abc');
377 * // => false
378 */
379function isSymbol(value) {
380 return typeof value == 'symbol' ||
381 (isObjectLike(value) && objectToString.call(value) == symbolTag);
382}
383
384/**
385 * Converts `value` to a string if it's not one. An empty string is returned
386 * for `null` and `undefined` values. The sign of `-0` is preserved.
387 *
388 * @static
389 * @memberOf _
390 * @category Lang
391 * @param {*} value The value to process.
392 * @returns {string} Returns the string.
393 * @example
394 *
395 * _.toString(null);
396 * // => ''
397 *
398 * _.toString(-0);
399 * // => '-0'
400 *
401 * _.toString([1, 2, 3]);
402 * // => '1,2,3'
403 */
404function toString(value) {
405 // Exit early for strings to avoid a performance hit in some environments.
406 if (typeof value == 'string') {
407 return value;
408 }
409 if (value == null) {
410 return '';
411 }
412 if (isSymbol(value)) {
413 return Symbol ? symbolToString.call(value) : '';
414 }
415 var result = (value + '');
416 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
417}
418
419/**
420 * This method returns the first argument provided to it.
421 *
422 * @static
423 * @memberOf _
424 * @category Util
425 * @param {*} value Any value.
426 * @returns {*} Returns `value`.
427 * @example
428 *
429 * var object = { 'user': 'fred' };
430 *
431 * _.identity(object) === object;
432 * // => true
433 */
434function identity(value) {
435 return value;
436}
437
438/**
439 * Creates a function that returns the value at `path` of a given object.
440 *
441 * @static
442 * @memberOf _
443 * @category Util
444 * @param {Array|string} path The path of the property to get.
445 * @returns {Function} Returns the new function.
446 * @example
447 *
448 * var objects = [
449 * { 'a': { 'b': { 'c': 2 } } },
450 * { 'a': { 'b': { 'c': 1 } } }
451 * ];
452 *
453 * _.map(objects, _.property('a.b.c'));
454 * // => [2, 1]
455 *
456 * _.map(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
457 * // => [1, 2]
458 */
459function property(path) {
460 return isKey(path) ? baseProperty(path) : basePropertyDeep(path);
461}
462
463module.exports = remove;