npm.io
1.2.0 • Published 10 years ago

ui-utils

Licence
MIT
Version
1.2.0
Deps
0
Vulns
0
Weekly
0

UI Utilities

A small ES6 library of common utility functions.

Use

Install

npm install ui-utils --save
Import
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]; // 3
each()
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); // true
isArray()
Check if provided variable is an array
isArray([]); // true
isArray(''); // false
isFunc()
Check if provided variable is a function
isFunc(function () {}); // true
isFunc(''); // false
isNum()
Check if provided variable is a number
isNum(0); // true
isNum(''); // false
isObj()
Check if provided variable is an object
isObj({}); // true
isObj(''); // false
isRelativeValue()
Check if provided variable is a relative value
isRelativeValue('+=200'); // true
isRelativeValue('200'); // false
isString()
Check if provided variable is a string
isString(''); // true
isString(0); // false
toDecimal()
Round number to x decimal places
const pi = 3.14159265359;

toDecimal(pi); // 3.14
toDecimal(pi, 5); // 3.14159
splitValueUnit()
Split a value with a unit into an object with value and unit props
splitValueUnit('200px');
/*
    Returns: {
        value: 200,
        unit: 'px'
    }
*/