@angular-wave/angular.ts 0.0.1

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 (231) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.cjs +29 -0
  3. package/.github/workflows/playwright.yml +27 -0
  4. package/CHANGELOG.md +17974 -0
  5. package/CODE_OF_CONDUCT.md +3 -0
  6. package/CONTRIBUTING.md +246 -0
  7. package/DEVELOPERS.md +488 -0
  8. package/LICENSE +22 -0
  9. package/Makefile +31 -0
  10. package/README.md +115 -0
  11. package/RELEASE.md +98 -0
  12. package/SECURITY.md +16 -0
  13. package/TRIAGING.md +135 -0
  14. package/css/angular.css +22 -0
  15. package/dist/angular-ts.cjs.js +36843 -0
  16. package/dist/angular-ts.esm.js +36841 -0
  17. package/dist/angular-ts.umd.js +36848 -0
  18. package/dist/build/angular-animate.js +4272 -0
  19. package/dist/build/angular-aria.js +426 -0
  20. package/dist/build/angular-message-format.js +1072 -0
  21. package/dist/build/angular-messages.js +829 -0
  22. package/dist/build/angular-mocks.js +3757 -0
  23. package/dist/build/angular-parse-ext.js +1275 -0
  24. package/dist/build/angular-resource.js +911 -0
  25. package/dist/build/angular-route.js +1266 -0
  26. package/dist/build/angular-sanitize.js +891 -0
  27. package/dist/build/angular-touch.js +368 -0
  28. package/dist/build/angular.js +36600 -0
  29. package/e2e/unit.spec.ts +15 -0
  30. package/images/android-chrome-192x192.png +0 -0
  31. package/images/android-chrome-512x512.png +0 -0
  32. package/images/apple-touch-icon.png +0 -0
  33. package/images/favicon-16x16.png +0 -0
  34. package/images/favicon-32x32.png +0 -0
  35. package/images/favicon.ico +0 -0
  36. package/images/site.webmanifest +1 -0
  37. package/index.html +104 -0
  38. package/package.json +47 -0
  39. package/playwright.config.ts +78 -0
  40. package/public/circle.html +1 -0
  41. package/public/my_child_directive.html +1 -0
  42. package/public/my_directive.html +1 -0
  43. package/public/my_other_directive.html +1 -0
  44. package/public/test.html +1 -0
  45. package/rollup.config.js +31 -0
  46. package/src/animations/animateCache.js +55 -0
  47. package/src/animations/animateChildrenDirective.js +105 -0
  48. package/src/animations/animateCss.js +1139 -0
  49. package/src/animations/animateCssDriver.js +291 -0
  50. package/src/animations/animateJs.js +367 -0
  51. package/src/animations/animateJsDriver.js +67 -0
  52. package/src/animations/animateQueue.js +851 -0
  53. package/src/animations/animation.js +506 -0
  54. package/src/animations/module.js +779 -0
  55. package/src/animations/ngAnimateSwap.js +119 -0
  56. package/src/animations/rafScheduler.js +50 -0
  57. package/src/animations/shared.js +378 -0
  58. package/src/constants.js +20 -0
  59. package/src/core/animate.js +845 -0
  60. package/src/core/animateCss.js +73 -0
  61. package/src/core/animateRunner.js +195 -0
  62. package/src/core/attributes.js +199 -0
  63. package/src/core/cache.js +45 -0
  64. package/src/core/compile.js +4727 -0
  65. package/src/core/controller.js +225 -0
  66. package/src/core/exceptionHandler.js +63 -0
  67. package/src/core/filter.js +146 -0
  68. package/src/core/interpolate.js +442 -0
  69. package/src/core/interval.js +188 -0
  70. package/src/core/intervalFactory.js +57 -0
  71. package/src/core/location.js +1086 -0
  72. package/src/core/parser/parse.js +2562 -0
  73. package/src/core/parser/parse.md +13 -0
  74. package/src/core/q.js +746 -0
  75. package/src/core/rootScope.js +1596 -0
  76. package/src/core/sanitizeUri.js +85 -0
  77. package/src/core/sce.js +1161 -0
  78. package/src/core/taskTrackerFactory.js +125 -0
  79. package/src/core/timeout.js +121 -0
  80. package/src/core/urlUtils.js +187 -0
  81. package/src/core/utils.js +1349 -0
  82. package/src/directive/a.js +37 -0
  83. package/src/directive/attrs.js +283 -0
  84. package/src/directive/bind.js +51 -0
  85. package/src/directive/bind.md +142 -0
  86. package/src/directive/change.js +12 -0
  87. package/src/directive/change.md +25 -0
  88. package/src/directive/cloak.js +12 -0
  89. package/src/directive/cloak.md +24 -0
  90. package/src/directive/events.js +75 -0
  91. package/src/directive/events.md +166 -0
  92. package/src/directive/form.js +725 -0
  93. package/src/directive/init.js +15 -0
  94. package/src/directive/init.md +41 -0
  95. package/src/directive/input.js +1783 -0
  96. package/src/directive/list.js +46 -0
  97. package/src/directive/list.md +22 -0
  98. package/src/directive/ngClass.js +249 -0
  99. package/src/directive/ngController.js +64 -0
  100. package/src/directive/ngCsp.js +82 -0
  101. package/src/directive/ngIf.js +134 -0
  102. package/src/directive/ngInclude.js +217 -0
  103. package/src/directive/ngModel.js +1356 -0
  104. package/src/directive/ngModelOptions.js +509 -0
  105. package/src/directive/ngOptions.js +670 -0
  106. package/src/directive/ngRef.js +90 -0
  107. package/src/directive/ngRepeat.js +650 -0
  108. package/src/directive/ngShowHide.js +255 -0
  109. package/src/directive/ngSwitch.js +178 -0
  110. package/src/directive/ngTransclude.js +98 -0
  111. package/src/directive/non-bindable.js +11 -0
  112. package/src/directive/non-bindable.md +17 -0
  113. package/src/directive/script.js +30 -0
  114. package/src/directive/select.js +624 -0
  115. package/src/directive/style.js +25 -0
  116. package/src/directive/style.md +23 -0
  117. package/src/directive/validators.js +329 -0
  118. package/src/exts/aria.js +544 -0
  119. package/src/exts/messages.js +852 -0
  120. package/src/filters/filter.js +207 -0
  121. package/src/filters/filter.md +69 -0
  122. package/src/filters/filters.js +239 -0
  123. package/src/filters/json.md +16 -0
  124. package/src/filters/limit-to.js +43 -0
  125. package/src/filters/limit-to.md +19 -0
  126. package/src/filters/order-by.js +183 -0
  127. package/src/filters/order-by.md +83 -0
  128. package/src/index.js +13 -0
  129. package/src/injector.js +1034 -0
  130. package/src/jqLite.js +1117 -0
  131. package/src/loader.js +1320 -0
  132. package/src/public.js +215 -0
  133. package/src/routeToRegExp.js +41 -0
  134. package/src/services/anchorScroll.js +135 -0
  135. package/src/services/browser.js +321 -0
  136. package/src/services/cacheFactory.js +398 -0
  137. package/src/services/cookieReader.js +72 -0
  138. package/src/services/document.js +64 -0
  139. package/src/services/http.js +1537 -0
  140. package/src/services/httpBackend.js +206 -0
  141. package/src/services/log.js +160 -0
  142. package/src/services/templateRequest.js +139 -0
  143. package/test/angular.spec.js +2153 -0
  144. package/test/aria/aria.spec.js +1245 -0
  145. package/test/binding.spec.js +504 -0
  146. package/test/build-test.html +14 -0
  147. package/test/injector.spec.js +2327 -0
  148. package/test/jasmine/jasmine-5.1.2/boot0.js +65 -0
  149. package/test/jasmine/jasmine-5.1.2/boot1.js +133 -0
  150. package/test/jasmine/jasmine-5.1.2/jasmine-html.js +963 -0
  151. package/test/jasmine/jasmine-5.1.2/jasmine.css +320 -0
  152. package/test/jasmine/jasmine-5.1.2/jasmine.js +10824 -0
  153. package/test/jasmine/jasmine-5.1.2/jasmine_favicon.png +0 -0
  154. package/test/jasmine/jasmine-browser.json +17 -0
  155. package/test/jasmine/jasmine.json +9 -0
  156. package/test/jqlite.spec.js +2133 -0
  157. package/test/loader.spec.js +219 -0
  158. package/test/messages/messages.spec.js +1146 -0
  159. package/test/min-err.spec.js +174 -0
  160. package/test/mock-test.html +13 -0
  161. package/test/module-test.html +15 -0
  162. package/test/ng/anomate.spec.js +606 -0
  163. package/test/ng/cache-factor.spec.js +334 -0
  164. package/test/ng/compile.spec.js +17956 -0
  165. package/test/ng/controller-provider.spec.js +227 -0
  166. package/test/ng/cookie-reader.spec.js +98 -0
  167. package/test/ng/directive/a.spec.js +192 -0
  168. package/test/ng/directive/bind.spec.js +334 -0
  169. package/test/ng/directive/boolean.spec.js +136 -0
  170. package/test/ng/directive/change.spec.js +71 -0
  171. package/test/ng/directive/class.spec.js +858 -0
  172. package/test/ng/directive/click.spec.js +38 -0
  173. package/test/ng/directive/cloak.spec.js +44 -0
  174. package/test/ng/directive/constoller.spec.js +194 -0
  175. package/test/ng/directive/element-style.spec.js +92 -0
  176. package/test/ng/directive/event.spec.js +282 -0
  177. package/test/ng/directive/form.spec.js +1518 -0
  178. package/test/ng/directive/href.spec.js +143 -0
  179. package/test/ng/directive/if.spec.js +402 -0
  180. package/test/ng/directive/include.spec.js +828 -0
  181. package/test/ng/directive/init.spec.js +68 -0
  182. package/test/ng/directive/input.spec.js +3810 -0
  183. package/test/ng/directive/list.spec.js +170 -0
  184. package/test/ng/directive/model-options.spec.js +1008 -0
  185. package/test/ng/directive/model.spec.js +1905 -0
  186. package/test/ng/directive/non-bindable.spec.js +55 -0
  187. package/test/ng/directive/options.spec.js +3583 -0
  188. package/test/ng/directive/ref.spec.js +575 -0
  189. package/test/ng/directive/repeat.spec.js +1675 -0
  190. package/test/ng/directive/script.spec.js +52 -0
  191. package/test/ng/directive/scrset.spec.js +67 -0
  192. package/test/ng/directive/select.spec.js +2541 -0
  193. package/test/ng/directive/show-hide.spec.js +253 -0
  194. package/test/ng/directive/src.spec.js +157 -0
  195. package/test/ng/directive/style.spec.js +178 -0
  196. package/test/ng/directive/switch.spec.js +647 -0
  197. package/test/ng/directive/validators.spec.js +717 -0
  198. package/test/ng/document.spec.js +52 -0
  199. package/test/ng/filter/filter.spec.js +714 -0
  200. package/test/ng/filter/filters.spec.js +35 -0
  201. package/test/ng/filter/limit-to.spec.js +251 -0
  202. package/test/ng/filter/order-by.spec.js +891 -0
  203. package/test/ng/filter.spec.js +149 -0
  204. package/test/ng/http-backend.spec.js +398 -0
  205. package/test/ng/http.spec.js +4071 -0
  206. package/test/ng/interpolate.spec.js +642 -0
  207. package/test/ng/interval.spec.js +343 -0
  208. package/test/ng/location.spec.js +3488 -0
  209. package/test/ng/on.spec.js +229 -0
  210. package/test/ng/parse.spec.js +4655 -0
  211. package/test/ng/prop.spec.js +805 -0
  212. package/test/ng/q.spec.js +2904 -0
  213. package/test/ng/root-element.spec.js +16 -0
  214. package/test/ng/sanitize-uri.spec.js +249 -0
  215. package/test/ng/sce.spec.js +660 -0
  216. package/test/ng/scope.spec.js +3442 -0
  217. package/test/ng/template-request.spec.js +236 -0
  218. package/test/ng/timeout.spec.js +351 -0
  219. package/test/ng/url-utils.spec.js +156 -0
  220. package/test/ng/utils.spec.js +144 -0
  221. package/test/original-test.html +21 -0
  222. package/test/public.spec.js +34 -0
  223. package/test/sanitize/bing-html.spec.js +36 -0
  224. package/test/server/express.js +158 -0
  225. package/test/test-utils.js +11 -0
  226. package/tsconfig.json +17 -0
  227. package/types/angular.d.ts +138 -0
  228. package/types/global.d.ts +9 -0
  229. package/types/index.d.ts +2357 -0
  230. package/types/jqlite.d.ts +558 -0
  231. package/vite.config.js +14 -0
@@ -0,0 +1,891 @@
1
+ import { publishExternalAPI } from "../../../src/public";
2
+ import { createInjector } from "../../../src/injector";
3
+
4
+ describe("Filter: orderBy", () => {
5
+ let orderBy;
6
+ let orderByFilter;
7
+
8
+ beforeEach(() => {
9
+ publishExternalAPI();
10
+ var injector = createInjector(["ng"]);
11
+ var $filter = injector.get("$filter");
12
+ orderBy = orderByFilter = $filter("orderBy");
13
+ });
14
+
15
+ describe("(Arrays)", () => {
16
+ it("should throw an exception if no array-like object is provided", () => {
17
+ expect(() => {
18
+ orderBy({});
19
+ }).toThrowError(/notarray/);
20
+ });
21
+
22
+ it("should not throw an exception if a null or undefined value is provided", () => {
23
+ expect(orderBy(null)).toEqual(null);
24
+ expect(orderBy(undefined)).toEqual(undefined);
25
+ });
26
+
27
+ it("should not throw an exception if an array-like object is provided", () => {
28
+ expect(orderBy("cba")).toEqual(["a", "b", "c"]);
29
+ });
30
+
31
+ it("should return sorted array if predicate is not provided", () => {
32
+ expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
33
+
34
+ expect(orderBy([2, 1, 3], "")).toEqual([1, 2, 3]);
35
+ expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
36
+ expect(orderBy([2, 1, 3], [""])).toEqual([1, 2, 3]);
37
+
38
+ expect(orderBy([2, 1, 3], "+")).toEqual([1, 2, 3]);
39
+ expect(orderBy([2, 1, 3], ["+"])).toEqual([1, 2, 3]);
40
+
41
+ expect(orderBy([2, 1, 3], "-")).toEqual([3, 2, 1]);
42
+ expect(orderBy([2, 1, 3], ["-"])).toEqual([3, 2, 1]);
43
+ });
44
+
45
+ it("should sort inherited from array", () => {
46
+ function BaseCollection() {}
47
+ BaseCollection.prototype = Array.prototype;
48
+ const child = new BaseCollection();
49
+ child.push({ a: 2 });
50
+ child.push({ a: 15 });
51
+
52
+ expect(Array.isArray(child)).toBe(false);
53
+ expect(child instanceof Array).toBe(true);
54
+
55
+ expect(orderBy(child, "a", true)).toEqual([{ a: 15 }, { a: 2 }]);
56
+ });
57
+
58
+ it("should sort array by predicate", () => {
59
+ expect(
60
+ orderBy(
61
+ [
62
+ { a: 15, b: 1 },
63
+ { a: 2, b: 1 },
64
+ ],
65
+ ["a", "b"],
66
+ ),
67
+ ).toEqual([
68
+ { a: 2, b: 1 },
69
+ { a: 15, b: 1 },
70
+ ]);
71
+ expect(
72
+ orderBy(
73
+ [
74
+ { a: 15, b: 1 },
75
+ { a: 2, b: 1 },
76
+ ],
77
+ ["b", "a"],
78
+ ),
79
+ ).toEqual([
80
+ { a: 2, b: 1 },
81
+ { a: 15, b: 1 },
82
+ ]);
83
+ expect(
84
+ orderBy(
85
+ [
86
+ { a: 15, b: 1 },
87
+ { a: 2, b: 1 },
88
+ ],
89
+ ["+b", "-a"],
90
+ ),
91
+ ).toEqual([
92
+ { a: 15, b: 1 },
93
+ { a: 2, b: 1 },
94
+ ]);
95
+ });
96
+
97
+ it("should sort array by date predicate", () => {
98
+ // same dates
99
+ expect(
100
+ orderBy(
101
+ [
102
+ { a: new Date("01/01/2014"), b: 1 },
103
+ { a: new Date("01/01/2014"), b: 3 },
104
+ { a: new Date("01/01/2014"), b: 4 },
105
+ { a: new Date("01/01/2014"), b: 2 },
106
+ ],
107
+ ["a", "b"],
108
+ ),
109
+ ).toEqual([
110
+ { a: new Date("01/01/2014"), b: 1 },
111
+ { a: new Date("01/01/2014"), b: 2 },
112
+ { a: new Date("01/01/2014"), b: 3 },
113
+ { a: new Date("01/01/2014"), b: 4 },
114
+ ]);
115
+
116
+ // one different date
117
+ expect(
118
+ orderBy(
119
+ [
120
+ { a: new Date("01/01/2014"), b: 1 },
121
+ { a: new Date("01/01/2014"), b: 3 },
122
+ { a: new Date("01/01/2013"), b: 4 },
123
+ { a: new Date("01/01/2014"), b: 2 },
124
+ ],
125
+ ["a", "b"],
126
+ ),
127
+ ).toEqual([
128
+ { a: new Date("01/01/2013"), b: 4 },
129
+ { a: new Date("01/01/2014"), b: 1 },
130
+ { a: new Date("01/01/2014"), b: 2 },
131
+ { a: new Date("01/01/2014"), b: 3 },
132
+ ]);
133
+ });
134
+
135
+ it("should compare timestamps when sorting dates", () => {
136
+ expect(orderBy([new Date("01/01/2015"), new Date("01/01/2014")])).toEqual(
137
+ [new Date("01/01/2014"), new Date("01/01/2015")],
138
+ );
139
+ });
140
+
141
+ it("should use function", () => {
142
+ expect(
143
+ orderBy(
144
+ [
145
+ { a: 15, b: 1 },
146
+ { a: 2, b: 1 },
147
+ ],
148
+ (value) => value.a,
149
+ ),
150
+ ).toEqual([
151
+ { a: 2, b: 1 },
152
+ { a: 15, b: 1 },
153
+ ]);
154
+ });
155
+
156
+ it("should support string predicates with names containing non-identifier characters", () => {
157
+ /* eslint-disable no-floating-decimal */
158
+ expect(
159
+ orderBy(
160
+ [{ "Tip %": 0.25 }, { "Tip %": 0.15 }, { "Tip %": 0.4 }],
161
+ '"Tip %"',
162
+ ),
163
+ ).toEqual([{ "Tip %": 0.15 }, { "Tip %": 0.25 }, { "Tip %": 0.4 }]);
164
+ expect(
165
+ orderBy([{ 원: 76000 }, { 원: 31000 }, { 원: 156000 }], '"원"'),
166
+ ).toEqual([{ 원: 31000 }, { 원: 76000 }, { 원: 156000 }]);
167
+ /* eslint-enable */
168
+ });
169
+
170
+ it("should throw if quoted string predicate is quoted incorrectly", () => {
171
+ /* eslint-disable no-floating-decimal */
172
+ expect(() =>
173
+ orderBy(
174
+ [{ "Tip %": 0.15 }, { "Tip %": 0.25 }, { "Tip %": 0.4 }],
175
+ "\"Tip %'",
176
+ ),
177
+ ).toThrow();
178
+ /* eslint-enable */
179
+ });
180
+
181
+ it("should not reverse array of objects with no predicate and reverse is not `true`", () => {
182
+ const array = [{ id: 2 }, { id: 1 }, { id: 4 }, { id: 3 }];
183
+ expect(orderBy(array)).toEqual(array);
184
+ });
185
+
186
+ it('should reverse array of objects with predicate of "-"', () => {
187
+ const array = [{ id: 2 }, { id: 1 }, { id: 4 }, { id: 3 }];
188
+ const reversedArray = [{ id: 3 }, { id: 4 }, { id: 1 }, { id: 2 }];
189
+ expect(orderBy(array, "-")).toEqual(reversedArray);
190
+ });
191
+
192
+ it("should not reverse array of objects with null prototype and no predicate", () => {
193
+ const array = [2, 1, 4, 3].map((id) => {
194
+ const obj = Object.create(null);
195
+ obj.id = id;
196
+ return obj;
197
+ });
198
+ expect(orderBy(array)).toEqual(array);
199
+ });
200
+
201
+ it("should sort nulls as Array.prototype.sort", () => {
202
+ const array = [{ id: 2 }, null, { id: 3 }, null];
203
+ expect(orderBy(array)).toEqual([{ id: 2 }, { id: 3 }, null, null]);
204
+ });
205
+
206
+ it("should sort array of arrays as Array.prototype.sort", () => {
207
+ expect(orderBy([["one"], ["two"], ["three"]])).toEqual([
208
+ ["one"],
209
+ ["three"],
210
+ ["two"],
211
+ ]);
212
+ });
213
+
214
+ it("should sort mixed array of objects and values in a stable way", () => {
215
+ expect(
216
+ orderBy([{ foo: 2 }, { foo: {} }, { foo: 3 }, { foo: 4 }], "foo"),
217
+ ).toEqual([{ foo: 2 }, { foo: 3 }, { foo: 4 }, { foo: {} }]);
218
+ });
219
+
220
+ it("should perform a stable sort", () => {
221
+ expect(
222
+ orderBy(
223
+ [
224
+ { foo: 2, bar: 1 },
225
+ { foo: 1, bar: 2 },
226
+ { foo: 2, bar: 3 },
227
+ { foo: 2, bar: 4 },
228
+ { foo: 1, bar: 5 },
229
+ { foo: 2, bar: 6 },
230
+ { foo: 2, bar: 7 },
231
+ { foo: 1, bar: 8 },
232
+ { foo: 2, bar: 9 },
233
+ { foo: 1, bar: 10 },
234
+ { foo: 2, bar: 11 },
235
+ { foo: 1, bar: 12 },
236
+ ],
237
+ "foo",
238
+ ),
239
+ ).toEqual([
240
+ { foo: 1, bar: 2 },
241
+ { foo: 1, bar: 5 },
242
+ { foo: 1, bar: 8 },
243
+ { foo: 1, bar: 10 },
244
+ { foo: 1, bar: 12 },
245
+ { foo: 2, bar: 1 },
246
+ { foo: 2, bar: 3 },
247
+ { foo: 2, bar: 4 },
248
+ { foo: 2, bar: 6 },
249
+ { foo: 2, bar: 7 },
250
+ { foo: 2, bar: 9 },
251
+ { foo: 2, bar: 11 },
252
+ ]);
253
+
254
+ expect(
255
+ orderBy(
256
+ [
257
+ { foo: 2, bar: 1 },
258
+ { foo: 1, bar: 2 },
259
+ { foo: 2, bar: 3 },
260
+ { foo: 2, bar: 4 },
261
+ { foo: 1, bar: 5 },
262
+ { foo: 2, bar: 6 },
263
+ { foo: 2, bar: 7 },
264
+ { foo: 1, bar: 8 },
265
+ { foo: 2, bar: 9 },
266
+ { foo: 1, bar: 10 },
267
+ { foo: 2, bar: 11 },
268
+ { foo: 1, bar: 12 },
269
+ ],
270
+ "foo",
271
+ true,
272
+ ),
273
+ ).toEqual([
274
+ { foo: 2, bar: 11 },
275
+ { foo: 2, bar: 9 },
276
+ { foo: 2, bar: 7 },
277
+ { foo: 2, bar: 6 },
278
+ { foo: 2, bar: 4 },
279
+ { foo: 2, bar: 3 },
280
+ { foo: 2, bar: 1 },
281
+ { foo: 1, bar: 12 },
282
+ { foo: 1, bar: 10 },
283
+ { foo: 1, bar: 8 },
284
+ { foo: 1, bar: 5 },
285
+ { foo: 1, bar: 2 },
286
+ ]);
287
+ });
288
+
289
+ describe("(reversing order)", () => {
290
+ it("should not reverse collection if `reverse` param is falsy", () => {
291
+ const items = [{ a: 2 }, { a: 15 }];
292
+ const expr = "a";
293
+ const sorted = [{ a: 2 }, { a: 15 }];
294
+
295
+ expect(orderBy(items, expr, false)).toEqual(sorted);
296
+ expect(orderBy(items, expr, 0)).toEqual(sorted);
297
+ expect(orderBy(items, expr, "")).toEqual(sorted);
298
+ expect(orderBy(items, expr, NaN)).toEqual(sorted);
299
+ expect(orderBy(items, expr, null)).toEqual(sorted);
300
+ expect(orderBy(items, expr, undefined)).toEqual(sorted);
301
+ });
302
+
303
+ it("should reverse collection if `reverse` param is truthy", () => {
304
+ const items = [{ a: 2 }, { a: 15 }];
305
+ const expr = "a";
306
+ const sorted = [{ a: 15 }, { a: 2 }];
307
+
308
+ expect(orderBy(items, expr, true)).toEqual(sorted);
309
+ expect(orderBy(items, expr, 1)).toEqual(sorted);
310
+ expect(orderBy(items, expr, "reverse")).toEqual(sorted);
311
+ expect(orderBy(items, expr, {})).toEqual(sorted);
312
+ expect(orderBy(items, expr, [])).toEqual(sorted);
313
+ expect(orderBy(items, expr, () => {})).toEqual(sorted);
314
+ });
315
+
316
+ it("should reverse collection if `reverse` param is `true`, even without an `expression`", () => {
317
+ const originalItems = [{ id: 2 }, { id: 1 }, { id: 4 }, { id: 3 }];
318
+ const reversedItems = [{ id: 3 }, { id: 4 }, { id: 1 }, { id: 2 }];
319
+ expect(orderBy(originalItems, null, true)).toEqual(reversedItems);
320
+ });
321
+ });
322
+
323
+ describe("(built-in comparator)", () => {
324
+ it("should compare numbers numerically", () => {
325
+ const items = [100, 3, 20];
326
+ const expr = null;
327
+ const sorted = [3, 20, 100];
328
+
329
+ expect(orderBy(items, expr)).toEqual(sorted);
330
+ });
331
+
332
+ it("should compare strings alphabetically", () => {
333
+ const items = ["100", "3", "20", "_b", "a"];
334
+ const expr = null;
335
+ const sorted = ["100", "20", "3", "_b", "a"];
336
+
337
+ expect(orderBy(items, expr)).toEqual(sorted);
338
+ });
339
+
340
+ it("should compare strings case-insensitively", () => {
341
+ const items = ["c", "B", "a"];
342
+ const expr = null;
343
+ const sorted = ["a", "B", "c"];
344
+
345
+ expect(orderBy(items, expr)).toEqual(sorted);
346
+ });
347
+
348
+ it("should compare objects based on `index`", () => {
349
+ const items = [{ c: 3 }, { b: 2 }, { a: 1 }];
350
+ const expr = null;
351
+ const sorted = [{ c: 3 }, { b: 2 }, { a: 1 }];
352
+
353
+ expect(orderBy(items, expr)).toEqual(sorted);
354
+ });
355
+
356
+ it("should compare values of different type alphabetically by type", () => {
357
+ const test = function () {};
358
+ const items = [undefined, "1", {}, 999, test, false];
359
+ const expr = null;
360
+ const sorted = [false, test, 999, {}, "1", undefined];
361
+
362
+ expect(orderBy(items, expr)).toEqual(sorted);
363
+ });
364
+
365
+ it("should consider null and undefined greater than any other value", () => {
366
+ const items = [undefined, null, "z", {}, 999, false];
367
+ const expr = null;
368
+ const sorted = [false, 999, {}, "z", null, undefined];
369
+ const reversed = [undefined, null, "z", {}, 999, false];
370
+ expect(orderBy(items, expr)).toEqual(sorted);
371
+ expect(orderBy(items, expr, true)).toEqual(reversed);
372
+ });
373
+ });
374
+
375
+ describe("(custom comparator)", () => {
376
+ it("should support a custom comparator", () => {
377
+ const items = [4, 42, 2];
378
+ const expr = null;
379
+ const reverse = null;
380
+ const sorted = [42, 2, 4];
381
+
382
+ const comparator = function (o1, o2) {
383
+ const v1 = o1.value;
384
+ const v2 = o2.value;
385
+
386
+ // 42 always comes first
387
+ if (v1 === v2) return 0;
388
+ if (v1 === 42) return -1;
389
+ if (v2 === 42) return 1;
390
+
391
+ // Default comparison for other values
392
+ return v1 < v2 ? -1 : 1;
393
+ };
394
+
395
+ expect(orderBy(items, expr, reverse, comparator)).toEqual(sorted);
396
+ });
397
+
398
+ it("should support `reverseOrder` with a custom comparator", () => {
399
+ const items = [4, 42, 2];
400
+ const expr = null;
401
+ const reverse = true;
402
+ const sorted = [4, 2, 42];
403
+
404
+ const comparator = function (o1, o2) {
405
+ const v1 = o1.value;
406
+ const v2 = o2.value;
407
+
408
+ // 42 always comes first
409
+ if (v1 === v2) return 0;
410
+ if (v1 === 42) return -1;
411
+ if (v2 === 42) return 1;
412
+
413
+ // Default comparison for other values
414
+ return v1 < v2 ? -1 : 1;
415
+ };
416
+
417
+ expect(orderBy(items, expr, reverse, comparator)).toEqual(sorted);
418
+ });
419
+
420
+ it("should pass `{value, type, index}` objects to comparators", () => {
421
+ const noop = function () {};
422
+ const items = [false, noop, 999, {}, "", undefined];
423
+ const expr = null;
424
+ const reverse = null;
425
+ const comparator = jasmine.createSpy("comparator").and.returnValue(-1);
426
+
427
+ orderBy(items, expr, reverse, comparator);
428
+ const allArgsFlat = Array.prototype.concat.apply(
429
+ [],
430
+ comparator.calls.allArgs(),
431
+ );
432
+
433
+ expect(allArgsFlat).toContain({
434
+ index: 0,
435
+ type: "boolean",
436
+ value: false,
437
+ });
438
+ expect(allArgsFlat).toContain({
439
+ index: 1,
440
+ type: "function",
441
+ value: noop,
442
+ });
443
+ expect(allArgsFlat).toContain({ index: 2, type: "number", value: 999 });
444
+ expect(allArgsFlat).toContain({ index: 3, type: "object", value: {} });
445
+ expect(allArgsFlat).toContain({ index: 4, type: "string", value: "" });
446
+ expect(allArgsFlat).toContain({
447
+ index: 5,
448
+ type: "undefined",
449
+ value: undefined,
450
+ });
451
+ });
452
+
453
+ it('should treat a value of `null` as type `"null"`', () => {
454
+ const items = [null, null];
455
+ const expr = null;
456
+ const reverse = null;
457
+ const comparator = jasmine.createSpy("comparator").and.returnValue(-1);
458
+
459
+ orderBy(items, expr, reverse, comparator);
460
+ const arg = comparator.calls.argsFor(0)[0];
461
+
462
+ expect(arg).toEqual(
463
+ jasmine.objectContaining({
464
+ type: "null",
465
+ value: null,
466
+ }),
467
+ );
468
+ });
469
+
470
+ it("should not convert strings to lower-case", () => {
471
+ const items = ["c", "B", "a"];
472
+ const expr = null;
473
+ const reverse = null;
474
+ const sorted = ["B", "a", "c"];
475
+
476
+ const comparator = function (o1, o2) {
477
+ return o1.value < o2.value ? -1 : 1;
478
+ };
479
+
480
+ expect(orderBy(items, expr, reverse, comparator)).toEqual(sorted);
481
+ });
482
+
483
+ it("should use `index` as `value` if no other predicate can distinguish between two items", () => {
484
+ const items = ["foo", "bar"];
485
+ const expr = null;
486
+ const reverse = null;
487
+ const comparator = jasmine.createSpy("comparator").and.returnValue(0);
488
+
489
+ orderBy(items, expr, reverse, comparator);
490
+
491
+ expect(comparator).toHaveBeenCalledTimes(2);
492
+ const lastArgs = comparator.calls.mostRecent().args;
493
+
494
+ expect(lastArgs).toContain(
495
+ jasmine.objectContaining({ value: 0, type: "number" }),
496
+ );
497
+ expect(lastArgs).toContain(
498
+ jasmine.objectContaining({ value: 1, type: "number" }),
499
+ );
500
+ });
501
+
502
+ it("should support multiple predicates and per-predicate sorting direction", () => {
503
+ const items = [
504
+ { owner: "ownerA", type: "typeA" },
505
+ { owner: "ownerB", type: "typeB" },
506
+ { owner: "ownerC", type: "typeB" },
507
+ { owner: "ownerD", type: "typeB" },
508
+ ];
509
+ const expr = ["type", "-owner"];
510
+ const reverse = null;
511
+ const sorted = [
512
+ { owner: "ownerA", type: "typeA" },
513
+ { owner: "ownerC", type: "typeB" },
514
+ { owner: "ownerB", type: "typeB" },
515
+ { owner: "ownerD", type: "typeB" },
516
+ ];
517
+
518
+ const comparator = function (o1, o2) {
519
+ const v1 = o1.value;
520
+ const v2 = o2.value;
521
+ const isNerd1 = v1.toLowerCase().indexOf("nerd") !== -1;
522
+ const isNerd2 = v2.toLowerCase().indexOf("nerd") !== -1;
523
+
524
+ // Shamelessly promote "nerds"
525
+ if (isNerd1 || isNerd2) {
526
+ return isNerd1 && isNerd2 ? 0 : isNerd1 ? -1 : 1;
527
+ }
528
+
529
+ // No "nerd"; alphabetical order
530
+ return v1 === v2 ? 0 : v1 < v2 ? -1 : 1;
531
+ };
532
+
533
+ expect(orderBy(items, expr, reverse, comparator)).toEqual(sorted);
534
+ });
535
+
536
+ it("should use the default comparator to break ties on a provided comparator", () => {
537
+ // Some list that won't be sorted "naturally", i.e. should sort to ['a', 'B', 'c']
538
+ const items = ["c", "a", "B"];
539
+ const expr = null;
540
+ function comparator() {
541
+ return 0;
542
+ }
543
+ const reversed = ["B", "a", "c"];
544
+
545
+ expect(orderBy(items, expr, false, comparator)).toEqual(items);
546
+ expect(orderBy(items, expr, true, comparator)).toEqual(reversed);
547
+ });
548
+ });
549
+
550
+ describe("(object as `value`)", () => {
551
+ it("should use the return value of `valueOf()` (if primitive)", () => {
552
+ const o1 = {
553
+ k: 1,
554
+ valueOf() {
555
+ return 2;
556
+ },
557
+ };
558
+ const o2 = {
559
+ k: 2,
560
+ valueOf() {
561
+ return 1;
562
+ },
563
+ };
564
+
565
+ const items = [o1, o2];
566
+ const expr = null;
567
+ const sorted = [o2, o1];
568
+
569
+ expect(orderBy(items, expr)).toEqual(sorted);
570
+ });
571
+
572
+ it("should use the return value of `toString()` (if primitive)", () => {
573
+ const o1 = {
574
+ k: 1,
575
+ toString() {
576
+ return 2;
577
+ },
578
+ };
579
+ const o2 = {
580
+ k: 2,
581
+ toString() {
582
+ return 1;
583
+ },
584
+ };
585
+
586
+ const items = [o1, o2];
587
+ const expr = null;
588
+ const sorted = [o2, o1];
589
+
590
+ expect(orderBy(items, expr)).toEqual(sorted);
591
+ });
592
+
593
+ it("should ignore the `toString()` inherited from `Object`", () => {
594
+ /* globals toString: true */
595
+
596
+ // The global `toString` variable (in 'src/Angular.js')
597
+ // has already captured `Object.prototype.toString`
598
+ const originalToString = toString;
599
+ window.toString = jasmine
600
+ .createSpy("toString")
601
+ .and.callFake(originalToString);
602
+
603
+ const o1 = Object.create({ toString });
604
+ const o2 = Object.create({ toString });
605
+
606
+ const items = [o1, o2];
607
+ const expr = null;
608
+
609
+ orderBy(items, expr);
610
+
611
+ expect(o1.toString).not.toHaveBeenCalled();
612
+ expect(o2.toString).not.toHaveBeenCalled();
613
+
614
+ toString = originalToString;
615
+ });
616
+
617
+ it("should use the return value of `valueOf()` for subsequent steps (if non-primitive)", () => {
618
+ const o1 = {
619
+ k: 1,
620
+ valueOf() {
621
+ return o3;
622
+ },
623
+ };
624
+ const o2 = {
625
+ k: 2,
626
+ valueOf() {
627
+ return o4;
628
+ },
629
+ };
630
+ let o3 = {
631
+ k: 3,
632
+ toString() {
633
+ return 4;
634
+ },
635
+ };
636
+ let o4 = {
637
+ k: 4,
638
+ toString() {
639
+ return 3;
640
+ },
641
+ };
642
+
643
+ const items = [o1, o2];
644
+ const expr = null;
645
+ const sorted = [o2, o1];
646
+
647
+ expect(orderBy(items, expr)).toEqual(sorted);
648
+ });
649
+
650
+ it("should use the return value of `toString()` for subsequent steps (if non-primitive)", () => {
651
+ const o1 = {
652
+ k: 1,
653
+ toString() {
654
+ return o3;
655
+ },
656
+ };
657
+ const o2 = {
658
+ k: 2,
659
+ toString() {
660
+ return o4;
661
+ },
662
+ };
663
+ let o3 = { k: 3 };
664
+ let o4 = { k: 4 };
665
+
666
+ const items = [o1, o2];
667
+ const expr = null;
668
+ const reverse = null;
669
+ const comparator = jasmine.createSpy("comparator").and.returnValue(-1);
670
+
671
+ orderBy(items, expr, reverse, comparator);
672
+ const args = comparator.calls.argsFor(0);
673
+
674
+ expect(args).toContain(
675
+ jasmine.objectContaining({ value: o3, type: "object" }),
676
+ );
677
+ expect(args).toContain(
678
+ jasmine.objectContaining({ value: o4, type: "object" }),
679
+ );
680
+ });
681
+
682
+ it("should use the object itself as `value` if no conversion took place", () => {
683
+ const o1 = { k: 1 };
684
+ const o2 = { k: 2 };
685
+
686
+ const items = [o1, o2];
687
+ const expr = null;
688
+ const reverse = null;
689
+ const comparator = jasmine.createSpy("comparator").and.returnValue(-1);
690
+
691
+ orderBy(items, expr, reverse, comparator);
692
+ const args = comparator.calls.argsFor(0);
693
+
694
+ expect(args).toContain(
695
+ jasmine.objectContaining({ value: o1, type: "object" }),
696
+ );
697
+ expect(args).toContain(
698
+ jasmine.objectContaining({ value: o2, type: "object" }),
699
+ );
700
+ });
701
+ });
702
+ });
703
+
704
+ describe("(Array-Like Objects)", () => {
705
+ function arrayLike(args) {
706
+ const result = {};
707
+ let i;
708
+ for (i = 0; i < args.length; ++i) {
709
+ result[i] = args[i];
710
+ }
711
+ result.length = i;
712
+ return result;
713
+ }
714
+
715
+ beforeEach(() => {
716
+ publishExternalAPI();
717
+ var injector = createInjector(["ng"]);
718
+ var $filter = injector.get("$filter");
719
+ orderBy = function (collection) {
720
+ const args = Array.prototype.slice.call(arguments, 0);
721
+ args[0] = arrayLike(args[0]);
722
+ return orderByFilter.apply(null, args);
723
+ };
724
+ });
725
+
726
+ it("should return sorted array if predicate is not provided", () => {
727
+ expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
728
+
729
+ expect(orderBy([2, 1, 3], "")).toEqual([1, 2, 3]);
730
+ expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
731
+ expect(orderBy([2, 1, 3], [""])).toEqual([1, 2, 3]);
732
+
733
+ expect(orderBy([2, 1, 3], "+")).toEqual([1, 2, 3]);
734
+ expect(orderBy([2, 1, 3], ["+"])).toEqual([1, 2, 3]);
735
+
736
+ expect(orderBy([2, 1, 3], "-")).toEqual([3, 2, 1]);
737
+ expect(orderBy([2, 1, 3], ["-"])).toEqual([3, 2, 1]);
738
+ });
739
+
740
+ it("shouldSortArrayInReverse", () => {
741
+ expect(orderBy([{ a: 15 }, { a: 2 }], "a", true)).toEqual([
742
+ { a: 15 },
743
+ { a: 2 },
744
+ ]);
745
+ expect(orderBy([{ a: 15 }, { a: 2 }], "a", "T")).toEqual([
746
+ { a: 15 },
747
+ { a: 2 },
748
+ ]);
749
+ expect(orderBy([{ a: 15 }, { a: 2 }], "a", "reverse")).toEqual([
750
+ { a: 15 },
751
+ { a: 2 },
752
+ ]);
753
+ });
754
+
755
+ it("should sort array by predicate", () => {
756
+ expect(
757
+ orderBy(
758
+ [
759
+ { a: 15, b: 1 },
760
+ { a: 2, b: 1 },
761
+ ],
762
+ ["a", "b"],
763
+ ),
764
+ ).toEqual([
765
+ { a: 2, b: 1 },
766
+ { a: 15, b: 1 },
767
+ ]);
768
+ expect(
769
+ orderBy(
770
+ [
771
+ { a: 15, b: 1 },
772
+ { a: 2, b: 1 },
773
+ ],
774
+ ["b", "a"],
775
+ ),
776
+ ).toEqual([
777
+ { a: 2, b: 1 },
778
+ { a: 15, b: 1 },
779
+ ]);
780
+ expect(
781
+ orderBy(
782
+ [
783
+ { a: 15, b: 1 },
784
+ { a: 2, b: 1 },
785
+ ],
786
+ ["+b", "-a"],
787
+ ),
788
+ ).toEqual([
789
+ { a: 15, b: 1 },
790
+ { a: 2, b: 1 },
791
+ ]);
792
+ });
793
+
794
+ it("should sort array by date predicate", () => {
795
+ // same dates
796
+ expect(
797
+ orderBy(
798
+ [
799
+ { a: new Date("01/01/2014"), b: 1 },
800
+ { a: new Date("01/01/2014"), b: 3 },
801
+ { a: new Date("01/01/2014"), b: 4 },
802
+ { a: new Date("01/01/2014"), b: 2 },
803
+ ],
804
+ ["a", "b"],
805
+ ),
806
+ ).toEqual([
807
+ { a: new Date("01/01/2014"), b: 1 },
808
+ { a: new Date("01/01/2014"), b: 2 },
809
+ { a: new Date("01/01/2014"), b: 3 },
810
+ { a: new Date("01/01/2014"), b: 4 },
811
+ ]);
812
+
813
+ // one different date
814
+ expect(
815
+ orderBy(
816
+ [
817
+ { a: new Date("01/01/2014"), b: 1 },
818
+ { a: new Date("01/01/2014"), b: 3 },
819
+ { a: new Date("01/01/2013"), b: 4 },
820
+ { a: new Date("01/01/2014"), b: 2 },
821
+ ],
822
+ ["a", "b"],
823
+ ),
824
+ ).toEqual([
825
+ { a: new Date("01/01/2013"), b: 4 },
826
+ { a: new Date("01/01/2014"), b: 1 },
827
+ { a: new Date("01/01/2014"), b: 2 },
828
+ { a: new Date("01/01/2014"), b: 3 },
829
+ ]);
830
+ });
831
+
832
+ it("should use function", () => {
833
+ expect(
834
+ orderBy(
835
+ [
836
+ { a: 15, b: 1 },
837
+ { a: 2, b: 1 },
838
+ ],
839
+ (value) => value.a,
840
+ ),
841
+ ).toEqual([
842
+ { a: 2, b: 1 },
843
+ { a: 15, b: 1 },
844
+ ]);
845
+ });
846
+
847
+ it("should support string predicates with names containing non-identifier characters", () => {
848
+ /* eslint-disable no-floating-decimal */
849
+ expect(
850
+ orderBy(
851
+ [{ "Tip %": 0.25 }, { "Tip %": 0.15 }, { "Tip %": 0.4 }],
852
+ '"Tip %"',
853
+ ),
854
+ ).toEqual([{ "Tip %": 0.15 }, { "Tip %": 0.25 }, { "Tip %": 0.4 }]);
855
+ expect(
856
+ orderBy([{ 원: 76000 }, { 원: 31000 }, { 원: 156000 }], '"원"'),
857
+ ).toEqual([{ 원: 31000 }, { 원: 76000 }, { 원: 156000 }]);
858
+ /* eslint-enable */
859
+ });
860
+
861
+ it("should throw if quoted string predicate is quoted incorrectly", () => {
862
+ /* eslint-disable no-floating-decimal */
863
+ expect(() =>
864
+ orderBy(
865
+ [{ "Tip %": 0.15 }, { "Tip %": 0.25 }, { "Tip %": 0.4 }],
866
+ "\"Tip %'",
867
+ ),
868
+ ).toThrow();
869
+ /* eslint-enable */
870
+ });
871
+
872
+ it("should not reverse array of objects with no predicate", () => {
873
+ const array = [{ id: 2 }, { id: 1 }, { id: 4 }, { id: 3 }];
874
+ expect(orderBy(array)).toEqual(array);
875
+ });
876
+
877
+ it("should not reverse array of objects with null prototype and no predicate", () => {
878
+ const array = [2, 1, 4, 3].map((id) => {
879
+ const obj = Object.create(null);
880
+ obj.id = id;
881
+ return obj;
882
+ });
883
+ expect(orderBy(array)).toEqual(array);
884
+ });
885
+
886
+ it("should sort nulls as Array.prototype.sort", () => {
887
+ const array = [{ id: 2 }, null, { id: 3 }, null];
888
+ expect(orderBy(array)).toEqual([{ id: 2 }, { id: 3 }, null, null]);
889
+ });
890
+ });
891
+ });