1.14.0 • Published 4 years ago

toolify v1.14.0

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

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' }
1.14.0

4 years ago

1.13.0

4 years ago

1.11.1

4 years ago

1.10.4

4 years ago

1.12.0

5 years ago

1.10.3

5 years ago

1.10.2

5 years ago

1.10.1

5 years ago

1.9.2

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.1

5 years ago