crio API
Instantiation
crio(object: any): (CrioArray|CrioObject)- Standard method, will
criothe object passed if an array or object, else it will return the object itself
- Standard method, will
const emptyObject = crio(); // {}
const populatedObject = crio({foo: 'bar'}); // {foo: 'bar'}
const populatedArray = crio(['foo', 'bar']); // ['foo', 'bar']crio.array(object: Array<any>): CrioArray- Shortcut method for
crioing array-specific values
- Shortcut method for
const empty = crio.array(); // []
const populated = crio.array(['foo']); // ['foo']crio.object(object: Object): CrioObject- Shortcut method for
crioing object-specific values
- Shortcut method for
const empty = crio.object(); // {}
const populated = crio.object({foo: 'bar'}); // {foo: 'bar'}crio.isCrio(object: any): boolean- Determine if the object passed is a
Crio
- Determine if the object passed is a
const normal = {foo: 'bar'};
const crioed = crio({foo: 'bar'});
console.log(crio.isCrio(normal)); // false
console.log(crio.isCrio(crioed)); // truecrio.isArray(object: any): boolean- Determine if the object passed is a
CrioArray
- Determine if the object passed is a
const normal = ['foo'];
const croiedArray = crio(['foo']);
const crioedObject = crio({foo: 'bar'});
console.log(crio.isObject(normal)); // false
console.log(crio.isObject(croiedArray)); // true
console.log(crio.isObject(crioedObject)); // falsecrio.isObject(object: any): boolean- Determine if the object passed is a
CrioObject
- Determine if the object passed is a
const normal = {foo: 'bar'};
const croiedArray = crio(['foo']);
const crioedObject = crio({foo: 'bar'});
console.log(crio.isObject(normal)); // false
console.log(crio.isObject(croiedArray)); // false
console.log(crio.isObject(crioedObject)); // trueMethods with the same name as the native method will be a link to MDN, as they are meant to be as similar to the native method as possible, with the exception of accepting thisArg. Where any specific differences from the default behavior exist, the crio-specific behavior is mentioned.
Arrays
clear(): CrioArray- returns an empty
CrioArray
- returns an empty
const populated = crio(['foo']);
console.log(populated.clear()); // []compact(): CrioArray- returns a new
CrioArraywith all falsy values filtered out
- returns a new
const array = crio(['foo', false, 0, '', {}, [], null, undefined]);
console.log(array.compact()); // ['foo', {}, []]
const array = crio(['foo']);
console.log(array.concat(['bar'])); // ['foo', 'bar']delete(key: (Array<number|string>|number)): CrioArray- Deletes the key provided from the
CrioArray - Supports shallow or deep values via array or dot-bracket syntax
- Deletes the key provided from the
const array = crio(['foo', {bar: 'baz', baz: 'quz'}]);
console.log(array.delete(1)); // ['foo']
console.log(array.delete('[1].bar')); // ['foo', {baz: 'quz'}]difference(array1: Array<any>[, array2: Array<any>[, ...arrayN: Array<any>]]): CrioArray- Returns a new array of the values that only exist in either the
CrioArrayor in one of the arrays passed
- Returns a new array of the values that only exist in either the
const array = crio(['foo', true, 1]);
console.log(array.difference(['foo'], [true])); // [1]
const array = crio(['foo', 'bar', 'baz']);
console.log(array.copyWithin(2, 0)); // ['foo', 'bar', 'foo']
const array = crio(['foo', 'bar']);
console.log(array.entries()); // [[0, 'foo'], [1, 'bar']]equals(object: any): boolean- Determines whether
objectis deeply equal to theCrioArray
- Determines whether
const array = crio(['foo']);
const matchingArray = crio(['foo']);
console.log(array === matchingArray); // false
console.log(array.equals(matchingArray)); // true
console.log(array.equals(crio(['bar']))); // false
const array = crio(['foo', 'bar']);
console.log(array.every(value => value.length === 3)); // true
console.log(array.every(value => value === 'bar')); // false- fill
- Returns new
CrioArraywith items fromstarttoendfilled withvalue
- Returns new
const array = crio(['foo', 'bar', 'baz']);
console.log(array.fill('same')); // ['same', 'same', 'same']
const array = crio(['foo', 'bar', 'baz']);
console.log(array.filter(value => value[0] === 'b')); // ['bar', 'baz']
const array = crio(['foo', 'bar', 'baz']);
console.log(array.find(value => value[0] === 'b')); // 'bar'
const array = crio(['foo', 'bar', 'baz']);
console.log(array.find(value => value[0] === 'b')); // 1findLast(fn: function(value: any, index: number, object: CrioArray)): any- Find the value that matches the result of
fnstarting at the lastindexin the object
- Find the value that matches the result of
const array = crio(['foo', 'bar', 'baz']);
console.log(array.find(value => value[0] === 'b')); // 'baz'findLastIndex(fn: function): number- Same as
findIndexbut starting from end and working to start
- Same as
const array = crio(['foo', 'bar', 'baz']);
console.log(array.find(value => value[0] === 'b')); // 2first(size: number = 1): CrioArray- Returns a new array of the first
sizenumber of items in the array
- Returns a new array of the first
const array = crio(['foo', 'bar', 'baz']);
console.log(array.first()); // ['foo']
console.log(array.first(2)); // ['foo', 'bar']
const array = crio(['foo', 'bar']);
let count = 0;
console.log(array.forEach(() => count++)); // ['foo', 'bar']
console.log(count); // 2get(key: (Array<number|string>|number)): any- Retrieve value at
key - Supports shallow or deep values via array or dot-bracket syntax
- Retrieve value at
const array = crio(['foo', {bar: 'baz'}]);
console.log(array.get(1)); // {bar: 'baz'}
console.log(array.get('[1].bar')); // 'baz'has(key: (Array<number|string>|number)): boolean- Does
keyexist in object - Supports shallow or deep values via array or dot-bracket syntax
- Does
const array = crio(['foo', {bar: 'baz'}]);
console.log(array.has(1)); // true
console.log(array.has(6)); // false
console.log(array.has('[1].bar')); // true
console.log(array.has('[1].quz')); // false
const array = crio(['foo', 'bar']);
console.log(array.includes('foo')); // true
console.log(array.includes('baz')); // falseintersection(array1: Array<any>[, array2: Array<any>[, ...arrayN: Array<any>]]): CrioArray- Returns a new
CrioArrayof the values that exist in all of the arrays
- Returns a new
const array = crio(['foo', 1, true]);
console.log(array.intersection(['foo', 1], [1])); // 1
const array = crio(['foo', 'bar', 'baz', 'bar']);
console.log(array.indexOf('bar')); // 1
const array = crio(['foo', 'bar']);
console.log(array.join()); // 'foo,bar'
console.log(array.join('|')); // 'foo|bar'
const array = crio(['foo', 'bar']);
console.log(array.keys()); // [0, 1]last(size: number = 1): CrioArray- Returns a new
CrioArrayof the last num number of items in the array
- Returns a new
const array = crio(['foo', 'bar', 'baz']);
console.log(array.last()); // ['baz']
console.log(array.last(2)); // ['bar', 'baz']
const array = crio(['foo', 'bar', 'baz', 'bar']);
console.log(array.indexOf('bar')); // 3
const array = crio(['foo', 'bar']);
console.log(
array.map(value =>
value
.slice('')
.reverse()
.join('')
)
); // ['oof', 'rab']merge(array1: Array<any>[, array2: Array<any>[, ...arrayN: Array<any>]]): CrioArray- Merge any number of objects into existing crio
- Supports shallow or deep key values via array or dot-bracket syntax
const array = crio(['foo', {bar: 'baz'}]);
console.log(array.merge(null, ['quz'])); // ['foo', {bar: 'baz'}, 'quz']
console.log(array.merge('[0].bar', {baz: 'quz'})); // ['foo', {bar: {baz: 'quz'}}]mutate(fn: function(array: Array<any>, crio: CrioArray)): any- Whatever you return in the callback is what is returned (as a
CrioArrayorCrioObjectif applicable)
- Whatever you return in the callback is what is returned (as a
const array = crio([{foo: 'bar'}]);
const result = array.mutate(thawed => {
thawed[0].foo = 'baz';
thawed.push('bar');
return thawed;
});
console.log(result); // [{foo: 'baz'}, 'bar']pluck(key: (Array<number|string>|number)): CrioArray- Iterates over the
CrioArrayand returns a newCrioArrayof values where thekeyexists as a property on the collection item - Supports shallow or deep values via array or dot-bracket syntax
- Iterates over the
const array = crio([{foo: 'bar'}, {bar: 'baz'}, {foo: 'quz'}]);
console.log(array.pluck('foo')); // ['bar', undefined, 'quz']
const deepArray = crio([
[{foo: 'foo'}, {bar: 'baz'}, {foo: 'bar'}],
[{foo: 'bar'}, {bar: 'baz'}, null, {foo: 'foo'}]
]);
console.log(array.pluck('[1].foo')); // ['bar', undefined, undefined, 'foo']- pop
- Returns new
CrioArraywith first item removed
- Returns new
const array = crio(['foo', 'bar']);
console.log(array.pop()); // ['foo']- push
- Returns new
CrioArraywith new item(s) added
- Returns new
const array = crio(['foo']);
console.log(array.push('bar', 'baz')); // ['foo', 'bar', 'baz']
const array = crio([1, 2, 3]);
const result = array.reduce(
({order, sum}, value) => {
return {order: [...order, value], sum: sum + value};
},
{order: [], sum: 0}
);
console.log(result); // {order: [1, 2, 3], sum: 6}
const array = crio([1, 2, 3]);
const result = array.reduceRight(
({order, sum}, value) => {
return {order: [...order, value], sum: sum + value};
},
{order: [], sum: 0}
);
console.log(result); // {order: [3, 2, 1], sum: 6}- reverse
- The original array's order is maintained (not mutated)
const array = crio(['foo', 'bar', 'baz']);
console.log(array.reverse()); // ['baz', 'bar', 'foo']set(key: (Array<number|string>|number), value: any): CrioArray- Sets value at
key - Supports shallow or deep values via array or dot-bracket syntax
- Sets value at
const array = crio(['foo', {bar: 'baz'}]);
console.log(array.set(1, 'quz')); // ['foo', 'quz']
console.log(array.set('[1].bar', 'quz')); // ['foo', {bar: 'quz'}]- shift
- Returns new
CrioArraywith first item removed
- Returns new
const array = crio(['foo', 'bar', 'baz']);
console.log(array.shift()); // ['bar', 'baz']
const array = crio(['foo', 'bar']);
console.log(array.slice(1)); // ['bar']
const array = crio(['foo', 'bar', 'baz']);
console.log(array.some(value => value === 'bar')); // true
console.log(array.some(value => value === 'quz')); // false- sort
- Returns new
CrioArraysorted by either callback or default
- Returns new
const array = crio(['foo', 'bar', 'baz']);
console.log(array.sort()); // ['bar', 'baz', 'foo']- splice
- Returns new
CrioArraywith item(s) added/removed based on splicing parameters
- Returns new
const array = crio(['foo', 'bar', 'baz']);
console.log(array.splice(1, 1)); // ['foo', 'baz']thaw(): Array<any>- Recursively thaws
CrioArraydeeply and returns standard array version of itself
- Recursively thaws
const array = crio(['foo', 'bar']);
console.log(array.thaw()); // ['foo', 'bar']
console.log(array.constructor === Array); // false
console.log(array.thaw().constructor === Array); // truetoObject(): CrioObject- Converts
CrioArrayinto aCrioObjectof{index: value}pairs
- Converts
const array = crio(['foo', 'bar']);
console.log(array.toObject()); // {0: 'foo', 1: 'bar'}- toLocaleString
- Returns stringified version of
CrioArray - Optionally accepts
serializerandindentarguments, which are identical to those arguments for JSON.stringify
- Returns stringified version of
const array = crio(['foo', 'bar']);
console.log(array.toLocaleString()); // ["foo","bar"]
console.log(arrat.toLocaleString(null, 2));
/*
[
"foo",
"bar"
]
*/- toString
- Returns stringified version of
CrioArray - Optionally accepts
serializerandindentarguments, which are identical to those arguments for JSON.stringify
- Returns stringified version of
const array = crio(['foo', 'bar']);
console.log(array.toString()); // ["foo","bar"]
console.log(arrat.toString(null, 2));
/*
[
"foo",
"bar"
]
*/unique(): CrioArray- Returns a new
CrioArrayof values filtered down to only existing in the array once
- Returns a new
const array = crio(['foo', 'bar', 'foo', 'foo', 'bar']);
console.log(array.unique()); // ['foo', 'bar']- unshift
- Returns new
CrioArraywith new item(s) added to beginning
- Returns new
const array = crio(['foo', 'bar']);
console.log(array.unshift('baz', 'quz')); // ['baz', 'quz', 'foo', 'bar']
const array = crio(['foo', 'bar']);
console.log(array.values()); // ['foo', 'bar']xor(array1: Array<any>[, array2: Array<any>[, ...arrayN: Array<any>]]): CrioArray- Returns a new
CrioArrayof the values that are the symmetric difference of theCrioArrayand the arrays passed
- Returns a new
const array = crio(['foo', 1, true]);
console.log(array.xor(['foo'], [true])); // [1]Objects
clear(): CrioObject- returns an empty
CrioObject
- returns an empty
const object = crio({foo: 'bar'});
console.log(object.clear());compact(): CrioObject- returns a new
CrioObjectwith all falsy values filtered out
- returns a new
const object = crio({
string: 'foo',
bool: false,
num: 0,
object: {},
array: [],
nul: null,
undef: undefined
});
console.log(object.compact()); // {string: 'foo', object: {}, array: []}delete(key: (Array<number|string>|string)): CrioObject- Deletes the key provided from the
CrioObject - Supports shallow or deep values via array or dot-bracket syntax
- Deletes the key provided from the
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(object.delete('bar')); // {foo; 'bar'}
console.log(object.delete('bar[0]')); // {foo: 'bar', bar: ['quz']}entries(): CrioArray- Gets an array of the
[key, value]pairs in theCrioObject
- Gets an array of the
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.entries()); // [['foo', 'bar'], ['bar', 'baz']]every(fn: function(value: any, key: string, object: CrioObject)): boolean- Performs same function as
everyinCrioArray, but on theCrioObject
- Performs same function as
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.every(value => value.length === 3)); // true
console.log(object.every(value => value === 'bar')); // falseforEach(fn: function(value: any, key: string, object: CrioObject)): CrioObject- Iterates over object executing
fnand returns the originalCrioObject - Iteration order is not guaranteed (due to object key order not being guaranteed per the spec)
- Iterates over object executing
const object = crio({foo: 'bar', bar: 'baz'});
let count = 0;
console.log(object.forEach(() => count++)); // {foo: 'bar', bar: 'baz'}
console.log(count); // 2filter(fn: function(value: any, key: string, object: CrioObject)): CrioObject- Iterates over object and filters out any returned values that are falsy
- Iteration order is not guaranteed (due to object key order not being guaranteed per the spec)
const object = crio({foo: 'bar', bar: 'baz', baz: 'quz'});
console.log(object.filter(value => value[0] === 'b')); // {foo: 'bar', bar: 'baz'}find(fn: function(value: any, key: string, object: CrioObject)): any- Same as
findforCrioArraybut on theCrioObject
- Same as
const object = crio({foo: 'bar', bar: 'baz'}, (baz: 'quz'));
console.log(object.find(value => value !== 'baz')); // 'bar'findKey(fn: function(value: any, key: string, object: CrioObject)): string- Same as
findIndexforCrioArraybut finding the appropriatekey
- Same as
const object = crio({foo: 'bar', bar: 'baz'}, (baz: 'quz'));
console.log(object.findKey(value => value !== 'baz')); // 'foo'findLast(fn: function(value: any, key: string, object: CrioObject)): any- Find the value that matches the result of
fnstarting at the last key in theCrioObject
- Find the value that matches the result of
const object = crio({foo: 'bar', bar: 'baz'}, (baz: 'quz'));
console.log(object.findLast(value => value !== 'baz')); // 'quz'findLastKey(fn: function(value: any, key: string, object: CrioObject)): string- Same as
findKeybut starting from end and working to start
- Same as
const object = crio({foo: 'bar', bar: 'baz'}, (baz: 'quz'));
console.log(object.findLastKey(value => value !== 'baz')); // 'baz'get(key: (Array<number|string>|string)): any- Retrieve value at
key - Supports shallow or deep values via array or dot-bracket syntax
- Retrieve value at
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(crio.get('bar')); // ['baz', 'quz']
console.log(crio.get('bar[0]')); // 'baz'has(key: (Array<number|string>|string)): boolean- Does
keyexist in object - Supports shallow or deep values via array or dot-bracket syntax
- Does
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(crio.has('bar')); // true
console.log(crio.has('baz')); // false
console.log(crio.has('bar[0]')); // true
console.log(crio.has('bar[6]')); // false- hasOwnProperty
- Only supports shallow values
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(crio.hasOwnProperty('bar')); // true
console.log(crio.hasOwnProperty('baz')); // falseincludes(item: any): boolean- Determine if the
CrioObjecthas a value that matchesvaluein strict equality
- Determine if the
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.includes('baz')); // true
console.log(object.includes('quz')); // falsekeyOf(item: any): string- Get the key for the
itempassed starting with the first key in theCrioObject
- Get the key for the
const object = crio({foo: 'bar', bar: 'baz', baz: 'bar'});
console.log(object.keyOf('bar')); // fookeys(): CrioArray- Returns an array of the keys in the
CrioObject
- Returns an array of the keys in the
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.keys()); // ['foo', 'bar']lastKeyOf(item: any): string- Get the key for the
itempassed starting with the last key in theCrioObject
- Get the key for the
const object = crio({foo: 'bar', bar: 'baz', baz: 'bar'});
console.log(object.keyOf('bar')); // bazmap(fn: function(value: any, key: string, object: CrioObject)): CrioObject- Iterates over object and maps returned value to the respective
key - Iteration order is not guaranteed (due to object key order not being guaranteed per the spec)
- Iterates over object and maps returned value to the respective
const object = crio({foo: 'bar', bar: 'baz', baz: 'quz'});
console.log(
object.map(value =>
value
.split('')
.reverse()
.join('')
)
); // {foo: 'rab', bar: 'zab', baz: 'zuq'}merge(object1: Object[, object2: Object[, ...objectN: Object]]): CrioObject- Merge any number of objects into existing
CrioObject - Supports shallow or deep values via array or dot-bracket syntax
- Merge any number of objects into existing
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(object.merge(null, {baz: 'quz'})); // {foo: 'bar', bar: ['baz', 'quz'], baz: 'quz'}
console.log(object.merge('bar[0]', ['blah'])); // {foo: 'bar', bar: ['baz', 'quz', 'blah']};mutate(fn: function(object: Object, crio: CrioObject)): any- Whatever you return in the callback is what is returned (as a
CrioArrayorCrioObjectif applicable)
- Whatever you return in the callback is what is returned (as a
const object = crio({foo: ['bar', 'baz']});
const result = object.mutate(thawed => {
thawed.foo.push('quz');
thawed.bar = 'baz';
return thawed;
});
console.log(result); // {foo: ['bar', 'baz', 'quz'], bar: 'baz'}pluck(key: (Array<number|string>|string)): CrioArray- Iterates over the
CrioObjectand returns aCrioArrayof values where thekeyexists as a property on the collection item - Supports shallow or deep values via array or dot-bracket syntax
- Iteration order is not guaranteed (due to object key order not being guaranteed per the spec)
- Iterates over the
const object = crio({
first: {foo: 'bar'},
second: {bar: 'baz'},
third: null,
fourth: {foo: 'foo'}
});
console.log(object.pluck('foo')); // ['bar', undefined, undefined, 'foo']
const otherObject = crio({
first: [{foo: 'foo'}, {bar: 'baz'}, {foo: 'bar'}],
second: [{foo: 'bar'}, {bar: 'baz'}, null, {foo: 'foo'}]
});
console.log(otherObject.pluck('second.foo')); // ['bar', undefined, undefined, 'foo']reduce(fn: function(accumulator: any, value: any, key: string, object:CrioObject)): any- Performs same function as
reduceinCrioArray, but on theCrioObject
- Performs same function as
const object = crio({one: 1, two; 2, three: 3});
const result = object.reduce(({order, sum}, value) => {
return {order: [...order, value], sum: sum + value};
}, {order: [], sum; 0});
console.log(result); // {order: [1, 2, 3], sum: 6}reduceRight(fn: function(accumulator: any, value: any, key: string, object:CrioObject)): any- Performs same function as
reduceRightinCrioArray, but on theCrioObject
- Performs same function as
const object = crio({one: 1, two; 2, three: 3});
const result = object.reduceRight(({order, sum}, value) => {
return {order: [...order, value], sum: sum + value};
}, {order: [], sum; 0});
console.log(result); // {order: [3, 2, 1], sum: 6}set(key: (Array<number|string>|string), value: any): CrioObject- Sets value at
key - Supports shallow or deep values via array or dot-bracket syntax
- Sets value at
const object = crio({foo: 'bar', bar: ['baz', 'quz']});
console.log(object.set('bar', 'baz')); // {foo: 'bar', bar: 'baz'}
console.log(object.set('bar[0]', {baz: 'blah'})); // {foo: 'bar', bar: [{baz: 'blah'}, 'quz']}some(fn: function(value: any, key: string, object: CrioObject)): boolean- Performs same function as
someinCrioArray, but on theCrioObject
- Performs same function as
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.some(value => value === 'baz')); // true
console.log(object.some(value => value === 'quz')); // falsetoArray(): CrioArray- Converts
CrioObjectto aCrioArrayof the object's values
- Converts
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.toArray()); // ['bar', 'baz']- toLocaleString
- Returns stringified version of
CrioObject - Optionally accepts
serializerandindentarguments, which are identical to those arguments for JSON.stringify
- Returns stringified version of
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.toLocaleString()); // {"foo":"bar","bar":"baz"}
console.log(object.toLocaleString(null, 2));
/*
{
"foo": "bar",
"bar": "baz"
}
*/- toString
- Returns stringified version of
CrioObject - Optionally accepts
serializerandindentarguments, which are identical to those arguments for JSON.stringify
- Returns stringified version of
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.toString()); // {"foo":"bar","bar":"baz"}
console.log(object.toString(null, 2));
/*
{
"foo": "bar",
"bar": "baz"
}
*/thaw(): Object- Recursively thaws
CrioObjectdeeply and returns standard object version of itself
- Recursively thaws
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.thaw()); // {foo: 'bar', bar: 'baz'}
console.log(object.constructor === Object); // false
console.log(object.thaw().constructor === Object); // truevalues(): CrioObject- Returns a
CrioArrayof the values in theCrioObject
- Returns a
const object = crio({foo: 'bar', bar: 'baz'});
console.log(object.values()); // ['bar', 'baz']