Skip to content

Commit

Permalink
Merge pull request #2 from JankGaming/support-arrays
Browse files Browse the repository at this point in the history
Support arrays
  • Loading branch information
twisterghost committed Feb 21, 2016
2 parents 070d443 + a6394f8 commit a67cc18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ node_modules

out.json
*.txt

test.vdf
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/JankGaming/vdfjs",
"dependencies": {
"lodash": "^3.10.0",
"lodash": "^4.5.0",
"minimist": "^1.1.1"
}
}
27 changes: 25 additions & 2 deletions vdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@ 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();
line = line.replace(/\/\/.+/, '');

line = stripComments(line);
var match;

if (match = line.match(levelRegex)) {
currentLocation.push(match[1]);
var matchedKey = match[1];
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){
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]);
}
Expand Down

0 comments on commit a67cc18

Please sign in to comment.