Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/browser/param.js
Original file line number Diff line number Diff line change
@@ -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 王勇 <https://github.com/wangyong1997>
* @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('&')
}

28 changes: 28 additions & 0 deletions lib/browser/paramToObj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


/**
* 地址栏参数转换为对象形式
* @author 王勇 <https://github.com/wangyong1997>
* @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
}
34 changes: 34 additions & 0 deletions lib/object/objectMerge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/**
* 合并两个对象,给最后一个优先级高
* @author 王勇 <https://github.com/wangyong1997>
* @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
}

19 changes: 19 additions & 0 deletions lib/string/byteLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 获取字符串的utf8字节长度
* @author 王勇 <https://github.com/wangyong1997>
* @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
}