1 Star 0 Fork 0

深度数据OS / js-paradise

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
UTILS.md 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
aonono 提交于 2020-05-31 17:20 . tools

重复

const repeat = (str, n) => {
    let res = ''
    while (n) {
      if (n % 2 === 1) res += str
      if (n > 1) str += str
      n >>= 1
    }
    return res
  }

缓存结果

function cached(fn) {
  const cache = Object.create(null)
  return function cachedFn (str) {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }
}

复制属性

/**
 * Mix properties into target object.
 */
function extend (to, _from) {
  for (const key in _from) {
    to[key] = _from[key]
  }
  return to
}

/**
 * Make a map and return a function for checking if a key
 * is in that map.
 */
export function makeMap (str, expectsLowerCase) {
  const map = Object.create(null)
  const list = str.split(',')
  for (let i = 0; i < list.length; i++) {
    map[list[i]] = true
  }
  return expectsLowerCase
    ? val => map[val.toLowerCase()]
    : val => map[val]
}

常用常量或方法

/**
 * Perform no operation.
 */
export function noop () {}

/**
 * Always return false.
 */
export const no = () => false

/* eslint-enable no-unused-vars */

/**
 * Return the same value.
 */
export const identity = (_) => _
1
https://gitee.com/ceshi123ceshi/js-paradise.git
git@gitee.com:ceshi123ceshi/js-paradise.git
ceshi123ceshi
js-paradise
js-paradise
master

搜索帮助