@segment/isodate-traverse
Advanced tools
Comparing version 1.1.0 to 1.1.1
'use strict'; | ||
var type = require('component-type'); | ||
var each = require('component-each'); | ||
var isodate = require('@segment/isodate'); | ||
@@ -10,18 +8,21 @@ | ||
*/ | ||
module.exports = traverse; | ||
/** | ||
* Traverse an object or array, and return a clone with all ISO strings parsed | ||
* into Date objects. | ||
* Recursively traverse an object or array, and convert | ||
* all ISO date strings parse into Date objects. | ||
* | ||
* @param {Object} obj | ||
* @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 (type(input) === 'object') return object(input, strict); | ||
if (type(input) === 'array') return array(input, strict); | ||
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; | ||
@@ -31,27 +32,12 @@ } | ||
/** | ||
* Object traverser. | ||
* Object traverser helper function. | ||
* | ||
* @param {Object} obj | ||
* @param {Boolean} strict | ||
* @param {Object} obj - object to traverse | ||
* @param {Boolean} strict - only convert strings with year, month, and date | ||
* @return {Object} | ||
*/ | ||
function object(obj, strict) { | ||
// 'each' utility uses obj.length to check whether the obj is array. To avoid incorrect classification, wrap call to 'each' with rename of obj.length | ||
if (obj.length && typeof obj.length === 'number' && !(obj.length - 1 in obj)) { // cross browser compatible way of checking has length and is not array | ||
obj.lengthNonArray = obj.length; | ||
delete obj.length; | ||
} | ||
each(obj, function(key, val) { | ||
if (isodate.is(val, strict)) { | ||
obj[key] = isodate.parse(val); | ||
} else if (type(val) === 'object' || type(val) === 'array') { | ||
traverse(val, strict); | ||
} | ||
function traverseObject(obj, strict) { | ||
Object.keys(obj).forEach(function(key) { | ||
obj[key] = traverse(obj[key], strict); | ||
}); | ||
// restore obj.length if it was renamed | ||
if (obj.lengthNonArray) { | ||
obj.length = obj.lengthNonArray; | ||
delete obj.lengthNonArray; | ||
} | ||
return obj; | ||
@@ -61,18 +47,13 @@ } | ||
/** | ||
* Array traverser. | ||
* Array traverser helper function | ||
* | ||
* @param {Array} arr | ||
* @param {Boolean} strict | ||
* @param {Array} arr - array to traverse | ||
* @param {Boolean} strict - only convert strings with year, month, and date | ||
* @return {Array} | ||
*/ | ||
function array(arr, strict) { | ||
each(arr, function(val, x) { | ||
if (type(val) === 'object') { | ||
traverse(val, strict); | ||
} else if (isodate.is(val, strict)) { | ||
arr[x] = isodate.parse(val); | ||
} | ||
function traverseArray(arr, strict) { | ||
arr.forEach(function(value, index) { | ||
arr[index] = traverse(value, strict); | ||
}); | ||
return arr; | ||
} |
{ | ||
"name": "@segment/isodate-traverse", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Traverse an object and convert all ISO strings into Dates.", | ||
@@ -28,13 +28,14 @@ "keywords": [ | ||
"dependencies": { | ||
"@segment/isodate": "^1.0.0", | ||
"component-each": "^0.2.6", | ||
"component-type": "^1.2.1" | ||
"@segment/isodate": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@segment/eslint-config": "^3.1.1", | ||
"@segment/to-iso-string": "0.0.2", | ||
"browserify": "^13.0.0", | ||
"browserify-istanbul": "^2.0.0", | ||
"codecov": "^3.0.2", | ||
"equals": "^1.0.1", | ||
"eslint": "^2.9.0", | ||
"eslint-plugin-mocha": "^2.2.0", | ||
"eslint-plugin-react": "^4.3.0", | ||
"eslint-plugin-require-path-exists": "^1.1.5", | ||
@@ -53,5 +54,4 @@ "istanbul": "^0.4.3", | ||
"proclaim": "^3.4.1", | ||
"to-iso-string": "0.0.2", | ||
"watchify": "^3.7.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var traverse = require('../lib'); | ||
var toISOString = require('to-iso-string'); | ||
var toISOString = require('@segment/to-iso-string'); | ||
@@ -8,0 +8,0 @@ describe('isodate-traverse', function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14457
1
0
23
226
- Removedcomponent-each@^0.2.6
- Removedcomponent-type@^1.2.1
- Removedcomponent-each@0.2.6(transitive)
- Removedcomponent-props@1.1.1(transitive)
- Removedcomponent-type@1.0.01.2.2(transitive)
- Removedto-function@2.0.6(transitive)
Updated@segment/isodate@^1.0.3