ts-object-fns
v0.2.3
Published
Type safe object utility functions.
Downloads
28
Readme
ts-object-fns
Type safe object utility functions.
Install
npm install ts-object-fns
Usage
These functions come bundled:
visit
Run a function for each key of target object.
import { visit } from "ts-object-fns";
const myObj = {
hello: "world",
marco: "polo"
};
visit(myObj, {
hello(value) {
console.log(value); // "world"
},
marco(value) {
console.log(value); // "polo"
}
});
trim
Remove empty keys from target object.
import { trim } from "ts-object-fns";
interface MyObj {
hello: string;
marco: string;
messy?: string;
data: Buffer | null;
}
const myObj: MyObj = {
hello: "world",
marco: "polo",
messy: undefined,
data: null
};
trim(myObj); // { hello: "world", marco: "polo", data: null }
omitKey
Remove property from target object.
import { omitKey } from "ts-object-fns";
const myObj = {
hello: "world",
marco: "polo"
};
omitKey(myObj, "marco"); // { hello: "world" }
isEmpty
Return true if the target object is empty.
import { isEmpty } from "ts-object-fns";
const myObj = {
hello: "world",
marco: "polo"
};
const emptyObj = {};
isEmpty(myObj); // false
isEmpty(emptyObj); // true
hasKeys
Return true is the target object contains at least one specified key.
import { hasKeys } from "ts-object-fns";
const myObj = {
hello: "world",
marco: "polo"
};
hasKeys(myObj, ["hello"]); // true
hasKeys(myObj, ["hello", "unknown"]); // true
hasKeys(myObj, ["polo"]); // false
Related
- ts-array-fns - Typesafe array utility functions