first commit
This commit is contained in:
99
web/common/static/wpj/wpj.serializer.js
Normal file
99
web/common/static/wpj/wpj.serializer.js
Normal file
@@ -0,0 +1,99 @@
|
||||
wpj.serializer = {
|
||||
serializeHash: function(values) {
|
||||
var value,
|
||||
serValue,
|
||||
result = [],
|
||||
enc = encodeURIComponent;
|
||||
|
||||
for (var index in values) {
|
||||
value = values[index];
|
||||
if ($.isArray(value)) {
|
||||
serValue = enc(JSON.stringify(value));
|
||||
} else if (typeof value === 'boolean') {
|
||||
serValue = value ? 1 : 0;
|
||||
} else {
|
||||
serValue = enc(JSON.stringify(value));
|
||||
}
|
||||
|
||||
result.push([index, serValue].join('='));
|
||||
}
|
||||
|
||||
return result.join('&');
|
||||
},
|
||||
deserializeHash: function(hash) {
|
||||
// Normalize hash
|
||||
if (hash[0] == '#') {
|
||||
hash = hash.substr(1);
|
||||
}
|
||||
|
||||
var values = hash.split('&'),
|
||||
result = {},
|
||||
parts,
|
||||
dec = decodeURIComponent;
|
||||
|
||||
for (var index in values) {
|
||||
parts = values[index].split('=');
|
||||
if (parts.length == 2) {
|
||||
result[parts[0]] = JSON.parse(dec(parts[1]));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
serializeUrl: function(values, name) {
|
||||
var value,
|
||||
serValue,
|
||||
result = [];
|
||||
|
||||
for (var index in values) {
|
||||
value = values[index];
|
||||
if ($.isArray(value)) {
|
||||
result.push(wpj.serializer.serializeUrl(value, index));
|
||||
} else {
|
||||
serValue = encodeURIComponent(value);
|
||||
result.push([name || index, serValue].join('='));
|
||||
}
|
||||
}
|
||||
|
||||
return result.join('&');
|
||||
},
|
||||
unserializeUrl: function(url) {
|
||||
var parts = url.split('&'),
|
||||
pair,
|
||||
values = [];
|
||||
|
||||
$.each(parts, function(index, value) {
|
||||
pair = value.split('=');
|
||||
if (pair.length == 2) {
|
||||
values.push({
|
||||
name: pair[0],
|
||||
value: decodeURIComponent(pair[1].replace(/\+/g, ' '))
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return wpj.serializer.arrayToDict(values);
|
||||
},
|
||||
arrayToDict: function(values) {
|
||||
var result = {},
|
||||
index,
|
||||
key,
|
||||
value;
|
||||
|
||||
for (index in values) {
|
||||
key = values[index]['name'];
|
||||
value = values[index]['value'];
|
||||
|
||||
if (result[key] != undefined) {
|
||||
if (typeof result[key] != 'object') {
|
||||
result[key] = [result[key]];
|
||||
}
|
||||
result[key].push(value);
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user