Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An array index number #7

Merged
merged 9 commits into from
Oct 18, 2016
Merged
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
106 changes: 55 additions & 51 deletions form-urlencoded.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
// Filename: formurlencoded.js
// Timestamp: 2016.03.07-12:29:28 (last modified)
// Author(s): Bumblehead (www.bumblehead.com), JBlashill ([email protected])
// Author(s): Bumblehead (www.bumblehead.com), JBlashill ([email protected]), Jumper423 ([email protected])
//
// http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
// input: {one:1,two:2} return: '[one]=1&[two]=2'

var formurlencoded = module.exports = function (data, opts) {
// ES5 compatible version of `/[^ !'()~\*]/gu`, https://mothereff.in/regexpu
var encodechar = new RegExp([
'(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|',
'[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|',
'(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])'
].join(''), 'g');
module.exports = function (data, opts) {
"use strict";

opts = typeof opts === 'object' ? opts : {};

function encode (value) {
return String(value)
.replace(encodechar, encodeURIComponent)
.replace(/ /g, '+')
.replace(/[!'()~\*]/g, function (ch) {
return '%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase();
});
}
// ES5 compatible version of `/[^ !'()~\*]/gu`, https://mothereff.in/regexpu
var encodechar = new RegExp([
'(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|',
'[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|',
'(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])'
].join(''), 'g');

function keys (obj) {
var keys = Object.keys(obj);
opts = typeof opts === 'object' ? opts : {};

return opts.sorted ? keys.sort() : keys;
}
function encode(value) {
return String(value)
.replace(encodechar, encodeURIComponent)
.replace(/ /g, '+')
.replace(/[!'()~\*]/g, function (ch) {
return '%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase();
});
}

function filterjoin (arr) {
return arr.filter(function (e) { return e; }).join('&');
}
function keys(obj) {
var itemsKeys = Object.keys(obj);

function objnest (name, obj) {
return filterjoin(keys(obj).map(function (key) {
return nest(name + '[' + key + ']', obj[key]);
}));
}
return opts.sorted ? itemsKeys.sort() : itemsKeys;
}

function arrnest (name, arr) {
return arr.length ? filterjoin(arr.map(function (elem) {
return nest(name + '[]', elem);
})) : encode(name + '[]');
}
function filterjoin(arr) {
return arr.filter(function (e) {
return e;
}).join('&');
}

function nest (name, value) {
var type = typeof value,
f = null;
function objnest(name, obj) {
return filterjoin(keys(obj).map(function (key) {
return nest(name + '[' + key + ']', obj[key]);
}));
}

if (value === f) {
f = opts.ignorenull ? f : encode(name) + '=' + f;
} else if (/string|number|boolean/.test(type)) {
f = encode(name) + '=' + encode(value);
} else if (Array.isArray(value)) {
f = arrnest(name, value);
} else if (type === 'object') {
f = objnest(name, value);
function arrnest(name, arr) {
return arr.length ? filterjoin(arr.map(function (elem, index) {
return nest(name + '[' + index + ']', elem);
})) : encode(name + '[]');
}

return f;
}
function nest(name, value) {
var type = typeof value,
f = null;

if (value === f) {
f = opts.ignorenull ? f : encode(name) + '=' + f;
} else if (/string|number|boolean/.test(type)) {
f = encode(name) + '=' + encode(value);
} else if (Array.isArray(value)) {
f = arrnest(name, value);
} else if (type === 'object') {
f = objnest(name, value);
}

return f;
}

return data && filterjoin(keys(data).map(function (key) {
return nest(key, data[key]);
}));
return data && filterjoin(keys(data).map(function (key) {
return nest(key, data[key]);
}));
};
10 changes: 5 additions & 5 deletions test/form-urlencoded.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("formurlencoded.encode", function () {
propStr2 : 'str2',
propArr1 : ['arrStr1', 'arrStr2']
})
).toBe( 'propStr1=str1&propStr2=str2&propArr1%5B%5D=arrStr1&propArr1%5B%5D=arrStr2' );
).toBe( 'propStr1=str1&propStr2=str2&propArr1%5B0%5D=arrStr1&propArr1%5B1%5D=arrStr2' );
});

it("should return encoded data, with object properties", function () {
Expand Down Expand Up @@ -57,19 +57,19 @@ describe("formurlencoded.encode", function () {
propArr1Obj2Str1 : 'obj2Str1'
}]
}
}) ).toBe( 'propStr1=str1&propStr2=str2&propObj1%5BobjPropStr1%5D=objStr1&propObj1%5BobjPropStr2%5D=objStr2&propObj1%5BobjPropObj1%5D%5BpropObj1Str1%5D=obj1Str1&propObj1%5BobjPropArr1%5D%5B%5D%5BpropArr1Obj1Str1%5D=obj1Str1&propObj1%5BobjPropArr1%5D%5B%5D%5BpropArr1Obj2Str1%5D=obj2Str1' );
}) ).toBe( 'propStr1=str1&propStr2=str2&propObj1%5BobjPropStr1%5D=objStr1&propObj1%5BobjPropStr2%5D=objStr2&propObj1%5BobjPropObj1%5D%5BpropObj1Str1%5D=obj1Str1&propObj1%5BobjPropArr1%5D%5B0%5D%5BpropArr1Obj1Str1%5D=obj1Str1&propObj1%5BobjPropArr1%5D%5B1%5D%5BpropArr1Obj2Str1%5D=obj2Str1' );
});

it("should return encoded data, with numbers", function () {
expect(
formurlencoded({ propArr1 : [1, 2, 3] })
).toBe( 'propArr1%5B%5D=1&propArr1%5B%5D=2&propArr1%5B%5D=3' );
).toBe( 'propArr1%5B0%5D=1&propArr1%5B1%5D=2&propArr1%5B2%5D=3' );
});

it("should return encoded data, with booleans", function () {
expect(
formurlencoded({propArr1 : [true, false, true]})
).toBe( 'propArr1%5B%5D=true&propArr1%5B%5D=false&propArr1%5B%5D=true' );
).toBe( 'propArr1%5B0%5D=true&propArr1%5B1%5D=false&propArr1%5B2%5D=true' );
});

it("should return encoded data, with null", function () {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("formurlencoded.encode", function () {
it("should return encoded data, without null", function () {
expect(
formurlencoded({propArr1 : [null, null, 1]}, {ignorenull : true})
).toBe( 'propArr1%5B%5D=1' );
).toBe( 'propArr1%5B2%5D=1' );
});

it("should return the correct test result", function () {
Expand Down