
Common Utils
import * as commonUtils from '@green-stack/utils/commonUtils'
Collection of common utility functions for general use:
- commonUtils.ts
Validation Utils
isEmpty()
Checks for null, undefined, empty strings, empty objects or empty arrays.
// Empty values
isEmpty(null) // => true
isEmpty(undefined) // => true
isEmpty('') // => true
isEmpty([]) // => true
isEmpty({}) // => true
// Non-empty values
isEmpty('hello') // => false
isEmpty([1, 2, 3]) // => false
isEmpty({ key: 'value' }) // => false
// Custom behavior for empty strings
isEmpty('', false) // => false (when failOnEmptyStrings is false)
isKvRecord()
Checks whether an object is a simple key-value record (all values are primitives).
// Valid key-value records
isKvRecord({ name: 'John', age: 30 }) // => true
isKvRecord({ active: true, count: 0 }) // => true
isKvRecord({ title: undefined, id: null }) // => true
// Invalid key-value records
isKvRecord({ data: [] }) // => false (array value)
isKvRecord({ handler: () => {} }) // => false (function value)
isKvRecord({ user: { name: 'John' } }) // => false (nested object)
isKvRecord([]) // => false (array)
isKvRecord(null) // => false (null)
Console Utils
These utilities help with logging messages only once, which is useful for one-off messages, warnings, or errors.
consoleOnce()
Base function that logs to the console only once, skip on subsequent logs.
consoleOnce('This will only show once', console.log, 'Additional data')
consoleOnce('This will only show once', console.log, 'Additional data') // Won't show
logOnce()
Logs a message to the console only once.
logOnce('This will only show once')
logOnce('This will only show once') // Won't show
warnOnce()
Shows a warning message only once.
warnOnce('This warning will only show once')
warnOnce('This warning will only show once') // Won't show
errorOnce()
Shows an error message only once.
errorOnce('This error will only show once')
errorOnce('This error will only show once') // Won't show
All console utilities support additional arguments that will be passed to the console method:
logOnce('User logged in:', { userId: 123, timestamp: new Date() })
warnOnce('Deprecated feature used:', { feature: 'oldMethod', version: '1.0.0' })
errorOnce('API Error:', { status: 500, message: 'Internal Server Error' })