diff --git a/lib/browser/param.js b/lib/browser/param.js new file mode 100644 index 0000000..80e678a --- /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..cc8bf02 --- /dev/null +++ b/lib/browser/paramToObj.js @@ -0,0 +1,28 @@ + + +/** + * 地址栏参数转换为对象形式 + * @author 王勇 + * @category browser + * @alias yd_browser_paramToObj + * @param {String} 字符串url地址后参数 + * @returns {Object} 返回json 对象 + * @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, ' ') + 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 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 diff --git a/lib/string/byteLength.js b/lib/string/byteLength.js new file mode 100644 index 0000000..b14c5d4 --- /dev/null +++ b/lib/string/byteLength.js @@ -0,0 +1,19 @@ +/** + * 获取字符串的utf8字节长度 + * @author 王勇 + * @category string + * @alias yd_string_byteLength + * @param {String} str 字符串数据 + * @returns {String} 返回utf8字符串的字节长度 + * @example yd_string_byteLength('123abc张三') -> 12 + */ +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