can-deparam
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -17,19 +17,19 @@ var deparam = require('./can-deparam'); | ||
/**/ | ||
test('Nested deparam', function () { | ||
QUnit.test('Nested deparam', function(assert) { | ||
var data = deparam('a[b]=1&a[c]=2'); | ||
equal(data.a.b, 1); | ||
equal(data.a.c, 2); | ||
assert.equal(data.a.b, 1); | ||
assert.equal(data.a.c, 2); | ||
data = deparam('a[]=1&a[]=2'); | ||
equal(data.a[0], 1); | ||
equal(data.a[1], 2); | ||
assert.equal(data.a[0], 1); | ||
assert.equal(data.a[1], 2); | ||
data = deparam('a[b][]=1&a[b][]=2'); | ||
equal(data.a.b[0], 1); | ||
equal(data.a.b[1], 2); | ||
assert.equal(data.a.b[0], 1); | ||
assert.equal(data.a.b[1], 2); | ||
data = deparam('a[0]=1&a[1]=2'); | ||
equal(data.a[0], 1); | ||
equal(data.a[1], 2); | ||
assert.equal(data.a[0], 1); | ||
assert.equal(data.a[1], 2); | ||
}); | ||
test('Remaining ampersand', function () { | ||
QUnit.test('Remaining ampersand', function(assert) { | ||
var data = deparam('a[b]=1&a[c]=2&'); | ||
deepEqual(data, { | ||
assert.deepEqual(data, { | ||
a: { | ||
@@ -41,5 +41,5 @@ b: '1', | ||
}); | ||
test('Invalid encoding', function() { | ||
QUnit.test('Invalid encoding', function(assert) { | ||
var data = deparam('foo=%0g'); | ||
deepEqual(data, { | ||
assert.deepEqual(data, { | ||
foo: '%0g' | ||
@@ -49,4 +49,4 @@ }); | ||
QUnit.test("deparam deep", function(){ | ||
QUnit.deepEqual(deparam("age[or][][lte]=5&age[or][]=null"), { | ||
QUnit.test("deparam deep", function(assert) { | ||
assert.deepEqual(deparam("age[or][][lte]=5&age[or][]=null"), { | ||
age: { | ||
@@ -57,3 +57,3 @@ or: [ {lte: "5"}, "null" ] | ||
/* | ||
QUnit.deepEqual(param({ | ||
assert.deepEqual(param({ | ||
"undefined": undefined, | ||
@@ -67,4 +67,4 @@ "null": null, | ||
QUnit.test("takes value deserializer", function(){ | ||
QUnit.deepEqual(deparam("age[or][][lte]=5&age[or][]=null", stringToAny), { | ||
QUnit.test("takes value deserializer", function(assert) { | ||
assert.deepEqual(deparam("age[or][][lte]=5&age[or][]=null", stringToAny), { | ||
age: { | ||
@@ -75,3 +75,3 @@ or: [ {lte: 5}, null ] | ||
QUnit.deepEqual(deparam("undefined=undefined&null=null&NaN=NaN&true=true&false=false", stringToAny), { | ||
assert.deepEqual(deparam("undefined=undefined&null=null&NaN=NaN&true=true&false=false", stringToAny), { | ||
"undefined": undefined, | ||
@@ -78,0 +78,0 @@ "null": null, |
@@ -1,3 +0,3 @@ | ||
/*can-deparam@1.1.1#can-deparam*/ | ||
define([ | ||
/*can-deparam@1.2.0#can-deparam*/ | ||
define('can-deparam', [ | ||
'require', | ||
@@ -4,0 +4,0 @@ 'exports', |
@@ -1,54 +0,61 @@ | ||
/*can-deparam@1.1.1#can-deparam*/ | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
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) { | ||
str = str.replace(/\+/g, ' '); | ||
try { | ||
return decodeURIComponent(str); | ||
} catch (e) { | ||
return decodeURIComponent(str.replace(entityRegex, function (match, hex) { | ||
return '%25' + hex; | ||
})); | ||
} | ||
}; | ||
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; | ||
if (params && paramTest.test(params)) { | ||
pairs = params.split('&'); | ||
pairs.forEach(function (pair) { | ||
var parts = pair.split('='), key = prep(parts.shift()), value = prep(parts.join('=')), current = data; | ||
if (key) { | ||
parts = key.match(keyBreaker); | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
var currentName = parts[j], nextName = parts[j + 1], currentIsArray = isArrayLikeName(currentName) && current instanceof Array; | ||
if (!current[currentName]) { | ||
/*can-deparam@1.2.0#can-deparam*/ | ||
define('can-deparam', [ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-namespace' | ||
], function (require, exports, module) { | ||
'use strict'; | ||
var namespace = require('can-namespace'); | ||
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) { | ||
str = str.replace(/\+/g, ' '); | ||
try { | ||
return decodeURIComponent(str); | ||
} catch (e) { | ||
return decodeURIComponent(str.replace(entityRegex, function (match, hex) { | ||
return '%25' + hex; | ||
})); | ||
} | ||
}; | ||
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; | ||
if (params && paramTest.test(params)) { | ||
pairs = params.split('&'); | ||
pairs.forEach(function (pair) { | ||
var parts = pair.split('='), key = prep(parts.shift()), value = prep(parts.join('=')), current = data; | ||
if (key) { | ||
parts = key.match(keyBreaker); | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
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) ? [] : {}; | ||
} | ||
} | ||
if (currentIsArray) { | ||
current.push(isArrayLikeName(nextName) ? [] : {}); | ||
current = current[current.length - 1]; | ||
} else { | ||
current[currentName] = isArrayLikeName(nextName) ? [] : {}; | ||
current = current[currentName]; | ||
} | ||
} | ||
if (currentIsArray) { | ||
current = current[current.length - 1]; | ||
lastPart = parts.pop(); | ||
if (isArrayLikeName(lastPart)) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current = current[currentName]; | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
} | ||
lastPart = parts.pop(); | ||
if (isArrayLikeName(lastPart)) { | ||
current.push(valueDeserializer(value)); | ||
} else { | ||
current[lastPart] = valueDeserializer(value); | ||
} | ||
} | ||
}); | ||
} | ||
return data; | ||
}; | ||
}); | ||
} | ||
return data; | ||
}; | ||
}); |
@@ -109,4 +109,5 @@ /*[process-shim]*/ | ||
args[2] = module; | ||
} else if (!args[0] && deps[0] === "exports") { | ||
// Babel uses the exports and module object. | ||
} | ||
// Babel uses the exports and module object. | ||
else if (!args[0] && deps[0] === "exports") { | ||
module = { exports: {} }; | ||
@@ -168,3 +169,3 @@ args[0] = module.exports; | ||
/*can-deparam@1.1.1#can-deparam*/ | ||
/*can-deparam@1.2.0#can-deparam*/ | ||
define('can-deparam', [ | ||
@@ -171,0 +172,0 @@ 'require', |
{ | ||
"name": "can-deparam", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Deserialize a query string into an array or object.", | ||
@@ -52,7 +52,7 @@ "homepage": "https://canjs.com/doc/can-deparam.html", | ||
"jshint": "^2.9.1", | ||
"steal": "^1.3.1", | ||
"steal-qunit": "^1.0.1", | ||
"steal-tools": "^1.2.0", | ||
"testee": "^0.3.0" | ||
"steal": "^2.2.1", | ||
"steal-qunit": "^2.0.0", | ||
"steal-tools": "^2.2.1", | ||
"testee": "^0.9.0" | ||
} | ||
} |
# can-deparam | ||
[![Join the chat at https://gitter.im/canjs/canjs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/canjs/canjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![Join our Slack](https://img.shields.io/badge/slack-join%20chat-611f69.svg)](https://www.bitovi.com/community/slack?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![Join our Discourse](https://img.shields.io/discourse/https/forums.bitovi.com/posts.svg)](https://forums.bitovi.com/?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/canjs/can-deparam/blob/master/LICENSE.md) | ||
[![npm version](https://badge.fury.io/js/can-deparam.svg)](https://www.npmjs.com/package/can-deparam) | ||
[![Travis build status](https://travis-ci.org/canjs/can-deparam.svg?branch=master)](https://travis-ci.org/canjs/can-deparam) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/canjs/can-deparam.svg)](https://greenkeeper.io/) | ||
@@ -12,3 +14,3 @@ Deserialize a query string into an array or object. | ||
Read the [API docs on CanJS.com](https://canjs.com/doc/can-deparam.html). | ||
Read the [can-deparam API docs on CanJS.com](https://canjs.com/doc/can-deparam.html). | ||
@@ -15,0 +17,0 @@ ## Changelog |
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
26688
551
27
2