diff --git a/main.js b/main.js index e8bf96d..8d14a4e 100644 --- a/main.js +++ b/main.js @@ -112,11 +112,12 @@ if (type === 'string') { // The index of that word in the dictionary - var index = _indexOf.call(dictionary.strings, item); + var encodedItem = _encode(item); + var index = _indexOf.call(dictionary.strings, encodedItem); // If not, add to the dictionary and actualize the index if (index == -1) { - dictionary.strings.push(_encode(item)); + dictionary.strings.push(encodedItem); index = dictionary.strings.length - 1; } diff --git a/test/test.js b/test/test.js index 99f962d..861cf3d 100644 --- a/test/test.js +++ b/test/test.js @@ -27,6 +27,12 @@ describe('jsonpack', function() { }, deepObjectPacked = "attr1|string|hello|integer|float|true|false|null|attr2|attr3^1J9|1J9|1J9|1J9^1.2^$0|$1|2|3|A|4|E|5|-1|6|-2|7|-3]|8|$1|2|3|B|4|E|5|-1|6|-2|7|-3]|9|@$1|2|3|C|4|E|5|-1|6|-2|7|-3]|$1|2|3|D|4|E|5|-1|6|-2|7|-3]]]"; + var escapedCharacterObject = { + attr1: " ", + attr2: "+", + }, + escapedCharacterObjectPacked = "attr1|+|attr2|%2B^^^$0|1|2|3]"; + var arrayObject = [ plainObject, deepObject @@ -71,6 +77,10 @@ describe('jsonpack', function() { expect(jsonpack.pack(deepObject)).to.eql(deepObjectPacked); }); + it('escaped string object', function() { + expect(jsonpack.pack(escapedCharacterObject)).to.eql(escapedCharacterObjectPacked); + }); + it('complex array object', function() { expect(jsonpack.pack(arrayObject)).to.eql(arrayObjectPacked); });