Skip to content
Shawn Xu edited this page Apr 10, 2019 · 2 revisions

how to use

import { xxx } from '../utils'

isProd: boolean

inBrowser: boolean

isAndroid: boolean

isIOS: boolean

UA: string

warn(msg: string): void

非生产环境打印报错/报警信息

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.'
)

requireRemoteScript(src: string, callback: () => void): void

动态加载js文件

requireRemoteScript('//xxx')

extend(to: object, from: object): object

Mix properties into target object

const foo0 = {
	a: 1
}
const foo1 = {
	a: 2,
	b: 3
}
extend(foo0, foo1) // {a: 2, b: 3}

traverse(data: Array, childrenKeys: Array, fn: Function): object

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]

toObject(arr: array): object

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}

toArray(list: object, start: number = 0): array

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]

toNumber(val: any): any

Convert a input value to a number for persistence

toNumber('123') // 123
toNumber('abc') // “abc“

toString(val: any): any

Convert a value to a string

toString(123) // "123"
toString({a:1, b:2}) // "{\"a\":1,\"b\":2}"

compareObjects (object0: Object | Array, object1: Object | Array): Boolean

Determine whether the two objects are equal or not "shallowly"

compareObjects({a:1}, {b:1}) // false

getDpr(): Number

Get device pixel ratio

getDpr() // 3

functionToUrl(fn): Blob

transform a Function to Blob Url

debounce(fn = noop, delay = 300): Function

creates a debounced function that delays invoking fn until after delay milliseconds

const debouceFn = Functions.debouce(() => {
  console.log('xxx')
}, 300)

debouceFn()
debouceFn()
debouceFn() // xxx

throttle(fn = noop, interval = 300): Function

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()

randomId(prefix = '', length = 8): String

generate random id

randomId('input-item') // input-item-78756391

transformCamelCase(str: String): String

kebab-case -> camelCase