Skip to content
Closed
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/logger",
"version": "3.0.0",
"version": "3.0.1-pr62.0",
"description": "logger for local and aws lambda execution",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "UNLICENSED",
Expand Down Expand Up @@ -35,7 +35,7 @@
"test:watch": "npm run test -- --watch"
},
"devDependencies": {
"@shiftcode/utilities": "^4.0.0"
"@shiftcode/utilities": "^4.1.0-pr62.0"
},
"peerDependencies": {
"@shiftcode/utilities": "^4.0.0 || ^4.0.0-pr45"
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/utilities",
"version": "4.0.0",
"version": "4.1.0-pr62.0",
"description": "Contains some utilities",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ export * from './lib/compare-with/compare-with.js'
export * from './lib/cookie/parse-cookies.function.js'
export * from './lib/enum/enum.js'
export * from './lib/group-by/group-by.js'
export * from './lib/guards/is-defined.function.js'
export * from './lib/http/http-constants.js'
export * from './lib/json-stringify-replacer/json-stringify-replacer.function.js'
export * from './lib/map-values-deep/map-values-deep.js'
export * from './lib/object/omit-props.function.js'
export * from './lib/object/pick-props.function.js'
export * from './lib/object/pick-props-assert-defined.function.js'
export * from './lib/object/get-value-assert-defined.function.js'
export * from './lib/promise/make-deferred.function.js'
export * from './lib/math/clamp.function.js'
export * from './lib/math/random-int.js'
Expand Down
25 changes: 25 additions & 0 deletions packages/utilities/src/lib/guards/is-defined.function.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isDefined } from './is-defined.function.js'

describe('isDefined()', () => {
test('returns false for undefined|null', () => {
expect(isDefined(null)).toBe(false)
expect(isDefined(undefined)).toBe(false)
})
test(`when 0|''|NaN|emptyString`, () => {
expect(isDefined(0)).toBe(true)
expect(isDefined(-0)).toBe(true)
expect(isDefined('')).toBe(true)
expect(isDefined(NaN)).toBe(true)
expect(isDefined(1 / 0)).toBe(true)
})
test('when empty objects|arrays', () => {
expect(isDefined([])).toBe(true)
expect(isDefined({})).toBe(true)
})
test('when truthy values', () => {
expect(isDefined(5)).toBe(true)
expect(isDefined('ok')).toBe(true)
expect(isDefined('undefined')).toBe(true)
expect(isDefined('null')).toBe(true)
})
})
6 changes: 6 additions & 0 deletions packages/utilities/src/lib/guards/is-defined.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Checks if a value is defined (not undefined or null).
*/
export function isDefined<T>(value: T | undefined | null): value is T {
return value !== undefined && value !== null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { getValueAssertDefined } from './get-value-assert-defined.function.js'

describe('getValueAssertDefined', () => {
interface Test {
x: string | null
y?: boolean
z: number | undefined
}

describe('throws when not defined', () => {
const empty: Test = { x: null, z: undefined }

test('when null', () => {
expect(() => getValueAssertDefined(empty, 'x')).toThrow()
})

test('when undefined', () => {
expect(() => getValueAssertDefined(empty, 'z')).toThrow()
})

test('when optional', () => {
expect(() => getValueAssertDefined(empty, 'y')).toThrow()
})
})

describe('returns value when defined', () => {
const obj: Test = {
x: '',
y: false,
z: 0,
}

test('when empty string', () => {
expect(getValueAssertDefined(obj, 'x')).toEqual('')
})
test('when false', () => {
expect(getValueAssertDefined(obj, 'y')).toEqual(false)
})
test('when zero', () => {
expect(getValueAssertDefined(obj, 'z')).toEqual(0)
})

test('makes it type safe', () => {
const tDefined: { num: number | null } = { num: 42 }
// assign to variable to ensure type safety
const res: number = getValueAssertDefined(tDefined, 'num')
expect(res).toEqual(42)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isDefined } from '../guards/is-defined.function.js'

/**
* returns the value of the provided key on given object. throws if the value is null or undefined
*/
export function getValueAssertDefined<T, K extends keyof T>(obj: T, key: K): NonNullable<T[K]> {
type X = NonNullable<T[K]>
const value: X | null | undefined = <any>obj[key]

if (!isDefined(value)) {
throw new Error(`Expected property "${String(key)}" to be defined. Was "${value}" instead`)
}
return value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { pickPropsAssertDefined } from './pick-props-assert-defined.function.js'

describe('pickPropsAssertDefined', () => {
test('returns object with picked props', () => {
const obj = { a: true, b: 'foo', c: 42 }
expect(pickPropsAssertDefined(obj, ['a', 'c'])).toEqual({ a: true, c: 42 })
})
test('throws when picked prop values are null or undefined', () => {
const obj = { a: 'ok', b: null }
expect(() => pickPropsAssertDefined(obj, ['a', 'b'])).toThrow(Error)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isDefined } from '../guards/is-defined.function.js'

export type PickedPropsDefined<T, K extends keyof T> = {
[key in K]-?: NonNullable<T[key]>
}

/**
* returns an object containing the provided props with their respective value. throws if their value is null or undefined
*/
export function pickPropsAssertDefined<T, TProp extends keyof T>(obj: T, props: readonly TProp[]): PickedPropsDefined<T, TProp> {
const entries = props.map((p) => {
if (!isDefined(obj[p])) {
throw new Error(`Expected property "${String(p)}" to be defined. Was "${String(obj[p])}" instead`)
}
return [p, obj[p]]
})
return Object.fromEntries(entries)
}