Objects
Installation
yarn add --exact @pro-functional/objects
Tools
isObject A type checker to evaluate the given value as an
NonNullable<object>
isObject(null)
isObject(undefined)
isObject(5)
isObject('random value')
isObject({})
isObjectWithProps A type checker that verifies the provided value to be an
object with the provided property keys
isObjectWithProps(null, 'name', 'id')
isObjectWithProps(undefined, 'name', 'id')
isObjectWithProps(5, 'name', 'id')
isObjectWithProps('random value', 'name', 'id')
isObjectWithProps({}, 'name', 'id')
isObjectWithProps({ name: 'bob' }, 'name', 'id')
isObjectWithProps({ name: 'bob', id: '007', uid: 'ga98as7fuhk' }, 'name', 'id')
interface User {
name: string
id: string
}
const maybeUser = { name: 'bob', id: '007', uid: 'ga98as7fuhk' }
const user: User | null = isObjectWithProps(maybeUser, 'name', 'id')
? maybeUser
: null
makeEnumResolver Create a resolver for an Enum allowing you to map a text
back to the enum. The fallback allows a guaranteed response.
enum Colors {
Green = 'green',
Blue = 'blue',
Invalid = 'invalid',
}
const resolveColor = makeEnumResolver(Colors, Colors.Invalid)
resolveColor('green')
resolveColor('blue')
resolveColor('purple')