-
Notifications
You must be signed in to change notification settings - Fork 771
Utils
- how to use
- isProd: boolean
- inBrowser: boolean
- isAndroid: boolean
- isIOS: boolean
- UA: string
- warn(msg: string): void
- requireRemoteScript(src: string, callback: () => void): void
- extend(to: object, from: object): object
- traverse(data: Array, childrenKeys: Array, fn: Function): object
- toObject(arr: array): object
- toArray(list: object, start: number = 0): array
- toNumber(val: any): any
- toString(val: any): any
- compareObjects (object0: Object | Array, object1: Object | Array): Boolean
- getDpr(): Number
- functionToUrl(fn): Blob
- debounce(fn = noop, delay = 300): Function
- throttle(fn = noop, interval = 300): Function
- randomId(prefix = '', length = 8): String
- transformCamelCase(str: String): String
import { xxx } from '../utils'
非生产环境打印报错/报警信息
warn(
'You are using a whole package of mfd-mobile, ' +
'please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size.'
)
动态加载js文件
requireRemoteScript('//xxx')
Mix properties into target object
const foo0 = {
a: 1
}
const foo1 = {
a: 2,
b: 3
}
extend(foo0, foo1) // {a: 2, b: 3}
Multiple array traversal 多重数组遍历, 用于以下两种结构
[1, [2, 3], 4]
, [{text: 1, children: [2, 3]}]
// 第一种结构遍历可省略`childrenKeys`
const foo0 = [1, [2, 3], 4]
traverse(foo0,(item: any, level: Number, indexs: Array<Number>) => {
// item为叶子节点元素
// level为当前层级
// indexs为从根节点到叶子节点的所有层级的索引
// return 1 相当于执行continue
// return 2 相当于执行break
console.log(item, level, JSON.stringify(indexs))
})
// 1 0 "[0]"
// 2 0 "[1]"
// 3 0 "[2]"
// 1 0 "[0]"
// 2 1 "[1,0]"
// 3 1 "[1,1]"
// 4 0 "[2]"
// 第二种结构
const foo1 = [{
text: '排量',
options: [{
text: '1.6L'
}, {
text: '1.8L'
}, {
text: '2.0L'
}, {
text: '1.2T'
}, {
text: '1.4T'
}, {
text: '1.6T'
}]
}, {
text: '变速箱'
}]
traverse(foo0, ['options'], (item: any, level: Number, indexs: Array<Number>) => {
console.log(item, level, JSON.stringify(indexs))
})
// {"text":"1.6L"} 1 [0,0]
// {"text":"1.8L"} 1 [0,1]
// {"text":"2.0L"} 1 [0,2]
// {"text":"1.2T"} 1 [0,3]
// {"text":"1.4T"} 1 [0,4]
// {"text":"1.6T"} 1 [0,5]
Merge an Array of Objects into a single Object
const foo0 = {
a: 1
}
const foo1 = {
a: 2,
b: 3
}
toObject([foo0, foo1}]) // {a: 2, b: 3}
Convert an Array-like object to a real Array
function foo0 () {
console.log(toArray(arguments))
}
function foo1 () {
console.log(toArray(arguments, 1))
}
foo0(1, 2, 3) // [1, 2, 3]
foo1(1, 2, 3) // [2, 3]
Convert a input value to a number for persistence
toNumber('123') // 123
toNumber('abc') // “abc“
Convert a value to a string
toString(123) // "123"
toString({a:1, b:2}) // "{\"a\":1,\"b\":2}"
Determine whether the two objects are equal or not "shallowly"
compareObjects({a:1}, {b:1}) // false
Get device pixel ratio
getDpr() // 3
transform a Function to Blob Url
creates a debounced function that delays invoking fn until after delay milliseconds
const debouceFn = Functions.debouce(() => {
console.log('xxx')
}, 300)
debouceFn()
debouceFn()
debouceFn() // xxx
creates a throttled function that only invokes fn at most once per every interval milliseconds
const throttleFn = Functions.throttle(() => {
console.log('xxx')
}, 300)
throttleFn() // xxx
throttleFn()
throttleFn()
generate random id
randomId('input-item') // input-item-78756391
kebab-case -> camelCase
Welcome to the Mand Mobile wiki, please check our homepage
Copyright © 2012-2018 Didi Chuxing. All Rights Reserved