form-urlencoded
Advanced tools
Comparing version 0.0.5 to 0.0.6
// Filename: formurlencoded.js | ||
// Timestamp: 2013.10.06-21:36:29 (last modified) | ||
// Author(s): Bumblehead (www.bumblehead.com) | ||
// Timestamp: 2014.04.18-10:14:24 (last modified) | ||
// Author(s): Bumblehead (www.bumblehead.com), JBlashill (james@blashill.com) | ||
if (!Array.isArray) { | ||
Array.isArray = function (vArg) { | ||
return Object.prototype.toString.call(vArg) === "[object Array]"; | ||
}; | ||
} | ||
var formurlencoded = ((typeof module === 'object') ? module : {}).exports = { | ||
// input: {one:1,two:2} return: '[one]=1&[two]-2' | ||
// input: {one:1,two:2} return: '[one]=1&[two]-2' | ||
encode : function (data, options) { | ||
var pairs = [], regexp = /%20/g, // regex to match whitespace | ||
nvObjKeyStr = ':name[:prop]', | ||
nvArrKeyStr = ':name[]'; | ||
options = options || {}; | ||
function getNestValsArrAsStr(arr) { | ||
return arr.filter(function (e) { | ||
return (typeof e === 'string' && e.length) ? true : false; | ||
return typeof e === 'string' && e.length; | ||
}).join('&'); | ||
} | ||
function getObjNestVals (name, obj) { | ||
var nestVals = []; | ||
function getKeys(obj) { | ||
var keys = Object.keys(obj); | ||
if (options.sorted) { | ||
keys.sort(); | ||
} | ||
keys.forEach(function(key) { | ||
nestVals.push(getNestVals(name + '[' + key + ']', obj[key])); | ||
}); | ||
return getNestValsArrAsStr(nestVals); | ||
return options && options.sorted ? keys.sort() : keys; | ||
} | ||
function getObjNestVals (name, obj) { | ||
var objKeyStr = ':name[:prop]'; | ||
return getNestValsArrAsStr(getKeys(obj).map(function (key) { | ||
return getNestVals( | ||
objKeyStr.replace(/:name/, name).replace(/:prop/, key), obj[key] | ||
); | ||
})); | ||
} | ||
function getArrNestVals (name, arr) { | ||
var nestVals = [], x, len; | ||
for (x = 0, len = arr.length; x < len; x++) { | ||
nestVals.push(getNestVals(name + '[]', arr[+x])); | ||
} | ||
return getNestValsArrAsStr(nestVals); | ||
var arrKeyStr = ':name[]'; | ||
return getNestValsArrAsStr(arr.map(function (elem) { | ||
return getNestVals( | ||
arrKeyStr.replace(/:name/, name), elem | ||
); | ||
})); | ||
} | ||
function getNestVals (name, value) { | ||
var type = typeof value, f = null; | ||
var whitespaceRe = /%20/g, | ||
type = typeof value, | ||
f = null; | ||
if (type === 'string') { | ||
@@ -53,3 +53,3 @@ f = encodeURIComponent(name) + '=' + | ||
f = encodeURIComponent(name) + '=' + | ||
encodeURIComponent(value).replace(regexp, "+"); | ||
encodeURIComponent(value).replace(whitespaceRe, '+'); | ||
} else if (type === 'boolean') { | ||
@@ -62,28 +62,21 @@ f = encodeURIComponent(name) + '=' + value; | ||
} | ||
return f; | ||
} | ||
// 5.1, http://www.w3.org/TR/html5/forms.html#url-encoded-form-data | ||
function manuallyEncodeChar (ch) { | ||
if (ch === ' ') { | ||
return '+'; | ||
} else { | ||
return "%" + ("0" + ch.charCodeAt(0).toString(16)).slice(-2).toUpperCase(); | ||
} | ||
return '%' + ('0' + ch.charCodeAt(0).toString(16)).slice(-2).toUpperCase(); | ||
}; | ||
var formEncodeString = function(value) { | ||
function formEncodeString (value) { | ||
return value | ||
.replace(/[^ !'()~\*]*/g, encodeURIComponent) | ||
.replace(/[ !'()~\*]/g, manuallyEncodeChar); | ||
.replace(/ /g, '+') | ||
.replace(/[!'()~\*]/g, manuallyEncodeChar); | ||
}; | ||
var keys = Object.keys(data); | ||
if (options.sorted) { | ||
keys.sort(); | ||
} | ||
keys.forEach(function(key) { | ||
pairs.push(getNestVals(key, data[key])); | ||
}); | ||
return getNestValsArrAsStr(pairs); | ||
return getNestValsArrAsStr(getKeys(data).map(function (key) { | ||
return getNestVals(key, data[key]); | ||
})); | ||
} | ||
@@ -90,0 +83,0 @@ }; |
{ | ||
"name": "form-urlencoded", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"author": "Chris <chris@bumblehead.com>", | ||
"contributors": [{ | ||
"name": "James", | ||
"email": "james@blashill.com" | ||
}], | ||
"repository": { | ||
@@ -5,0 +10,0 @@ "type": "git", |
form-urlencoded | ||
=============== | ||
**(c)[Bumblehead][0],[JBlashill][6] 2013** [MIT-license](#license) | ||
**(c)[Bumblehead][0],[JBlashill][6] 2013,2014** [MIT-license](#license) | ||
@@ -9,5 +9,5 @@ ### OVERVIEW: | ||
`application/x-www-form-urlencoded` is a string encoding used, primarily, when an HTML form is submitted. The data from a form will be serialised in this format before it is sent to a server. | ||
`application/x-www-form-urlencoded` is a string encoding used, primarily, when an HTML form is submitted. The form data is serialised in this format and sent to a server. | ||
This format is not [well][1] [defined][2]. Serialising complex data, such as a javascript object, is a beyond current specifications. | ||
The format is not [well][1] [defined][2]. Serialising complex data, such as a javascript object, is beyond the specification. | ||
@@ -31,3 +31,3 @@ [Server][3] [softwares][5] do share methods for handling complex urlencoded data and so there is a "defacto" standard for serializing such data. This is the standard that form-urlencoded follows. | ||
* **npm** | ||
* **npm** | ||
@@ -40,3 +40,3 @@ ```bash | ||
```bash | ||
```bash | ||
$ git clone https://github.com/iambumblehead/form-urlencoded.git | ||
@@ -55,6 +55,5 @@ ``` | ||
--------------------------------------------------------- | ||
#### <a id="get-started">GET STARTED: | ||
1. **Object encoding** | ||
1. **Object encoding** | ||
@@ -69,3 +68,3 @@ > ```javascript | ||
objPropStr2 : 'objStr2' | ||
} | ||
} | ||
}); | ||
@@ -77,5 +76,6 @@ console.log(result); | ||
--------------------------------------------------------- | ||
#### <a id="license">License: | ||
![scrounge](http://github.com/iambumblehead/scroungejs/raw/master/img/hand.png) | ||
(The MIT License) | ||
@@ -90,2 +90,2 @@ | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
1
85
12802
181