Comparing version 1.2.0 to 2.0.0
@@ -10,3 +10,3 @@ (function (global, factory) { | ||
title: cycni | ||
version: 1.2.0 | ||
version: 2.0.0 | ||
license: mpl-2.0 | ||
@@ -20,142 +20,141 @@ author: alexander elias | ||
var Cycni = { | ||
END: '$END', | ||
START: '$START' | ||
}; | ||
const Cycni = {}; | ||
Cycni.clone = function (variable) { | ||
var clone; | ||
Cycni.traverse = async function (opt) { | ||
if (variable === null || variable === undefined || typeof variable !== 'object') { | ||
if (!opt.keys && opt.keys.length === 1 && opt.keys[0] === '.') { | ||
return { | ||
key: '.', | ||
data: res.data | ||
} | ||
} | ||
return variable; | ||
var data = opt.data; | ||
var keys = opt.keys; | ||
var create = opt.create; | ||
var length = keys.length; | ||
var last = length === 0 ? 0 : length - 1; | ||
} else if (variable.constructor.name === 'Date') { | ||
for (var i = 0; i < last; i++) { | ||
var key = keys[i]; | ||
clone = new Date(); | ||
clone.setTime(variable.getTime()); | ||
} else if (variable.constructor.name === 'Array') { | ||
clone = []; | ||
for (var i = 0, l = variable.length; i < l; i++) { | ||
clone[i] = this.clone(variable[i]); | ||
if (!(key in data)) { | ||
if (create) { | ||
if (isNaN(keys[i+1])) { | ||
data[key] = {}; | ||
} else { | ||
data[key] = []; | ||
} | ||
} else { | ||
throw new Error('Cycni.traverse - property ' + key + ' of undefined'); | ||
} | ||
} | ||
} else if (variable.constructor.name === 'Object') { | ||
clone = {}; | ||
data = data[key]; | ||
} | ||
for (var key in variable) { | ||
return { | ||
key: keys[last], | ||
data: data | ||
}; | ||
}; | ||
if (variable.hasOwnProperty(key)) { | ||
clone[key] = this.clone(variable[key]); | ||
} | ||
Cycni.get = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
return res.data[res.key]; | ||
}; | ||
} | ||
Cycni.set = async function (opt) { | ||
opt.create = true; | ||
const res = await Cycni.traverse(opt); | ||
if (res.data.constructor === Object) { | ||
res.data[res.key] = opt.value; | ||
} else if (res.data.constructor === Array) { | ||
res.data.splice(res.key, 1, opt.value); | ||
} | ||
}; | ||
Cycni.add = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
if (res.data.constructor === Object && !(res.key in res.data)) { | ||
res.data[res.key] = opt.value; | ||
} else if (res.data.constructor === Array && res.key < 1) { | ||
res.data.splice(res.key === 0 ? 0 : res.data.length, 0, opt.value); | ||
} else { | ||
throw new Error('Unable to clone variable. Type is not supported'); | ||
throw new Error('Cycni.add - property ' + res.key + ' exists'); | ||
} | ||
}; | ||
return clone; | ||
Cycni.has = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
return res.key in res.data; | ||
}; | ||
Cycni.traverse = function (collection, keys, callback, create) { | ||
Cycni.remove = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
var key, index = 0; | ||
var length = keys.length; | ||
var last = length === 0 ? 0 : length - 1; | ||
let value; | ||
for (index; index < last; index++) { | ||
key = keys[index]; | ||
if (res.data.constructor === Object) { | ||
value = res.data[res.key]; | ||
delete res.data[res.key]; | ||
} else if (res.data.constructor === Array) { | ||
value = res.data.splice(res.key, 1); | ||
} | ||
if (!(key in collection)) { | ||
if (create) { | ||
if (isNaN(keys[index+1])) { | ||
collection[key] = {}; | ||
} else { | ||
collection[key] = []; | ||
} | ||
} else { | ||
return callback.call(this, new Error('Cannot read property ' + key + ' of undefined')); | ||
} | ||
} | ||
return value; | ||
}; | ||
collection = collection[key]; | ||
Cycni.size = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
let data; | ||
if (res.key === '.') { | ||
data = res.data; | ||
} else { | ||
data = res.data[res.key]; | ||
} | ||
return callback.call(this, undefined, collection, keys[last]); | ||
}; | ||
if (data.constructor === Object) { | ||
return Object.keys(data).length; | ||
} | ||
Cycni.get = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
return callback(e, c[k]); | ||
} | ||
}); | ||
if (data.constructor === Array) { | ||
return data.length; | ||
} | ||
return 0; | ||
}; | ||
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); | ||
} | ||
Cycni.clone = async function (variable) { | ||
var clone; | ||
return callback(e); | ||
} | ||
}, true); | ||
}; | ||
if (variable === null || variable === undefined || typeof variable !== 'object') { | ||
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')); | ||
} | ||
} | ||
}); | ||
}; | ||
return variable; | ||
Cycni.has = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
return callback(e, k in c); | ||
} else if (variable.constructor.name === 'Array') { | ||
clone = []; | ||
for (var i = 0, l = variable.length; i < l; i++) { | ||
clone[i] = this.clone(variable[i]); | ||
} | ||
}); | ||
}; | ||
Cycni.remove = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
var v; | ||
} else if (variable.constructor.name === 'Object') { | ||
clone = {}; | ||
if (c.constructor.name === 'Object') { | ||
v = c[k]; | ||
delete c[k]; | ||
} else if (c.constructor.name === 'Array') { | ||
v = c.splice(k, 1); | ||
for (var key in variable) { | ||
if (variable.hasOwnProperty(key)) { | ||
clone[key] = this.clone(variable[key]); | ||
} | ||
return callback(e, v); | ||
} | ||
}); | ||
} else { | ||
throw new Error('Cycni.clone - type is not supported'); | ||
} | ||
return clone; | ||
}; | ||
@@ -162,0 +161,0 @@ |
/* | ||
title: cycni | ||
version: 1.2.0 | ||
version: 2.0.0 | ||
license: mpl-2.0 | ||
@@ -11,2 +11,2 @@ author: alexander elias | ||
*/ | ||
(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)})}}}); | ||
(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';const a={};return a.traverse=async function(a){if(!a.keys&&1===a.keys.length&&'.'===a.keys[0])return{key:'.',data:res.data};for(var b,c=a.data,d=a.keys,e=a.create,f=d.length,g=0===f?0:f-1,h=0;h<g;h++){if(b=d[h],!(b in c))if(e)c[b]=isNaN(d[h+1])?{}:[];else throw new Error('Cycni.traverse - property '+b+' of undefined');c=c[b]}return{key:d[g],data:c}},a.get=async function(b){const c=await a.traverse(b);return c.data[c.key]},a.set=async function(b){b.create=!0;const c=await a.traverse(b);c.data.constructor===Object?c.data[c.key]=b.value:c.data.constructor===Array&&c.data.splice(c.key,1,b.value)},a.add=async function(b){const c=await a.traverse(b);if(c.data.constructor===Object&&!(c.key in c.data))c.data[c.key]=b.value;else if(c.data.constructor===Array&&1>c.key)c.data.splice(0===c.key?0:c.data.length,0,b.value);else throw new Error('Cycni.add - property '+c.key+' exists')},a.has=async function(b){const c=await a.traverse(b);return c.key in c.data},a.remove=async function(b){const c=await a.traverse(b);let d;return c.data.constructor===Object?(d=c.data[c.key],delete c.data[c.key]):c.data.constructor===Array&&(d=c.data.splice(c.key,1)),d},a.size=async function(b){const c=await a.traverse(b);let d;return d='.'===c.key?c.data:c.data[c.key],d.constructor===Object?Object.keys(d).length:d.constructor===Array?d.length:0},a.clone=async function(a){var b;if(null===a||void 0===a||'object'!=typeof a)return a;if('Array'===a.constructor.name){b=[];for(var c=0,d=a.length;c<d;c++)b[c]=this.clone(a[c])}else if('Object'===a.constructor.name)for(var e in b={},a)a.hasOwnProperty(e)&&(b[e]=this.clone(a[e]));else throw new Error('Cycni.clone - type is not supported');return b},a}); |
{ | ||
"name": "cycni", | ||
"version": "1.2.0", | ||
"description": "Collection manipulation tool.", | ||
"version": "2.0.0", | ||
"description": "A collection manipulation tool", | ||
"browser": "dist/cycni.min.js", | ||
@@ -9,4 +9,4 @@ "main": "dist/cycni.js", | ||
"scripts": { | ||
"build-dev": "muleify pack -b src/cycni.js dist/cycni.js", | ||
"build-min": "muleify pack -bm src/cycni.js dist/cycni.min.js", | ||
"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" | ||
@@ -31,3 +31,9 @@ }, | ||
}, | ||
"homepage": "https://github.com/AlexanderElias/cycni#readme" | ||
"homepage": "https://github.com/AlexanderElias/cycni#readme", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"devDependencies": { | ||
"muleify": "^2.7.10" | ||
} | ||
} |
178
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. | ||
A collection manipulation tool | ||
See the test directory for examples. | ||
### Overview | ||
Cycni enables the ability to manipulation and interact with an infinitely deep collection. It provides an interface to manipulate Objects and Arrays in a seamless and consistent method. Now using async/await. | ||
## Install | ||
- `npm install cycni --save` | ||
### Install | ||
- `npm i cycni --save` | ||
- UMD `dist/cycni.js` | ||
- ESMD `src/cycni.js` | ||
### Examples | ||
```js | ||
let data = { | ||
id: '0', | ||
name: 'Cake', | ||
batters: [ | ||
{ id: 'zero', type: 'Regular' }, | ||
{ id: 'one', type: 'Chocolate' }, | ||
{ id: 'two', type: 'Blueberry' } | ||
] | ||
}; | ||
## Api | ||
let res, opt = { | ||
data: data, | ||
keys: ['batters', 0] | ||
}; | ||
### 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 | ||
try { | ||
res = await Cycni.remove(opt); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
### Cycni.set() | ||
Creates an object or array if the key does not exists. Overwrites if the key exists. | ||
console.log(data); /* | ||
{ | ||
id: '0', | ||
name: 'Cake', | ||
batters: [ | ||
{ id: 'one', type: 'Chocolate' }, | ||
{ id: 'two', type: 'Blueberry' } | ||
] | ||
} | ||
*/ | ||
- `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` | ||
console.log(res); /* | ||
{ id: 'zero', type: 'Regular' } | ||
*/ | ||
``` | ||
### 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. | ||
## Api | ||
- `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.get(opt) | ||
Returns the retrieved value. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### 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.set(opt) | ||
If a key does not exists and the key is a String then an Object is created. | ||
If a key does not exists and the key is a Number then an Array is created. | ||
If the key already exists it overwrites that value. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### 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.add(opt) | ||
Errors if any key in the keys does not exists on the collection or if the you try to set a key that already exists. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### 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.remove(opt) | ||
Returns any removed data. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### Cycni.clone() | ||
- `collection: Object, Array, Date` returns a clone | ||
### Cycni.has(opt) | ||
Returns true or false. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### Cycni.size(opt) | ||
Returns the length of the Array or the length of the Keys if it is an object. | ||
- `opt: Object` | ||
- `keys: Array` if the value is `['.']` then the size of the top level element will be provided | ||
- `value: Any` | ||
- `data: Object, Array` | ||
## Examples | ||
### Cycni.traverse(opt) | ||
Returns an Object with two properties. | ||
The last key in the keys array `res.key`. | ||
The parent of the last key in the keys array `res.data`. | ||
- `opt: Object` | ||
- `keys: Array` | ||
- `value: Any` | ||
- `data: Object, Array` | ||
### Collection | ||
```JavaScript | ||
var collection = { | ||
id: '0', | ||
name: 'Cake', | ||
batters: [{ | ||
id: '0', | ||
type: 'Regular' | ||
}, { | ||
id: '1', | ||
type: 'Chocolate' | ||
}, { | ||
id: 'u', | ||
type: 'Blueberry' | ||
}], | ||
hello: ['bob'] | ||
}; | ||
``` | ||
### Cycni.clone(Any) | ||
Returns a clone. | ||
### Remove | ||
```JavaScript | ||
Cycni.remove(collection, ['batters', '0'], function (error, data) { | ||
if (error) throw error; | ||
## Authors | ||
[AlexanderElias](https://github.com/AlexanderElias) | ||
console.log(collection); | ||
/* | ||
{ id: '0', | ||
name: 'Cake', | ||
batters: | ||
[ { id: '1', type: 'Chocolate' }, | ||
{ id: 'u', type: 'Blueberry' } ], | ||
hello: [ 'bob' ] } | ||
*/ | ||
console.log(data | ||
/* | ||
{ id: '0', type: 'Regular' } | ||
*/ | ||
}); | ||
``` | ||
## License | ||
[Why You Should Choose MPL-2.0](http://veldstra.org/2016/12/09/yoo-should-choose-mpl2-for-your-opensource-project.html) | ||
This project is licensed under the MPL-2.0 License |
213
src/cycni.js
/* | ||
@banner | ||
title: cycni | ||
version: 1.2.0 | ||
version: 2.0.0 | ||
license: mpl-2.0 | ||
@@ -13,144 +13,143 @@ author: alexander elias | ||
var Cycni = { | ||
END: '$END', | ||
START: '$START' | ||
}; | ||
const Cycni = {}; | ||
Cycni.clone = function (variable) { | ||
var clone; | ||
Cycni.traverse = async function (opt) { | ||
if (variable === null || variable === undefined || typeof variable !== 'object') { | ||
if (!opt.keys && opt.keys.length === 1 && opt.keys[0] === '.') { | ||
return { | ||
key: '.', | ||
data: res.data | ||
} | ||
} | ||
return variable; | ||
var data = opt.data; | ||
var keys = opt.keys; | ||
var create = opt.create; | ||
var length = keys.length; | ||
var last = length === 0 ? 0 : length - 1; | ||
} else if (variable.constructor.name === 'Date') { | ||
for (var i = 0; i < last; i++) { | ||
var key = keys[i]; | ||
clone = new Date(); | ||
clone.setTime(variable.getTime()); | ||
} else if (variable.constructor.name === 'Array') { | ||
clone = []; | ||
for (var i = 0, l = variable.length; i < l; i++) { | ||
clone[i] = this.clone(variable[i]); | ||
if (!(key in data)) { | ||
if (create) { | ||
if (isNaN(keys[i+1])) { | ||
data[key] = {}; | ||
} else { | ||
data[key] = []; | ||
} | ||
} else { | ||
throw new Error('Cycni.traverse - property ' + key + ' of undefined'); | ||
} | ||
} | ||
} else if (variable.constructor.name === 'Object') { | ||
clone = {}; | ||
data = data[key]; | ||
} | ||
for (var key in variable) { | ||
return { | ||
key: keys[last], | ||
data: data | ||
}; | ||
}; | ||
if (variable.hasOwnProperty(key)) { | ||
clone[key] = this.clone(variable[key]); | ||
} | ||
Cycni.get = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
return res.data[res.key]; | ||
}; | ||
} | ||
Cycni.set = async function (opt) { | ||
opt.create = true; | ||
const res = await Cycni.traverse(opt); | ||
if (res.data.constructor === Object) { | ||
res.data[res.key] = opt.value; | ||
} else if (res.data.constructor === Array) { | ||
res.data.splice(res.key, 1, opt.value); | ||
} | ||
}; | ||
Cycni.add = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
if (res.data.constructor === Object && !(res.key in res.data)) { | ||
res.data[res.key] = opt.value; | ||
} else if (res.data.constructor === Array && res.key < 1) { | ||
res.data.splice(res.key === 0 ? 0 : res.data.length, 0, opt.value); | ||
} else { | ||
throw new Error('Unable to clone variable. Type is not supported'); | ||
throw new Error('Cycni.add - property ' + res.key + ' exists'); | ||
} | ||
}; | ||
return clone; | ||
Cycni.has = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
return res.key in res.data; | ||
}; | ||
Cycni.traverse = function (collection, keys, callback, create) { | ||
Cycni.remove = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
var key, index = 0; | ||
var length = keys.length; | ||
var last = length === 0 ? 0 : length - 1; | ||
let value; | ||
for (index; index < last; index++) { | ||
key = keys[index]; | ||
if (res.data.constructor === Object) { | ||
value = res.data[res.key]; | ||
delete res.data[res.key]; | ||
} else if (res.data.constructor === Array) { | ||
value = res.data.splice(res.key, 1); | ||
} | ||
if (!(key in collection)) { | ||
if (create) { | ||
if (isNaN(keys[index+1])) { | ||
collection[key] = {}; | ||
} else { | ||
collection[key] = []; | ||
} | ||
} else { | ||
return callback.call(this, new Error('Cannot read property ' + key + ' of undefined')); | ||
} | ||
} | ||
return value; | ||
}; | ||
collection = collection[key]; | ||
Cycni.size = async function (opt) { | ||
const res = await Cycni.traverse(opt); | ||
let data; | ||
if (res.key === '.') { | ||
data = res.data; | ||
} else { | ||
data = res.data[res.key]; | ||
} | ||
return callback.call(this, undefined, collection, keys[last]); | ||
}; | ||
if (data.constructor === Object) { | ||
return Object.keys(data).length; | ||
} | ||
Cycni.get = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
return callback(e, c[k]); | ||
} | ||
}); | ||
if (data.constructor === Array) { | ||
return data.length; | ||
} | ||
return 0; | ||
}; | ||
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); | ||
} | ||
Cycni.clone = async function (variable) { | ||
var clone; | ||
return callback(e); | ||
} | ||
}, true); | ||
}; | ||
if (variable === null || variable === undefined || typeof variable !== 'object') { | ||
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')); | ||
} | ||
} | ||
}); | ||
}; | ||
return variable; | ||
Cycni.has = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
return callback(e, k in c); | ||
} else if (variable.constructor.name === 'Array') { | ||
clone = []; | ||
for (var i = 0, l = variable.length; i < l; i++) { | ||
clone[i] = this.clone(variable[i]); | ||
} | ||
}); | ||
}; | ||
Cycni.remove = function (collection, keys, callback) { | ||
return this.traverse(collection, keys, function (e, c, k) { | ||
if (e) { | ||
return callback(e); | ||
} else { | ||
var v; | ||
} else if (variable.constructor.name === 'Object') { | ||
clone = {}; | ||
if (c.constructor.name === 'Object') { | ||
v = c[k]; | ||
delete c[k]; | ||
} else if (c.constructor.name === 'Array') { | ||
v = c.splice(k, 1); | ||
for (var key in variable) { | ||
if (variable.hasOwnProperty(key)) { | ||
clone[key] = this.clone(variable[key]); | ||
} | ||
return callback(e, v); | ||
} | ||
}); | ||
} else { | ||
throw new Error('Cycni.clone - type is not supported'); | ||
} | ||
return clone; | ||
}; | ||
export default Cycni; |
@@ -1,13 +0,26 @@ | ||
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
'use strict'; | ||
Cycni.add(collection, ['batters', 0], 'new', function (error, result) { | ||
if (error) { | ||
throw error; | ||
} else { | ||
console.log('\n'); | ||
console.log(collection); | ||
console.log('\n'); | ||
console.log(result); | ||
const Cycni = require('../dist/cycni'); | ||
const data = require('./data'); | ||
(async function() { | ||
let res; | ||
const opt = { | ||
data: data, | ||
value: { name: 'new' }, | ||
keys: ['batters', 0] | ||
}; | ||
try { | ||
res = await Cycni.add(opt); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
console.log('\n'); | ||
console.log(data); | ||
console.log('\n'); | ||
console.log(res); | ||
}()); |
@@ -1,13 +0,25 @@ | ||
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
'use strict'; | ||
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); | ||
const Cycni = require('../dist/cycni'); | ||
const data = require('./data'); | ||
(async function() { | ||
let res; | ||
let opt = { | ||
data: data, | ||
keys: ['batters', 0] | ||
}; | ||
try { | ||
res = await Cycni.get(opt); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}); | ||
console.log('\n'); | ||
console.log(data); | ||
console.log('\n'); | ||
console.log(res); | ||
}()); |
@@ -1,13 +0,25 @@ | ||
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
'use strict'; | ||
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); | ||
const Cycni = require('../dist/cycni'); | ||
const data = require('./data'); | ||
(async function() { | ||
let res; | ||
const opt = { | ||
data: data, | ||
keys: ['batters', 0] | ||
}; | ||
try { | ||
res = await Cycni.remove(opt); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
console.log('\n'); | ||
console.log(data); | ||
console.log('\n'); | ||
console.log(res); | ||
}()); |
@@ -1,13 +0,26 @@ | ||
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
'use strict'; | ||
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); | ||
const Cycni = require('../dist/cycni'); | ||
const data = require('./data'); | ||
(async function() { | ||
let res; | ||
const opt = { | ||
data: data, | ||
keys: ['array', 0], | ||
value: { name: 'new' } | ||
}; | ||
try { | ||
res = await Cycni.set(opt); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
console.log('\n'); | ||
console.log(data); | ||
console.log('\n'); | ||
console.log(res); | ||
}()); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
115
36420
1
14
507
1