Created
November 23, 2014 20:26
-
-
Save slaykovsky/7e298ed40e828575cfa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var util = require('util'); | |
var async = require('async'); | |
var randomInt = function(low, high) { | |
return Math.floor(Math.random() * (high - low) + low); | |
}; | |
var find = function(key, object, callback) { | |
console.log('----------------'); | |
console.log('In find function'); | |
async.each(object, function(element, done) { | |
console.log('Trying to find key: %s in %s', key, util.inspect(element, {depth: null})); | |
if (element.key === key) { | |
console.log('Key found!: %s, value: %s', key, element.val); | |
done(); | |
return callback(null, true, element.val); | |
} | |
}, function(err) { | |
if (err) console.log('ERROR ' + err); | |
console.log('All elements has been proceeded!'); | |
}); | |
console.log('Exiting from find function'); | |
console.log('--------------------------'); | |
}; | |
var getValue = function(key, obj, callback) { | |
// callback(null, 'Not Working!'); | |
// return; | |
var delay = randomInt(300, 1500); | |
console.log('Start getVal "' + key + '", delay ' + delay + 'ms'); | |
setTimeout(function() { | |
// Find key there | |
find(key, obj, function(err, result, val) { | |
if (result) { | |
callback(null, val); | |
} else { | |
console.error(err); | |
} | |
}); | |
}, delay); | |
console.log('End getVal "' + key + '", delay ' + delay + 'ms'); | |
}; | |
var getKey = function(map, callback) { | |
console.log('In getKey function'); | |
var keys = Object.keys(map); | |
async.each(keys, function(key, done) { | |
if (typeof key === 'object') getKey(key, callback); | |
done(); | |
return callback(null, key); | |
}, function(err) { | |
if (err) console.log('Some error!'); | |
console.log('All keys has been proceed!'); | |
}); | |
console.log('Exit getKey function'); | |
}; | |
var transformAsync = function(objects, map, callback) { | |
var counter = 0; | |
console.log('Function transformAsync is started.'); | |
console.log('-----------------------------------'); | |
getKey(map, function(err, key) { | |
if (typeof map[key] === 'object') { | |
getKey(map[key], function(err, nestedKey) { | |
counter++; | |
getValue(map[key][nestedKey], objects, function(err, value) { | |
map[key][nestedKey] = value; | |
if (--counter === 0) callback(null, map); | |
}); | |
}); | |
} else { | |
counter++; | |
getValue(map[key], objects, function(err, value) { | |
map[key] = value; | |
if (--counter === 0) { | |
callback(null, map); | |
} | |
}); | |
} | |
}); | |
console.log('------------------------'); | |
console.log('Function async is ended.'); | |
}; | |
transformAsync(require('./data.json'), require('./map.json'), function(err, map) { | |
console.log('------ RESULT ------'); | |
console.log(util.inspect(map, {depth: null})); | |
console.log('----- SUCCESS! -----'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment