Type safe object & array utility functions.
npm install ts-util-fns
These functions come bundled:
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"
},
});
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 }
Remove property from target object.
import { omitKey } from "ts-object-fns";
const myObj = {
hello: "world",
marco: "polo",
};
omitKey(myObj, "marco"); // { hello: "world" }
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
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