bareutil
Yet Another Javascript Utility library
Contains recurring goto methods I encounter during development If you come across any problems, please create an issue
How to use
var bare = ;var misc = baremisc;var obj = bareobj;var val = bareval; //Expose to client *.com/bare.misc.js using expressvar express = ;var app = ; baremisc;bareobj;bareval;
How to run tests
cd bareutilnpm run testnpm run coverage
Validation (bare.val)
All validation is done by performing a check on the object's constructor
- bare.val.undefined(obj) -
var obj;
is true - defined(obj) -
var obj;
is false - function(obj) -
function() { }
is true - object(obj) -
{ }
is true - number(obj) -
12
is true - string(obj) -
"Hello"
is true - date(obj) -
new Date()
is true - boolean(obj) -
false
is true - regex(obj) -
\(w*)\g
is true - promise(obj) -
new Promise()
is true
Object (bare.obj)
Functional programming for objects
Array-like methods have 2 required and 1 optional parameter:
- obj - The object to perform the operation on
- cb(key, value) - Function called for each element in the object
- allowFuncs - Will include functions in iterations
Array-like - function(obj, cb, allowFuncs)
- bare.obj.find - Returns value if a match is found Performs a strict comparison when a non-function is provided
- each - Executes cb once per property
- map - Performs transformation on object's values. Keys are preserved.
- reduce - Provides an accumulator to reduce object down to a single value cb(accumulated, value, key)
- filter - Constructs new object from calls that return true. Keys are preserved.
- keys - An array of the keys of the object
- values - An array of the values of the object
- toArray - Transforms into an array of
{key, value}
pairs - sum - Reduces by adding. Return true/false from cb to sum value
Other
- bare.obj.increment(obj, map) - Increment each matching key by value
- write(obj, data) - Assigns all key/values from data to obj
- merge(obj, data) - Assigns all matching key/values from data to obj
- slim(obj, prop) - Flattens a nested object by the property
- copy(obj) - Creates a shallow copy the object
- has(obj, prop) -
true
if object has that property defined - contains(obj, coll) -
true
if obj shares all keys with coll
Misc (bare.misc)
- bare.misc.supplant(str, values) - String substitution,
{key}
or$key
syntax - random(length, possible) - Random string from possible characters
- once(fn, context) - Calls functions with context only once. Subsequent calls return result
- throwLater(err, msg) - Throws error sometime later
- throwWith(msg) - Returns throw later function with msg prepended
Ajax (bare.ajax)
Not available for server-side yet, hasn't been tested. Don't use it