Comparing version 1.1.8 to 1.2.0
@@ -10,3 +10,3 @@ (function (global, factory) { | ||
title: cycni | ||
version: 1.1.8 | ||
version: 1.2.0 | ||
license: mpl-2.0 | ||
@@ -20,6 +20,7 @@ author: alexander elias | ||
var Cycni = {}; | ||
var Cycni = { | ||
END: '$END', | ||
START: '$START' | ||
}; | ||
Cycni.SET = 2; | ||
Cycni.clone = function (variable) { | ||
@@ -62,22 +63,14 @@ var clone; | ||
Cycni.paths = function (paths) { | ||
return paths.split('.'); | ||
}; | ||
Cycni.traverse = function (collection, keys, callback, create) { | ||
Cycni.traverse = function (collection, path, callback, type) { | ||
path = typeof path === 'number' ? path.toString() : path; | ||
path = typeof path === 'string' ? this.paths(path) : path; | ||
var key, index = 0; | ||
var length = path.length; | ||
var length = keys.length; | ||
var last = length === 0 ? 0 : length - 1; | ||
for (index; index < length; index++) { | ||
key = path[index]; | ||
for (index; index < last; index++) { | ||
key = keys[index]; | ||
if (!(key in collection)) { | ||
if (type === this.SET) { | ||
if (isNaN(path[index+1])) { | ||
if (create) { | ||
if (isNaN(keys[index+1])) { | ||
collection[key] = {}; | ||
@@ -87,54 +80,83 @@ } else { | ||
} | ||
} else { | ||
return callback.call(this, collection, key, false); | ||
return callback.call(this, new Error('Cannot read property ' + key + ' of undefined')); | ||
} | ||
} | ||
if (index === last) { | ||
return callback.call(this, collection, key, true); | ||
collection = collection[key]; | ||
} | ||
return callback.call(this, undefined, collection, keys[last]); | ||
}; | ||
Cycni.get = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
collection = collection[key]; | ||
return callback(e, c[k]); | ||
} | ||
} | ||
}); | ||
}; | ||
Cycni.set = function (collection, path, value) { | ||
return this.traverse(collection, path, function (c, k) { | ||
return c[k] = value; | ||
}, this.SET); | ||
Cycni.set = function (collection, keys, value, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
if (c.constructor.name === 'Object') { | ||
c[k] = value; | ||
} else if (c.constructor.name === 'Array') { | ||
c.splice(k, 1, value); | ||
} | ||
return callback(e); | ||
} | ||
}, true); | ||
}; | ||
Cycni.get = function (collection, path) { | ||
return this.traverse(collection, path, function (c, k) { | ||
return c[k]; | ||
Cycni.add = function (collection, keys, value, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
if (c.constructor.name === 'Object' && !(k in c)) { | ||
c[k] = value; | ||
return callback(e); | ||
} else if (c.constructor.name === 'Array' && k < 1) { | ||
c.splice(k === 0 ? 0 : c.length, 0, value); | ||
return callback(e); | ||
} else { | ||
return callback(new Error('Cannot add property ' + k + ' already exists')); | ||
} | ||
} | ||
}); | ||
}; | ||
Cycni.has = function (collection, path, value) { | ||
return this.traverse(collection, path, function (c, k, e) { | ||
if (e === false) { | ||
return false; | ||
} else if (value.constructor.name === 'Function') { | ||
return value(c[k]) || false; | ||
} else if (value.constructor.name === 'RegExp') { | ||
return value.test(c[k]); | ||
Cycni.has = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
return c[k] === value; | ||
return callback(e, k in c); | ||
} | ||
}); | ||
}; | ||
Cycni.remove = function (collection, path) { | ||
return this.traverse(collection, path, function (c, k) { | ||
Cycni.remove = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
var v; | ||
if (c.constructor.name === 'Object') { | ||
delete c[k]; | ||
} else if (c.constructor.name === 'Array') { | ||
c.splice(k, 1); | ||
if (c.constructor.name === 'Object') { | ||
v = c[k]; | ||
delete c[k]; | ||
} else if (c.constructor.name === 'Array') { | ||
v = c.splice(k, 1); | ||
} | ||
return callback(e, v); | ||
} | ||
}); | ||
@@ -141,0 +163,0 @@ }; |
/* | ||
title: cycni | ||
version: 1.1.8 | ||
license: mpl-2.0 | ||
author: alexander elias | ||
title: cycni | ||
version: 1.2.0 | ||
license: mpl-2.0 | ||
author: alexander elias | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?module.exports=b():'function'==typeof define&&define.amd?define('Cycni',b):a.Cycni=b()})(this,function(){'use strict';return{SET:2,clone:function(b){var d;if(null===b||b===void 0||'object'!=typeof b)return b;if('Date'===b.constructor.name)d=new Date,d.setTime(b.getTime());else if('Array'===b.constructor.name){d=[];for(var f=0,g=b.length;f<g;f++)d[f]=this.clone(b[f])}else if('Object'===b.constructor.name)for(var h in d={},b)b.hasOwnProperty(h)&&(d[h]=this.clone(b[h]));else throw new Error('Unable to clone variable. Type is not supported');return d},paths:function(b){return b.split('.')},traverse:function(b,d,f,g){d='number'==typeof d?d.toString():d,d='string'==typeof d?this.paths(d):d;var h,j=0,m=d.length,n=0===m?0:m-1;for(j;j<m;j++){if(h=d[j],!(h in b))if(g===this.SET)b[h]=isNaN(d[j+1])?{}:[];else return f.call(this,b,h,!1);if(j==n)return f.call(this,b,h,!0);b=b[h]}},set:function(b,d,f){return this.traverse(b,d,function(g,h){return g[h]=f},this.SET)},get:function(b,d){return this.traverse(b,d,function(f,g){return f[g]})},has:function(b,d,f){return this.traverse(b,d,function(g,h,j){return!1!==j&&('Function'===f.constructor.name?f(g[h])||!1:'RegExp'===f.constructor.name?f.test(g[h]):g[h]===f)})},remove:function(b,d){return this.traverse(b,d,function(f,g){'Object'===f.constructor.name?delete f[g]:'Array'===f.constructor.name&&f.splice(g,1)})}}}); | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?module.exports=b():'function'==typeof define&&define.amd?define('Cycni',b):a.Cycni=b()})(this,function(){'use strict';return{END:'$END',START:'$START',clone:function(b){var d;if(null===b||b===void 0||'object'!=typeof b)return b;if('Date'===b.constructor.name)d=new Date,d.setTime(b.getTime());else if('Array'===b.constructor.name){d=[];for(var f=0,g=b.length;f<g;f++)d[f]=this.clone(b[f])}else if('Object'===b.constructor.name)for(var h in d={},b)b.hasOwnProperty(h)&&(d[h]=this.clone(b[h]));else throw new Error('Unable to clone variable. Type is not supported');return d},traverse:function(b,d,f,g){var h,j=0,m=d.length,n=0===m?0:m-1;for(j;j<n;j++){if(h=d[j],!(h in b))if(g)b[h]=isNaN(d[j+1])?{}:[];else return f.call(this,new Error('Cannot read property '+h+' of undefined'));b=b[h]}return f.call(this,void 0,b,d[n])},get:function(b,d,f){return this.traverse(b,d,function(g,h,j){return g?f(g):f(g,h[j])})},set:function(b,d,f,g){return this.traverse(b,d,function(h,j,m){return h?g(h):('Object'===j.constructor.name?j[m]=f:'Array'===j.constructor.name&&j.splice(m,1,f),g(h))},!0)},add:function(b,d,f,g){return this.traverse(b,d,function(h,j,m){return h?g(h):'Object'!==j.constructor.name||m in j?'Array'===j.constructor.name&&1>m?(j.splice(0===m?0:j.length,0,f),g(h)):g(new Error('Cannot add property '+m+' already exists')):(j[m]=f,g(h))})},has:function(b,d,f){return this.traverse(b,d,function(g,h,j){return g?f(g):f(g,j in h)})},remove:function(b,d,f){return this.traverse(b,d,function(g,h,j){if(g)return f(g);var m;return'Object'===h.constructor.name?(m=h[j],delete h[j]):'Array'===h.constructor.name&&(m=h.splice(j,1)),f(g,m)})}}}); |
{ | ||
"name": "cycni", | ||
"version": "1.1.8", | ||
"version": "1.2.0", | ||
"description": "Collection manipulation tool.", | ||
"browser": "dist/cycni.min.js", | ||
"main": "dist/cycni.js", | ||
"module": "src/cycni.b.js", | ||
"module": "src/cycni.js", | ||
"scripts": { | ||
"build-dev": "muleify pack src/cycni.b.js dist/cycni.js", | ||
"build-min": "muleify pack src/cycni.b.js dist/cycni.min.js -m", | ||
"build-dev": "muleify pack -b src/cycni.js dist/cycni.js", | ||
"build-min": "muleify pack -bm src/cycni.js dist/cycni.min.js", | ||
"build": "npm run build-dev && npm run build-min" | ||
@@ -12,0 +12,0 @@ }, |
101
README.md
# Cycni | ||
A collection manipulation tool. It enables the ability to manipulation and interact with an infinitely deep collection. It provides an interface to manipulate objects, arrays, and others (coming soon) in a seamless and consistent method. | ||
Cycni is a collection manipulation tool. It enables the ability to get, set, remove, and interact with an infinite depth collection. It provides an interface/api to manipulate objects, arrays, and others (coming soon) in a seamless and consistent mannor. | ||
See the test directory for examples. | ||
@@ -10,3 +9,3 @@ | ||
- UMD `dist/cycni.js` | ||
- ESM import `src/cycni.b.js` | ||
- ESMD `src/cycni.js` | ||
@@ -16,29 +15,53 @@ | ||
## Cycni.get | ||
- collection `Object, Array` infinitely nested and combinations | ||
- path `String, Array` key names to destination on object or array | ||
### Cycni.get() | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `callback: Function` | ||
- `error: Error` | ||
- `data: Any` the retrieved value | ||
## Cycni.set | ||
- collection `Object, Array` infinitely nested and combinations. If the path contains a key that does not exists Cycni will create an object, if the path contains a number that does not exists Cycni will create an array. Then will continue on to set the final path key/index to the value. | ||
- path `String, Array` key names to destination on object or array to be assigned | ||
- value `Any` the value to assign | ||
### Cycni.set() | ||
Creates an object or array if the key does not exists. Overwrites if the key exists. | ||
## Cycni.has | ||
- collection `Object, Array` infinitely nested and combinations | ||
- path `String, Array` key names to destination on object or array to compare values | ||
- value `Function, RegExp, Any` the value to compare to the destination returns a boolean | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `value: Any` the value to assign | ||
- `callback: Function` | ||
- `error: Error` | ||
## Cycni.remove | ||
- collection `Object, Array` infinitely nested and combinations | ||
- path `String, Array` key names to destination on object or array to be removed | ||
### Cycni.add() | ||
Errors if any key in the keys does not exists on the collection. Errors if the you try to set a key that already exists. | ||
## Cycni.traverse | ||
- collection `Object, Array` infinitely nested and combinations | ||
- path `String, Array` key names to destination on object or array to be removed | ||
- callback `Function` arguments are the collection and key of the destination | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `value: Any` the value to assign | ||
- `callback: Function` | ||
- `error: Error` | ||
## Cycni.clone | ||
- clones `Object, Array, Date` or returns the value of any another | ||
### Cycni.remove() | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `callback: Function` | ||
- `error: Error` | ||
- `data: Any` the removed item from the collection | ||
### Cycni.has() | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `callback: Function` | ||
- `error: Error` | ||
- `data: Boolean` | ||
### Cycni.traverse() | ||
- `collection: Object, Array` collection to retrieve or manipulate | ||
- `keys: Array` key names to destination on object or array | ||
- `callback: Function` | ||
- `error: Error` | ||
- `collection: Object, Array` the parent of the last key in the keys array | ||
- `key: String, Number` the last key in the keys array | ||
### Cycni.clone() | ||
- `collection: Object, Array, Date` returns a clone | ||
## Examples | ||
@@ -67,20 +90,20 @@ | ||
```JavaScript | ||
Cycni.remove(collection, ['batters', '0'], function (error, data) { | ||
if (error) throw error; | ||
var results = Cycni.remove(collection, 'batters.0'); | ||
console.log(collection); | ||
/* | ||
{ id: '0', | ||
name: 'Cake', | ||
batters: | ||
[ { id: '1', type: 'Chocolate' }, | ||
{ id: 'u', type: 'Blueberry' } ], | ||
hello: [ 'bob' ] } | ||
*/ | ||
console.log(collection); | ||
/* | ||
{ id: '0', | ||
name: 'Cake', | ||
batters: | ||
[ { id: '1', type: 'Chocolate' }, | ||
{ id: 'u', type: 'Blueberry' } ], | ||
hello: [ 'bob' ] } | ||
*/ | ||
console.log(results); | ||
/* | ||
{ id: '0', type: 'Regular' } | ||
*/ | ||
console.log(data | ||
/* | ||
{ id: '0', type: 'Regular' } | ||
*/ | ||
}); | ||
``` |
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
var results = Cycni.get(collection, 'batters.0'); | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(results); | ||
Cycni.get(collection, ['batters', 0], function (error, result) { | ||
if (error) { | ||
throw error; | ||
} else { | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(result); | ||
} | ||
}); |
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
var results = Cycni.remove(collection, 'batters.0'); | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(results); | ||
Cycni.remove(collection, ['batters', 0], function (error, result) { | ||
if (error) { | ||
throw error; | ||
} else { | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(result); | ||
} | ||
}); |
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
var results = Cycni.set(collection, 'arr.0.obj', 'blue'); | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(results); | ||
Cycni.set(collection, ['array', 0], 'blue', function (error, result) { | ||
if (error) { | ||
throw error; | ||
} else { | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(result); | ||
} | ||
}); |
37730
15
559
107