json-dup-key-validator
Advanced tools
Comparing version 1.0.1 to 1.0.2
57
index.js
@@ -16,2 +16,3 @@ var backslash = require('backslash'); | ||
var error; | ||
allowDuplicatedKeys = allowDuplicatedKeys || false; | ||
if (typeof jsonString !== 'string') { | ||
@@ -22,3 +23,3 @@ error = 'Input must be a string'; | ||
// Try to find a value starting from index 0 | ||
_findValue(jsonString, 0, allowDuplicatedKeys); | ||
_findValue(jsonString, 0, allowDuplicatedKeys, false); | ||
} catch(e) { | ||
@@ -41,4 +42,7 @@ error = e.message; | ||
} | ||
allowDuplicatedKeys = allowDuplicatedKeys || false; | ||
// Try to find a value starting from index 0 | ||
var value = _findValue(jsonString, 0, allowDuplicatedKeys); | ||
var value = _findValue(jsonString, 0, allowDuplicatedKeys, true); | ||
return value.value; | ||
@@ -124,3 +128,3 @@ } | ||
*/ | ||
function _findValue(str, startInd, allowDuplicatedKeys) { | ||
function _findValue(str, startInd, allowDuplicatedKeys, parse) { | ||
var len = str.length; | ||
@@ -134,2 +138,3 @@ var valueStartInd; | ||
var dotFound = false; | ||
var whiteSpaceInNumber = false; | ||
var value; | ||
@@ -168,2 +173,4 @@ | ||
isNumber = true; | ||
} else if (ch === '-') { | ||
isNumber = true; | ||
} else { | ||
@@ -176,3 +183,3 @@ throw _syntaxError(str, i, ''); | ||
if (isArray) { | ||
var arr = _findArray(str, i); | ||
var arr = _findArray(str, i, allowDuplicatedKeys, parse); | ||
valueEndInd = arr.end; | ||
@@ -182,3 +189,3 @@ value = arr.value; | ||
} else if (isObject) { | ||
var obj = _findObject(str, i, allowDuplicatedKeys); | ||
var obj = _findObject(str, i, allowDuplicatedKeys, parse); | ||
valueEndInd = obj.end; | ||
@@ -192,10 +199,12 @@ value = obj.value; | ||
} else if (isNumber) { | ||
if (_isNumber(ch)) { | ||
continue; | ||
} else if (ch === '.' && !dotFound) { | ||
dotFound = true; | ||
} else if (_isWhiteSpace(ch) || ch === ',' || ch === ']' || ch === '}') { | ||
if(_isWhiteSpace(ch)) { | ||
whiteSpaceInNumber = true; | ||
} else if (ch === ',' || ch === ']' || ch === '}') { | ||
value = parseFloat(str.substring(valueStartInd, valueEndInd), 10); | ||
valueEndInd = i - 1; | ||
break; | ||
} else if (_isNumber(ch) && !whiteSpaceInNumber) { | ||
continue; | ||
} else if (ch === '.' && !dotFound && !whiteSpaceInNumber) { | ||
dotFound = true; | ||
} else { | ||
@@ -276,6 +285,8 @@ throw _syntaxError(str, i, 'expecting number'); | ||
*/ | ||
function _findObject(str, startInd, allowDuplicatedKeys) { | ||
function _findObject(str, startInd, allowDuplicatedKeys, parse) { | ||
var i = startInd; | ||
var sepValue = ','; | ||
var obj = {}; | ||
var keys = []; | ||
var values = []; | ||
@@ -298,14 +309,23 @@ var j = startInd; | ||
var semi = _findSemiColonSeparator(str, key.end); | ||
var value = _findValue(str, semi.end); | ||
var value = _findValue(str, semi.end, allowDuplicatedKeys, parse); | ||
var sepIndex = _findSeparator(str, value.end); | ||
if (!allowDuplicatedKeys) { | ||
if(obj[key.value] !== undefined) { | ||
if(keys.indexOf(key.value) !== -1) { | ||
throw _syntaxError(str, key.end, 'duplicated keys "' + key.value + '"'); | ||
} | ||
} | ||
obj[key.value] = value.value; | ||
keys.push(key.value); | ||
values.push(value.value); | ||
i = sepIndex.end; | ||
sepValue = sepIndex.value; | ||
} | ||
if (parse) { | ||
var indx = 0; | ||
for(indx = 0; indx < keys.length; indx++) { | ||
obj[keys[indx]] = values[indx]; | ||
} | ||
} | ||
return { | ||
@@ -343,3 +363,3 @@ start: startInd, | ||
*/ | ||
function _findArray(str, startInd) { | ||
function _findArray(str, startInd, allowDuplicatedKeys, parse) { | ||
var i = startInd; | ||
@@ -363,5 +383,8 @@ var sepValue = ','; | ||
while (sepValue === ',') { | ||
var value = _findValue(str, i); | ||
var value = _findValue(str, i, allowDuplicatedKeys, parse); | ||
var sepIndex = _findSeparator(str, value.end); | ||
arr.push(value.value); | ||
if (parse) { | ||
arr.push(value.value); | ||
} | ||
i = sepIndex.end; | ||
@@ -368,0 +391,0 @@ sepValue = sepIndex.value; |
{ | ||
"name": "json-dup-key-validator", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,4 +7,6 @@ [ | ||
"a string", | ||
123, | ||
123 , | ||
-123, | ||
23.455, | ||
-23.455, | ||
{ | ||
@@ -11,0 +13,0 @@ "k": |
@@ -34,3 +34,5 @@ var fs = require('fs'); | ||
123, | ||
-123, | ||
23.455, | ||
-23.455, | ||
{ | ||
@@ -66,2 +68,7 @@ "k": "v" | ||
expectedValue: 123.456 | ||
}, { | ||
path: './test/fixture/valid-to-string.json', | ||
expectedValue: { | ||
toString: 'asd' | ||
} | ||
}]; | ||
@@ -84,2 +91,8 @@ | ||
expectedError: 'Syntax error: expecting \':\' near d"": 123\n}' | ||
}, { | ||
path: './test/fixture/invalid-number-space.json', | ||
expectedError: 'Syntax error: expecting number near 3.112 24\n]' | ||
}, { | ||
path: './test/fixture/invalid-number-negative-negative.json', | ||
expectedError: 'Syntax error: expecting number near 3.112-24\n]' | ||
}]; | ||
@@ -86,0 +99,0 @@ |
19428
17
608