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