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