Comparing version 1.1.5 to 1.1.6
@@ -10,3 +10,3 @@ (function (global, factory) { | ||
title: cycni | ||
version: 1.1.5 | ||
version: 1.1.6 | ||
license: mpl-2.0 | ||
@@ -16,3 +16,5 @@ author: alexander elias | ||
var cycni_b = { | ||
SET: 2, | ||
@@ -50,3 +52,3 @@ clone: function (variable) { | ||
traverse: function (collection, path, callback) { | ||
traverse: function (collection, path, callback, type) { | ||
path = typeof path === 'number' ? path.toString() : path; | ||
@@ -63,3 +65,11 @@ path = typeof path === 'string' ? this.paths(path) : path; | ||
if (!(key in collection)) { | ||
return callback.call(this, collection, key, false); | ||
if (type === this.SET) { | ||
if (isNaN(path[index+1])) { | ||
collection = collection[key] = {}; | ||
} else { | ||
collection = collection[key] = []; | ||
} | ||
} else { | ||
return callback.call(this, collection, key, false); | ||
} | ||
} else if (index === last) { | ||
@@ -77,3 +87,3 @@ return callback.call(this, collection, key, true); | ||
return c[k] = value; | ||
}); | ||
}, this.SET); | ||
}, | ||
@@ -80,0 +90,0 @@ |
/* | ||
@preserve | ||
title: cycni | ||
version: 1.1.5 | ||
version: 1.1.6 | ||
license: mpl-2.0 | ||
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{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){d='number'==typeof d?d.toString():d,d='string'==typeof d?this.paths(d):d;var g,h=0,j=d.length,m=0===j?0:j-1;for(h;h<j;h++){if(g=d[h],!(g in b))return f.call(this,b,g,!1);if(h==m)return f.call(this,b,g,!0);b=b[g]}},set:function(b,d,f){return this.traverse(b,d,function(g,h){return g[h]=f})},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)})}}}); | ||
(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(j==n)return f.call(this,b,h,!0);b=b[h]}else if(g===this.SET)b=isNaN(d[j+1])?b[h]={}:b[h]=[];else return f.call(this,b,h,!1)},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)})}}}); |
{ | ||
"name": "cycni", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Collection manipulation tool.", | ||
@@ -5,0 +5,0 @@ "browser": "dist/cycni.min.js", |
@@ -19,3 +19,3 @@ # Cycni | ||
## Cycni.get | ||
- collection `Object, Array` (Infinitely nested and combinations) | ||
- collection `Object, Array` infinitely nested and combinations | ||
- path `String, Array` key names to destination on object or array | ||
@@ -25,3 +25,3 @@ | ||
## Cycni.set | ||
- collection `Object, Array` infinitely nested and combinations | ||
- 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 | ||
@@ -28,0 +28,0 @@ - value `Any` the value to assign |
/* | ||
@preserve | ||
title: cycni | ||
version: 1.1.5 | ||
version: 1.1.6 | ||
license: mpl-2.0 | ||
@@ -9,3 +9,5 @@ author: alexander elias | ||
export default { | ||
SET: 2, | ||
@@ -43,3 +45,3 @@ clone: function (variable) { | ||
traverse: function (collection, path, callback) { | ||
traverse: function (collection, path, callback, type) { | ||
path = typeof path === 'number' ? path.toString() : path; | ||
@@ -56,3 +58,11 @@ path = typeof path === 'string' ? this.paths(path) : path; | ||
if (!(key in collection)) { | ||
return callback.call(this, collection, key, false); | ||
if (type === this.SET) { | ||
if (isNaN(path[index+1])) { | ||
collection = collection[key] = {}; | ||
} else { | ||
collection = collection[key] = []; | ||
} | ||
} else { | ||
return callback.call(this, collection, key, false); | ||
} | ||
} else if (index === last) { | ||
@@ -70,3 +80,3 @@ return callback.call(this, collection, key, true); | ||
return c[k] = value; | ||
}); | ||
}, this.SET); | ||
}, | ||
@@ -73,0 +83,0 @@ |
var Cycni = require('../dist/cycni'); | ||
var collection = require('./collection'); | ||
var results = Cycni.set(collection, 'batters.0', {}); | ||
var results = Cycni.set(collection, 'arr.0.obj', {}); | ||
@@ -6,0 +6,0 @@ console.log('\n'); |
34895
469