From dfb40ce38c8d0ad33420e88d8aa8cfbaf69415ac Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Sun, 21 Feb 2016 17:27:38 -0500 Subject: [PATCH 1/3] Support object arrays --- .gitignore | 2 ++ vdf.js | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2633e57..3913aa1 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ node_modules out.json *.txt + +test.vdf \ No newline at end of file diff --git a/vdf.js b/vdf.js index 5c32cff..0fe3d35 100644 --- a/vdf.js +++ b/vdf.js @@ -9,11 +9,25 @@ var propertyRegex = /^"([A-Za-z1-9_\-]*)"[\t\ ]+"(.*)"$/; function parseLine(line) { line = line.trim(); - line = line.replace(/\/\/.+/, ''); + + line = stripComments(line); var match; if (match = line.match(levelRegex)) { - currentLocation.push(match[1]); + var matchedKey = match[1]; + var nextLocation = _.clone(currentLocation); + nextLocation.push(matchedKey); + nextLocation = nextLocation.join('.'); + var checkKey = _.get(obj, nextLocation); + if (checkKey && !_.isArray(checkKey)) { + convertToArray(nextLocation); + currentLocation.push(matchedKey + '[1]'); + } else if (checkKey && _.isArray(checkKey)){ + nextLocationLength = _.get(obj, nextLocation).length; + currentLocation.push(matchedKey + '[' + nextLocationLength + ']'); + } else { + currentLocation.push(match[1]); + } } else if (match = line.match(propertyRegex)) { _.set(obj, currentLocation.join('.') + '.' + match[1], match[2]); } @@ -24,6 +38,15 @@ function parseLine(line) { } +function convertToArray(location) { + var currentValue = _.get(obj, location); + _.set(obj, location, [currentValue]); +} + +function stripComments(line) { + return line.replace(/\/\/.+/, ''); +} + function parse(vdf) { obj = {}; From e6b01f326ea85d1216b191e258826fcf8c38a6f8 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Sun, 21 Feb 2016 17:34:12 -0500 Subject: [PATCH 2/3] Clean up code --- package.json | 2 +- vdf.js | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 891e599..d52b528 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "homepage": "https://github.com/JankGaming/vdfjs", "dependencies": { - "lodash": "^3.10.0", + "lodash": "^4.5.0", "minimist": "^1.1.1" } } diff --git a/vdf.js b/vdf.js index 0fe3d35..aa0a628 100644 --- a/vdf.js +++ b/vdf.js @@ -6,6 +6,15 @@ var obj = {}; var levelRegex = /^"([A-Za-z0-9\_\-]*)"$/; var propertyRegex = /^"([A-Za-z1-9_\-]*)"[\t\ ]+"(.*)"$/; +function convertToArray(location) { + var currentValue = _.get(obj, location); + _.set(obj, location, [currentValue]); +} + +function stripComments(line) { + return line.replace(/\/\/.+/, ''); +} + function parseLine(line) { line = line.trim(); @@ -15,14 +24,14 @@ function parseLine(line) { if (match = line.match(levelRegex)) { var matchedKey = match[1]; - var nextLocation = _.clone(currentLocation); - nextLocation.push(matchedKey); - nextLocation = nextLocation.join('.'); + var nextLocation = _.chain(currentLocation).clone().push(matchedKey).value().join('.'); + + // If this key already exists, convert it to an array or append to the array var checkKey = _.get(obj, nextLocation); if (checkKey && !_.isArray(checkKey)) { convertToArray(nextLocation); currentLocation.push(matchedKey + '[1]'); - } else if (checkKey && _.isArray(checkKey)){ + } else if (checkKey){ nextLocationLength = _.get(obj, nextLocation).length; currentLocation.push(matchedKey + '[' + nextLocationLength + ']'); } else { @@ -38,15 +47,6 @@ function parseLine(line) { } -function convertToArray(location) { - var currentValue = _.get(obj, location); - _.set(obj, location, [currentValue]); -} - -function stripComments(line) { - return line.replace(/\/\/.+/, ''); -} - function parse(vdf) { obj = {}; From a6394f818b4e2f1fcbd61b5d3d62eeeb7f6276c0 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Sun, 21 Feb 2016 17:34:43 -0500 Subject: [PATCH 3/3] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d52b528..544796f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vdfjs", - "version": "1.0.0", + "version": "1.1.0", "description": "Parser for Valve's KeyValue data files - converts VDF to JSON", "main": "vdf.js", "scripts": {