From 5839af209729549ed39cdb21bb224b38bd1c3300 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Tue, 30 Apr 2024 01:11:50 +0900 Subject: [PATCH 1/2] add orderBy --- src/stress/orderBy.spec.ts | 12 ++++++++++++ src/stress/orderBy.ts | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/stress/orderBy.spec.ts create mode 100644 src/stress/orderBy.ts diff --git a/src/stress/orderBy.spec.ts b/src/stress/orderBy.spec.ts new file mode 100644 index 0000000..172a7ca --- /dev/null +++ b/src/stress/orderBy.spec.ts @@ -0,0 +1,12 @@ +import { orderBy } from './orderBy'; + +describe('orderBy', () => { + it('sorts value DESC by "natural" JS ordering', () => { + expect(orderBy([2, 3, 1], (v) => v)).toEqual([3, 2, 1]); + expect(orderBy([1, 1, 2], (v) => v)).toEqual([2, 1, 1]); + }); + it('sorts value ASC by "natural" JS ordering', () => { + expect(orderBy([2, 3, 1], (v) => v, true)).toEqual([1, 2, 3]); + expect(orderBy([1, 1, 2], (v) => v, true)).toEqual([1, 1, 2]); + }); +}); diff --git a/src/stress/orderBy.ts b/src/stress/orderBy.ts new file mode 100644 index 0000000..fdbe34f --- /dev/null +++ b/src/stress/orderBy.ts @@ -0,0 +1,18 @@ +/** + * (for more complicated case, consider fp-ts Order typeclass) + * @param values + * @param key + * @param asc + */ +export function orderBy(values: T[], key: (v: T) => O, asc = false): T[] { + const indexes = values.map((v, i) => ({ v, i })); + return indexes.sort((a, b) => (asc ? compare(a.v, b.v) : -compare(a.v, b.v))).map((_) => _.v); +} + +function compare(a: any, b: any): number { + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } else return 0; +} From db73ee53b5b263221a2939427afb892b19624879 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Tue, 7 May 2024 00:03:42 +0900 Subject: [PATCH 2/2] fix --- src/stress/groupBy.ts | 2 +- src/stress/orderBy.spec.ts | 12 ------------ src/stress/orderBy.ts | 18 ------------------ src/stress/sortBy.spec.ts | 2 +- 4 files changed, 2 insertions(+), 32 deletions(-) delete mode 100644 src/stress/orderBy.spec.ts delete mode 100644 src/stress/orderBy.ts diff --git a/src/stress/groupBy.ts b/src/stress/groupBy.ts index bed1f0d..524d60d 100644 --- a/src/stress/groupBy.ts +++ b/src/stress/groupBy.ts @@ -3,7 +3,7 @@ import { DefaultMap } from '../collection/default-map'; export function groupBy( values: Iterable, keyer: (value: T) => K, -): Record { +): Record { return Object.fromEntries(groupByAsMap(values, keyer)) as Record; } diff --git a/src/stress/orderBy.spec.ts b/src/stress/orderBy.spec.ts deleted file mode 100644 index 172a7ca..0000000 --- a/src/stress/orderBy.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { orderBy } from './orderBy'; - -describe('orderBy', () => { - it('sorts value DESC by "natural" JS ordering', () => { - expect(orderBy([2, 3, 1], (v) => v)).toEqual([3, 2, 1]); - expect(orderBy([1, 1, 2], (v) => v)).toEqual([2, 1, 1]); - }); - it('sorts value ASC by "natural" JS ordering', () => { - expect(orderBy([2, 3, 1], (v) => v, true)).toEqual([1, 2, 3]); - expect(orderBy([1, 1, 2], (v) => v, true)).toEqual([1, 1, 2]); - }); -}); diff --git a/src/stress/orderBy.ts b/src/stress/orderBy.ts deleted file mode 100644 index fdbe34f..0000000 --- a/src/stress/orderBy.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * (for more complicated case, consider fp-ts Order typeclass) - * @param values - * @param key - * @param asc - */ -export function orderBy(values: T[], key: (v: T) => O, asc = false): T[] { - const indexes = values.map((v, i) => ({ v, i })); - return indexes.sort((a, b) => (asc ? compare(a.v, b.v) : -compare(a.v, b.v))).map((_) => _.v); -} - -function compare(a: any, b: any): number { - if (a < b) { - return -1; - } else if (a > b) { - return 1; - } else return 0; -} diff --git a/src/stress/sortBy.spec.ts b/src/stress/sortBy.spec.ts index bd34033..7cb9c36 100644 --- a/src/stress/sortBy.spec.ts +++ b/src/stress/sortBy.spec.ts @@ -1,6 +1,6 @@ import { sortBy } from './sortBy'; -describe('orderBy', () => { +describe('sortBy', () => { it('sorts value DESC by "natural" JS ordering', () => { expect(sortBy([2, 3, 1], (v) => v, false)).toEqual([3, 2, 1]); expect(sortBy([1, 1, 2], (v) => v, false)).toEqual([2, 1, 1]);