form-urlencoded
Advanced tools
Comparing version 2.0.0 to 2.0.1
// Filename: formurlencoded.js | ||
// Timestamp: 2017.07.04-14:35:29 (last modified) | ||
// Timestamp: 2017.07.04-19:19:11 (last modified) | ||
// Author(s): Bumblehead (www.bumblehead.com), JBlashill (james@blashill.com), Jumper423 (jump.e.r@yandex.ru) | ||
@@ -13,15 +13,10 @@ // | ||
// ES5 compatible version of `/[^ !'()~\*]/gu`, https://mothereff.in/regexpu | ||
encodechar = new RegExp([ | ||
'(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|', | ||
'[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|', | ||
'(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])' | ||
].join(''), 'g'), | ||
// ES5 compatible version of `/[^ !'()~\*]/gu`, https://mothereff.in/regexpu | ||
encodechar = /(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, | ||
encode = value => | ||
String(value) | ||
.replace(encodechar, encodeURIComponent) | ||
.replace(/ /g, '+') | ||
.replace(/[!'()~\*]/g, ch => | ||
'%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase()), | ||
encode = value => String(value) | ||
.replace(encodechar, encodeURIComponent) | ||
.replace(/ /g, '+') | ||
.replace(/[!'()~\*]/g, ch => | ||
'%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase()), | ||
@@ -31,4 +26,3 @@ keys = (obj, keyarr = Object.keys(obj)) => | ||
filterjoin = arr => | ||
arr.filter(e => e).join('&'), | ||
filterjoin = arr => arr.filter(e => e).join('&'), | ||
@@ -39,24 +33,17 @@ objnest = (name, obj) => | ||
arrnest = (name, arr) => | ||
arr.length | ||
? filterjoin(arr.map((elem, index) => | ||
skipIndex | ||
? nest(name + '[]', elem) | ||
: nest(name + '[' + index + ']', elem) | ||
)) | ||
: encode(name + '[]'), | ||
arrnest = (name, arr) => arr.length | ||
? filterjoin(arr.map((elem, index) => skipIndex | ||
? nest(name + '[]', elem) | ||
: nest(name + '[' + index + ']', elem))) | ||
: encode(name + '[]'), | ||
nest = (name, value) => { | ||
let type = typeof value, | ||
f = null; | ||
if (value === f) { | ||
nest = (name, value, type = typeof value, f = null) => { | ||
if (value === f) | ||
f = ignorenull ? f : encode(name) + '=' + f; | ||
} else if (/string|number|boolean/.test(type)) { | ||
else if (/string|number|boolean/.test(type)) | ||
f = encode(name) + '=' + encode(value); | ||
} else if (Array.isArray(value)) { | ||
else if (Array.isArray(value)) | ||
f = arrnest(name, value); | ||
} else if (type === 'object') { | ||
else if (type === 'object') | ||
f = objnest(name, value); | ||
} | ||
@@ -66,5 +53,3 @@ return f; | ||
return data && filterjoin(keys(data).map(key => | ||
nest(key, data[key]) | ||
)); | ||
return data && filterjoin(keys(data).map(key => nest(key, data[key]))); | ||
}; |
{ | ||
"name": "form-urlencoded", | ||
"main": "form-urlencoded", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Return an object as an 'x-www-form-urlencoded' string", | ||
@@ -6,0 +6,0 @@ "author": "Chris <chris@bumblehead.com>", |
@@ -19,12 +19,13 @@ form-urlencoded | ||
console.log(formurlencoded(obj)); | ||
// str=val&num=0&arr%5B%5D=3&arr%5B%5D%5Bprop%5D=false&arr% | ||
// 5B%5D=1&arr%5B%5D=null&arr%5B%5D=6&obj%5Bprop1%5D=null&o | ||
// bj%5Bprop2%5D%5B%5D=elem | ||
// str=val&num=0&arr%5B0%5D=3&arr%5B1%5D%5Bprop%5D=false | ||
// &arr%5B2%5D=1&arr%5B3%5D=null&arr%5B4%5D=6&obj%5Bprop | ||
// 1%5D=null&obj%5Bprop2%5D%5B0%5D=elem | ||
console.log(formurlencoded(obj, { | ||
ignorenull : true, | ||
skipIndex : true, | ||
sorted : true | ||
})); | ||
// arr%5B%5D=3&arr%5B%5D%5Bprop%5D=false&arr%5B%5D=1&arr%5B | ||
// %5D=6&num=0&obj%5Bprop2%5D%5B%5D=elem&str=val | ||
// arr%5B%5D=3&arr%5B%5D%5Bprop%5D=false&arr%5B%5D=1&arr | ||
// %5B%5D=6&num=0&obj%5Bprop2%5D%5B%5D=elem&str=val | ||
``` | ||
@@ -31,0 +32,0 @@ |
// Filename: form-urlencoded.spec.js | ||
// Timestamp: 2017.07.04-14:46:35 (last modified) | ||
// Timestamp: 2017.07.05-02:33:29 (last modified) | ||
// Author(s): bumblehead <chris@bumblehead.com> | ||
@@ -12,9 +12,39 @@ | ||
it("should return encoded data", () => | ||
var obj = { | ||
str: 'val', | ||
num: 0, | ||
arr: [3, { prop: false }, 1, null, 6], | ||
obj: { prop1: null, prop2: ['elem'] } | ||
}; | ||
console.log(` | ||
var formurlencoded = require('form-urlencoded'); | ||
var obj = { | ||
str : 'val', | ||
num : 0, | ||
arr : [3, {prop : false}, 1, null, 6], | ||
obj : {prop1 : null, prop2 : ['elem']} | ||
}; | ||
console.log(formurlencoded(obj)); | ||
${formurlencoded(obj).match(/(.{1,53})/g).map(n => '// ' + n).join('\n')} | ||
console.log(formurlencoded(obj, { | ||
ignorenull : true, | ||
skipIndex : true, | ||
sorted : true | ||
})); | ||
${formurlencoded(obj, { | ||
ignorenull: true, | ||
skipIndex: true, | ||
sorted: true | ||
}).match(/(.{1,53})/g).map(n => '// ' + n).join('\n')}`); | ||
it("should return encoded data", () => | ||
expect( | ||
formurlencoded({ | ||
propStr1 : 'str1', | ||
propStr2 : 'str2' | ||
}), { sorted : true } | ||
).toBe( 'propStr1=str1&propStr2=str2' )); | ||
propStr1: 'str1', | ||
propStr2: 'str2' | ||
}), { sorted: true } | ||
).toBe('propStr1=str1&propStr2=str2')); | ||
@@ -24,7 +54,7 @@ it("should return encoded data, with array properties", () => | ||
formurlencoded({ | ||
propStr1 : 'str1', | ||
propStr2 : 'str2', | ||
propArr1 : ['arrStr1', 'arrStr2'] | ||
propStr1: 'str1', | ||
propStr2: 'str2', | ||
propArr1: ['arrStr1', 'arrStr2'] | ||
}) | ||
).toBe( 'propStr1=str1&propStr2=str2&propArr1%5B0%5D=arrStr1&propArr1%5B1%5D=arrStr2' )); | ||
).toBe('propStr1=str1&propStr2=str2&propArr1%5B0%5D=arrStr1&propArr1%5B1%5D=arrStr2')); | ||
@@ -34,38 +64,38 @@ it("should return encoded data, with object properties", () => | ||
formurlencoded({ | ||
propStr1 : 'str1', | ||
propStr2 : 'str2', | ||
propObj1 : { | ||
objPropStr1 : 'objStr1', | ||
objPropStr2 : 'objStr2' | ||
propStr1: 'str1', | ||
propStr2: 'str2', | ||
propObj1: { | ||
objPropStr1: 'objStr1', | ||
objPropStr2: 'objStr2' | ||
} | ||
}) | ||
).toBe( 'propStr1=str1&propStr2=str2&propObj1%5BobjPropStr1%5D=objStr1&propObj1%5BobjPropStr2%5D=objStr2' )); | ||
).toBe('propStr1=str1&propStr2=str2&propObj1%5BobjPropStr1%5D=objStr1&propObj1%5BobjPropStr2%5D=objStr2')); | ||
it("should return encoded data, with mixed object and array properties", () => | ||
expect( formurlencoded({ | ||
propStr1 : 'str1', | ||
propStr2 : 'str2', | ||
propObj1 : { | ||
objPropStr1 : 'objStr1', | ||
objPropStr2 : 'objStr2', | ||
objPropObj1 : { | ||
propObj1Str1 : 'obj1Str1' | ||
expect(formurlencoded({ | ||
propStr1: 'str1', | ||
propStr2: 'str2', | ||
propObj1: { | ||
objPropStr1: 'objStr1', | ||
objPropStr2: 'objStr2', | ||
objPropObj1: { | ||
propObj1Str1: 'obj1Str1' | ||
}, | ||
objPropArr1 : [{ | ||
propArr1Obj1Str1 : 'obj1Str1' | ||
objPropArr1: [{ | ||
propArr1Obj1Str1: 'obj1Str1' | ||
}, { | ||
propArr1Obj2Str1 : 'obj2Str1' | ||
propArr1Obj2Str1: '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' )); | ||
})).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", () => | ||
expect( | ||
formurlencoded({ propArr1 : [1, 2, 3] }) | ||
).toBe( 'propArr1%5B0%5D=1&propArr1%5B1%5D=2&propArr1%5B2%5D=3' )); | ||
formurlencoded({ propArr1: [1, 2, 3] }) | ||
).toBe('propArr1%5B0%5D=1&propArr1%5B1%5D=2&propArr1%5B2%5D=3')); | ||
it("should return encoded data, with booleans", () => | ||
expect( | ||
formurlencoded({propArr1 : [true, false, true]}) | ||
).toBe( 'propArr1%5B0%5D=true&propArr1%5B1%5D=false&propArr1%5B2%5D=true' )); | ||
formurlencoded({ propArr1: [true, false, true] }) | ||
).toBe('propArr1%5B0%5D=true&propArr1%5B1%5D=false&propArr1%5B2%5D=true')); | ||
@@ -75,11 +105,11 @@ it("should return encoded data, with null", () => | ||
formurlencoded({ | ||
propNull1 : null, | ||
propStr1 : 'str1' | ||
propNull1: null, | ||
propStr1: 'str1' | ||
}) | ||
).toBe( 'propNull1=null&propStr1=str1' )); | ||
).toBe('propNull1=null&propStr1=str1')); | ||
it("should return encoded data, with properties sorted", () => | ||
expect( | ||
formurlencoded({c : 4, b : { z : 3, y : 2 }, a : 1}, { sorted: true }) | ||
).toBe( 'a=1&b%5By%5D=2&b%5Bz%5D=3&c=4' )); | ||
formurlencoded({ c: 4, b: { z: 3, y: 2 }, a: 1 }, { sorted: true }) | ||
).toBe('a=1&b%5By%5D=2&b%5Bz%5D=3&c=4')); | ||
@@ -89,7 +119,7 @@ it("should not break when null argument is given", () => { | ||
formurlencoded(null, { sorted: true }) | ||
).toBe( null ); | ||
).toBe(null); | ||
expect( | ||
formurlencoded(undefined, { sorted: true }) | ||
).toBe( undefined ); | ||
).toBe(undefined); | ||
}); | ||
@@ -105,40 +135,22 @@ | ||
formurlencoded({ | ||
test : testCharEncodingString | ||
test: testCharEncodingString | ||
}) | ||
).toBe( | ||
'test=%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E%7F%C2%80%C2%81%C2%82%C2%83%C2%84%C2%85%C2%86%C2%87%C2%88%C2%89%C2%8A%C2%8B%C2%8C%C2%8D%C2%8E%C2%8F%C2%90%C2%91%C2%92%C2%93%C2%94%C2%95%C2%96%C2%97%C2%98%C2%99%C2%9A%C2%9B%C2%9C%C2%9D%C2%9E%C2%9F%C2%A0%C2%A1%C2%A2%C2%A3%C2%A4%C2%A5%C2%A6%C2%A7%C2%A8%C2%A9%C2%AA%C2%AB%C2%AC%C2%AD%C2%AE%C2%AF%C2%B0%C2%B1%C2%B2%C2%B3%C2%B4%C2%B5%C2%B6%C2%B7%C2%B8%C2%B9%C2%BA%C2%BB%C2%BC%C2%BD%C2%BE%C2%BF%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3%86%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C3%90%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3%9B%C3%9C%C3%9D%C3%9E%C3%9F%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5%C3%A6%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C3%B0%C3%B1%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B7%C3%B8%C3%B9%C3%BA%C3%BB%C3%BC%C3%BD%C3%BE%C3%BF' | ||
); | ||
); | ||
}); | ||
it("should return encoded data, without null", () => | ||
it("should return encoded data, without null", () => | ||
expect( | ||
formurlencoded({propArr1 : [null, null, 1]}, {ignorenull : true}) | ||
).toBe( 'propArr1%5B2%5D=1' )); | ||
formurlencoded({ propArr1: [null, null, 1] }, { ignorenull: true }) | ||
).toBe('propArr1%5B2%5D=1')); | ||
it("should return the correct test result", () => { | ||
var formurlencoded = require('../form-urlencoded'); | ||
var obj = { | ||
str : 'val', | ||
num : 0, | ||
arr : [3, {prop : false}, 1, null, 6], | ||
obj : {prop1 : null, prop2 : ['elem']} | ||
}; | ||
console.log(formurlencoded(obj)); | ||
console.log(formurlencoded(obj, { | ||
ignorenull : true, | ||
sorted : true | ||
})); | ||
expect(true).toBe(true); | ||
}); | ||
it("should return encoded empty array", () => | ||
expect( | ||
formurlencoded({ | ||
emptyArr : [] | ||
emptyArr: [] | ||
}) | ||
).toBe( 'emptyArr%5B%5D' )); | ||
).toBe('emptyArr%5B%5D')); | ||
it("should return encoded empty array inside an object", () => | ||
it("should return encoded empty array inside an object", () => | ||
expect( | ||
@@ -151,3 +163,3 @@ formurlencoded({ | ||
}) | ||
).toBe( 'parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B%5D' )); | ||
).toBe('parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B%5D')); | ||
@@ -162,3 +174,3 @@ it("should return encoded array inside an object with index", () => | ||
}) | ||
).toBe( 'parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B0%5D=first&parent%5BemptyArr%5D%5B1%5D=second' )); | ||
).toBe('parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B0%5D=first&parent%5BemptyArr%5D%5B1%5D=second')); | ||
@@ -173,3 +185,3 @@ it("should return encoded array inside an object without index", () => | ||
}, { skipIndex: true }) | ||
).toBe( 'parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B%5D=first&parent%5BemptyArr%5D%5B%5D=second' )); | ||
).toBe('parent%5Bfoo%5D=bar&parent%5BemptyArr%5D%5B%5D=first&parent%5BemptyArr%5D%5B%5D=second')); | ||
@@ -181,3 +193,3 @@ it("should return array with index", () => | ||
}) | ||
).toBe( 'key%5B0%5D=val1' )); | ||
).toBe('key%5B0%5D=val1')); | ||
@@ -189,3 +201,3 @@ it("should return array without index", () => | ||
}, { skipIndex: true }) | ||
).toBe( 'key%5B%5D=val1' )); | ||
).toBe('key%5B%5D=val1')); | ||
@@ -196,7 +208,6 @@ it("should return encoded urls with unicode characters", () => | ||
parent: { | ||
foo: '😀', | ||
foo: '😀' | ||
} | ||
}) | ||
).toBe( 'parent%5Bfoo%5D=%F0%9F%98%80' )); | ||
).toBe('parent%5Bfoo%5D=%F0%9F%98%80')); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
54
0
14427
244