@tim-code/my-util 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
package/src/array.js CHANGED
@@ -92,6 +92,23 @@ export function descending(key) {
92
92
  }
93
93
  }
94
94
 
95
+ /**
96
+ * Combines multiple ascending and descending comparators.
97
+ * @param {...Function} comparators
98
+ * @returns {Function}
99
+ */
100
+ export function multilevel(...comparators) {
101
+ return (a, b) => {
102
+ for (const comparator of comparators) {
103
+ const result = comparator(a, b)
104
+ if (result) {
105
+ return result
106
+ }
107
+ }
108
+ return 0
109
+ }
110
+ }
111
+
95
112
  /**
96
113
  * Creates a function that accesses an object's value at key.
97
114
  * @param {string} key
package/src/array.test.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-restricted-syntax */
2
2
  import { describe, expect, it, jest } from "@jest/globals"
3
3
 
4
- const { chunk, unique, mutateValues, ascending, descending, via } = await import("./array.js")
4
+ const { chunk, unique, mutateValues, ascending, descending, multilevel, via } = await import("./array.js")
5
5
 
6
6
  describe("chunk", () => {
7
7
  it("splits array into chunks of specified size", () => {
@@ -188,6 +188,64 @@ describe("descending", () => {
188
188
  })
189
189
  })
190
190
 
191
+ describe("multilevel", () => {
192
+ it("returns 0 if all comparators return 0", () => {
193
+ const cmp = multilevel(
194
+ () => 0,
195
+ () => 0
196
+ )
197
+ expect(cmp(1, 2)).toBe(0)
198
+ expect(cmp("a", "b")).toBe(0)
199
+ })
200
+
201
+ it("returns first non-zero comparator result", () => {
202
+ const cmp = multilevel(
203
+ () => 0,
204
+ () => -1,
205
+ () => 1
206
+ )
207
+ expect(cmp(1, 2)).toBe(-1)
208
+ const cmp2 = multilevel(
209
+ () => 0,
210
+ () => 0,
211
+ () => 1
212
+ )
213
+ expect(cmp2(1, 2)).toBe(1)
214
+ })
215
+
216
+ it("works with ascending and descending comparators", () => {
217
+ const arr = [
218
+ { a: 1, b: 2 },
219
+ { a: 2, b: 1 },
220
+ { a: 1, b: 1 },
221
+ { a: 2, b: 2 },
222
+ ]
223
+ arr.sort(multilevel(ascending("a"), descending("b")))
224
+ expect(arr).toEqual([
225
+ { a: 1, b: 2 },
226
+ { a: 1, b: 1 },
227
+ { a: 2, b: 2 },
228
+ { a: 2, b: 1 },
229
+ ])
230
+ })
231
+
232
+ it("short-circuits after first non-zero comparator", () => {
233
+ const calls = []
234
+ const cmp1 = jest.fn(() => { calls.push("cmp1"); return 0 })
235
+ const cmp2 = jest.fn(() => { calls.push("cmp2"); return -1 })
236
+ const cmp3 = jest.fn(() => { calls.push("cmp3"); return 1 })
237
+ const cmp = multilevel(cmp1, cmp2, cmp3)
238
+ expect(cmp({}, {})).toBe(-1)
239
+ expect(calls).toEqual(["cmp1", "cmp2"])
240
+ })
241
+
242
+ it("returns 0 if no comparators are provided", () => {
243
+ const cmp = multilevel()
244
+ expect(cmp(1, 2)).toBe(0)
245
+ expect(cmp("a", "b")).toBe(0)
246
+ })
247
+ })
248
+
191
249
  describe("via", () => {
192
250
  it("returns a function that accesses the given key", () => {
193
251
  const getFoo = via("foo")