Skip to content

Commit

Permalink
Merge branch 'release/v0.15.26'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Dec 13, 2023
2 parents ef1e8c2 + e654e7e commit f57fd8e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.15.25",
"version": "0.15.26",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -69,18 +69,18 @@
"watch": "nr build:tsup -- --watch"
},
"devDependencies": {
"@antfu/eslint-config": "^2.4.2",
"@antfu/eslint-config": "^2.4.5",
"@antfu/ni": "^0.21.12",
"@types/node": "^20.10.4",
"c8": "^8.0.1",
"cross-fetch": "^4.0.0",
"esbuild": "^0.19.8",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
"madge": "^6.1.0",
"tsup": "^8.0.1",
"typedoc": "^0.25.4",
"typescript": "^5.3.3",
"vite": "^5.0.6",
"vitest": "^1.0.2"
"vite": "^5.0.8",
"vitest": "^1.0.4"
}
}
25 changes: 24 additions & 1 deletion src/common/data/object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useDispose } from '../dispose-defer'
import { Emitter } from '../msg/emitter'
import { objectMap, objectMergeDisposable } from './object'
import { objectInclusivePick, objectMap, objectMergeDisposable, objectOmit, objectPick } from './object'

describe('object.spec', () => {
it('should map it', async () => {
Expand Down Expand Up @@ -60,4 +60,27 @@ describe('object.spec', () => {
// await m.dispose()
// expect(x).toBe(3)
// })

it('should omit and pick', () => {
const o = { a: 1, b: 2, c: 3, d: 4 }
expect(objectPick(o, 'a', 'c')).toMatchInlineSnapshot(`
Object {
"a": 1,
"c": 3,
}
`)
expect(objectOmit(o, 'a', 'c')).toMatchInlineSnapshot(`
Object {
"b": 2,
"d": 4,
}
`)
expect(objectInclusivePick(o, 'a', 'c', 'x')).toMatchInlineSnapshot(`
Object {
"a": 1,
"c": 3,
"x": undefined,
}
`)
})
})
13 changes: 13 additions & 0 deletions src/common/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ export function objectMergeDisposable<A extends object, B extends object>(
export function objectIsEmpty(obj: object) {
return Object.keys(obj).length <= 0
}

// https://stackoverflow.com/a/56592365/140927
export function objectPick<T extends object, K extends keyof T>(obj: T, ...keys: K[]) {
return Object.fromEntries(keys.filter(key => key in obj).map(key => [key, obj[key]])) as Pick<T, K>
}

export function objectInclusivePick<T extends object, K extends (string | number | symbol)>(obj: T, ...keys: K[]) {
return Object.fromEntries(keys.map(key => [key, obj[key as unknown as keyof T]])) as { [key in K]: key extends keyof T ? T[key] : undefined }
}

export function objectOmit<T extends object, K extends keyof T>(obj: T, ...keys: K[]) {
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key as K))) as Omit<T, K>
}

0 comments on commit f57fd8e

Please sign in to comment.