0.2.3 • Published 1 year ago

ts-object-fns v0.2.3

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

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

0.2.3

1 year ago

0.2.1

1 year ago

0.2.0

2 years ago

0.2.2

1 year ago

0.1.0

2 years ago