Skip to content

Commit

Permalink
Add support for parse custom separators (resolves #3).
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiquelhas committed Sep 19, 2014
1 parent 5fd33d6 commit 339b541
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
30 changes: 17 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ exports = module.exports = {};

var internals = {};

internals.customize = function (del, sep) {
del = del && !string(del).isAlphaNumeric() &&
del !== sep ? del : '.';

sep = sep && !string(sep).isAlphaNumeric() &&
sep !== del ? sep : '&';

return { del: del, sep: sep };
};

internals.isArray = function (obj) {
for (var key in obj) {
if (string(key).isNumeric()) {
Expand Down Expand Up @@ -59,32 +69,26 @@ internals.parse = function (obj) {
}(0));
};

exports.parse = function (nvp, del) {
del = del && !string(del).isAlphaNumeric() ? del : '.';
exports.parse = function (nvp, del, sep) {
var custom = internals.customize(del, sep);
var transformable = qs.parse(nvp, custom.sep);

var transformable = qs.parse(nvp);

var parser = new Parser(del);
var parser = new Parser(custom.del);
parser.object(transformable);

return internals.parse(transformable);
};

exports.stringify = function (input, del, sep) {
var output = {};

del = del && !string(del).isAlphaNumeric() &&
del !== sep ? del : '.';

sep = sep && !string(sep).isAlphaNumeric() &&
sep !== del ? sep : '&';
var custom = internals.customize(del, sep);

function _stringify(obj, current) {
var value, dotNotationKey;

for (var key in obj) {
value = obj[key];
dotNotationKey = (current ? [current, key].join(del) : key);
dotNotationKey = (current ? [current, key].join(custom.del) : key);

if (_.isObject(value)) {
_stringify(value, dotNotationKey);
Expand All @@ -96,5 +100,5 @@ exports.stringify = function (input, del, sep) {
return output;
}

return qs.stringify(_stringify(input), sep);
return qs.stringify(_stringify(input), custom.sep);
};
12 changes: 2 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ lab.experiment('NVP encoded querystring utility belt', function () {

done();
});

lab.test('returns matching object using a custom delimiter', function (done) {
var input = qs.stringify(nvp_stub).replace(/\./g, '-');
var output = nvpqs.parse(input, '-');

Lab.expect(output).to.be.a('object');
Lab.expect(output).to.deep.equal(object_stub);

done();
});
});

lab.experiment('stringifies a JavaScript object to a proper querystring and', function () {
Expand All @@ -55,7 +45,9 @@ lab.experiment('NVP encoded querystring utility belt', function () {

done();
});
});

lab.experiment('allows custom notitation delimiter and separator characters', function() {
lab.test('returns a matching querystring using a custom delimiter character', function (done) {
var output = nvpqs.stringify(object_stub, '-');

Expand Down

0 comments on commit 339b541

Please sign in to comment.