@segment/isodate-traverse
Version:
Traverse an object and convert all ISO strings into Dates.
57 lines (51 loc) • 1.4 kB
JavaScript
var isodate = require('@segment/isodate');
/**
* Expose `traverse`.
*/
module.exports = traverse;
/**
* Recursively traverse an object or array, and convert
* all ISO date strings parse into Date objects.
*
* @param {Object} input - object, array, or string to convert
* @param {Boolean} strict - only convert strings with year, month, and date
* @return {Object}
*/
function traverse(input, strict) {
if (strict === undefined) strict = true;
if (input && typeof input === 'object') {
return traverseObject(input, strict);
} else if (Array.isArray(input)) {
return traverseArray(input, strict);
} else if (isodate.is(input, strict)) {
return isodate.parse(input);
}
return input;
}
/**
* Object traverser helper function.
*
* @param {Object} obj - object to traverse
* @param {Boolean} strict - only convert strings with year, month, and date
* @return {Object}
*/
function traverseObject(obj, strict) {
Object.keys(obj).forEach(function(key) {
obj[key] = traverse(obj[key], strict);
});
return obj;
}
/**
* Array traverser helper function
*
* @param {Array} arr - array to traverse
* @param {Boolean} strict - only convert strings with year, month, and date
* @return {Array}
*/
function traverseArray(arr, strict) {
arr.forEach(function(value, index) {
arr[index] = traverse(value, strict);
});
return arr;
}
;