JS Abstraction via "dit"
dot.get, dot.abstract dot.make
by andypotanin
JavaScript
console.clear();
var dot = {
iterate: function(obj, stack) {
var result = {};
function iterate( obj, stack ) {
for( var property in obj ) {
if( obj.hasOwnProperty( property ) ) {
if( typeof obj[property] == "object" ) {
iterate( obj[property], stack + ( stack ? '.' : '' ) + property );
} else {
result[ stack + '.' + property ] = obj[ property ];
}
}
}
}
iterate( obj, stack || '' );
return result;
},
abstract: function( config ) {
var result = {};
Object.keys( config ).forEach( function( key ) {
dot.make( key, config[ key ], result );
});
return result;
},
get: function( target, path ) {
try {
return !path ? target : path.split( '.' ).reduce( function(o, x) { return o[x]; }, target );
} catch( error ) { return null; }
},
make: function ( string, value, hash ) {
hash = hash || {};
var parts = string.split( '.' );
var refHash = hash;
var depth = 0;
parts.forEach( function( part ) {
var parent = parts[ depth-1 ];
// Second to end
if( depth == parts.length - 2 ) {
//console.log( part );
}
if( 'number' === typeof parseInt( part ) ) {
// console.log( parent );
//console.log( refHash )
//part = parseInt( part );
}
if( depth == parts.length - 1 ) {
refHash[part] = value;
if( 'object' === typeof value ) {
var numeric = Object.keys( value ).reduce( function(...