-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c8c1f8
commit 3605cc5
Showing
7 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { aggregate } from './aggregate'; | ||
export { takeFirst, takeFirstOrThrow } from './take'; | ||
export { transform } from './transform'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { describe, test, expect } from 'bun:test'; | ||
import { transform } from './transform'; | ||
import { aggregate } from './aggregate'; | ||
|
||
describe('transform', () => { | ||
test('data-first', async () => { | ||
const rows = [ | ||
{ id: 1, name: 'salem', age: 8, post: { id: 1, title: '1' } }, | ||
{ id: 1, name: 'salem', age: 8, post: { id: 4, title: '4' } }, | ||
{ id: 2, name: 'mimo', age: 7, post: { id: 2, title: '2' } }, | ||
{ id: 3, name: 'tapi', age: 6, post: { id: 3, title: '3' } }, | ||
]; | ||
|
||
const transformed = transform({ | ||
rows, | ||
fields: { | ||
name: (row) => row.name.toUpperCase(), | ||
post: (row) => ({ id: row.post.id, title: +row.post.title }), | ||
}, | ||
}); | ||
|
||
expect(transformed).toEqual([ | ||
{ id: 1, name: 'SALEM', age: 8, post: { id: 1, title: 1 } }, | ||
{ id: 1, name: 'SALEM', age: 8, post: { id: 4, title: 4 } }, | ||
{ id: 2, name: 'MIMO', age: 7, post: { id: 2, title: 2 } }, | ||
{ id: 3, name: 'TAPI', age: 6, post: { id: 3, title: 3 } }, | ||
]); | ||
}); | ||
|
||
test('data-last', async () => { | ||
const getRows = async () => [ | ||
{ id: 1, name: 'salem', age: 8, post: { id: 1, title: '1' } }, | ||
{ id: 1, name: 'salem', age: 8, post: { id: 4, title: '4' } }, | ||
{ id: 2, name: 'mimo', age: 7, post: { id: 2, title: '2' } }, | ||
{ id: 3, name: 'tapi', age: 6, post: { id: 3, title: '3' } }, | ||
]; | ||
|
||
const transformed = await getRows().then( | ||
transform({ | ||
fields: { | ||
age: (row) => row.age + 1, | ||
}, | ||
}), | ||
); | ||
|
||
expect(transformed).toEqual([ | ||
{ id: 1, name: 'salem', age: 9, post: { id: 1, title: '1' } }, | ||
{ id: 1, name: 'salem', age: 9, post: { id: 4, title: '4' } }, | ||
{ id: 2, name: 'mimo', age: 8, post: { id: 2, title: '2' } }, | ||
{ id: 3, name: 'tapi', age: 7, post: { id: 3, title: '3' } }, | ||
]); | ||
}); | ||
|
||
test('with aggregate', async () => { | ||
const getRows = async () => [ | ||
{ id: 1, name: 'salem', age: 8, post: { id: 1, title: '1' } }, | ||
{ id: 1, name: 'salem', age: 8, post: { id: 4, title: '4' } }, | ||
{ id: 2, name: 'mimo', age: 7, post: { id: 2, title: '2' } }, | ||
{ id: 3, name: 'tapi', age: 6, post: { id: 3, title: '3' } }, | ||
]; | ||
|
||
const transformed = await getRows() | ||
.then( | ||
aggregate({ | ||
pkey: 'id', | ||
fields: { posts: 'post.id' }, | ||
}), | ||
) | ||
.then( | ||
transform({ | ||
fields: { | ||
age: (row) => row.age + 1, | ||
}, | ||
}), | ||
); | ||
|
||
expect(transformed).toEqual([ | ||
{ | ||
id: 1, | ||
name: 'salem', | ||
age: 9, | ||
posts: [ | ||
{ id: 1, title: '1' }, | ||
{ id: 4, title: '4' }, | ||
], | ||
}, | ||
{ id: 2, name: 'mimo', age: 8, posts: [{ id: 2, title: '2' }] }, | ||
{ id: 3, name: 'tapi', age: 7, posts: [{ id: 3, title: '3' }] }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Prettify } from './utils'; | ||
|
||
export function transform< | ||
TRow extends Record<PropertyKey, any>, | ||
TTransform extends Partial<{ [K in keyof TRow]: (row: TRow) => any }>, | ||
>(params: { | ||
rows: TRow[]; | ||
fields: TTransform; | ||
}): Prettify< | ||
Omit<TRow, keyof TTransform> & { | ||
[K in keyof TTransform]: TTransform[K] extends (params: any) => infer Ret ? Ret : never; | ||
} | ||
>[]; | ||
|
||
export function transform< | ||
TRow extends Record<PropertyKey, any>, | ||
TTransform extends Partial<{ [K in keyof TRow]: (row: TRow) => any }>, | ||
>(params: { | ||
fields: TTransform; | ||
}): (rows: TRow[]) => Prettify< | ||
Omit<TRow, keyof TTransform> & { | ||
[K in keyof TTransform]: TTransform[K] extends (params: any) => infer Ret ? Ret : never; | ||
} | ||
>[]; | ||
|
||
export function transform< | ||
TRow extends Record<PropertyKey, any>, | ||
TTransform extends Partial<{ [K in keyof TRow]: (row: TRow) => any }>, | ||
>(params: { | ||
rows?: TRow[]; | ||
fields: TTransform; | ||
}) { | ||
// if rows is missing, build a data-last function | ||
if (params.rows === undefined) return (rows: TRow[]) => transform({ rows, ...params }); | ||
|
||
for (const row of params.rows) { | ||
for (const key in params.fields) { | ||
const transform = params.fields[key]; | ||
row[key] = transform?.(row); | ||
} | ||
} | ||
|
||
return params.rows; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; |