1.1.4 • Published 5 years ago

@jsxtools/utils v1.1.4

Weekly downloads
1
License
CC0-1.0
Repository
github
Last release
5 years ago

utils

utils is a tree-shakable collection of utilities for JavaScript.

It is 349 bytes (150 gzipped).

Installation

npm install @jsxtools/utils

classes

classes returns a normalized string of class names from strings, arrays, or objects.

import { classes } from '@jsxtools/utils';

classes('this', 'that', 'this'); // 'this that'
classes(['this', 'that', 'this']); // 'this that'
classes({ 'this': true, 'that': true, 'else': false }); // 'this that'

debounce

debounce returns a function that throttles updates coming in rapid succession.

import { debounce } from '@jsxtools/utils';

window.addEventListener('resize', debounce(() => {
  // resize events
}, 250, true));

is-equal

is-equal returns whether two values are the same value.

import { isEqual } from '@jsxtools/utils';

// these objects are shallow equal
objectA = { name: 'Adam', age: 930 };
objectB = { name: 'Adam', age: 930 };
isEqual(objectA, objectB);

// these objects are not shallow equal
objectA = { name: 'Adam', age: 930, pets: ['dog'] };
objectB = { name: 'Adam', age: 930, pets: ['dog'] };
isEqual(objectA, objectB);

// but they are deeply equal
isEqual(objectA, objectB, isEqual);

is-type

is-type is a collection of functions that return whether a value is a certain type.

import { isArray, isBoolean, isNumber, isObject, isString } from '@jsxtools/utils';

isArray(''); // false
isArray([]); // true
isArray({}); // false

isBoolean(false); // true
isBoolean(0); // false
isBoolean(!0); // true

isNumber(0); // true
isNumber(Infinity); // false
isNumber(NaN); // false
isNumber('0'); // false

isObject({}); // true
isObject([]); // true
isObject(''); // false

isString(''); // true
isString([]); // false
isString({}); // false

prevent-default

prevent-default returns a function that invokes preventDefault on any event passed into it.

import { preventDefault } from '@jsxtools/utils';

document.querySelector('form').addEventListener('submit', preventDefault(() => {
  // handle submission via JS without submission
}));

uid

uid returns a unique URL-safe ID.

import { uid } from '@jsxtools/utils';

// give <body> a unique ID with 5 characters, like "wAl_H"
// in 1s at 4000 IDs/s it has a 1% probability of at least 1 collision
document.body.id = uid(5);

// give <body> a unique ID like "wAl_Hh9fYRe"
// in 4d at 4000 IDs/s it has a 1% probability of at least 1 collision
document.body.id = uid(11);

// give <body> a unique ID like "wAl_Hh9fYReEakFYN-7qr"
// in 10,000,000y at 4000 IDs/s it has a 1% probability of at least 1 collision
document.body.id = uid(21);