1.2.0 • Published 10 years ago
ui-utils v1.2.0
UI Utilities
A small ES6 library of common utility functions.
Use
Install
npm install ui-utils --saveImport
import { each } from 'ui-utils';API
camelToDash()
Convert camelCase strings to dash-case
camelToDash('camelCase'); // 'camel-case'currentTime()
Get current page timestamp
Uses performance.now() if available, falls back to Date().
createBuffer()
Create a buffer array
Create a FIFO buffer array. Default maximum size is 3 items.
Currently only push checks for maximum size.
Set maximum size
const buffer = createBuffer(2);
buffer.push(1, 2, 3);
buffer[1]; // 3each()
Iterate over an object
function callback(value, key, fullObject) {}
each(object, callback);Checks for hasOwnProperty before firing callback.
hasChanged()
Compare two iterations of the same object and return true if different
const a = { foo: 1 };
const b = { foo: 1 };
const c = { foo: 2 };
hasChanged(a, b); // false
hasChanged(a, c); // trueisArray()
Check if provided variable is an array
isArray([]); // true
isArray(''); // falseisFunc()
Check if provided variable is a function
isFunc(function () {}); // true
isFunc(''); // falseisNum()
Check if provided variable is a number
isNum(0); // true
isNum(''); // falseisObj()
Check if provided variable is an object
isObj({}); // true
isObj(''); // falseisRelativeValue()
Check if provided variable is a relative value
isRelativeValue('+=200'); // true
isRelativeValue('200'); // falseisString()
Check if provided variable is a string
isString(''); // true
isString(0); // falsetoDecimal()
Round number to x decimal places
const pi = 3.14159265359;
toDecimal(pi); // 3.14
toDecimal(pi, 5); // 3.14159splitValueUnit()
Split a value with a unit into an object with value and unit props
splitValueUnit('200px');
/*
Returns: {
value: 200,
unit: 'px'
}
*/