@ouroboros/tools
TypeScript icon, indicating that this package has built-in type declarations

0.5.4 • Public • Published

@ouroboros/tools

npm version MIT License

A library with several generic functions for standard javascript problems

Installation

npm

npm install @ouroboros/tools

Functions

afindi

Use to find the index of an object in an array based on a key and value.

import { afindi } from  '@ouroboros/tools';

let  i;
const  a = [
    {iso: 'CA', name: 'Canada'},
    {iso: 'MX', name: 'Mexico'},
    {iso: 'US', name: 'United States'}
];

// i = 1
i = afindi(a, 'iso', 'MX');

// i = 0
i = afindi(a, 'name', 'Canada');

// i = -1
i = afindi(a, 'iso', 'GB');

afindo

Used to find an object in an array of objects based on key and value.

import { afindo } from '@ouroboros/tools';

let  o;
const  a = [
    {iso: 'CA', name: 'Canada'},
    {iso: 'MX', name: 'Mexico'},
    {iso: 'US', name: 'United States'}
];

// o = {iso: 'MX', name: 'Mexico'}
o = afindo(a, 'iso', 'MX');

// o = {iso: 'CA', name: 'Canada'}
o = afindo(a, 'name', 'Canada');

// o = null
o = afindo(a, 'iso', 'GB');

arrayFindDelete

Used to delete an object in an array of objects based on key and value.

Passing the optional returnClone argument as true will return a new copy of the array instead of updating it in place.

import { arrayFindDelete } from '@ouroboros/tools';

const a = [
    {iso: 'CA', name: 'Canada'},
    {iso: 'MX', name: 'Mexico'},
    {iso: 'US', name: 'United States'}
];

// returns true
// a = [
//	{iso: 'CA', name: 'Canada'},
//	{iso: 'US', name: 'United States'}
// ]
arrayFindDelete(a, 'iso', 'MX');

// returns false
arrayFindDelete(a, 'iso', 'GB');

// returns new array [
//	{iso: 'CA', name: 'Canada'},
// ]
arrayFindDelete(a, 'iso', 'US', true);

arrayFindMerge

Used to find an object in an array of objects based on key and value, then merges the new passed data with the existing object found.

Passing the optional returnClone argument as true will return a new copy of the array instead of updating it in place.

import { arrayFindMerge } from '@ouroboros/tools';

const a = [
    {iso: 'CA', name: 'Canada'},
    {iso: 'MX', name: 'Mexico'},
    {iso: 'US', name: 'United States'}
];

// returns true
// a = [
//	{iso: 'CA', name: 'Canada'},
//	{iso: 'MX', name: 'Mexico'},
//	{iso: 'US', name: 'United States of America'}
// ]
arrayFindMerge(a, 'iso', 'US', { name: 'United States of America' });

// returns false
arrayFindMerge(a, 'iso', 'GB', { name: 'Great Britain' });

// returns new array [
//	{iso: 'CA', name: 'Kanata'},
//	{iso: 'MX', name: 'Mexico'},
//	{iso: 'US', name: 'United States of America'}
// ]
arrayFindMerge(a, 'iso', 'CA', { name: 'Kanata' }, true);

arrayFindOverwrite

Used to find an object in an array of objects based on key and value, then overwrites it with the new passed data.

Passing the optional returnClone argument as true will return a new copy of the array instead of updating it in place.

import { arrayFindOverwrite } from '@ouroboros/tools';

const a = [
    {iso: 'CA', name: 'Canada'},
    {iso: 'MX', name: 'Mexico'},
    {iso: 'US', name: 'United States'}
];

// returns true
// a = [
//	{iso: 'CA', name: 'Canada'},
//	{iso: 'MX', name: 'Mexico'},
//	{iso: 'GB', name: 'Great Britain'}
// ]
arrayFindOverwrite(a, 'iso', 'US', {iso: 'GB', name: 'Great Britain'});

// returns false
arrayFindOverwrite(a, 'iso', 'US', {iso: 'GB', name: 'Great Britain'});

// returns new array [
//	{name: 'France'},
//	{iso: 'MX', name: 'Mexico'},
//	{iso: 'GB', name: 'Great Britain'}
// ]
arrayFindOverwrite(a, 'iso', 'CA', {name: 'France'}, true);

// returns `a` untouched
arrayFindOverwrite(a, 'iso', 'CA', {name: 'France'}, true);

ashift

Shifts an item in an array from one index to another.

import { ashift } from '@ouroboros/tools';

const a = ['!', 'hello', 'world'];

// a = ['hello', 'world', '!']
ashift(a, 0, 2);

bytesHuman

Converts a number of bytes into a human readable string.

import { bytesHuman } from '@ouroboros/tools';

const bytes = 1073741824; // 1024 * 1024 * 1024

// s = '1.0GiB'
const s = bytesHuman(bytes);

combine

Combines two objects into a new one and returns it. If there are any duplicate keys, those in "a" are overwritten by those in "b".

import { combine } from '@ouroboros/tools';

const a = {hello: 'world', one: 1, three: 4};
const b = {two: 2, three: 3}

// c = { hello: 'world', one: 1, three: 3, two: 2 }
const c = combine(a, b);

compare

Compares two values of any type to see if they contain the same data or not.

import { compare } from '@ouroboros/tools';

// true
compare([1, 2, 3], [1, 2, 3])

// false
compare([1, 2, 3], [1, 3, 2]);

// true
compare(
    {one: 1, two: 2, three: 3},
    {three: 3, two: 2, one: 1}
);

// false
compare(
    {one: [1, 1, 1]},
    {one: [1, 1}
);

divmod

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division.

import { divmod } from '@ouroboros/tools';
let l;

// l = [11, 1]
l = divmod(100, 9);

// l = [24, 3]
l = divmod(123, 5)

empty

Returns true if the value is "empty" for its type. Similar to how Python checks for False in dicts, lists, and strs.

import { empty } from '@ouroboros/tools';

// true
empty('');
empty([]);
empty({});
let a;
empty(a);

// false
empty(' ');
empty(['']);

isDecimal

Returns true if the variable is a number.

import { isDecimal } from '@ouroboros/tools';

// true
isDecimal(0);
isDecimal(1);
isDecimal(1.0);
isDecimal(0.1);
isDecimal(0x00);

// false
isDecimal('');
isDecimal('1');
isDecimal([]);

isInteger

Returns true if the variable is a true integer.

import { isInteger } from '@ouroboros/tools';

// true
isInteger(0);
isInteger(123);
isInteger(1.0);

// false
isInteger('1');
isInteger(1.1);
isInteger([]);

isNumeric

Returns true if a string is made up only of digits.

import { isNumeric } from '@ouroboros/tools';

// true
isNumeric(1);
isNumeric('1');

// false
isNumeric('one');
isNumeric(' 1');
isNumeric('1.');
isNumeric('1.1');

isObject

Returns true if the variable is a true object.

import { isObject } from '@ouroboros/tools';

// true
isObject({});
isObject({one: 1, two: 2, three: 3});

// false
isObject(null);
isObject([]);
isObject('hello');

join

Creates a single string from a list of members that may or may not exist in the passed object.

import { join } from '@ouroboros/tools';

const name1 = {title: 'Mr', first: 'Chris', last: 'Nasr'};
const name2 = {first: 'Frank', middle: 'Gary', last: 'Smith'};

// Mr Chris Nasr
join(name1, ['title', 'first', 'middle', 'last']);

// FrankGarySmith
join(name2, ['title', 'first', 'middle', 'last'], '');

max

Returns the maximum (largest) value in an array.

import { max } from '@ouroboros/tools';

// 3
max([2, 3, 1]);

// Zebra
max(['Lion', 'Alligator', 'Zebra']);

merge

Merges the keys from the second object into the first.

import { merge } from '@ouroboros/tools';

let a = {one: 1, two: 3};
let b = {three: 3};

// a = {one: 1, two: 2, three: 3}
merge(a, b);

min

Returns the minimum (smallest) value in an array

import { min } from '@ouroboros/tools';

// 1
max([2, 3, 1]);

// Alligator
max(['Lion', 'Alligator', 'Zebra']);

nicePhone

Returns a more easily readable phone number in the NA format.

import { nicePhone } from '@ouroboros/tools';

// +1 (555) 123-4444
nicePhone('15551234444');

// +1 (800) 555-1234
nicePhone('8005551234');

normalize

Returns, as well as possible, a normalized string converted from another string containing characters with special accents. It does this by finding special characters and converting them into their simpler, single character, versions. This is useful for things like automaticlaly generating urls, or for generating from unicode into ascii.

import { normalize } from '@ouroboros/tools';

// 'Hello, World!'
normalize('Ȟěƚľỡ, Ẉợɽḷᶁ!');

// 'ffiDAEij'
normalize('ffiDzǼij');

objectArrayToObject

Turns an Array of Objects with set key names into an object built of two spefici keys.

import { objectArrayToObject } from '@ouroboros/tools';

const a = [
    { a: 'first_a', b: 'first_b', c: 'first_c' },
    { a: 'second_a', b: 'second_b', c: 'second_c' },
    { a: 'third_a', b: 'third_b', c: 'third_c' }
];

// {first_a: 'first_b', second_a: 'second_b', third_a: 'third_b'}
objectArrayToObject(a, 'a', 'b');

// {first_a: 'first_c', second_a: 'second_c', third_a: 'third_c'}
objectArrayToObject(a, 'a', 'c');

// {first_c: 'first_b', second_c: 'second_b', third_c: 'third_b'}
objectArrayToObject(a, 'c', 'b');

omap

Works like map for arrays, but iterates over an object returning the value, the key, and the index, in that order.

import { omap } from '@ouroboros/tools';

const o = {one: 'uno', two: 'dos', three: 'tres'};

// ['uno', 'dos', 'tres']
omap(o, (v, k) => v);

// ['one', 'two', 'three']
omap(o, (v, k) => k);

// [0, 1, 2]
omap(o, (v, k, i) => i);

// ['one:uno', 'two:dos', 'three:tres']
omap(o, (v, k) => {
    return k + ':' + v;
});

opop

Removes an element from an object by name, then returns it.

import { opop } from '@ouroboros/tools';

let o = {one: 1, two: 2, three: 3};

// o = {one: 1, three: 3}
// i = 2
let i = opop(o, 'two');

owithout

Takes an object and removes the given key(s) from it and returns a copy of it

import { owithout } from '@ouroboros/tools';

let o1 = {'one': 1, 'two': 2, 'three': 3}

// {'one': 1, 'two': 2}
owithout(o1, 'three');

// {'two': 2}
owithout(o1, ['one', 'three']);

parseQuery

Turns a query string into an object.

import { parseQuery } from '@ouroboros/tools';

// { one: '1', two: '2', three: '3' }
parseQuery('one=1&two=2&three=3');

// { numbers: ['one', 'two', 'three'] }
parseQuery('numbers[0]=one&numbers[1]=two&numbers[2]=three');

// { n: { one: '1', two: '2' } }
parseQuery('n[one]=1&n[two]=2');

pathToTree

Turns a array containing arrays of paths and values, and converts them to a tree

import { pathToTree } from '@ouroboros/tools';

// { address: { line_one: 'missing',
//              postal_code: 'invalid' },
//   title: 'missing' }
pathToTree([['address.line_one', 'missing'],
            ['address.postal_code', 'invalid'],
            ['title', 'missing']
]);

random

Generates random strings

import { random } from '@ouroboros/tools';

// 'YBHhTfsy'
random(8)

// Generate a 4 character string from 'abcd', allow duplicates
// 'ccdb'
random(4, 'abcd')

// Generate a 4 character string from 'abcd', don't allow duplicates
// 'cdab'
random(4, 'abcd', false);

// Throws Error, not enough available characters
random(10, '01234567', false);

random also has predefined sets of characters which can be used

0x    0123456789abcdef
0     01234567
10    0123456789
az    abcdefghijklmnopqrstuvwxyz
az*   abcdefghijkmnopqrstuvwxyz
AZ    ABCDEFGHIJKLMNOPQRSTUVWXYZ
AZ*   ABCDEFGHJKLMNPQRSTUVWXYZ
aZ    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
aZ*   abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ
!     !@#$%^&*-_+.?
!*    !@$%^*-_.
// Use hexedecimal characters
// '961f37208cae'
random(12, ['0x'])

// Use all letters, lower and upper case, and decimal numbers
// 'TP8KEFTvlgqTnxK1CVi1'
random(20, ['aZ', '10'])

// Use special characters, and hexedecimal characters
// '!c66c_&b'
random(8, ['!', '0x'])

// Use special characters, and hexedecimal characters, no duplicates
// '*.c#ab46'
random(8, ['!', '0x'], false)

sortByKey

Returns a callback function that will compare two objects by the key name. Useful for sorting arrays of objects using sort

import { sortByKey } from '@ouroboros/tools';

let people = [
    {first: 'Chris', last: 'Nasr'},
    {first: 'Brendan', last: 'Eich'},
    {first: 'Guido', last: 'van Rossum'}
];

// people = [
//   { first: 'Brendan', last: 'Eich' },
//   { first: 'Chris', last: 'Nasr' },
//   { first: 'Guido', last: 'van Rossum' }
// ]
people.sort(sortByKey('first'));

ucfirst

Makes the first character of each word in the text upper case.

import { ucfirst } from '@ouroboros/tools';

// Hello, World!
ucfirst('hello, world!');

// Hello, World!
ucfirst('HELLO, WORLD!');

Readme

Keywords

Package Sidebar

Install

npm i @ouroboros/tools

Weekly Downloads

247

Version

0.5.4

License

MIT

Unpacked Size

106 kB

Total Files

6

Last publish

Collaborators

  • ouroboros