Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-clouds-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@giraugh/tools": minor
---

Implement `sortArrayBy`
6 changes: 3 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v7
with:
fetch-depth: 0

- name: Setup node & yarn
uses: actions/setup-node@v3
uses: actions/setup-node@v7
with:
node-version: 18
node-version: 24
cache: 'yarn'

- name: Install dependencies
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: actions/setup-node@v3
- uses: actions/setup-node@v7
with:
node-version: 18
node-version: 24
cache: 'yarn'
- run: yarn install --immutable
- uses: changesets/action@v1
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 18
node-version: 24
cache: 'yarn'
- run: yarn install --immutable
- run: yarn test

typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 18
node-version: 24
cache: 'yarn'
- run: yarn install --immutable
- run: yarn tsc
1 change: 1 addition & 0 deletions lib/arrays/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './partitionArray'
export * from './rotateArray'
export * from './splitArray'
export * from './splitArrayBy'
export * from './sortArrayBy'
41 changes: 41 additions & 0 deletions lib/arrays/sortArrayBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Return a sorted array using a comparison function that returns a sorting key
* @param array the array to sort
* @param keyFn the key to sort by. The elements of the array will be sorted using standard `>` and `<` operations on this key.
* @param order - whether ascending or descending. 'asc' by default.
* @returns an object containing arrays with an entry for each group
*
* @example
* const sorted = sortArrayBy(events, event => event.date.valueOf())
*/
export const sortArrayBy = <T, K extends number | string>(
array: T[],
keyFn: (element: T) => K,
order: 'asc' | 'desc' = 'asc',
): T[] => {
const fw = order === 'asc' ? 1 : -1;
return array
.toSorted((a, b) => keyFn(a) > keyFn(b) ? fw : -fw)
}

// Tests
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest

it('works for example 1', () => {
const events = [
{ message: 'Checked into hotel', date: new Date('2026-03-03 08:00') },
{ message: 'Plane took off', date: new Date('2026-03-02 13:00') },
{ message: 'Arrived at the airport', date: new Date('2026-03-02 10:00') },
{ message: 'Arrived in Italy', date: new Date('2026-03-03 05:00') },
]

const sorted = sortArrayBy(events, e => e.date.valueOf())
expect(sorted.map(s => s.message)).toEqual([
'Arrived at the airport',
'Plane took off',
'Arrived in Italy',
'Checked into hotel',
])
})
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
"typedoc": "^0.24.4",
"typescript": "^4.6.4",
"typedoc": "^0.28.20",
"typescript": "^5.2.2",
Comment thread
giraugh marked this conversation as resolved.
"vercel": "^28.19.0",
"vite": "^4.2.1",
"vite-plugin-dts": "^1.6.5",
"vite-plugin-dts": "^5.1.1",
"vitest": "^0.30.0"
},
"engines": {
"node": ">=24.0.0"
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"lib": ["ESNext", "ES2023", "DOM", "DOM.Iterable"],
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
Expand Down
Loading
Loading