From d1b16535c243bdb66f91990e1ecda01effcf84ea Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 09:57:30 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20string=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20yd=5Fstring=5FbyteLength=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84utf8=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/string/byteLength.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/string/byteLength.js diff --git a/lib/string/byteLength.js b/lib/string/byteLength.js new file mode 100644 index 0000000..bb78bac --- /dev/null +++ b/lib/string/byteLength.js @@ -0,0 +1,18 @@ +/** + * 获取字符串的utf8字节长度 + * @author 王勇 + * @category string + * @alias yd_string_byteLength + * @param {String} str 字符串数据 + * @returns {String} 返回utf8字符串的字节长度 + */ +export default (str) => { + let s = str.length + for (var i = str.length - 1; i >= 0; i--) { + const code = str.charCodeAt(i) + if (code > 0x7f && code <= 0x7ff) s++ + else if (code > 0x7ff && code <= 0xffff) s += 2 + if (code >= 0xDC00 && code <= 0xDFFF) i-- + } + return s +} \ No newline at end of file From 79db340da429b5b9a6797229b02d87f31525727f Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 10:14:37 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20browser=20yd=5Fbrowser?= =?UTF-8?q?=5Fparam=20=E4=BB=A5=E5=8F=8A=20yd=5Fbrowser=5Fparam2Obj?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/browser/param.js | 30 ++++++++++++++++++++++++++++++ lib/browser/paramToObj.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 lib/browser/param.js create mode 100644 lib/browser/paramToObj.js diff --git a/lib/browser/param.js b/lib/browser/param.js new file mode 100644 index 0000000..13c49fb --- /dev/null +++ b/lib/browser/param.js @@ -0,0 +1,30 @@ + +function cleanArray(actual) { + const newArray = [] + for (let i = 0; i < actual.length; i++) { + if (actual[i]) { + newArray.push(actual[i]) + } + } + return newArray +} + +/** + * get请求对象转换地址栏参数 + * @author 王勇 + * @category browser + * @alias yd_browser_param + * @param {Object} json对象 + * @returns {String} 处理后结果 + * @example yd_browser_param({name:'张三', age:18}) -> name=%E5%BC%A0%E4%B8%89&age=18 + */ +export default (json) => { + if (!json) return '' + return cleanArray( + Object.keys(json).map(key => { + if (json[key] === undefined) return '' + return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) + }) + ).join('&') +} + \ No newline at end of file diff --git a/lib/browser/paramToObj.js b/lib/browser/paramToObj.js new file mode 100644 index 0000000..cd537f7 --- /dev/null +++ b/lib/browser/paramToObj.js @@ -0,0 +1,28 @@ + + +/** + * 地址栏参数转换为对象形式 + * @author 王勇 + * @category browser + * @alias yd_browser_param2Obj + * @param {String} 字符串url地址后参数 + * @returns {Object} 返回json 对象 + * @example yd_browser_param2Obj('?name=%E5%BC%A0%E4%B8%89&age=18') -> {name:'张三', age:18} + */ +export default (url) => { + const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') + if (!search) { + return {} + } + const obj = {} + const searchArr = search.split('&') + searchArr.forEach(v => { + const index = v.indexOf('=') + if (index !== -1) { + const name = v.substring(0, index) + const val = v.substring(index + 1, v.length) + obj[name] = val + } + }) + return obj + } \ No newline at end of file From 35fe5c9fa9b4114e5c18014ca9aef68b74dce079 Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 10:15:24 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20browser=20yd=5Fbrowser?= =?UTF-8?q?=5Fparam=20=E4=BB=A5=E5=8F=8A=20yd=5Fbrowser=5FparamToObj?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/browser/paramToObj.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/browser/paramToObj.js b/lib/browser/paramToObj.js index cd537f7..bf662ae 100644 --- a/lib/browser/paramToObj.js +++ b/lib/browser/paramToObj.js @@ -4,10 +4,10 @@ * 地址栏参数转换为对象形式 * @author 王勇 * @category browser - * @alias yd_browser_param2Obj + * @alias yd_browser_paramToObj * @param {String} 字符串url地址后参数 * @returns {Object} 返回json 对象 - * @example yd_browser_param2Obj('?name=%E5%BC%A0%E4%B8%89&age=18') -> {name:'张三', age:18} + * @example yd_browser_paramToObj('?name=%E5%BC%A0%E4%B8%89&age=18') -> {name:'张三', age:18} */ export default (url) => { const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') From 7a05d7045f55256ff14a129999b74ffecee85cdd Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 10:17:32 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20yd=5Fstring=5FbyteLeng?= =?UTF-8?q?th=20=E6=96=B9=E6=B3=95=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/string/byteLength.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/string/byteLength.js b/lib/string/byteLength.js index bb78bac..b14c5d4 100644 --- a/lib/string/byteLength.js +++ b/lib/string/byteLength.js @@ -5,6 +5,7 @@ * @alias yd_string_byteLength * @param {String} str 字符串数据 * @returns {String} 返回utf8字符串的字节长度 + * @example yd_string_byteLength('123abc张三') -> 12 */ export default (str) => { let s = str.length From 616457cacac870ffbf4890abf3e91f5707096e8b Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 10:23:10 +0800 Subject: [PATCH 5/6] =?UTF-8?q?yd=5Fbrowser=5Fparam=20=20paramToObj=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/browser/param.js | 8 ++++---- lib/browser/paramToObj.js | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/browser/param.js b/lib/browser/param.js index 13c49fb..80e678a 100644 --- a/lib/browser/param.js +++ b/lib/browser/param.js @@ -21,10 +21,10 @@ function cleanArray(actual) { export default (json) => { if (!json) return '' return cleanArray( - Object.keys(json).map(key => { - if (json[key] === undefined) return '' - return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) - }) + Object.keys(json).map(key => { + if (json[key] === undefined) return '' + return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) + }) ).join('&') } \ No newline at end of file diff --git a/lib/browser/paramToObj.js b/lib/browser/paramToObj.js index bf662ae..cc8bf02 100644 --- a/lib/browser/paramToObj.js +++ b/lib/browser/paramToObj.js @@ -12,17 +12,17 @@ export default (url) => { const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') if (!search) { - return {} + return {} } const obj = {} const searchArr = search.split('&') searchArr.forEach(v => { - const index = v.indexOf('=') - if (index !== -1) { - const name = v.substring(0, index) - const val = v.substring(index + 1, v.length) - obj[name] = val - } + const index = v.indexOf('=') + if (index !== -1) { + const name = v.substring(0, index) + const val = v.substring(index + 1, v.length) + obj[name] = val + } }) return obj - } \ No newline at end of file +} \ No newline at end of file From 78876867e7141a41ba35a25cf739c21e979f850e Mon Sep 17 00:00:00 2001 From: wangyong1997 <1044656020@qq.com> Date: Tue, 6 Aug 2024 10:33:23 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95=20yd?= =?UTF-8?q?=5Fobject=5FobjectMerge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/object/objectMerge.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/object/objectMerge.js diff --git a/lib/object/objectMerge.js b/lib/object/objectMerge.js new file mode 100644 index 0000000..cb449e0 --- /dev/null +++ b/lib/object/objectMerge.js @@ -0,0 +1,34 @@ + +/** + * 合并两个对象,给最后一个优先级高 + * @author 王勇 + * @category object + * @alias yd_object_objectMerge + * @param {Object} target + * @param {(Object|Array)} source + * @returns {Object} + * @example + yd_object_objectMerge({aa:{cc:123, bb:456}, cc:123}, {aa: {cc: 'abc'}, cc: 123}) ' + => '{"aa":{"cc":"abc","bb":456},"cc":123}' + + yd_object_objectMerge({obj:{cc:123, bb:[1,2,3,4]}, age:12}, {obj: {bb: ['a', 'b']}, cc: 123}) + => '{"obj":{"cc":123,"bb":["a","b"]},"age":12,"cc":123}' + */ +export default function objectMerge(target, source) { + if (typeof target !== 'object') { + target = {} + } + if (Array.isArray(source)) { + return source.slice() + } + Object.keys(source).forEach(property => { + const sourceProperty = source[property] + if (typeof sourceProperty === 'object') { + target[property] = objectMerge(target[property], sourceProperty) + } else { + target[property] = sourceProperty + } + }) + return target +} + \ No newline at end of file