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

toolify

v1.14.0

Published

Various quality of life tools

Downloads

12

Readme

Toolify

Various quality of life functions to reduce number of lines in common operations.

Table of Contents

Functions

objectify

From an array of objects, return an object where the key is the specified property value.

Important note: This key must exist and have a unique value in every object within the input array.

objectify example

const { objectify } = require('toolify');
let input = [
  {
    id: 1,
    value1: 'something',
    value2: 'something else'
  },
  {
    id: '2',
    value1: 'a value',
    value2: 'another value'
  },
  {
    id: 'textIdentifier',
    andNow: 'for something',
    completely: 'different'
  }
]

let kvs = objectify(input, 'id');

// Result:
// >> {
// >>   '1': {
// >>     id: 1,
// >>     value1: 'something',
// >>     value2: 'something else'
// >>   },
// >>   '2': {
// >>     id: '2',
// >>     value1: 'a value',
// >>     value2: 'another value'
// >>   },
// >>   'textIdentifier': {
// >>     id: 'textIdentifier',
// >>     andNow: 'for something',
// >>     completely: 'different'
// >>   }
// >> }

arrayify

If input is not an array, return an array with one element as the input. Otherwise, return the input.

arrayify example

const { arrayify } = require('toolify');

let input = 'value';

input = arrayify(input);

console.log(input);
// logs ['value'] to console

input = ['value'];

console.log(input);
// logs ['value'] to console

arrayify example with Iteration

const { arrayify } = require('toolify');

let input = 'value';

for (let value of arrayify(input)) {
  console.log(value);
}
// Logs 'value' to console, instead of logging each character separately as it would without arrayify

isObjectEmpty

Check if input is an empty object.

isObjectEmpty example

const { isObjectEmpty } = require('toolify');

let input = {};

let inputNotEmpty = { key: 'value' };

isObjectEmpty(input);
// >> true

isObjectEmpty(inputNotEmpty);
// >> false

nullify

Simply return null if input is undefined.

nullify example

const { nullify } = require('toolify');

let input = undefined;

console.log(nullify(input));
// logs null to console

console.log(nullify('value'));
// logs 'value' to console.

denilify

For use with XML to JSON parsers. Return null for xsi:nil elements in XML after conversion to JSON; otherwise, return the input.

Accepts two parameters; the input value/object, and a string that defines the name of the attribute node from your xml parser.

The Second Parameter, "attributeNode", defaults to 'attr', which is the default attribute node property name in the fast-xml-parser npm module.

denilify example with default node name

const { denilify } = require('toolify');

let parsedXML = {
  attr: {
    'xsi:nil': 'true'
  }
}

console.log(denilify(parsedXMl));
// logs null to console

parsedXML = {
  elementName: 'value'
}

console.log(denilify(parsedXML));
// logs 'value' to console

denilify example with specified node name

const { denilify } = require('toolify');

let parsedXML = {
  '$': {
    'xsi:nil': 'true'
  }
}

console.log(denilify(parsedXML, '$'));
// logs null to console

areObjectsEqual

Test two objects, passed as arguments, for equality. Optionally pass a third argument, an options object literal. Available options seen below:

Options

  • truthy (boolean) default false
    • set as true to use truthy comparisons (i.e. "==" vs. "===")
  • unidirectional (boolean) default false
    • set as true to only run the comparison "one way". See example below labeled "unidirectional comparison" for context.

areObjectsEqual example, full comparison

const { areObjectsEqual } = require('toolify');

let obj1 = {
  prop1: '1',
  prop2: 2
}

let obj2 = {
  prop1: '1',
  prop2: 2
}

let obj3 = {
  name: 'Jefferson',
  age: 28
}

console.log(areObjectsEqual(obj1, obj2);
// Returns true

console.log(areObjectsEqual(obj1, obj3));
// Returns false

areObjectsEqual example, "truthy" comparison

const { areObjectsEqual } = require('toolify');
let obj1 = {
  prop1: 1,
  prop2: 2
}

let obj2 = {
  prop1: '1',
  prop2: '2'
}

let options = {
  truthy: true
}

console.log(areObjectsEqual(obj1, obj2, options));
// returns true

areObjectsEqual example, "unidirectional" comparison

const {areObjectsEqual} = require('toolify');
let obj1 = {
  prop1: 1,
  prop2: 2
}

let obj2 = {
  prop1: 1,
  prop2: 2,
  extraProp: 'yeah.'
}

let options = {
  unidirectional: true
}

console.log(areObjectsEqual(obj1, obj2, options));
// >> true

console.log(areObjectsEqual(obj2, obj1, options));
// >> false

pushIfNotExist

Only push an item if its not already in the array (idempotent push)

Usage:

pushIfNotExist(dataToPush, targetArray);

pushIfNotExist example

const { pushIfNotExist } = require('toolify');

let array = [1, 13, 7, 3];

pushIfNotExist(4, array); // Push a non-existent value
console.log(array);
// >> [1, 13, 7, 3, 4]

pushIfNotExist(13, array); // Push an existing value
console.log(array);
// >> [1, 13, 7, 3, 4]

asynctimeout

An asynchronous version of javascript's native setTimeout.

Usage:

  await asynctimeout(delay);

removeNull

Remove all properties with null values of an input object.

removeNull example

let { removeNull } = require('toolify');

let input = {
  key1: 'value',
  key2: null
}

t.removeNull(input);

console.log(input);
// >> { key1: 'value' }