Skip to content

twrayden/ts-util-fns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-util-fns

Type safe object & array utility functions.

Install

npm install ts-util-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