npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utilitify

v1.0.5

Published

The utilities for working with a collections such as objects, arrays and primitives such as numbers, strings, etc.

Downloads

136

Readme

utilitify

The utilities for working with a collections such as objects, arrays and primitives such as numbers, strings, etc.

Build Status

Install

Install with yarn:

$ yarn add utilitify

Install with npm:

$ npm install --save utilitify

Usage

These utilities include a different methods for working with JavaScript collections and primitives.

For example, if you want to merge two objects recursively, you can use a method 'mergeDeep':

import { mergeDeep } from 'utilitify';

mergeDeep({ a: { b: { c: 'c', d: 'd' } } }, { a: { b: { e: 'e', f: 'f' } } });
//=> { a: { b: { c: 'c', d: 'd', e: 'e', f: 'f' } } }

You can find all methods in API section.

About

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ yarn && yarn test

API

mergeDeep

Recursively merge values in a javascript object.

import { mergeDeep } from 'utilitify';

mergeDeep({ a: 1, b: [2, 3] }, { b: [3, 4], c: 3 });
//=> { a: 1, b: [2, 3, 4], c: 3 }

cloneDeep

Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.

import { cloneDeep } from 'utilitify';

const obj = { a: 'b' };
const arr = [obj];
const copy = cloneDeep(arr);
obj.c = 'd';
 
console.log(copy);
//=> [{ a: 'b' }]
 
console.log(arr);
//=> [{ a: 'b', c: 'd' }]

cloneShallow

Creates a shallow clone of any JavaScript value.

import { cloneShallow } from 'utilitify';

const arr = [{ a: 0 }, { b: 1 }];
const foo = cloneShallow(arr);
// foo =>  [{ 'a': 0 }, { 'b': 1 }]
 
// array is cloned
assert(actual === expected); // false
 
// array elements are not
assert.deepEqual(actual[0], expected[0]); // true

union

Combines a list of arrays, returning a single array with unique values, using strict equality for comparisons.

import { union } from 'utilitify';

union(['a'], ['b', 'c'], ['d', 'e', 'f']);
//=> ['a', 'b', 'c', 'd', 'e', 'f']

union(['a', 'a'], ['b', 'c']);
//=> ['a', 'b', 'c']

isObject

Check if any JavaScript value is object.

import { isObject } from 'utilitify';

const obj = { a: 1, b: 2 };

isObject(obj);
//=> true

isObject(1);
//=> false

isNil

Check if any JavaScript value is null or undefined.

import { isNil } from 'utilitify';

const obj = { a: 1, b: 2 };

isNil(obj);
//=> false

isNil(null);
//=> true

isNull

Check if any JavaScript value is null.

import { isNull } from 'utilitify';

isNull(undefined);
//=> false

isNull(null);
//=> true

isUndefined

Check if any JavaScript value is undefined.

import { isUndefined } from 'utilitify';

isUndefined(null);
//=> false

isUndefined(undefined);
//=> true

getObjectWithoutEmptyPropsFrom

Remove from object all values which equal 'null', 'undefined' or empty string.

import { getObjectWithoutEmptyPropsFrom } from 'utilitify';

const objectWithEmptyProps = {
  a: 1,
  b: null,
  c: 'string',
  d: undefined,
  e: '',
};

getObjectWithoutEmptyPropsFrom(objectWithEmptyProps);
//=> { a: 1, c: 'string' }

getObjectWithoutUndefinedPropsFrom

Remove from object all values which equal 'undefined'.

import { getObjectWithoutUndefinedPropsFrom } from 'utilitify';

const objectWithUndefinedProps = {
  a: 1,
  b: null,
  c: 'string',
  d: undefined,
  e: '',
};

getObjectWithoutUndefinedPropsFrom(objectWithUndefinedProps);
//=> { a: 1, b: null, c: 'string', e: '' }

getTruncatedString

Get truncated string by number of symbols (second argument) with punctuation mark on the end if need.

import { getTruncatedString } from 'utilitify';

getTruncatedString('string', 3);
//=> str

getTruncatedString('string', 3, '...');
//=> str...

upsertObjectToArray

Update object in array. If object does not exist, method push new value (third argument) to array.

import { upsertObjectToArray } from 'utilitify';

const arr = [{ a: 1 }, { b: 2 }];

upsertObjectToArray(arr, { a: 1 }, { a: 3 });
console.log(arr);
//=> [{ a: 3 }, { b: 2 }]

getObjectFromArrayByProp

Get object from array by it's property.

import { getObjectFromArrayByProp } from 'utilitify';

const arr = [{ a: 1 }, { b: 2 }];

getObjectFromArrayByProp(arr, 'a');
//=> { a: 1 }

getArrayOfObjectsWithoutProp

Get new array of objects from which are deleted property (second argument).

import { getArrayOfObjectsWithoutProp } from 'utilitify';

const arr = [{ a: 1, c: 10 }, { b: 2 }];

getArrayOfObjectsWithoutProp(arr, 'a');
//=> [{ c: 10 }, { b: 2 }]

isJsonString

Check if string is JSON.

import { isJsonString } from 'utilitify';

const str = '{"a":2}';

isJsonString(str);
//=> true

getJsonFromString

Get JSON from string if possibly. If string is not valid JSON string, method returns empty object.

import { getJsonFromString } from 'utilitify';

const validStr = '{"a":2}';
const invalidStr = '{a:2}';

getJsonFromString(validStr);
//=> { a: 2 }

getJsonFromString(invalidStr);
//=> {}

toPascalCase

Convert any string to 'PascalCase' string.

import { toPascalCase } from 'utilitify';

toPascalCase('pascal case');
//=> PascalCase

compose

Compose several functions which return some result.

import { compose } from 'utilitify';

const inc = (value) => value + 1;
const mul2 = (value) => value * 2;

compose(inc, mul2)(1);
//=> 4

setIn

Set new value (second argument) in object by property (third argument) and return new object.

import { setIn } from 'utilitify';

const obj = {
  a: 1,
  b: 2,
  c: {
    d: 3,
  },
};

const result = setIn(obj, 5, 'd.f.r');
console.log(result);
//=> { a: 1, b: 2, c: { d: 3 }, d: { f: { r: 5 } } }

delIn

Delete property (second argument) in object and return new object.

import { delIn } from 'utilitify';

const obj = {
  a: 1,
  b: 2,
  c: {
    d: 3,
  },
};

const result = delIn(obj, 'c.d');
console.log(result);
//=> { a: 1, b: 2, c: {} }

difference

Compare two objects (or arrays) and return a new object (or array) who represent the diff.

import { difference } from 'utilitify';

const obj = {
  a: 1,
  b: 2,
  c: {
    d: 3,
  },
};

const obj2 = {
  c: {
    d: {
      f: 5,
    },
  },
  g: 5,
};

const testObj = { ...obj, ...obj2 };

const result = difference(obj, testObj);
console.log(result);
//=> { c: { d: 3 } }

isDifference

Compare two objects (or arrays) and return 'true' if equal or 'false' if not.

import { isDifference } from 'utilitify';

const arr1 = [1, 2, 3];

const arr2 = [2, 3, 4];

isDifference(arr1, arr2);
//=> true