Skip to content

Commit

Permalink
feat(utilities): add extractProperty and pickProperties utilities
Browse files Browse the repository at this point in the history
These utility functions make it easy to handle object values when all you want are some of their
values
  • Loading branch information
dancrumb committed May 25, 2024
1 parent f9d0823 commit da42d4e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/utilities.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, describe, test } from 'vitest';
import { extractProperty, pickProperties } from './utilities.js';

// We may need to play fast and loose with typing here, just to test edge cases
describe('extractProperty', () => {
test('works on an empty object', () => {
expect(extractProperty('')({} as any)).toBeUndefined()
})
test('returns undefined for a non-existent key', () => {
expect(extractProperty('a')({} as any)).toBeUndefined()
})
test('returns undefined for a non-existent key', () => {
expect(extractProperty('a')({} as any)).toBeUndefined()
})
test('returns a single value', () => {
expect(extractProperty('a')({ a: 42 })).toBe(42)
})
})

describe('pickProperties', () => {
test('works on an empty object', () => {
expect(pickProperties([''])({} as any)).toStrictEqual({})
})
test('returns undefined for a non-existent key', () => {
expect(pickProperties(['a'])({} as any)).toStrictEqual({})
})
test('returns undefined for a non-existent key', () => {
expect(pickProperties(['a'])({} as any)).toStrictEqual({})
})
test('returns an object with a single property', () => {
expect(pickProperties(['a'])({ a: 42 })).toStrictEqual({ a: 42 })
})
test('does not include non-existent properties', () => {
expect(pickProperties(['a', 'b'])({ a: 42 } as any)).toStrictEqual({ a: 42 })
})
test('does include explicitly undefined properties', () => {
expect(pickProperties(['a', 'b'])({ a: 42, b: undefined } as any)).toStrictEqual({ a: 42, b: undefined })
})
test('handles duplicated properties', () => {
expect(pickProperties(['a', 'a'])({ a: 42, b: undefined } as any)).toStrictEqual({ a: 42, })
})
})
17 changes: 15 additions & 2 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export const drop = (t: unknown): void => void undefined
export const asUndefined = (t: unknown) => undefined


export const logError = (e: Error) => {
export const logError = (e: Error) =>
console.error(e)
}

/**
* Sometimes, all you're looking to do is extract a single property from and object.
* That's what this function does
*/
export const extractProperty = <O extends object>(key: keyof O) => (o: O) => o[key]

/**
* Sometimes, you want a subset of an object, with just a few properties
* That's what this function does
*/
export const pickProperties = <O extends object>(keys: (keyof O)[]) => (o: O) => {
return keys.reduce((acc, key) => key in o ? ({ ...acc, [key]: o[key] }) : acc, {} satisfies Partial<O>);
}
17 changes: 12 additions & 5 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {

coverage: {
include: ['src/**'],
enabled: true,
reporter: ['html', 'lcov', 'cobertura'],
include: ['src/**'],
exclude: ['coverage', 'dist'],
reporter: ['html', 'lcov', 'cobertura', 'json'],
thresholds: {
autoUpdate: true,
lines: 93,
statements: 93,
branches: 98.12,
functions: 85.56
}
},
},
})

})

0 comments on commit da42d4e

Please sign in to comment.