-
Notifications
You must be signed in to change notification settings - Fork 1
/
can-param.js
68 lines (64 loc) · 2.22 KB
/
can-param.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";
var namespace = require("can-namespace");
var standardsMode = false;
function buildParam(prefix, obj, add) {
if (Array.isArray(obj)) {
for (var i = 0, l = obj.length; i < l; ++i) {
var inner = obj[i];
var shouldIncludeIndex = typeof inner === 'object';
var arrayIndex = shouldIncludeIndex ? '[' + i + ']' : '[]';
buildParam(prefix + arrayIndex, inner, add);
}
} else if ( obj && typeof obj === "object" ) {
for (var name in obj) {
buildParam(prefix + '[' + name + ']', obj[name], add);
}
} else {
add(prefix, obj);
}
}
if ( namespace.param ) {
throw new Error( "You can't have two versions of can-param, check your dependencies" );
} else {
module.exports = namespace.param = function param(object) {
var pairs = [],
add = function (key, value) {
value = standardsMode && value == null ? '' : value;
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
};
for (var name in object) {
if (!standardsMode || typeof object[name] !== 'undefined') {
buildParam(name, object[name], add);
}
}
return pairs.join('&')
.replace(/%20/g, '+');
};
/**
* @function can-param.setStandardsMode setStandardsMode
* @parent can-param.methods
* @description Set whether to treat null and undefined specially when serializing
*
* @signature `param.setStandardsMode(boolean)`
*
* Set whether to serialize values in a manner more consistent with jQuery[1] and URLSearchParams[2], or to use the classic
* can-param value serialization. By default this value is false (classic mode).
*
* The differences between the two are:
* - `null` serializes to an empty string in standards mode, "null" in classic mode
* - `undefined` is removed from the serialized form entirely in standards mode, serialized to "undefined" in classic mode
*
* All other values are treated the same in both modes.
*
* @param {boolean} value `true` to use DOM/jQuery style param serialization, `false` to use classic can-param serializtion
*
* @body
* <hr>
* [1] [https://api.jquery.com/jquery.param/]
*
* [2] [https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams]
*/
namespace.param.setStandardsMode = function (value) {
standardsMode = !!value;
};
}