can-deparam
Advanced tools
Comparing version 1.0.3 to 1.1.0
var deparam = require('./can-deparam'); | ||
var QUnit = require('steal-qunit'); | ||
var stringToAny = require("can-string-to-any"); | ||
QUnit.module('can/util/string/deparam'); | ||
QUnit.module('can-deparam'); | ||
/** / | ||
@@ -45,2 +46,36 @@ test("Basic deparam",function(){ | ||
}); | ||
QUnit.test("deparam deep", function(){ | ||
QUnit.deepEqual(deparam("age[or][][lte]=5&age[or][]=null"), { | ||
age: { | ||
or: [ {lte: "5"}, "null" ] | ||
} | ||
}); | ||
/* | ||
QUnit.deepEqual(param({ | ||
"undefined": undefined, | ||
"null": null, | ||
"NaN": NaN, | ||
"true": true, | ||
"false": false | ||
}),"undefined=undefined&null=null&NaN=NaN&true=true&false=false","true, false, undefined, etc");*/ | ||
}); | ||
QUnit.test("takes value deserializer", function(){ | ||
QUnit.deepEqual(deparam("age[or][][lte]=5&age[or][]=null", stringToAny), { | ||
age: { | ||
or: [ {lte: 5}, null ] | ||
} | ||
}); | ||
QUnit.deepEqual(deparam("undefined=undefined&null=null&NaN=NaN&true=true&false=false", stringToAny), { | ||
"undefined": undefined, | ||
"null": null, | ||
"NaN": NaN, | ||
"true": true, | ||
"false": false | ||
}); | ||
}); | ||
/** / | ||
@@ -47,0 +82,0 @@ test("deparam an array", function(){ |
@@ -11,3 +11,10 @@ var namespace = require("can-namespace"); | ||
* | ||
* @param {String} params a form-urlencoded string of key-value pairs | ||
* @param {String} params A form-urlencoded string of key-value pairs. | ||
* @param {function} [valueDeserializer] A function that decodes the string values. For example, using | ||
* [can-string-to-any] will convert `"null"` to `null` like: | ||
* | ||
* ```js | ||
* import stringToAny from "can-string-to-any"; | ||
* deparam("value=null", stringToAny) //-> {value: null} | ||
* ``` | ||
* @return {Object} The params formatted into an object | ||
@@ -49,3 +56,12 @@ * | ||
}; | ||
module.exports = namespace.deparam = function (params) { | ||
function isArrayLikeName(name) { | ||
return digitTest.test(name) || name === '[]'; | ||
} | ||
function idenity(value){ return value; } | ||
module.exports = namespace.deparam = function (params, valueDeserializer) { | ||
valueDeserializer = valueDeserializer || idenity; | ||
var data = {}, pairs, lastPart; | ||
@@ -62,13 +78,26 @@ if (params && paramTest.test(params)) { | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
// If what we are pointing to looks like an `array` | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
var currentName = parts[j], | ||
nextName = parts[j + 1], | ||
currentIsArray = isArrayLikeName(currentName) && current instanceof Array; | ||
if (!current[currentName]) { | ||
if(currentIsArray) { | ||
current.push( isArrayLikeName(nextName) ? [] : {} ); | ||
} else { | ||
// If what we are pointing to looks like an `array` | ||
current[currentName] = isArrayLikeName(nextName) ? [] : {}; | ||
} | ||
} | ||
current = current[parts[j]]; | ||
if(currentIsArray) { | ||
current = current[current.length - 1]; | ||
} else { | ||
current = current[currentName]; | ||
} | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
if ( isArrayLikeName(lastPart) ) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current[lastPart] = value; | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
@@ -75,0 +104,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*can-deparam@1.0.2#can-deparam*/ | ||
/*can-deparam@1.0.3#can-deparam*/ | ||
define([ | ||
@@ -19,3 +19,10 @@ 'require', | ||
}; | ||
module.exports = namespace.deparam = function (params) { | ||
function isArrayLikeName(name) { | ||
return digitTest.test(name) || name === '[]'; | ||
} | ||
function idenity(value) { | ||
return value; | ||
} | ||
module.exports = namespace.deparam = function (params, valueDeserializer) { | ||
valueDeserializer = valueDeserializer || idenity; | ||
var data = {}, pairs, lastPart; | ||
@@ -29,12 +36,21 @@ if (params && paramTest.test(params)) { | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
var currentName = parts[j], nextName = parts[j + 1], currentIsArray = isArrayLikeName(currentName) && current instanceof Array; | ||
if (!current[currentName]) { | ||
if (currentIsArray) { | ||
current.push(isArrayLikeName(nextName) ? [] : {}); | ||
} else { | ||
current[currentName] = isArrayLikeName(nextName) ? [] : {}; | ||
} | ||
} | ||
current = current[parts[j]]; | ||
if (currentIsArray) { | ||
current = current[current.length - 1]; | ||
} else { | ||
current = current[currentName]; | ||
} | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
if (isArrayLikeName(lastPart)) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current[lastPart] = value; | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
@@ -41,0 +57,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*can-deparam@1.0.2#can-deparam*/ | ||
/*can-deparam@1.0.3#can-deparam*/ | ||
var namespace = require('can-namespace'); | ||
@@ -13,3 +13,10 @@ var digitTest = /^\d+$/, keyBreaker = /([^\[\]]+)|(\[\])/g, paramTest = /([^?#]*)(#.*)?$/, entityRegex = /%([^0-9a-f][0-9a-f]|[0-9a-f][^0-9a-f]|[^0-9a-f][^0-9a-f])/i, prep = function (str) { | ||
}; | ||
module.exports = namespace.deparam = function (params) { | ||
function isArrayLikeName(name) { | ||
return digitTest.test(name) || name === '[]'; | ||
} | ||
function idenity(value) { | ||
return value; | ||
} | ||
module.exports = namespace.deparam = function (params, valueDeserializer) { | ||
valueDeserializer = valueDeserializer || idenity; | ||
var data = {}, pairs, lastPart; | ||
@@ -23,12 +30,21 @@ if (params && paramTest.test(params)) { | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
var currentName = parts[j], nextName = parts[j + 1], currentIsArray = isArrayLikeName(currentName) && current instanceof Array; | ||
if (!current[currentName]) { | ||
if (currentIsArray) { | ||
current.push(isArrayLikeName(nextName) ? [] : {}); | ||
} else { | ||
current[currentName] = isArrayLikeName(nextName) ? [] : {}; | ||
} | ||
} | ||
current = current[parts[j]]; | ||
if (currentIsArray) { | ||
current = current[current.length - 1]; | ||
} else { | ||
current = current[currentName]; | ||
} | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
if (isArrayLikeName(lastPart)) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current[lastPart] = value; | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
@@ -35,0 +51,0 @@ } |
@@ -123,3 +123,8 @@ /*[global-shim-start]*/ | ||
{}, | ||
typeof self == "object" && self.Object == Object ? self : window, | ||
typeof self == "object" && self.Object == Object | ||
? self | ||
: typeof process === "object" && | ||
Object.prototype.toString.call(process) === "[object process]" | ||
? global | ||
: window, | ||
function(__$source__, __$global__) { | ||
@@ -131,3 +136,3 @@ // jshint ignore:line | ||
/*can-deparam@1.0.2#can-deparam*/ | ||
/*can-deparam@1.0.3#can-deparam*/ | ||
define('can-deparam', [ | ||
@@ -150,3 +155,10 @@ 'require', | ||
}; | ||
module.exports = namespace.deparam = function (params) { | ||
function isArrayLikeName(name) { | ||
return digitTest.test(name) || name === '[]'; | ||
} | ||
function idenity(value) { | ||
return value; | ||
} | ||
module.exports = namespace.deparam = function (params, valueDeserializer) { | ||
valueDeserializer = valueDeserializer || idenity; | ||
var data = {}, pairs, lastPart; | ||
@@ -160,12 +172,21 @@ if (params && paramTest.test(params)) { | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
var currentName = parts[j], nextName = parts[j + 1], currentIsArray = isArrayLikeName(currentName) && current instanceof Array; | ||
if (!current[currentName]) { | ||
if (currentIsArray) { | ||
current.push(isArrayLikeName(nextName) ? [] : {}); | ||
} else { | ||
current[currentName] = isArrayLikeName(nextName) ? [] : {}; | ||
} | ||
} | ||
current = current[parts[j]]; | ||
if (currentIsArray) { | ||
current = current[current.length - 1]; | ||
} else { | ||
current = current[currentName]; | ||
} | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
if (isArrayLikeName(lastPart)) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current[lastPart] = value; | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
@@ -183,2 +204,2 @@ } | ||
} | ||
)(typeof self == "object" && self.Object == Object ? self : window); | ||
)(typeof self == "object" && self.Object == Object ? self : (typeof process === "object" && Object.prototype.toString.call(process) === "[object process]") ? global : window); |
{ | ||
"name": "can-deparam", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Deserialize a query string into an array or object.", | ||
"homepage": "https://canjs.com/", | ||
"homepage": "https://canjs.com/doc/can-deparam.html", | ||
"repository": { | ||
@@ -29,3 +29,3 @@ "type": "git", | ||
}, | ||
"main": "dist/cjs/can-deparam", | ||
"main": "can-deparam", | ||
"keywords": [ | ||
@@ -36,3 +36,2 @@ "canjs", | ||
"steal": { | ||
"main": "can-deparam", | ||
"configDependencies": [ | ||
@@ -52,6 +51,4 @@ "live-reload" | ||
"devDependencies": { | ||
"can-string-to-any": "^1.0.0", | ||
"detect-cyclic-packages": "^1.1.0", | ||
"done-serve": "^1.0.0", | ||
"donejs-cli": "^1.0.0", | ||
"generator-donejs": "^1.0.0", | ||
"jshint": "^2.9.1", | ||
@@ -58,0 +55,0 @@ "steal": "^1.3.1", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
25200
7
507
15
2