bareutil v1.5.4
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 = require("bareutil");
var misc = bare.misc;
var obj = bare.obj;
var val = bare.val;
//Expose to client *.com/bare.misc.js using express
var express = require('express');
var app = express();
bare.misc.expose(app, express);
bare.obj.expose(app, express);
bare.val.expose(app, express);How to run tests
cd bareutil
npm run test
npm run coverageValidation (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) -
12is true - string(obj) -
"Hello"is true - date(obj) -
new Date()is true - boolean(obj) -
falseis true - regex(obj) -
\(w*)\gis true - promise(obj) -
new Promise()is true
Object (bare.obj)
Functional programming for objects
Array-like methods have 2 required and 1 optional parameter: 1. obj - The object to perform the operation on 2. cb(key, value) - Function called for each element in the object 3. 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) -
trueif object has that property defined - contains(obj, coll) -
trueif obj shares all keys with coll
Misc (bare.misc)
- bare.misc.supplant(str, values) - String substitution,
{key}or$keysyntax - 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 it9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago