Package Exports
- is-empty-typed
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (is-empty-typed) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
is-empty-typed
Simple JS library that checks if value is an empty object, collection, string. It also narrows types with typescript
Installation
npm i is-empty-typed --save
Examples
Objects are considered empty if they have no own enumerable string keyed properties.
import isEmpty from 'is-empty-typed';
isEmpty({}) // true
isEmpty({name: 'John'}) // falseArray-like values such as arrays are considered empty if their length equals 0
import isEmpty from 'is-empty-typed';
isEmpty([]) // true
isEmpty([1,2,3]) // falseStrings are considered empty if their length equals 0 or if they consist of just whitespaces
import isEmpty from 'is-empty-typed';
isEmpty('John') // false
isEmpty(' ') // true
isEmpty('') // true0, NaN, undefined, null are also considered empty.
import isEmpty from 'is-empty-typed';
isEmpty(undefined) // true
isEmpty(null) // true
isEmpty(NaN) // trueIt also supports Type Guards that allow you to narrow down the type of an object within a conditional block.
import isEmpty from 'is-empty-typed';
function checkArray(items: any[] | null | undefined) {
if (!isEmpty(items)) {
items.map((item) => item); // Narrowed to 'any[]'. No compiler issues
}
}
function checkObject(obj: { name } | null | undefined) {
if (!isEmpty(obj)) {
console.log(obj.name) // Narrowed to '{ name }'. No compiler issues
}
}
function checkString(str: string | null | undefined) {
if (!isEmpty(str)) {
console.log(str.length); // Narrowed to 'string'. No compiler issues
}
}
function checkNumber(value: number | null | undefined) {
if (!isEmpty(value)) {
console.log(value++); // Narrowed to 'number'. No compiler issues
}
}