Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
Expand Down