Skip to content

Commit

Permalink
upd:obj2str方法
Browse files Browse the repository at this point in the history
  • Loading branch information
livehigh committed Nov 16, 2021
1 parent c10c7de commit 52da287
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 129 deletions.
74 changes: 31 additions & 43 deletions demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -8904,53 +8904,41 @@ module.exports.init = function (COS, task) {
/* 18 */
/***/ (function(module, exports) {

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = Object.keys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] || '';
list.push(key + '=' + encodeURIComponent(val));
}
return list.join('&');
};
function camSafeUrlEncode(str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}

var stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var queryStringify = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).filter(Boolean).join(sep);

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
return list.join('&');
};

var request = function (params, callback) {
Expand Down Expand Up @@ -9028,7 +9016,7 @@ var request = function (params, callback) {
});
});
} else {
var qsStr = params.qs && queryStringify(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down
74 changes: 31 additions & 43 deletions demo/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -8904,53 +8904,41 @@ module.exports.init = function (COS, task) {
/* 18 */
/***/ (function(module, exports) {

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = Object.keys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] || '';
list.push(key + '=' + encodeURIComponent(val));
}
return list.join('&');
};
function camSafeUrlEncode(str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}

var stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var queryStringify = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).filter(Boolean).join(sep);

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
return list.join('&');
};

var request = function (params, callback) {
Expand Down Expand Up @@ -9028,7 +9016,7 @@ var request = function (params, callback) {
});
});
} else {
var qsStr = params.qs && queryStringify(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down
74 changes: 31 additions & 43 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = Object.keys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] || '';
list.push(key + '=' + encodeURIComponent(val));
}
return list.join('&');
};
function camSafeUrlEncode(str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}

var stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var queryStringify = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).filter(Boolean).join(sep);

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
return list.join('&');
};

var request = function (params, callback) {
Expand Down Expand Up @@ -122,7 +110,7 @@ var request = function (params, callback) {
});
});
} else {
var qsStr = params.qs && queryStringify(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down

0 comments on commit 52da287

Please sign in to comment.