-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
7b81f41
commit 28fb496
Showing
10 changed files
with
284 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
type ObjectIterator<Obj, Result> = | ||
( value: Obj[ keyof Obj ], key: string, collection: Obj ) => Result | ||
|
||
export const mapValues = <Obj extends object, Result> | ||
( fn: ObjectIterator<Obj, Result> ) => ( obj?: Obj ) => { | ||
if ( !obj ) return {} | ||
const map = Object.keys( obj ).reduce( ( map, key ) => { | ||
map.set( key, fn( obj[ key ], key, obj ) ) | ||
return map | ||
}, new Map ) | ||
return Object.fromEntries( map ) | ||
} |
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,8 @@ | ||
export const omit = <Obj extends Record<string, unknown>> | ||
( obj: Obj, keys: ( keyof Obj | string )[] ) => { | ||
if ( keys.length === 0 ) return obj | ||
const entries = Object.entries( obj ) as [ keyof Obj, unknown ][] | ||
return Object.fromEntries( | ||
entries.filter( ( [ key ] ) => !keys.includes( key ) ) | ||
) | ||
} |
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,23 @@ | ||
import { omit } from './omit.ts' | ||
import { assertEquals } from 'std/testing/asserts.ts' | ||
|
||
const obj = { | ||
foo: 'foo', | ||
bar: 42, | ||
baz: true, | ||
} | ||
|
||
Deno.test( 'omit', () => { | ||
assertEquals( | ||
omit( obj, [] ), | ||
obj | ||
) | ||
assertEquals( | ||
omit( obj, [ 'foo' ] ), | ||
{ bar: 42, baz: true } | ||
) | ||
assertEquals( | ||
omit( obj, [ 'foo', 'bar' ] ), | ||
{ baz: true } | ||
) | ||
} ) |
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,8 @@ | ||
export const pick = <Obj extends Record<string, unknown>> | ||
( obj: Obj, keys: ( keyof Obj )[] ) => { | ||
if ( keys.length === 0 ) return {} | ||
const entries = Object.entries( obj ) as [ keyof Obj, unknown ][] | ||
return Object.fromEntries( | ||
entries.filter( ( [ key ] ) => keys.includes( key ) ) | ||
) | ||
} |
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,23 @@ | ||
import { pick } from './pick.ts' | ||
import { assertEquals } from 'std/testing/asserts.ts' | ||
|
||
const obj = { | ||
foo: 'foo', | ||
bar: 42, | ||
baz: true, | ||
} | ||
|
||
Deno.test( 'pick', () => { | ||
assertEquals( | ||
pick( obj, [] ), | ||
{} | ||
) | ||
assertEquals( | ||
pick( obj, [ 'foo' ] ), | ||
{ foo: 'foo' } | ||
) | ||
assertEquals( | ||
pick( obj, [ 'foo', 'bar' ] ), | ||
{ foo: 'foo', bar: 42 } | ||
) | ||
} ) |
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