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