UNPKG

54.1 kBTypeScriptView Raw
1declare module '@ember/array' {
2 import Mixin from '@ember/object/mixin';
3 import Enumerable from '@ember/enumerable';
4 import MutableEnumerable from '@ember/enumerable/mutable';
5 import Observable from '@ember/object/observable';
6 import type { MethodNamesOf, MethodParams, MethodReturns } from '@ember/-internals/utility-types';
7 export { default as makeArray } from '@ember/array/make';
8 export type EmberArrayLike<T> = EmberArray<T> | NativeArray<T>;
9 export function uniqBy<T>(
10 array: T[] | EmberArray<T>,
11 keyOrFunc?: string | ((item: T) => unknown)
12 ): T[] | EmberArray<T>;
13 export function removeAt<T, A extends T[] | MutableArray<T>>(
14 array: A,
15 index: number,
16 len?: number
17 ): A;
18 /**
19 Returns true if the passed object is an array or Array-like.
20
21 Objects are considered Array-like if any of the following are true:
22
23 - the object is a native Array
24 - the object has an objectAt property
25 - the object is an Object, and has a length property
26
27 Unlike `typeOf` this method returns true even if the passed object is
28 not formally an array but appears to be array-like (i.e. implements `Array`)
29
30 ```javascript
31 import { isArray } from '@ember/array';
32 import ArrayProxy from '@ember/array/proxy';
33
34 isArray(); // false
35 isArray([]); // true
36 isArray(ArrayProxy.create({ content: [] })); // true
37 ```
38
39 @method isArray
40 @static
41 @for @ember/array
42 @param {Object} obj The object to test
43 @return {Boolean} true if the passed object is an array or Array-like
44 @public
45 */
46 export function isArray(obj: unknown): obj is ArrayLike<unknown> | EmberArray<unknown>;
47 /**
48 This mixin implements Observer-friendly Array-like behavior. It is not a
49 concrete implementation, but it can be used up by other classes that want
50 to appear like arrays.
51
52 For example, ArrayProxy is a concrete class that can be instantiated to
53 implement array-like behavior. This class uses the Array Mixin by way of
54 the MutableArray mixin, which allows observable changes to be made to the
55 underlying array.
56
57 This mixin defines methods specifically for collections that provide
58 index-ordered access to their contents. When you are designing code that
59 needs to accept any kind of Array-like object, you should use these methods
60 instead of Array primitives because these will properly notify observers of
61 changes to the array.
62
63 Although these methods are efficient, they do add a layer of indirection to
64 your application so it is a good idea to use them only when you need the
65 flexibility of using both true JavaScript arrays and "virtual" arrays such
66 as controllers and collections.
67
68 You can use the methods defined in this module to access and modify array
69 contents in an observable-friendly way. You can also be notified whenever
70 the membership of an array changes by using `.observes('myArray.[]')`.
71
72 To support `EmberArray` in your own class, you must override two
73 primitives to use it: `length()` and `objectAt()`.
74
75 @class EmberArray
76 @uses Enumerable
77 @since Ember 0.9.0
78 @public
79 */
80 interface EmberArray<T> extends Enumerable {
81 /**
82 __Required.__ You must implement this method to apply this mixin.
83
84 Your array must support the `length` property. Your replace methods should
85 set this property whenever it changes.
86
87 @property {Number} length
88 @public
89 */
90 length: number;
91 /**
92 Returns the object at the given `index`. If the given `index` is negative
93 or is greater or equal than the array length, returns `undefined`.
94
95 This is one of the primitives you must implement to support `EmberArray`.
96 If your object supports retrieving the value of an array item using `get()`
97 (i.e. `myArray.get(0)`), then you do not need to implement this method
98 yourself.
99
100 ```javascript
101 let arr = ['a', 'b', 'c', 'd'];
102
103 arr.objectAt(0); // 'a'
104 arr.objectAt(3); // 'd'
105 arr.objectAt(-1); // undefined
106 arr.objectAt(4); // undefined
107 arr.objectAt(5); // undefined
108 ```
109
110 @method objectAt
111 @param {Number} idx The index of the item to return.
112 @return {*} item at index or undefined
113 @public
114 */
115 objectAt(idx: number): T | undefined;
116 /**
117 This returns the objects at the specified indexes, using `objectAt`.
118
119 ```javascript
120 let arr = ['a', 'b', 'c', 'd'];
121
122 arr.objectsAt([0, 1, 2]); // ['a', 'b', 'c']
123 arr.objectsAt([2, 3, 4]); // ['c', 'd', undefined]
124 ```
125
126 @method objectsAt
127 @param {Array} indexes An array of indexes of items to return.
128 @return {Array}
129 @public
130 */
131 objectsAt(indexes: number[]): Array<T | undefined>;
132 /**
133 This is the handler for the special array content property. If you get
134 this property, it will return this. If you set this property to a new
135 array, it will replace the current content.
136
137 ```javascript
138 let peopleToMoon = ['Armstrong', 'Aldrin'];
139
140 peopleToMoon.get('[]'); // ['Armstrong', 'Aldrin']
141
142 peopleToMoon.set('[]', ['Collins']); // ['Collins']
143 peopleToMoon.get('[]'); // ['Collins']
144 ```
145
146 @property []
147 @return this
148 @public
149 */
150 get '[]'(): this;
151 set '[]'(newValue: T[] | EmberArray<T>);
152 /**
153 The first object in the array, or `undefined` if the array is empty.
154
155 ```javascript
156 let vowels = ['a', 'e', 'i', 'o', 'u'];
157 vowels.firstObject; // 'a'
158
159 vowels.shiftObject();
160 vowels.firstObject; // 'e'
161
162 vowels.reverseObjects();
163 vowels.firstObject; // 'u'
164
165 vowels.clear();
166 vowels.firstObject; // undefined
167 ```
168
169 @property firstObject
170 @return {Object | undefined} The first object in the array
171 @public
172 */
173 firstObject: T | undefined;
174 /**
175 The last object in the array, or `undefined` if the array is empty.
176
177 @property lastObject
178 @return {Object | undefined} The last object in the array
179 @public
180 */
181 lastObject: T | undefined;
182 /**
183 Returns a new array that is a slice of the receiver. This implementation
184 uses the observable array methods to retrieve the objects for the new
185 slice.
186
187 ```javascript
188 let arr = ['red', 'green', 'blue'];
189
190 arr.slice(0); // ['red', 'green', 'blue']
191 arr.slice(0, 2); // ['red', 'green']
192 arr.slice(1, 100); // ['green', 'blue']
193 ```
194
195 @method slice
196 @param {Number} beginIndex (Optional) index to begin slicing from.
197 @param {Number} endIndex (Optional) index to end the slice at (but not included).
198 @return {Array} New array with specified slice
199 @public
200 */
201 slice(beginIndex?: number, endIndex?: number): NativeArray<T>;
202 /**
203 Used to determine the passed object's first occurrence in the array.
204 Returns the index if found, -1 if no match is found.
205
206 The optional `startAt` argument can be used to pass a starting
207 index to search from, effectively slicing the searchable portion
208 of the array. If it's negative it will add the array length to
209 the startAt value passed in as the index to search from. If less
210 than or equal to `-1 * array.length` the entire array is searched.
211
212 ```javascript
213 let arr = ['a', 'b', 'c', 'd', 'a'];
214
215 arr.indexOf('a'); // 0
216 arr.indexOf('z'); // -1
217 arr.indexOf('a', 2); // 4
218 arr.indexOf('a', -1); // 4, equivalent to indexOf('a', 4)
219 arr.indexOf('a', -100); // 0, searches entire array
220 arr.indexOf('b', 3); // -1
221 arr.indexOf('a', 100); // -1
222
223 let people = [{ name: 'Zoey' }, { name: 'Bob' }]
224 let newPerson = { name: 'Tom' };
225 people = [newPerson, ...people, newPerson];
226
227 people.indexOf(newPerson); // 0
228 people.indexOf(newPerson, 1); // 3
229 people.indexOf(newPerson, -4); // 0
230 people.indexOf(newPerson, 10); // -1
231 ```
232
233 @method indexOf
234 @param {Object} object the item to search for
235 @param {Number} startAt optional starting location to search, default 0
236 @return {Number} index or -1 if not found
237 @public
238 */
239 indexOf(object: T, startAt?: number): number;
240 /**
241 Returns the index of the given `object`'s last occurrence.
242
243 - If no `startAt` argument is given, the search starts from
244 the last position.
245 - If it's greater than or equal to the length of the array,
246 the search starts from the last position.
247 - If it's negative, it is taken as the offset from the end
248 of the array i.e. `startAt + array.length`.
249 - If it's any other positive number, will search backwards
250 from that index of the array.
251
252 Returns -1 if no match is found.
253
254 ```javascript
255 let arr = ['a', 'b', 'c', 'd', 'a'];
256
257 arr.lastIndexOf('a'); // 4
258 arr.lastIndexOf('z'); // -1
259 arr.lastIndexOf('a', 2); // 0
260 arr.lastIndexOf('a', -1); // 4
261 arr.lastIndexOf('a', -3); // 0
262 arr.lastIndexOf('b', 3); // 1
263 arr.lastIndexOf('a', 100); // 4
264 ```
265
266 @method lastIndexOf
267 @param {Object} object the item to search for
268 @param {Number} startAt optional starting location to search from
269 backwards, defaults to `(array.length - 1)`
270 @return {Number} The last index of the `object` in the array or -1
271 if not found
272 @public
273 */
274 lastIndexOf(object: T, startAt?: number): number;
275 /**
276 Iterates through the array, calling the passed function on each
277 item. This method corresponds to the `forEach()` method defined in
278 JavaScript 1.6.
279
280 The callback method you provide should have the following signature (all
281 parameters are optional):
282
283 ```javascript
284 function(item, index, array);
285 ```
286
287 - `item` is the current item in the iteration.
288 - `index` is the current index in the iteration.
289 - `array` is the array itself.
290
291 Note that in addition to a callback, you can also pass an optional target
292 object that will be set as `this` on the context. This is a good way
293 to give your iterator function access to the current object.
294
295 Example Usage:
296
297 ```javascript
298 let foods = [
299 { name: 'apple', eaten: false },
300 { name: 'banana', eaten: false },
301 { name: 'carrot', eaten: false }
302 ];
303
304 foods.forEach((food) => food.eaten = true);
305
306 let output = '';
307 foods.forEach((item, index, array) =>
308 output += `${index + 1}/${array.length} ${item.name}\n`;
309 );
310 console.log(output);
311 // 1/3 apple
312 // 2/3 banana
313 // 3/3 carrot
314 ```
315
316 @method forEach
317 @param {Function} callback The callback to execute
318 @param {Object} [target] The target object to use
319 @return {Object} receiver
320 @public
321 */
322 forEach<Target>(
323 callback: (this: Target, item: T, index: number, arr: this) => void,
324 target?: Target
325 ): this;
326 /**
327 Alias for `mapBy`.
328
329 Returns the value of the named
330 property on all items in the enumeration.
331
332 ```javascript
333 let people = [{name: 'Joe'}, {name: 'Matt'}];
334
335 people.getEach('name');
336 // ['Joe', 'Matt'];
337
338 people.getEach('nonexistentProperty');
339 // [undefined, undefined];
340 ```
341
342 @method getEach
343 @param {String} key name of the property
344 @return {Array} The mapped array.
345 @public
346 */
347 getEach<K extends keyof T>(key: K): NativeArray<T[K]>;
348 /**
349 Sets the value on the named property for each member. This is more
350 ergonomic than using other methods defined on this helper. If the object
351 implements Observable, the value will be changed to `set(),` otherwise
352 it will be set directly. `null` objects are skipped.
353
354 ```javascript
355 let people = [{name: 'Joe'}, {name: 'Matt'}];
356
357 people.setEach('zipCode', '10011');
358 // [{name: 'Joe', zipCode: '10011'}, {name: 'Matt', zipCode: '10011'}];
359 ```
360
361 @method setEach
362 @param {String} key The key to set
363 @param {Object} value The object to set
364 @return {Object} receiver
365 @public
366 */
367 setEach<K extends keyof T>(key: K, value: T[K]): this;
368 /**
369 Maps all of the items in the enumeration to another value, returning
370 a new array. This method corresponds to `map()` defined in JavaScript 1.6.
371
372 The callback method you provide should have the following signature (all
373 parameters are optional):
374
375 ```javascript
376 function(item, index, array);
377 let arr = [1, 2, 3, 4, 5, 6];
378
379 arr.map(element => element * element);
380 // [1, 4, 9, 16, 25, 36];
381
382 arr.map((element, index) => element + index);
383 // [1, 3, 5, 7, 9, 11];
384 ```
385
386 - `item` is the current item in the iteration.
387 - `index` is the current index in the iteration.
388 - `array` is the array itself.
389
390 It should return the mapped value.
391
392 Note that in addition to a callback, you can also pass an optional target
393 object that will be set as `this` on the context. This is a good way
394 to give your iterator function access to the current object.
395
396 @method map
397 @param {Function} callback The callback to execute
398 @param {Object} [target] The target object to use
399 @return {Array} The mapped array.
400 @public
401 */
402 map<U, Target>(
403 callback: (this: Target, item: T, index: number, arr: this) => U,
404 target?: Target
405 ): NativeArray<U>;
406 /**
407 Similar to map, this specialized function returns the value of the named
408 property on all items in the enumeration.
409
410 ```javascript
411 let people = [{name: 'Joe'}, {name: 'Matt'}];
412
413 people.mapBy('name');
414 // ['Joe', 'Matt'];
415
416 people.mapBy('unknownProperty');
417 // [undefined, undefined];
418 ```
419
420 @method mapBy
421 @param {String} key name of the property
422 @return {Array} The mapped array.
423 @public
424 */
425 mapBy<K extends keyof T>(key: K): NativeArray<T[K]>;
426 mapBy(key: string): NativeArray<unknown>;
427 /**
428 Returns a new array with all of the items in the enumeration that the provided
429 callback function returns true for. This method corresponds to [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
430
431 The callback method should have the following signature:
432
433 ```javascript
434 function(item, index, array);
435 ```
436
437 - `item` is the current item in the iteration.
438 - `index` is the current index in the iteration.
439 - `array` is the array itself.
440
441 All parameters are optional. The function should return `true` to include the item
442 in the results, and `false` otherwise.
443
444 Example:
445
446 ```javascript
447 function isAdult(person) {
448 return person.age > 18;
449 };
450
451 let people = Ember.A([{ name: 'John', age: 14 }, { name: 'Joan', age: 45 }]);
452
453 people.filter(isAdult); // returns [{ name: 'Joan', age: 45 }];
454 ```
455
456 Note that in addition to a callback, you can pass an optional target object
457 that will be set as `this` on the context. This is a good way to give your
458 iterator function access to the current object. For example:
459
460 ```javascript
461 function isAdultAndEngineer(person) {
462 return person.age > 18 && this.engineering;
463 }
464
465 class AdultsCollection {
466 engineering = false;
467
468 constructor(opts = {}) {
469 super(...arguments);
470
471 this.engineering = opts.engineering;
472 this.people = Ember.A([{ name: 'John', age: 14 }, { name: 'Joan', age: 45 }]);
473 }
474 }
475
476 let collection = new AdultsCollection({ engineering: true });
477 collection.people.filter(isAdultAndEngineer, { target: collection });
478 ```
479
480 @method filter
481 @param {Function} callback The callback to execute
482 @param {Object} [target] The target object to use
483 @return {Array} A filtered array.
484 @public
485 */
486 filter<Target>(
487 callback: (this: Target, item: T, index: number, arr: this) => unknown,
488 target?: Target
489 ): NativeArray<T>;
490 /**
491 Returns an array with all of the items in the enumeration where the passed
492 function returns false. This method is the inverse of filter().
493
494 The callback method you provide should have the following signature (all
495 parameters are optional):
496
497 ```javascript
498 function(item, index, array);
499 ```
500
501 - *item* is the current item in the iteration.
502 - *index* is the current index in the iteration
503 - *array* is the array itself.
504
505 It should return a falsey value to include the item in the results.
506
507 Note that in addition to a callback, you can also pass an optional target
508 object that will be set as "this" on the context. This is a good way
509 to give your iterator function access to the current object.
510
511 Example Usage:
512
513 ```javascript
514 const food = [
515 { food: 'apple', isFruit: true },
516 { food: 'bread', isFruit: false },
517 { food: 'banana', isFruit: true }
518 ];
519 const nonFruits = food.reject(function(thing) {
520 return thing.isFruit;
521 }); // [{food: 'bread', isFruit: false}]
522 ```
523
524 @method reject
525 @param {Function} callback The callback to execute
526 @param {Object} [target] The target object to use
527 @return {Array} A rejected array.
528 @public
529 */
530 reject<Target>(
531 callback: (this: Target, item: T, index: number, arr: this) => unknown,
532 target?: Target
533 ): NativeArray<T>;
534 /**
535 Filters the array by the property and an optional value. If a value is given, it returns
536 the items that have said value for the property. If not, it returns all the items that
537 have a truthy value for the property.
538
539 Example Usage:
540
541 ```javascript
542 let things = Ember.A([{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }]);
543
544 things.filterBy('food', 'beans'); // [{ food: 'beans', isFruit: false }]
545 things.filterBy('isFruit'); // [{ food: 'apple', isFruit: true }]
546 ```
547
548 @method filterBy
549 @param {String} key the property to test
550 @param {*} [value] optional value to test against.
551 @return {Array} filtered array
552 @public
553 */
554 filterBy(key: string, value?: unknown): NativeArray<T>;
555 /**
556 Returns an array with the items that do not have truthy values for the provided key.
557 You can pass an optional second argument with a target value to reject for the key.
558 Otherwise this will reject objects where the provided property evaluates to false.
559
560 Example Usage:
561
562 ```javascript
563 let food = [
564 { name: "apple", isFruit: true },
565 { name: "carrot", isFruit: false },
566 { name: "bread", isFruit: false },
567 ];
568 food.rejectBy('isFruit'); // [{ name: "carrot", isFruit: false }, { name: "bread", isFruit: false }]
569 food.rejectBy('name', 'carrot'); // [{ name: "apple", isFruit: true }}, { name: "bread", isFruit: false }]
570 ```
571
572 @method rejectBy
573 @param {String} key the property to test
574 @param {*} [value] optional value to test against.
575 @return {Array} rejected array
576 @public
577 */
578 rejectBy(key: string, value?: unknown): NativeArray<T>;
579 /**
580 Returns the first item in the array for which the callback returns true.
581 This method is similar to the `find()` method defined in ECMAScript 2015.
582
583 The callback method you provide should have the following signature (all
584 parameters are optional):
585
586 ```javascript
587 function(item, index, array);
588 ```
589
590 - `item` is the current item in the iteration.
591 - `index` is the current index in the iteration.
592 - `array` is the array itself.
593
594 It should return the `true` to include the item in the results, `false`
595 otherwise.
596
597 Note that in addition to a callback, you can also pass an optional target
598 object that will be set as `this` on the context. This is a good way
599 to give your iterator function access to the current object.
600
601 Example Usage:
602
603 ```javascript
604 let users = [
605 { id: 1, name: 'Yehuda' },
606 { id: 2, name: 'Tom' },
607 { id: 3, name: 'Melanie' },
608 { id: 4, name: 'Leah' }
609 ];
610
611 users.find((user) => user.name == 'Tom'); // [{ id: 2, name: 'Tom' }]
612 users.find(({ id }) => id == 3); // [{ id: 3, name: 'Melanie' }]
613 ```
614
615 @method find
616 @param {Function} callback The callback to execute
617 @param {Object} [target] The target object to use
618 @return {Object} Found item or `undefined`.
619 @public
620 */
621 find<S extends T, Target = void>(
622 predicate: (this: void, value: T, index: number, obj: T[]) => value is S,
623 thisArg?: Target
624 ): S | undefined;
625 find<Target = void>(
626 callback: (this: Target, item: T, index: number, arr: this) => unknown,
627 target?: Target
628 ): T | undefined;
629 /**
630 Returns the first item with a property matching the passed value. You
631 can pass an optional second argument with the target value. Otherwise
632 this will match any property that evaluates to `true`.
633
634 This method works much like the more generic `find()` method.
635
636 Usage Example:
637
638 ```javascript
639 let users = [
640 { id: 1, name: 'Yehuda', isTom: false },
641 { id: 2, name: 'Tom', isTom: true },
642 { id: 3, name: 'Melanie', isTom: false },
643 { id: 4, name: 'Leah', isTom: false }
644 ];
645
646 users.findBy('id', 4); // { id: 4, name: 'Leah', isTom: false }
647 users.findBy('name', 'Melanie'); // { id: 3, name: 'Melanie', isTom: false }
648 users.findBy('isTom'); // { id: 2, name: 'Tom', isTom: true }
649 ```
650
651 @method findBy
652 @param {String} key the property to test
653 @param {String} [value] optional value to test against.
654 @return {Object} found item or `undefined`
655 @public
656 */
657 findBy<K extends keyof T>(key: K, value?: T[K]): T | undefined;
658 findBy(key: string, value?: unknown): T | undefined;
659 /**
660 Returns `true` if the passed function returns true for every item in the
661 enumeration. This corresponds with the `Array.prototype.every()` method defined in ES5.
662
663 The callback method should have the following signature:
664
665 ```javascript
666 function(item, index, array);
667 ```
668
669 - `item` is the current item in the iteration.
670 - `index` is the current index in the iteration.
671 - `array` is the array itself.
672
673 All params are optional. The method should return `true` or `false`.
674
675 Note that in addition to a callback, you can also pass an optional target
676 object that will be set as `this` on the context. This is a good way
677 to give your iterator function access to the current object.
678
679 Usage example:
680
681 ```javascript
682 function isAdult(person) {
683 return person.age > 18;
684 };
685
686 const people = Ember.A([{ name: 'John', age: 24 }, { name: 'Joan', age: 45 }]);
687 const areAllAdults = people.every(isAdult);
688 ```
689
690 @method every
691 @param {Function} callback The callback to execute
692 @param {Object} [target] The target object to use
693 @return {Boolean}
694 @public
695 */
696 every<Target = void>(
697 callback: (this: Target, item: T, index: number, arr: this) => unknown,
698 target?: Target
699 ): boolean;
700 /**
701 Returns `true` if the passed property resolves to the value of the second
702 argument for all items in the array. This method is often simpler/faster
703 than using a callback.
704
705 Note that like the native `Array.every`, `isEvery` will return true when called
706 on any empty array.
707 ```javascript
708 class Language {
709 constructor(name, isProgrammingLanguage) {
710 this.name = name;
711 this.programmingLanguage = isProgrammingLanguage;
712 }
713 }
714
715 const compiledLanguages = [
716 new Language('Java', true),
717 new Language('Go', true),
718 new Language('Rust', true)
719 ]
720
721 const languagesKnownByMe = [
722 new Language('Javascript', true),
723 new Language('English', false),
724 new Language('Ruby', true)
725 ]
726
727 compiledLanguages.isEvery('programmingLanguage'); // true
728 languagesKnownByMe.isEvery('programmingLanguage'); // false
729 ```
730
731 @method isEvery
732 @param {String} key the property to test
733 @param {String} [value] optional value to test against. Defaults to `true`
734 @return {Boolean}
735 @since 1.3.0
736 @public
737 */
738 isEvery<K extends keyof T>(key: K, value?: T[K]): boolean;
739 isEvery(key: string, value?: unknown): boolean;
740 /**
741 The any() method executes the callback function once for each element
742 present in the array until it finds the one where callback returns a truthy
743 value (i.e. `true`). If such an element is found, any() immediately returns
744 true. Otherwise, any() returns false.
745
746 ```javascript
747 function(item, index, array);
748 ```
749
750 - `item` is the current item in the iteration.
751 - `index` is the current index in the iteration.
752 - `array` is the array object itself.
753
754 Note that in addition to a callback, you can also pass an optional target
755 object that will be set as `this` on the context. It can be a good way
756 to give your iterator function access to an object in cases where an ES6
757 arrow function would not be appropriate.
758
759 Usage Example:
760
761 ```javascript
762 let includesManager = people.any(this.findPersonInManagersList, this);
763
764 let includesStockHolder = people.any(person => {
765 return this.findPersonInStockHoldersList(person)
766 });
767
768 if (includesManager || includesStockHolder) {
769 Paychecks.addBiggerBonus();
770 }
771 ```
772
773 @method any
774 @param {Function} callback The callback to execute
775 @param {Object} [target] The target object to use
776 @return {Boolean} `true` if the passed function returns `true` for any item
777 @public
778 */
779 any<Target = void>(
780 callback: (this: Target, item: T, index: number, arr: this) => unknown,
781 target?: Target
782 ): boolean;
783 /**
784 Returns `true` if the passed property resolves to the value of the second
785 argument for any item in the array. This method is often simpler/faster
786 than using a callback.
787
788 Example usage:
789
790 ```javascript
791 const food = [
792 { food: 'apple', isFruit: true },
793 { food: 'bread', isFruit: false },
794 { food: 'banana', isFruit: true }
795 ];
796
797 food.isAny('isFruit'); // true
798 ```
799
800 @method isAny
801 @param {String} key the property to test
802 @param {String} [value] optional value to test against. Defaults to `true`
803 @return {Boolean}
804 @since 1.3.0
805 @public
806 */
807 isAny<K extends keyof T>(key: K, value?: T[K]): boolean;
808 isAny(key: string, value?: unknown): boolean;
809 /**
810 This will combine the values of the array into a single value. It
811 is a useful way to collect a summary value from an array. This
812 corresponds to the `reduce()` method defined in JavaScript 1.8.
813
814 The callback method you provide should have the following signature (all
815 parameters are optional):
816
817 ```javascript
818 function(previousValue, item, index, array);
819 ```
820
821 - `previousValue` is the value returned by the last call to the iterator.
822 - `item` is the current item in the iteration.
823 - `index` is the current index in the iteration.
824 - `array` is the array itself.
825
826 Return the new cumulative value.
827
828 In addition to the callback you can also pass an `initialValue`. An error
829 will be raised if you do not pass an initial value and the enumerator is
830 empty.
831
832 Note that unlike the other methods, this method does not allow you to
833 pass a target object to set as this for the callback. It's part of the
834 spec. Sorry.
835
836 Example Usage:
837
838 ```javascript
839 let numbers = [1, 2, 3, 4, 5];
840
841 numbers.reduce(function(summation, current) {
842 return summation + current;
843 }); // 15 (1 + 2 + 3 + 4 + 5)
844
845 numbers.reduce(function(summation, current) {
846 return summation + current;
847 }, -15); // 0 (-15 + 1 + 2 + 3 + 4 + 5)
848
849
850 let binaryValues = [true, false, false];
851
852 binaryValues.reduce(function(truthValue, current) {
853 return truthValue && current;
854 }); // false (true && false && false)
855 ```
856
857 @method reduce
858 @param {Function} callback The callback to execute
859 @param {Object} initialValue Initial value for the reduce
860 @return {Object} The reduced value.
861 @public
862 */
863 reduce<V>(
864 callback: (summation: V, current: T, index: number, arr: this) => V,
865 initialValue?: V
866 ): V;
867 /**
868 Invokes the named method on every object in the receiver that
869 implements it. This method corresponds to the implementation in
870 Prototype 1.6.
871
872 ```javascript
873 class Person {
874 name = null;
875
876 constructor(name) {
877 this.name = name;
878 }
879
880 greet(prefix='Hello') {
881 return `${prefix} ${this.name}`;
882 }
883 }
884
885 let people = [new Person('Joe'), new Person('Matt')];
886
887 people.invoke('greet'); // ['Hello Joe', 'Hello Matt']
888 people.invoke('greet', 'Bonjour'); // ['Bonjour Joe', 'Bonjour Matt']
889 ```
890
891 @method invoke
892 @param {String} methodName the name of the method
893 @param {Object...} args optional arguments to pass as well.
894 @return {Array} return values from calling invoke.
895 @public
896 */
897 invoke<M extends MethodNamesOf<T>>(
898 methodName: M,
899 ...args: MethodParams<T, M>
900 ): NativeArray<MethodReturns<T, M>>;
901 /**
902 Simply converts the object into a genuine array. The order is not
903 guaranteed. Corresponds to the method implemented by Prototype.
904
905 @method toArray
906 @return {Array} the object as an array.
907 @public
908 */
909 toArray(): T[];
910 /**
911 Returns a copy of the array with all `null` and `undefined` elements removed.
912
913 ```javascript
914 let arr = ['a', null, 'c', undefined];
915 arr.compact(); // ['a', 'c']
916 ```
917
918 @method compact
919 @return {Array} the array without null and undefined elements.
920 @public
921 */
922 compact(): NativeArray<NonNullable<T>>;
923 /**
924 Used to determine if the array contains the passed object.
925 Returns `true` if found, `false` otherwise.
926
927 The optional `startAt` argument can be used to pass a starting
928 index to search from, effectively slicing the searchable portion
929 of the array. If it's negative it will add the array length to
930 the startAt value passed in as the index to search from. If less
931 than or equal to `-1 * array.length` the entire array is searched.
932
933 This method has the same behavior of JavaScript's [Array.includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes).
934
935 ```javascript
936 [1, 2, 3].includes(2); // true
937 [1, 2, 3].includes(4); // false
938 [1, 2, 3].includes(3, 2); // true
939 [1, 2, 3].includes(3, 3); // false
940 [1, 2, 3].includes(3, -1); // true
941 [1, 2, 3].includes(1, -1); // false
942 [1, 2, 3].includes(1, -4); // true
943 [1, 2, NaN].includes(NaN); // true
944 ```
945
946 @method includes
947 @param {Object} object The object to search for.
948 @param {Number} startAt optional starting location to search, default 0
949 @return {Boolean} `true` if object is found in the array.
950 @public
951 */
952 includes(object: T, startAt?: number): boolean;
953 /**
954 Sorts the array by the keys specified in the argument.
955
956 You may provide multiple arguments to sort by multiple properties.
957
958 ```javascript
959 let colors = [
960 { name: 'red', weight: 500 },
961 { name: 'green', weight: 600 },
962 { name: 'blue', weight: 500 }
963 ];
964
965 colors.sortBy('name');
966 // [{name: 'blue', weight: 500}, {name: 'green', weight: 600}, {name: 'red', weight: 500}]
967
968 colors.sortBy('weight', 'name');
969 // [{name: 'blue', weight: 500}, {name: 'red', weight: 500}, {name: 'green', weight: 600}]
970 ```
971 @method sortBy
972 @param {String} property name(s) to sort on
973 @return {Array} The sorted array.
974 @since 1.2.0
975 @public
976 */
977 sortBy(...keys: string[]): T[];
978 /**
979 Returns a new array that contains only unique values. The default
980 implementation returns an array regardless of the receiver type.
981
982 ```javascript
983 let arr = ['a', 'a', 'b', 'b'];
984 arr.uniq(); // ['a', 'b']
985 ```
986
987 This only works on primitive data types, e.g. Strings, Numbers, etc.
988
989 @method uniq
990 @return {EmberArray}
991 @public
992 */
993 uniq(): NativeArray<T>;
994 /**
995 Returns a new array that contains only items containing a unique property value.
996 The default implementation returns an array regardless of the receiver type.
997
998 ```javascript
999 let arr = [{ value: 'a' }, { value: 'a' }, { value: 'b' }, { value: 'b' }];
1000 arr.uniqBy('value'); // [{ value: 'a' }, { value: 'b' }]
1001
1002 let arr = [2.2, 2.1, 3.2, 3.3];
1003 arr.uniqBy(Math.floor); // [2.2, 3.2];
1004 ```
1005
1006 @method uniqBy
1007 @param {String,Function} key
1008 @return {EmberArray}
1009 @public
1010 */
1011 uniqBy(key: string): NativeArray<T>;
1012 uniqBy(callback: (value: T) => unknown): NativeArray<T>;
1013 /**
1014 Returns a new array that excludes the passed value. The default
1015 implementation returns an array regardless of the receiver type.
1016 If the receiver does not contain the value it returns the original array.
1017
1018 ```javascript
1019 let arr = ['a', 'b', 'a', 'c'];
1020 arr.without('a'); // ['b', 'c']
1021 ```
1022
1023 @method without
1024 @param {Object} value
1025 @return {EmberArray}
1026 @public
1027 */
1028 without(value: T): NativeArray<T>;
1029 }
1030 const EmberArray: Mixin;
1031 /**
1032 This mixin defines the API for modifying array-like objects. These methods
1033 can be applied only to a collection that keeps its items in an ordered set.
1034 It builds upon the Array mixin and adds methods to modify the array.
1035 One concrete implementations of this class include ArrayProxy.
1036
1037 It is important to use the methods in this class to modify arrays so that
1038 changes are observable. This allows the binding system in Ember to function
1039 correctly.
1040
1041
1042 Note that an Array can change even if it does not implement this mixin.
1043 For example, one might implement a SparseArray that cannot be directly
1044 modified, but if its underlying enumerable changes, it will change also.
1045
1046 @class MutableArray
1047 @uses EmberArray
1048 @uses MutableEnumerable
1049 @public
1050 */
1051 interface MutableArray<T> extends EmberArray<T>, MutableEnumerable {
1052 /**
1053 __Required.__ You must implement this method to apply this mixin.
1054
1055 This is one of the primitives you must implement to support `Array`.
1056 You should replace amt objects started at idx with the objects in the
1057 passed array.
1058
1059 Note that this method is expected to validate the type(s) of objects that it expects.
1060
1061 @method replace
1062 @param {Number} idx Starting index in the array to replace. If
1063 idx >= length, then append to the end of the array.
1064 @param {Number} amt Number of elements that should be removed from
1065 the array, starting at *idx*.
1066 @param {EmberArray} [objects] An optional array of zero or more objects that should be
1067 inserted into the array at *idx*
1068 @public
1069 */
1070 replace(idx: number, amt: number, objects?: readonly T[]): void;
1071 /**
1072 Remove all elements from the array. This is useful if you
1073 want to reuse an existing array without having to recreate it.
1074
1075 ```javascript
1076 let colors = ['red', 'green', 'blue'];
1077
1078 colors.length; // 3
1079 colors.clear(); // []
1080 colors.length; // 0
1081 ```
1082
1083 @method clear
1084 @return {Array} An empty Array.
1085 @public
1086 */
1087 clear(): this;
1088 /**
1089 This will use the primitive `replace()` method to insert an object at the
1090 specified index.
1091
1092 ```javascript
1093 let colors = ['red', 'green', 'blue'];
1094
1095 colors.insertAt(2, 'yellow'); // ['red', 'green', 'yellow', 'blue']
1096 colors.insertAt(5, 'orange'); // Error: Index out of range
1097 ```
1098
1099 @method insertAt
1100 @param {Number} idx index of insert the object at.
1101 @param {Object} object object to insert
1102 @return {EmberArray} receiver
1103 @public
1104 */
1105 insertAt(idx: number, object: T): this;
1106 /**
1107 Remove an object at the specified index using the `replace()` primitive
1108 method. You can pass either a single index, or a start and a length.
1109
1110 If you pass a start and length that is beyond the
1111 length this method will throw an assertion.
1112
1113 ```javascript
1114 let colors = ['red', 'green', 'blue', 'yellow', 'orange'];
1115
1116 colors.removeAt(0); // ['green', 'blue', 'yellow', 'orange']
1117 colors.removeAt(2, 2); // ['green', 'blue']
1118 colors.removeAt(4, 2); // Error: Index out of range
1119 ```
1120
1121 @method removeAt
1122 @param {Number} start index, start of range
1123 @param {Number} len length of passing range
1124 @return {EmberArray} receiver
1125 @public
1126 */
1127 removeAt(start: number, len?: number): this;
1128 /**
1129 Push the object onto the end of the array. Works just like `push()` but it
1130 is KVO-compliant.
1131
1132 ```javascript
1133 let colors = ['red', 'green'];
1134
1135 colors.pushObject('black'); // ['red', 'green', 'black']
1136 colors.pushObject(['yellow']); // ['red', 'green', ['yellow']]
1137 ```
1138
1139 @method pushObject
1140 @param {*} obj object to push
1141 @return object same object passed as a param
1142 @public
1143 */
1144 pushObject(obj: T): T;
1145 /**
1146 Add the objects in the passed array to the end of the array. Defers
1147 notifying observers of the change until all objects are added.
1148
1149 ```javascript
1150 let colors = ['red'];
1151
1152 colors.pushObjects(['yellow', 'orange']); // ['red', 'yellow', 'orange']
1153 ```
1154
1155 @method pushObjects
1156 @param {Array} objects the objects to add
1157 @return {MutableArray} receiver
1158 @public
1159 */
1160 pushObjects(objects: T[]): this;
1161 /**
1162 Pop object from array or nil if none are left. Works just like `pop()` but
1163 it is KVO-compliant.
1164
1165 ```javascript
1166 let colors = ['red', 'green', 'blue'];
1167
1168 colors.popObject(); // 'blue'
1169 console.log(colors); // ['red', 'green']
1170 ```
1171
1172 @method popObject
1173 @return object
1174 @public
1175 */
1176 popObject(): T | null | undefined;
1177 /**
1178 Shift an object from start of array or nil if none are left. Works just
1179 like `shift()` but it is KVO-compliant.
1180
1181 ```javascript
1182 let colors = ['red', 'green', 'blue'];
1183
1184 colors.shiftObject(); // 'red'
1185 console.log(colors); // ['green', 'blue']
1186 ```
1187
1188 @method shiftObject
1189 @return object
1190 @public
1191 */
1192 shiftObject(): T | null | undefined;
1193 /**
1194 Unshift an object to start of array. Works just like `unshift()` but it is
1195 KVO-compliant.
1196
1197 ```javascript
1198 let colors = ['red'];
1199
1200 colors.unshiftObject('yellow'); // ['yellow', 'red']
1201 colors.unshiftObject(['black']); // [['black'], 'yellow', 'red']
1202 ```
1203
1204 @method unshiftObject
1205 @param {*} obj object to unshift
1206 @return object same object passed as a param
1207 @public
1208 */
1209 unshiftObject(object: T): T;
1210 /**
1211 Adds the named objects to the beginning of the array. Defers notifying
1212 observers until all objects have been added.
1213
1214 ```javascript
1215 let colors = ['red'];
1216
1217 colors.unshiftObjects(['black', 'white']); // ['black', 'white', 'red']
1218 colors.unshiftObjects('yellow'); // Type Error: 'undefined' is not a function
1219 ```
1220
1221 @method unshiftObjects
1222 @param {Enumerable} objects the objects to add
1223 @return {EmberArray} receiver
1224 @public
1225 */
1226 unshiftObjects(objects: T[]): this;
1227 /**
1228 Reverse objects in the array. Works just like `reverse()` but it is
1229 KVO-compliant.
1230
1231 @method reverseObjects
1232 @return {EmberArray} receiver
1233 @public
1234 */
1235 reverseObjects(): this;
1236 /**
1237 Replace all the receiver's content with content of the argument.
1238 If argument is an empty array receiver will be cleared.
1239
1240 ```javascript
1241 let colors = ['red', 'green', 'blue'];
1242
1243 colors.setObjects(['black', 'white']); // ['black', 'white']
1244 colors.setObjects([]); // []
1245 ```
1246
1247 @method setObjects
1248 @param {EmberArray} objects array whose content will be used for replacing
1249 the content of the receiver
1250 @return {EmberArray} receiver with the new content
1251 @public
1252 */
1253 setObjects(object: T[]): this;
1254 /**
1255 Remove all occurrences of an object in the array.
1256
1257 ```javascript
1258 let cities = ['Chicago', 'Berlin', 'Lima', 'Chicago'];
1259
1260 cities.removeObject('Chicago'); // ['Berlin', 'Lima']
1261 cities.removeObject('Lima'); // ['Berlin']
1262 cities.removeObject('Tokyo') // ['Berlin']
1263 ```
1264
1265 @method removeObject
1266 @param {*} obj object to remove
1267 @return {EmberArray} receiver
1268 @public
1269 */
1270 removeObject(object: T): this;
1271 /**
1272 Removes each object in the passed array from the receiver.
1273
1274 @method removeObjects
1275 @param {EmberArray} objects the objects to remove
1276 @return {EmberArray} receiver
1277 @public
1278 */
1279 removeObjects(objects: T[]): this;
1280 /**
1281 Push the object onto the end of the array if it is not already
1282 present in the array.
1283
1284 ```javascript
1285 let cities = ['Chicago', 'Berlin'];
1286
1287 cities.addObject('Lima'); // ['Chicago', 'Berlin', 'Lima']
1288 cities.addObject('Berlin'); // ['Chicago', 'Berlin', 'Lima']
1289 ```
1290
1291 @method addObject
1292 @param {*} obj object to add, if not already present
1293 @return {EmberArray} receiver
1294 @public
1295 */
1296 addObject(obj: T): this;
1297 /**
1298 Adds each object in the passed array to the receiver.
1299
1300 @method addObjects
1301 @param {EmberArray} objects the objects to add.
1302 @return {EmberArray} receiver
1303 @public
1304 */
1305 addObjects(objects: T[]): this;
1306 }
1307 const MutableArray: Mixin;
1308 /**
1309 Creates an `Ember.NativeArray` from an Array-like object.
1310 Does not modify the original object's contents.
1311
1312 Example
1313
1314 ```app/components/my-component.js
1315 import Component from '@ember/component';
1316 import { A } from '@ember/array';
1317
1318 export default Component.extend({
1319 tagName: 'ul',
1320 classNames: ['pagination'],
1321
1322 init() {
1323 this._super(...arguments);
1324
1325 if (!this.get('content')) {
1326 this.set('content', A());
1327 this.set('otherContent', A([1,2,3]));
1328 }
1329 }
1330 });
1331 ```
1332
1333 @method A
1334 @static
1335 @for @ember/array
1336 @return {Ember.NativeArray}
1337 @public
1338 */
1339 /**
1340 @module ember
1341 */
1342 type AnyArray<T> = EmberArray<T> | Array<T> | ReadonlyArray<T>;
1343 /**
1344 * The final definition of NativeArray removes all native methods. This is the list of removed methods
1345 * when run in Chrome 106.
1346 */
1347 type IGNORED_MUTABLE_ARRAY_METHODS =
1348 | 'length'
1349 | 'slice'
1350 | 'indexOf'
1351 | 'lastIndexOf'
1352 | 'forEach'
1353 | 'map'
1354 | 'filter'
1355 | 'find'
1356 | 'every'
1357 | 'reduce'
1358 | 'includes';
1359 /**
1360 * These additional items must be redefined since `Omit` causes methods that return `this` to return the
1361 * type at the time of the Omit.
1362 */
1363 type RETURN_SELF_ARRAY_METHODS =
1364 | '[]'
1365 | 'clear'
1366 | 'insertAt'
1367 | 'removeAt'
1368 | 'pushObjects'
1369 | 'unshiftObjects'
1370 | 'reverseObjects'
1371 | 'setObjects'
1372 | 'removeObject'
1373 | 'removeObjects'
1374 | 'addObject'
1375 | 'addObjects'
1376 | 'setEach';
1377 interface MutableArrayWithoutNative<T>
1378 extends Omit<MutableArray<T>, IGNORED_MUTABLE_ARRAY_METHODS | RETURN_SELF_ARRAY_METHODS> {
1379 /**
1380 * Remove all elements from the array. This is useful if you
1381 * want to reuse an existing array without having to recreate it.
1382 */
1383 clear(): this;
1384 /**
1385 * This will use the primitive `replace()` method to insert an object at the
1386 * specified index.
1387 */
1388 insertAt(idx: number, object: T): this;
1389 /**
1390 * Remove an object at the specified index using the `replace()` primitive
1391 * method. You can pass either a single index, or a start and a length.
1392 */
1393 removeAt(start: number, len?: number): this;
1394 /**
1395 * Add the objects in the passed numerable to the end of the array. Defers
1396 * notifying observers of the change until all objects are added.
1397 */
1398 pushObjects(objects: AnyArray<T>): this;
1399 /**
1400 * Adds the named objects to the beginning of the array. Defers notifying
1401 * observers until all objects have been added.
1402 */
1403 unshiftObjects(objects: AnyArray<T>): this;
1404 /**
1405 * Reverse objects in the array. Works just like `reverse()` but it is
1406 * KVO-compliant.
1407 */
1408 reverseObjects(): this;
1409 /**
1410 * Replace all the receiver's content with content of the argument.
1411 * If argument is an empty array receiver will be cleared.
1412 */
1413 setObjects(objects: AnyArray<T>): this;
1414 /**
1415 Remove all occurrences of an object in the array.
1416
1417 ```javascript
1418 let cities = ['Chicago', 'Berlin', 'Lima', 'Chicago'];
1419
1420 cities.removeObject('Chicago'); // ['Berlin', 'Lima']
1421 cities.removeObject('Lima'); // ['Berlin']
1422 cities.removeObject('Tokyo') // ['Berlin']
1423 ```
1424
1425 @method removeObject
1426 @param {*} obj object to remove
1427 @return {EmberArray} receiver
1428 @public
1429 */
1430 removeObject(object: T): this;
1431 /**
1432 * Removes each object in the passed array from the receiver.
1433 */
1434 removeObjects(objects: AnyArray<T>): this;
1435 /**
1436 Push the object onto the end of the array if it is not already
1437 present in the array.
1438
1439 ```javascript
1440 let cities = ['Chicago', 'Berlin'];
1441
1442 cities.addObject('Lima'); // ['Chicago', 'Berlin', 'Lima']
1443 cities.addObject('Berlin'); // ['Chicago', 'Berlin', 'Lima']
1444 ```
1445
1446 @method addObject
1447 @param {*} obj object to add, if not already present
1448 @return {EmberArray} receiver
1449 @public
1450 */
1451 addObject(obj: T): this;
1452 /**
1453 * Adds each object in the passed enumerable to the receiver.
1454 */
1455 addObjects(objects: AnyArray<T>): this;
1456 /**
1457 Sets the value on the named property for each member. This is more
1458 ergonomic than using other methods defined on this helper. If the object
1459 implements Observable, the value will be changed to `set(),` otherwise
1460 it will be set directly. `null` objects are skipped.
1461
1462 ```javascript
1463 let people = [{name: 'Joe'}, {name: 'Matt'}];
1464
1465 people.setEach('zipCode', '10011');
1466 // [{name: 'Joe', zipCode: '10011'}, {name: 'Matt', zipCode: '10011'}];
1467 ```
1468
1469 @method setEach
1470 @param {String} key The key to set
1471 @param {Object} value The object to set
1472 @return {Object} receiver
1473 @public
1474 */
1475 setEach<K extends keyof T>(key: K, value: T[K]): this;
1476 /**
1477 This is the handler for the special array content property. If you get
1478 this property, it will return this. If you set this property to a new
1479 array, it will replace the current content.
1480
1481 ```javascript
1482 let peopleToMoon = ['Armstrong', 'Aldrin'];
1483
1484 peopleToMoon.get('[]'); // ['Armstrong', 'Aldrin']
1485
1486 peopleToMoon.set('[]', ['Collins']); // ['Collins']
1487 peopleToMoon.get('[]'); // ['Collins']
1488 ```
1489
1490 @property []
1491 @return this
1492 @public
1493 */
1494 get '[]'(): this;
1495 set '[]'(newValue: T[] | this);
1496 }
1497 /**
1498 The NativeArray mixin contains the properties needed to make the native
1499 Array support MutableArray and all of its dependent APIs.
1500
1501 @class Ember.NativeArray
1502 @uses MutableArray
1503 @uses Observable
1504 @public
1505 */
1506 interface NativeArray<T> extends Array<T>, Observable, MutableArrayWithoutNative<T> {}
1507 let NativeArray: Mixin;
1508 let A: <T>(arr?: Array<T>) => NativeArray<T>;
1509 export { A, NativeArray, MutableArray };
1510 export default EmberArray;
1511}