Skip to content

Moushudyx/foreslash

Repository files navigation

Foreslash

GitHub top lang codecov GitHub license NPM Version NPM Downloads NPM package gzipped size

Foreslash 是一个 Javascript 工具库,包含大量实用函数。

Foreslash is a Javascript utilities lib which contains plenty of practical functions.

文档 | Documentation

中文 | English

为何选择 Foreslash

完整类型提示

Foreslash 自带完整的类型提示,无需安装 @types/XXX

低副作用

若无特殊说明,Foreslash 的任何方法都几乎没有副作用

  • Foreslash 的方法不会修改传入的原数据,返回值将是一个新的数据。
import { shuffle } from 'foreslash'

const arr = [1, 2, 3, 4, 5, 6, 7, 8]
const shuffled = shuffle(arr) // 返回新的数组 [3, 2, 6, 5, 8, 1, 7, 4] 同时 arr 并没有受到影响

函数式编程

Foreslash 提供了诸如 currypipe 等函数式编程的方法。

  • 出于性能优化的考量,若无特殊说明,此库的任何方法都不是柯里化的。
  • Foreslash 的柯里化方法 curry 和柯里化占位符 _ramda 兼容。
import { curry, _ } from 'foreslash'

// const regTest = (regex) => (str) => regex.test(str) // 传统方法,传入参数的顺序有强制规定不够灵活
const regTest = curry((str, regex) => regex.test(str)) // 这里柯里化了一个函数使其能更灵活地复用

const testString = regTest('123')
testString(/^\d+$/) // true
testString(/^[a-z]+$/) // false

const isDigits = regTest(_, /^\d+$/) // 使用占位符来跳过填充某些参数,传统方法做不到这点
isDigits('123') // true
isDigits('abc') // false

安装与使用

pnpm add foreslash # 使用 pnpm 安装
npm install foreslash # 使用 npm 安装

Foreslash 支持 ESM、CJS、UMD 三种引入方式,推荐使用 ESM。

更多 API 请参考文档

// curry & randomString
import { _, curry, randomString } from 'foreslash'

randomString(3) // 'bcD' 或 'T30' 或 '7c5' 或 ...

const curriedRanStr = curry(randomString)

const randomABCD = curriedRanStr(_, 'ABCD')
randomABCD(3) // 'BDC' 或 'ACD' 或 'DBB' 或 ...

const random1234 = curriedRanStr(_, '1234')
random1234(3) // '431' 或 '213' 或 '241' 或 ...

// fastClone
import { fastClone } from 'foreslash'

const obj = { a: { b: { c: {} } }, map: new Map() }
obj.a.b.c.d = obj // 常见的循环引用
obj.map.set(obj, 'val') // Map 键上的循环引用

const clone = fastClone(obj)
clone === obj // false
// 处理深层级对象
clone.a.b.c === obj.a.b.c // false
clone.a.b.c.d === clone // true
// 处理 Map
clone.map === obj.map // false
clone.map.get(clone) === 'val' // true

兼容性

Foreslash 兼容任何能正确运行 ES6 代码的 Javascript 环境,包括 Node.js 和浏览器。

  • 不支持 Internet Explorer,但是使用 core-js 处理并由 babel 转译为 ES5(ES2009) 后可以使用。

开源软件

Foreslash 的诞生离不开这些开源项目:

部分方法灵感源于以下开源项目:

About

Foreslash 是一个 Javascript 工具库,包含大量实用函数。Foreslash is a Javascript utilities lib which contains plenty of practical functions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors