foreslash 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +97 -0
- package/README.EN.md +131 -0
- package/README.md +83 -39
- package/lib/index.cmn.cjs +91 -0
- package/lib/index.d.ts +80 -1
- package/lib/index.mjs +88 -1
- package/lib/index.umd.js +91 -0
- package/package.json +5 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
## Version 0.2.2 - 2025-03-18
|
|
4
|
+
|
|
5
|
+
Unstable version
|
|
6
|
+
|
|
7
|
+
- Feat 🥥 Functions added: `throttle` `debounce` `remove` and `clamp`
|
|
8
|
+
- Other fixes and improvements
|
|
9
|
+
|
|
10
|
+
不稳定版本
|
|
11
|
+
|
|
12
|
+
- 功能 🥥 添加函数: `throttle` `debounce` `remove` 和 `clamp`
|
|
13
|
+
- 其他修复与优化
|
|
14
|
+
|
|
15
|
+
## Version 0.2.1 - 2025-02-23
|
|
16
|
+
|
|
17
|
+
Unstable version
|
|
18
|
+
|
|
19
|
+
- Feat 🥥 Functions added: `memo`
|
|
20
|
+
- Feat 🥥 Function change: `range` now support single parameter invocation, *compatible with Radash*
|
|
21
|
+
- Fix 🥕 Bug fixed: `deepClone` now processes `DataView.buffer` properly
|
|
22
|
+
- Fix 🥕 Bug fixed: Correct type definition filepath
|
|
23
|
+
- Other fixes and improvements
|
|
24
|
+
|
|
25
|
+
不稳定版本
|
|
26
|
+
|
|
27
|
+
- 功能 🥥 添加函数: `memo`
|
|
28
|
+
- 功能 🥥 变更函数: `range` 现在支持单个参数的调用, *与 Radash 兼容*
|
|
29
|
+
- 修复 🥕 缺陷修复: `deepClone` 现在能正确处理 `DataView.buffer`
|
|
30
|
+
- 修复 🥕 缺陷修复: 修复了类型定义文件路径
|
|
31
|
+
- 其他修复与优化
|
|
32
|
+
|
|
33
|
+
## Version 0.2.0 - 2025-02-16
|
|
34
|
+
|
|
35
|
+
Unstable version
|
|
36
|
+
|
|
37
|
+
- Change 🥟 Functions changed: `caseCamel` `casePascal` `caseKebab` `caseSnake` now deprecated, will be removed in the future, use `camelCase` `pascalCase` `kebabCase` `snakeCase` instead
|
|
38
|
+
- Feat 🥥 Functions added: `isTypedArray` `range` `titleCase` `tryit` `withResolvers` and so forth
|
|
39
|
+
- Feat 🥥 Function change: `isEmpty` now validates Date object
|
|
40
|
+
- Feat 🥥 Function change: `fastClone` now clones FormData object
|
|
41
|
+
- Wip 🍉 Function wip: `deepClone` still on progress
|
|
42
|
+
- Other fixes and improvements
|
|
43
|
+
|
|
44
|
+
不稳定版本
|
|
45
|
+
|
|
46
|
+
- 变更 🥟 变更函数: `caseCamel` `casePascal` `caseKebab` `caseSnake` 弃用,将来会移除,请改用 `camelCase` `pascalCase` `kebabCase` `snakeCase`
|
|
47
|
+
- 功能 🥥 添加函数: `isTypedArray` `range` `titleCase` `tryit` `withResolvers` 等
|
|
48
|
+
- 功能 🥥 变更函数: `isEmpty` 现在会校验 Date 对象
|
|
49
|
+
- 功能 🥥 变更函数: `fastClone` 现在会复制 FormData 对象
|
|
50
|
+
- 开发中 🍉 函数开发中: `deepClone` 尚在开发中
|
|
51
|
+
- 其他修复与优化
|
|
52
|
+
|
|
53
|
+
## Version 0.1.2 - 2025-01-09
|
|
54
|
+
|
|
55
|
+
Unstable version
|
|
56
|
+
|
|
57
|
+
- Feat 🥥 Functions added: `caseConvert` `uuidV4` `ulid` and so forth
|
|
58
|
+
- Feat 🥥 Misc functions added: `acceptableFileName` `acceptableFileType` `getAcceptableExtByMIME` and `getAcceptableMIMEByExt`
|
|
59
|
+
- Fix 🥕 Bug fixed: Unexpected export `_fastClone`
|
|
60
|
+
- Other fixes and improvements
|
|
61
|
+
|
|
62
|
+
不稳定版本
|
|
63
|
+
|
|
64
|
+
- 功能 🥥 添加函数: `caseConvert` `uuidV4` `ulid` 等
|
|
65
|
+
- 功能 🥥 添加杂项函数: `acceptableFileName` `acceptableFileType` `getAcceptableExtByMIME` 和 `getAcceptableMIMEByExt`
|
|
66
|
+
- 修复 🥕 缺陷修复: 不应导出 `_fastClone`
|
|
67
|
+
- 其他修复与优化
|
|
68
|
+
|
|
69
|
+
## Version 0.1.1 - 2024-11-11
|
|
70
|
+
|
|
71
|
+
Unstable version
|
|
72
|
+
|
|
73
|
+
- Feat 🥥 Functions added: `isEmpty` `shuffle` `isPrimitive` and so forth
|
|
74
|
+
- Fix 🥕 Bug fixed: `isNil(document.all)` expected to be `false`
|
|
75
|
+
- Other fixes and improvements
|
|
76
|
+
|
|
77
|
+
不稳定版本
|
|
78
|
+
|
|
79
|
+
- 功能 🥥 添加函数: `isEmpty` `shuffle` `isPrimitive` 等
|
|
80
|
+
- 修复 🥕 缺陷修复: `isNil(document.all)` 应为 `false`
|
|
81
|
+
- 其他修复与优化
|
|
82
|
+
|
|
83
|
+
## Version 0.1.0 - 2024-09-28
|
|
84
|
+
|
|
85
|
+
Unstable version
|
|
86
|
+
|
|
87
|
+
- Feat 🥥 Functions added: `fastClone` `compose` `getTag` and so forth
|
|
88
|
+
- Feat 🥥 Function change: `curry` now supports optional arguments
|
|
89
|
+
- Fix 🥕 Bug fixed: Type hint for `passWith` now corrected
|
|
90
|
+
- Other fixes and improvements
|
|
91
|
+
|
|
92
|
+
不稳定版本
|
|
93
|
+
|
|
94
|
+
- 功能 🥥 添加函数: `fastClone` `compose` `getTag` 等
|
|
95
|
+
- 功能 🥥 变更函数: `curry` 支持可选参数
|
|
96
|
+
- 修复 🥕 缺陷修复: 修复了 `passWith` 的类型提示
|
|
97
|
+
- 其他修复与优化
|
package/README.EN.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Foreslash
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<p align="center">
|
|
5
|
+
<img src="https://img.shields.io/github/languages/top/Moushudyx/foreslash" alt="GitHub top lang" />
|
|
6
|
+
<img src="https://img.shields.io/badge/license-Mulan_PSL_v2-blue" alt="GitHub license" />
|
|
7
|
+
<img src="https://img.shields.io/npm/v/foreslash" alt="NPM Version" />
|
|
8
|
+
<img src="https://img.shields.io/npm/dm/foreslash" alt="NPM Downloads" />
|
|
9
|
+
<img src="https://img.shields.io/bundlejs/size/foreslash?label=gzipped" alt="NPM package gzipped size" />
|
|
10
|
+
</p>
|
|
11
|
+
<p align="center">
|
|
12
|
+
Foreslash is a Javascript utilities lib which contains plenty of practical functions.
|
|
13
|
+
</p>
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://moushudyx.github.io/foreslash/en">Documentation</a>
|
|
16
|
+
</p>
|
|
17
|
+
<p align="center">
|
|
18
|
+
<a href="./README.md">中文</a>
|
|
19
|
+
|
|
|
20
|
+
<span>English</span>
|
|
21
|
+
</p>
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
## Why Foreslash
|
|
25
|
+
|
|
26
|
+
### Type definitions
|
|
27
|
+
|
|
28
|
+
Foreslash comes with comprehensive type definitions out of the box, eliminating the need to install `@types/XXX`.
|
|
29
|
+
|
|
30
|
+
- Foreslash is developed using [typescript](https://github.com/microsoft/TypeScript), and we uses [jest](https://github.com/facebook/jest) and [ts-jest](https://github.com/kulshekhar/ts-jest) for unit testing.
|
|
31
|
+
|
|
32
|
+
### Minimal Side Effects
|
|
33
|
+
|
|
34
|
+
Unless explicitly stated, methods in Foreslash are designed with **minimal side effects**.
|
|
35
|
+
|
|
36
|
+
- Methods from Foreslash do not modify mutate input data, they return a new data instead.
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { shuffle } from 'foreslash'
|
|
40
|
+
|
|
41
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
42
|
+
const shuffled = shuffle(arr) // Returns a new array [3, 2, 6, 5, 8, 1, 7, 4] while `arr` remains unchanged
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Functional Programming
|
|
46
|
+
|
|
47
|
+
Foreslash offers functional programming utilities methods like `curry`、`pipe`.
|
|
48
|
+
|
|
49
|
+
- For performance reasons, methods are **not curried** unless explicitly stated.
|
|
50
|
+
- The curry method `curry` and the placeholder `_` in Foreslash are compatible with [Ramda](https://github.com/ramda/ramda).
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { curry, _ } from 'foreslash'
|
|
54
|
+
|
|
55
|
+
// const regTest = (regex) => (str) => regex.test(str) // The order of arguments is strictly defined and less flexible
|
|
56
|
+
const regTest = curry((str, regex) => regex.test(str)) // Currying a function to make it more flexible and reusable
|
|
57
|
+
|
|
58
|
+
const testString = regTest('123')
|
|
59
|
+
testString(/^\d+$/) // true
|
|
60
|
+
testString(/^[a-z]+$/) // false
|
|
61
|
+
|
|
62
|
+
const isDigits = regTest(_, /^\d+$/) // Using a placeholder to skip certain arguments
|
|
63
|
+
isDigits('123') // true
|
|
64
|
+
isDigits('abc') // false
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Install & Usage
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install foreslash # Install with npm
|
|
71
|
+
yarn add foreslash # Install with yarn
|
|
72
|
+
pnpm install foreslash # Install with pnpm
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Foreslash supports ESM/CJS/UMD, and ESM is recommended.
|
|
76
|
+
|
|
77
|
+
Visit [document](https://moushudyx.github.io/foreslash) for more API.
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
// curry & randomString
|
|
81
|
+
import { _, curry, randomString } from 'foreslash'
|
|
82
|
+
|
|
83
|
+
randomString(3) // 'bcD' or 'T30' or '7c5' or ...
|
|
84
|
+
|
|
85
|
+
const curriedRanStr = curry(randomString)
|
|
86
|
+
|
|
87
|
+
const randomABCD = curriedRanStr(_, 'ABCD')
|
|
88
|
+
randomABCD(3) // 'BDC' or 'ACD' or 'DBB' or ...
|
|
89
|
+
|
|
90
|
+
const random1234 = curriedRanStr(_, '1234')
|
|
91
|
+
random1234(3) // '431' or '213' or '241' or ...
|
|
92
|
+
|
|
93
|
+
// fastClone
|
|
94
|
+
import { fastClone } from 'foreslash'
|
|
95
|
+
|
|
96
|
+
const obj = { a: { b: { c: {} } }, map: new Map() }
|
|
97
|
+
obj.a.b.c.d = obj
|
|
98
|
+
obj.map.set(obj, 'val')
|
|
99
|
+
|
|
100
|
+
const clone = fastClone(obj)
|
|
101
|
+
clone === obj // false
|
|
102
|
+
// clone Deep
|
|
103
|
+
clone.a.b.c === obj.a.b.c // false
|
|
104
|
+
clone.a.b.c.d === clone // true
|
|
105
|
+
// clone Map
|
|
106
|
+
clone.map === obj.map // false
|
|
107
|
+
clone.map.get(clone) === 'val' // true
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Compatibility
|
|
111
|
+
|
|
112
|
+
Foreslash compatible with any Javascript runtime that runs ES6 code, including Node.js and web browser.
|
|
113
|
+
|
|
114
|
+
- Internet Explorer is not supported, [core-js](https://github.com/zloirock/core-js) and transformation into ES5(ES2009) via [babel](https://babeljs.io/) might be needed.
|
|
115
|
+
|
|
116
|
+
## Credits
|
|
117
|
+
|
|
118
|
+
Foreslash based on the following open source projects.
|
|
119
|
+
|
|
120
|
+
- [jest](https://github.com/facebook/jest)
|
|
121
|
+
- [rollup](https://github.com/rollup/rollup)
|
|
122
|
+
- [ts-jest](https://github.com/kulshekhar/ts-jest)
|
|
123
|
+
- [ts-toolbelt](https://github.com/millsp/ts-toolbelt)
|
|
124
|
+
- [typescript](https://github.com/microsoft/TypeScript)
|
|
125
|
+
- [vitepress](https://github.com/vuejs/vitepress)
|
|
126
|
+
- [yarn](https://github.com/yarnpkg/yarn)
|
|
127
|
+
|
|
128
|
+
Some of the methods are inspired by the following open source projects.
|
|
129
|
+
|
|
130
|
+
- [radash](https://github.com/sodiray/radash)
|
|
131
|
+
- [ramda](https://github.com/ramda/ramda)
|
package/README.md
CHANGED
|
@@ -1,36 +1,77 @@
|
|
|
1
1
|
# Foreslash
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
<div align="center">
|
|
4
|
+
<p align="center">
|
|
5
|
+
<img src="https://img.shields.io/github/languages/top/Moushudyx/foreslash" alt="GitHub top lang" />
|
|
6
|
+
<a href="https://github.com/moushudyx/foreslash/blob/master/LICENSE">
|
|
7
|
+
<img src="https://img.shields.io/badge/license-Mulan_PSL_v2-blue" alt="GitHub license" />
|
|
8
|
+
</a>
|
|
9
|
+
<img src="https://img.shields.io/npm/v/foreslash" alt="NPM Version" />
|
|
10
|
+
<img src="https://img.shields.io/npm/dm/foreslash" alt="NPM Downloads" />
|
|
11
|
+
<img src="https://img.shields.io/bundlejs/size/foreslash?label=gzipped" alt="NPM package gzipped size" />
|
|
12
|
+
</p>
|
|
13
|
+
<p align="center">
|
|
14
|
+
Foreslash 是一个 Javascript 工具库,包含大量实用函数。
|
|
15
|
+
</p>
|
|
16
|
+
<p align="center">
|
|
17
|
+
Foreslash is a Javascript utilities lib which contains plenty of practical functions.
|
|
18
|
+
</p>
|
|
19
|
+
<p align="center">
|
|
20
|
+
<a href="https://moushudyx.github.io/foreslash">文档</a>
|
|
21
|
+
|
|
|
22
|
+
<a href="https://moushudyx.github.io/foreslash/en">Documentation</a>
|
|
23
|
+
</p>
|
|
24
|
+
<p align="center">
|
|
25
|
+
<span>中文</span>
|
|
26
|
+
|
|
|
27
|
+
<a href="./README.EN.md">English</a>
|
|
28
|
+
</p>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
## 为何选择 Foreslash
|
|
32
|
+
|
|
33
|
+
### 完整类型提示
|
|
34
|
+
|
|
35
|
+
Foreslash 自带完整的类型提示,无需安装 `@types/XXX`。
|
|
36
|
+
|
|
37
|
+
- Foreslash 使用 [typescript](https://github.com/microsoft/TypeScript) 开发,并使用 [jest](https://github.com/facebook/jest) 和 [ts-jest](https://github.com/kulshekhar/ts-jest) 编写单元测试。
|
|
38
|
+
|
|
39
|
+
### 低副作用
|
|
40
|
+
|
|
41
|
+
若无特殊说明,Foreslash 的任何方法都**几乎没有副作用**
|
|
42
|
+
|
|
43
|
+
- Foreslash 的方法不会修改传入的原数据,返回值将是一个新的数据。
|
|
8
44
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Foreslash is a Javascript utilities lib which contains plenty of practical functions.
|
|
12
|
-
|
|
13
|
-
[文档](https://moushudyx.github.io/foreslash) | [Documentation](https://moushudyx.github.io/foreslash/en)
|
|
14
|
-
|
|
15
|
-
## 设计理念 Design Concept
|
|
45
|
+
```js
|
|
46
|
+
import { shuffle } from 'foreslash'
|
|
16
47
|
|
|
17
|
-
|
|
48
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
49
|
+
const shuffled = shuffle(arr) // 返回新的数组 [3, 2, 6, 5, 8, 1, 7, 4] 同时 arr 并没有受到影响
|
|
50
|
+
```
|
|
18
51
|
|
|
19
|
-
|
|
52
|
+
### 函数式编程
|
|
20
53
|
|
|
21
|
-
|
|
54
|
+
Foreslash 提供了诸如 `curry`、`pipe` 等函数式编程的方法。
|
|
22
55
|
|
|
23
|
-
|
|
56
|
+
- 出于性能优化的考量,若无特殊说明,此库的任何方法都**不是柯里化**的。
|
|
57
|
+
- Foreslash 的柯里化方法 `curry` 和柯里化占位符 `_` 与 [ramda](https://github.com/ramda/ramda) 兼容。
|
|
24
58
|
|
|
25
|
-
|
|
59
|
+
```js
|
|
60
|
+
import { curry, _ } from 'foreslash'
|
|
26
61
|
|
|
27
|
-
|
|
62
|
+
// const regTest = (regex) => (str) => regex.test(str) // 传统方法,传入参数的顺序有强制规定不够灵活
|
|
63
|
+
const regTest = curry((str, regex) => regex.test(str)) // 这里柯里化了一个函数使其能更灵活地复用
|
|
28
64
|
|
|
29
|
-
|
|
65
|
+
const testString = regTest('123')
|
|
66
|
+
testString(/^\d+$/) // true
|
|
67
|
+
testString(/^[a-z]+$/) // false
|
|
30
68
|
|
|
31
|
-
|
|
69
|
+
const isDigits = regTest(_, /^\d+$/) // 使用占位符来跳过填充某些参数,传统方法做不到这点
|
|
70
|
+
isDigits('123') // true
|
|
71
|
+
isDigits('abc') // false
|
|
72
|
+
```
|
|
32
73
|
|
|
33
|
-
## 安装与使用
|
|
74
|
+
## 安装与使用
|
|
34
75
|
|
|
35
76
|
```bash
|
|
36
77
|
npm install foreslash # 使用 npm 安装
|
|
@@ -38,52 +79,50 @@ yarn add foreslash # 使用 yarn 安装
|
|
|
38
79
|
pnpm install foreslash # 使用 pnpm 安装
|
|
39
80
|
```
|
|
40
81
|
|
|
82
|
+
Foreslash 支持 ESM、CJS、UMD 三种引入方式,推荐使用 ESM。
|
|
83
|
+
|
|
84
|
+
更多 API 请参考[文档](https://moushudyx.github.io/foreslash)。
|
|
85
|
+
|
|
41
86
|
```js
|
|
42
87
|
// curry & randomString
|
|
43
88
|
import { _, curry, randomString } from 'foreslash'
|
|
44
89
|
|
|
45
|
-
randomString(3) // 'bcD'
|
|
90
|
+
randomString(3) // 'bcD' 或 'T30' 或 '7c5' 或 ...
|
|
46
91
|
|
|
47
92
|
const curriedRanStr = curry(randomString)
|
|
48
93
|
|
|
49
94
|
const randomABCD = curriedRanStr(_, 'ABCD')
|
|
50
|
-
randomABCD(3) // 'BDC'
|
|
95
|
+
randomABCD(3) // 'BDC' 或 'ACD' 或 'DBB' 或 ...
|
|
51
96
|
|
|
52
97
|
const random1234 = curriedRanStr(_, '1234')
|
|
53
|
-
random1234(3) // '431'
|
|
98
|
+
random1234(3) // '431' 或 '213' 或 '241' 或 ...
|
|
54
99
|
|
|
55
100
|
// fastClone
|
|
56
101
|
import { fastClone } from 'foreslash'
|
|
57
102
|
|
|
58
103
|
const obj = { a: { b: { c: {} } }, map: new Map() }
|
|
59
|
-
obj.a.b.c.d = obj
|
|
60
|
-
obj.map.set(obj, 'val')
|
|
104
|
+
obj.a.b.c.d = obj // 常见的循环引用
|
|
105
|
+
obj.map.set(obj, 'val') // Map 键上的循环引用
|
|
61
106
|
|
|
62
107
|
const clone = fastClone(obj)
|
|
63
108
|
clone === obj // false
|
|
64
|
-
//
|
|
109
|
+
// 处理深层级对象
|
|
65
110
|
clone.a.b.c === obj.a.b.c // false
|
|
66
111
|
clone.a.b.c.d === clone // true
|
|
67
|
-
//
|
|
112
|
+
// 处理 Map
|
|
68
113
|
clone.map === obj.map // false
|
|
69
114
|
clone.map.get(clone) === 'val' // true
|
|
70
115
|
```
|
|
71
116
|
|
|
72
|
-
## 兼容性
|
|
73
|
-
|
|
74
|
-
此库兼容任何能正确运行 ES6 代码的 Javascript 环境,包括 node.js 和浏览器
|
|
75
|
-
|
|
76
|
-
- 不支持 Internet Explorer,但是使用 [core-js](https://github.com/zloirock/core-js) 处理并由 [babel](https://babeljs.io/) 转译为 ES5(ES2009) 后可以使用
|
|
117
|
+
## 兼容性
|
|
77
118
|
|
|
78
|
-
|
|
119
|
+
Foreslash 兼容任何能正确运行 ES6 代码的 Javascript 环境,包括 Node.js 和浏览器。
|
|
79
120
|
|
|
80
|
-
|
|
121
|
+
- 不支持 Internet Explorer,但是使用 [core-js](https://github.com/zloirock/core-js) 处理并由 [babel](https://babeljs.io/) 转译为 ES5(ES2009) 后可以使用。
|
|
81
122
|
|
|
82
|
-
|
|
123
|
+
## 开源软件
|
|
83
124
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
## 开源软件 Credits
|
|
125
|
+
Foreslash 的诞生离不开这些开源项目:
|
|
87
126
|
|
|
88
127
|
- [jest](https://github.com/facebook/jest)
|
|
89
128
|
- [rollup](https://github.com/rollup/rollup)
|
|
@@ -92,3 +131,8 @@ clone.map.get(clone) === 'val' // true
|
|
|
92
131
|
- [typescript](https://github.com/microsoft/TypeScript)
|
|
93
132
|
- [vitepress](https://github.com/vuejs/vitepress)
|
|
94
133
|
- [yarn](https://github.com/yarnpkg/yarn)
|
|
134
|
+
|
|
135
|
+
部分方法灵感源于以下开源项目:
|
|
136
|
+
|
|
137
|
+
- [radash](https://github.com/sodiray/radash)
|
|
138
|
+
- [ramda](https://github.com/ramda/ramda)
|
package/lib/index.cmn.cjs
CHANGED
|
@@ -47,6 +47,26 @@ function range(start, end, stepOrOptions) {
|
|
|
47
47
|
return res;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
function remove(arr, ...item) {
|
|
51
|
+
const removeSet = new Set();
|
|
52
|
+
const judgerList = [];
|
|
53
|
+
for (let i = 0; i < item.length; i++) {
|
|
54
|
+
const cond = item[i];
|
|
55
|
+
if (typeof cond === 'function')
|
|
56
|
+
judgerList.push(cond);
|
|
57
|
+
else
|
|
58
|
+
removeSet.add(cond);
|
|
59
|
+
}
|
|
60
|
+
const res = [];
|
|
61
|
+
for (let i = 0; i < arr.length; i++) {
|
|
62
|
+
const el = arr[i];
|
|
63
|
+
if (removeSet.has(el) || judgerList.some((judger) => judger(el)))
|
|
64
|
+
continue;
|
|
65
|
+
res.push(el);
|
|
66
|
+
}
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
|
|
50
70
|
function sleep(time = 1000) {
|
|
51
71
|
return new Promise((res) => {
|
|
52
72
|
setTimeout(res, time);
|
|
@@ -285,6 +305,23 @@ function withResolvers(PromiseLike = Promise) {
|
|
|
285
305
|
return { promise, resolve, reject };
|
|
286
306
|
}
|
|
287
307
|
|
|
308
|
+
function clamp(num, min, max, options) {
|
|
309
|
+
var _a, _b;
|
|
310
|
+
if (isNaN(min))
|
|
311
|
+
throw new Error('Invalid min parameter');
|
|
312
|
+
if (isNaN(max))
|
|
313
|
+
throw new Error('Invalid max parameter');
|
|
314
|
+
if (max < min) {
|
|
315
|
+
[min, max] = [max, min];
|
|
316
|
+
}
|
|
317
|
+
const { default: def, defaultMin: _dMin, defaultMax: _dMax } = options || {};
|
|
318
|
+
const defaultMin = (_a = _dMin !== null && _dMin !== void 0 ? _dMin : def) !== null && _a !== void 0 ? _a : min;
|
|
319
|
+
const defaultMax = (_b = _dMax !== null && _dMax !== void 0 ? _dMax : def) !== null && _b !== void 0 ? _b : max;
|
|
320
|
+
if (isNaN(num))
|
|
321
|
+
return defaultMin;
|
|
322
|
+
return num < min ? defaultMin : num > max ? defaultMax : num;
|
|
323
|
+
}
|
|
324
|
+
|
|
288
325
|
const mimeMap = {
|
|
289
326
|
application: {
|
|
290
327
|
acrobat: ['pdf'],
|
|
@@ -1283,6 +1320,49 @@ function _curryMore(fn) {
|
|
|
1283
1320
|
}
|
|
1284
1321
|
}
|
|
1285
1322
|
|
|
1323
|
+
function _throttle(fn, delay, options) {
|
|
1324
|
+
var _a, _b;
|
|
1325
|
+
const trailing = (_a = options === null || options === void 0 ? void 0 : options.trailing) !== null && _a !== void 0 ? _a : false;
|
|
1326
|
+
const leading = (_b = options === null || options === void 0 ? void 0 : options.leading) !== null && _b !== void 0 ? _b : true;
|
|
1327
|
+
let timer = null;
|
|
1328
|
+
let lastTime = 0;
|
|
1329
|
+
const clearTimer = () => {
|
|
1330
|
+
if (timer) {
|
|
1331
|
+
clearInterval(timer);
|
|
1332
|
+
timer = null;
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
const reset = () => {
|
|
1336
|
+
clearTimer();
|
|
1337
|
+
lastTime = 0;
|
|
1338
|
+
};
|
|
1339
|
+
return Object.assign(function (...args) {
|
|
1340
|
+
const now = Date.now();
|
|
1341
|
+
const timeGap = now - lastTime - delay;
|
|
1342
|
+
if (timeGap >= 0) {
|
|
1343
|
+
lastTime = now;
|
|
1344
|
+
clearTimer();
|
|
1345
|
+
}
|
|
1346
|
+
if (timeGap >= 0 && leading) {
|
|
1347
|
+
fn.apply(this, args);
|
|
1348
|
+
}
|
|
1349
|
+
else if (timeGap >= 0 || (timeGap < 0 && trailing && !timer)) {
|
|
1350
|
+
timer = setTimeout(() => {
|
|
1351
|
+
lastTime = Date.now();
|
|
1352
|
+
timer = null;
|
|
1353
|
+
fn.apply(this, args);
|
|
1354
|
+
}, timeGap >= 0 ? delay : -timeGap);
|
|
1355
|
+
}
|
|
1356
|
+
}, { reset });
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
function debounce(fn, delay, options) {
|
|
1360
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1361
|
+
throw new Error('Invalid delay parameter');
|
|
1362
|
+
}
|
|
1363
|
+
return _throttle(fn, delay, Object.assign({ trailing: true, leading: false }, options));
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1286
1366
|
function _cloneArray(obj, map, cloner, ...args) {
|
|
1287
1367
|
const res = obj.slice();
|
|
1288
1368
|
map.set(obj, res);
|
|
@@ -1547,6 +1627,13 @@ function pipe(...pipeFunc) {
|
|
|
1547
1627
|
};
|
|
1548
1628
|
}
|
|
1549
1629
|
|
|
1630
|
+
function throttle(fn, delay, options) {
|
|
1631
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1632
|
+
throw new Error('Invalid delay parameter');
|
|
1633
|
+
}
|
|
1634
|
+
return _throttle(fn, delay, Object.assign({ trailing: false, leading: true }, options));
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1550
1637
|
exports._ = _;
|
|
1551
1638
|
exports.acceptableFileName = acceptableFileName;
|
|
1552
1639
|
exports.acceptableFileType = acceptableFileType;
|
|
@@ -1556,8 +1643,10 @@ exports.caseConvert = caseConvert;
|
|
|
1556
1643
|
exports.caseKebab = caseKebab;
|
|
1557
1644
|
exports.casePascal = casePascal;
|
|
1558
1645
|
exports.caseSnake = caseSnake;
|
|
1646
|
+
exports.clamp = clamp;
|
|
1559
1647
|
exports.compose = compose;
|
|
1560
1648
|
exports.curry = _curryMore;
|
|
1649
|
+
exports.debounce = debounce;
|
|
1561
1650
|
exports.deepClone = deepClone;
|
|
1562
1651
|
exports.fastClone = fastClone;
|
|
1563
1652
|
exports.getAcceptableExtByMIME = getAcceptableExtByMIME;
|
|
@@ -1627,10 +1716,12 @@ exports.randomInt = randomInt;
|
|
|
1627
1716
|
exports.randomIntFloor = randomIntFloor;
|
|
1628
1717
|
exports.randomString = randomString;
|
|
1629
1718
|
exports.range = range;
|
|
1719
|
+
exports.remove = remove;
|
|
1630
1720
|
exports.shuffle = shuffle;
|
|
1631
1721
|
exports.sleep = sleep;
|
|
1632
1722
|
exports.snakeCase = snakeCase;
|
|
1633
1723
|
exports.splitWords = splitWords;
|
|
1724
|
+
exports.throttle = throttle;
|
|
1634
1725
|
exports.titleCase = titleCase;
|
|
1635
1726
|
exports.tryit = tryit;
|
|
1636
1727
|
exports.ulid = ulid;
|
package/lib/index.d.ts
CHANGED
|
@@ -57,6 +57,26 @@ declare function range(start: number, end: number, step?: number): number[];
|
|
|
57
57
|
*/
|
|
58
58
|
declare function range<T = number>(start: number, end: number, options?: RangeOptions<T>): T[];
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* 根据给定的条件来移除数组中的元素(不会影响原数组)
|
|
62
|
+
* @param arr 需要移除部分元素的数组
|
|
63
|
+
* @param item 需要移除的元素, 或判断某个元素是否应该移除的函数
|
|
64
|
+
* @returns 返回移除后的新数组
|
|
65
|
+
* @example
|
|
66
|
+
* ```js
|
|
67
|
+
* // 基本用法
|
|
68
|
+
* remove([1, 2, 3, 4, 5], 5) // [1, 2, 3, 4]
|
|
69
|
+
* remove([1, 2, 3, 4, 5], (num) => num % 2) // [1, 3, 5]
|
|
70
|
+
* // 可以传入多个过滤条件
|
|
71
|
+
* remove([1, 2, 3, 4, 5], 3, 4, 5) // [1, 2]
|
|
72
|
+
* remove([1, 2, 3, 4, 5], (num) => num % 2, (num) => num > 4) // [1, 3]
|
|
73
|
+
* // 可以混合传入过滤条件
|
|
74
|
+
* remove([1, 2, 3, 4, 5], (num) => num % 2, (num) => num > 4, 3) // [1]
|
|
75
|
+
* ```
|
|
76
|
+
* @version 0.2.2
|
|
77
|
+
*/
|
|
78
|
+
declare function remove<T>(arr: ArrayLike<T>, ...item: (T | ((val: T) => unknown))[]): T[];
|
|
79
|
+
|
|
60
80
|
/**
|
|
61
81
|
* 延迟一定时间
|
|
62
82
|
* @param time 延迟时间, 单位为毫秒(ms), 默认为 `1000` (即 1 秒)
|
|
@@ -701,6 +721,32 @@ declare function isWrapperSymbol(value: unknown): value is Symbol;
|
|
|
701
721
|
*/
|
|
702
722
|
declare function isWrapperBigInt(value: unknown): value is BigInt;
|
|
703
723
|
|
|
724
|
+
/**
|
|
725
|
+
* 将一个数字限制在指定范围内
|
|
726
|
+
* @param num 初始值
|
|
727
|
+
* @param min 最小值, 没有默认值, 初始值小于最小值或初始值不为数字时返回最小值
|
|
728
|
+
* @param max 最大值, 没有默认值, 初始值大于最大值时返回最大值
|
|
729
|
+
* @param options 配置项, 可以配置默认值等
|
|
730
|
+
* - `default` 默认值, 如果初始值不在范围内, 则返回默认值
|
|
731
|
+
* - `defaultMin` 初始值小于最小值时返回该值, 覆盖 `default` 参数
|
|
732
|
+
* - `defaultMax` 初始值大于最大值时返回该值
|
|
733
|
+
* @returns 返回一个在指定范围内的数字
|
|
734
|
+
* @example
|
|
735
|
+
* ```js
|
|
736
|
+
* clamp(5, 0, 10) // 5
|
|
737
|
+
* clamp(15, 0, 10) // 10
|
|
738
|
+
* clamp(-5, 0, 10) // 0
|
|
739
|
+
* clamp(5, 0, 10, { default: 6 }) // 5
|
|
740
|
+
* clamp(15, 0, 10, { default: 6 }) // 6
|
|
741
|
+
* clamp(-5, 0, 10, { default: 6 }) // 6
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
declare function clamp(num: number, min: number, max: number, options?: {
|
|
745
|
+
default?: number;
|
|
746
|
+
defaultMin?: number;
|
|
747
|
+
defaultMax?: number;
|
|
748
|
+
}): number;
|
|
749
|
+
|
|
704
750
|
/**
|
|
705
751
|
* 根据文件名称判断是否匹配支持的文件类型,需要注意 `.C` 与 `.c` 的区别
|
|
706
752
|
* - `text/x-c++src` 对应 `.C`
|
|
@@ -1271,6 +1317,26 @@ declare function _curryMore<Arg1, Arg2, Res>(fn: (arg1: Arg1, arg2: Arg2) => Res
|
|
|
1271
1317
|
declare function _curryMore<Arg1, Arg2, Arg3, Res>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3) => Res): F.Curry<(arg1: Arg1, arg2: Arg2, arg3: Arg3) => Res>;
|
|
1272
1318
|
declare function _curryMore<Args extends Array<any>, Res>(fn: (...args: Args) => Res): F.Curry<(...args: Args) => Res>;
|
|
1273
1319
|
|
|
1320
|
+
interface ThrottleOptions {
|
|
1321
|
+
/** 节流时触发的最后一次调用是否执行, 默认为否 */
|
|
1322
|
+
trailing?: boolean;
|
|
1323
|
+
/** 第一次触发是否直接执行, 默认为是 */
|
|
1324
|
+
leading?: boolean;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
/**
|
|
1328
|
+
* 将一个高频调用的函数处理为防抖函数, 一段时间内多次调用会合并为一次
|
|
1329
|
+
* - 比如说 `delay` 设置为 120, 那么方法在 120ms 内多次触发, 也只会在第一次触发的 120ms 后执行一次
|
|
1330
|
+
* @param fn 需要防抖的函数
|
|
1331
|
+
* @param delay 防抖时间间隔, 单位毫秒, 建议 100 以上
|
|
1332
|
+
* @param options 防抖函数的配置, 默认值为 `{ trailing: true, leading: false }`
|
|
1333
|
+
* @returns 一个防抖处理的函数, 你可以使用其上的 `reset` 方法重置节流函数内部状态(会停止计划中的函数执行)
|
|
1334
|
+
* @version 0.2.2
|
|
1335
|
+
*/
|
|
1336
|
+
declare function debounce<T extends any[]>(fn: (...args: T) => any, delay: number, options?: ThrottleOptions): ((this: any, ...args: T) => void) & {
|
|
1337
|
+
reset: () => void;
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1274
1340
|
interface CustomCloner<T = any> {
|
|
1275
1341
|
cloner(val: T, map: Map<any, any>): T;
|
|
1276
1342
|
judger(val: any): boolean;
|
|
@@ -1552,4 +1618,17 @@ declare function pipe<PipeArgs extends any[], PipeResult, Mid1, Mid2, Mid3, Mid4
|
|
|
1552
1618
|
declare function pipe<PipeArgs extends any[], PipeResult, Mid1, Mid2, Mid3, Mid4, Mid5, Mid6, Mid7>(...pipeFunc: PipeFuncList8<PipeArgs, PipeResult, Mid1, Mid2, Mid3, Mid4, Mid5, Mid6, Mid7>): (...args: PipeArgs) => PipeResult;
|
|
1553
1619
|
declare function pipe<PipeArgs extends any[], PipeResult, Mid1, Mid2, Mid3, Mid4, Mid5, Mid6, Mid7>(...pipeFunc: PipeFuncListMore<PipeArgs, PipeResult, Mid1, Mid2, Mid3, Mid4, Mid5, Mid6, Mid7>): (...args: PipeArgs) => PipeResult;
|
|
1554
1620
|
|
|
1555
|
-
|
|
1621
|
+
/**
|
|
1622
|
+
* 将一个高频调用的函数处理为节流函数, 一段时间内只会调用一次
|
|
1623
|
+
* - 比如说 `delay` 设置为 120, 那么方法在第一次触发后执行, 随后的 120ms 内多次触发也不会再执行
|
|
1624
|
+
* @param fn 需要节流的函数
|
|
1625
|
+
* @param delay 节流时间间隔, 单位毫秒, 建议 100 以上
|
|
1626
|
+
* @param options 节流函数的配置, 默认值为 `{ trailing: false, leading: true }`
|
|
1627
|
+
* @returns 一个节流处理的函数, 你可以使用其上的 `reset` 方法重置节流函数内部状态(会停止计划中的函数执行)
|
|
1628
|
+
* @version 0.2.2
|
|
1629
|
+
*/
|
|
1630
|
+
declare function throttle<T extends any[]>(fn: (...args: T) => any, delay: number, options?: ThrottleOptions): ((this: any, ...args: T) => void) & {
|
|
1631
|
+
reset: () => void;
|
|
1632
|
+
};
|
|
1633
|
+
|
|
1634
|
+
export { type CloneOptions, type CustomCloner, type RangeOptions, type TypedArray, _, acceptableFileName, acceptableFileType, camelCase, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, clamp, compose, _curryMore as curry, debounce, deepClone, fastClone, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getTag, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBoolean, isBuffer, isDataView, isDate, isEmpty, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isNil, isNull, isNumber, isObject, isPlaceholder, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, memo, noop, not, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomHexString, randomInt, randomIntFloor, randomString, range, remove, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uuidNil, uuidV4, withResolvers };
|
package/lib/index.mjs
CHANGED
|
@@ -45,6 +45,26 @@ function range(start, end, stepOrOptions) {
|
|
|
45
45
|
return res;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
function remove(arr, ...item) {
|
|
49
|
+
const removeSet = new Set();
|
|
50
|
+
const judgerList = [];
|
|
51
|
+
for (let i = 0; i < item.length; i++) {
|
|
52
|
+
const cond = item[i];
|
|
53
|
+
if (typeof cond === 'function')
|
|
54
|
+
judgerList.push(cond);
|
|
55
|
+
else
|
|
56
|
+
removeSet.add(cond);
|
|
57
|
+
}
|
|
58
|
+
const res = [];
|
|
59
|
+
for (let i = 0; i < arr.length; i++) {
|
|
60
|
+
const el = arr[i];
|
|
61
|
+
if (removeSet.has(el) || judgerList.some((judger) => judger(el)))
|
|
62
|
+
continue;
|
|
63
|
+
res.push(el);
|
|
64
|
+
}
|
|
65
|
+
return res;
|
|
66
|
+
}
|
|
67
|
+
|
|
48
68
|
function sleep(time = 1000) {
|
|
49
69
|
return new Promise((res) => {
|
|
50
70
|
setTimeout(res, time);
|
|
@@ -283,6 +303,23 @@ function withResolvers(PromiseLike = Promise) {
|
|
|
283
303
|
return { promise, resolve, reject };
|
|
284
304
|
}
|
|
285
305
|
|
|
306
|
+
function clamp(num, min, max, options) {
|
|
307
|
+
var _a, _b;
|
|
308
|
+
if (isNaN(min))
|
|
309
|
+
throw new Error('Invalid min parameter');
|
|
310
|
+
if (isNaN(max))
|
|
311
|
+
throw new Error('Invalid max parameter');
|
|
312
|
+
if (max < min) {
|
|
313
|
+
[min, max] = [max, min];
|
|
314
|
+
}
|
|
315
|
+
const { default: def, defaultMin: _dMin, defaultMax: _dMax } = options || {};
|
|
316
|
+
const defaultMin = (_a = _dMin !== null && _dMin !== void 0 ? _dMin : def) !== null && _a !== void 0 ? _a : min;
|
|
317
|
+
const defaultMax = (_b = _dMax !== null && _dMax !== void 0 ? _dMax : def) !== null && _b !== void 0 ? _b : max;
|
|
318
|
+
if (isNaN(num))
|
|
319
|
+
return defaultMin;
|
|
320
|
+
return num < min ? defaultMin : num > max ? defaultMax : num;
|
|
321
|
+
}
|
|
322
|
+
|
|
286
323
|
const mimeMap = {
|
|
287
324
|
application: {
|
|
288
325
|
acrobat: ['pdf'],
|
|
@@ -1281,6 +1318,49 @@ function _curryMore(fn) {
|
|
|
1281
1318
|
}
|
|
1282
1319
|
}
|
|
1283
1320
|
|
|
1321
|
+
function _throttle(fn, delay, options) {
|
|
1322
|
+
var _a, _b;
|
|
1323
|
+
const trailing = (_a = options === null || options === void 0 ? void 0 : options.trailing) !== null && _a !== void 0 ? _a : false;
|
|
1324
|
+
const leading = (_b = options === null || options === void 0 ? void 0 : options.leading) !== null && _b !== void 0 ? _b : true;
|
|
1325
|
+
let timer = null;
|
|
1326
|
+
let lastTime = 0;
|
|
1327
|
+
const clearTimer = () => {
|
|
1328
|
+
if (timer) {
|
|
1329
|
+
clearInterval(timer);
|
|
1330
|
+
timer = null;
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
const reset = () => {
|
|
1334
|
+
clearTimer();
|
|
1335
|
+
lastTime = 0;
|
|
1336
|
+
};
|
|
1337
|
+
return Object.assign(function (...args) {
|
|
1338
|
+
const now = Date.now();
|
|
1339
|
+
const timeGap = now - lastTime - delay;
|
|
1340
|
+
if (timeGap >= 0) {
|
|
1341
|
+
lastTime = now;
|
|
1342
|
+
clearTimer();
|
|
1343
|
+
}
|
|
1344
|
+
if (timeGap >= 0 && leading) {
|
|
1345
|
+
fn.apply(this, args);
|
|
1346
|
+
}
|
|
1347
|
+
else if (timeGap >= 0 || (timeGap < 0 && trailing && !timer)) {
|
|
1348
|
+
timer = setTimeout(() => {
|
|
1349
|
+
lastTime = Date.now();
|
|
1350
|
+
timer = null;
|
|
1351
|
+
fn.apply(this, args);
|
|
1352
|
+
}, timeGap >= 0 ? delay : -timeGap);
|
|
1353
|
+
}
|
|
1354
|
+
}, { reset });
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
function debounce(fn, delay, options) {
|
|
1358
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1359
|
+
throw new Error('Invalid delay parameter');
|
|
1360
|
+
}
|
|
1361
|
+
return _throttle(fn, delay, Object.assign({ trailing: true, leading: false }, options));
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1284
1364
|
function _cloneArray(obj, map, cloner, ...args) {
|
|
1285
1365
|
const res = obj.slice();
|
|
1286
1366
|
map.set(obj, res);
|
|
@@ -1545,4 +1625,11 @@ function pipe(...pipeFunc) {
|
|
|
1545
1625
|
};
|
|
1546
1626
|
}
|
|
1547
1627
|
|
|
1548
|
-
|
|
1628
|
+
function throttle(fn, delay, options) {
|
|
1629
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1630
|
+
throw new Error('Invalid delay parameter');
|
|
1631
|
+
}
|
|
1632
|
+
return _throttle(fn, delay, Object.assign({ trailing: false, leading: true }, options));
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
export { _, acceptableFileName, acceptableFileType, camelCase, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, clamp, compose, _curryMore as curry, debounce, deepClone, fastClone, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getTag, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBoolean, isBuffer, isDataView, isDate, isEmpty, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isNil, isNull, isNumber, isObject, isPlaceholder, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, memo, noop, not, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomHexString, randomInt, randomIntFloor, randomString, range, remove, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uuidNil, uuidV4, withResolvers };
|
package/lib/index.umd.js
CHANGED
|
@@ -51,6 +51,26 @@ See the Mulan PSL v2 for more details.
|
|
|
51
51
|
return res;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
function remove(arr, ...item) {
|
|
55
|
+
const removeSet = new Set();
|
|
56
|
+
const judgerList = [];
|
|
57
|
+
for (let i = 0; i < item.length; i++) {
|
|
58
|
+
const cond = item[i];
|
|
59
|
+
if (typeof cond === 'function')
|
|
60
|
+
judgerList.push(cond);
|
|
61
|
+
else
|
|
62
|
+
removeSet.add(cond);
|
|
63
|
+
}
|
|
64
|
+
const res = [];
|
|
65
|
+
for (let i = 0; i < arr.length; i++) {
|
|
66
|
+
const el = arr[i];
|
|
67
|
+
if (removeSet.has(el) || judgerList.some((judger) => judger(el)))
|
|
68
|
+
continue;
|
|
69
|
+
res.push(el);
|
|
70
|
+
}
|
|
71
|
+
return res;
|
|
72
|
+
}
|
|
73
|
+
|
|
54
74
|
function sleep(time = 1000) {
|
|
55
75
|
return new Promise((res) => {
|
|
56
76
|
setTimeout(res, time);
|
|
@@ -289,6 +309,23 @@ See the Mulan PSL v2 for more details.
|
|
|
289
309
|
return { promise, resolve, reject };
|
|
290
310
|
}
|
|
291
311
|
|
|
312
|
+
function clamp(num, min, max, options) {
|
|
313
|
+
var _a, _b;
|
|
314
|
+
if (isNaN(min))
|
|
315
|
+
throw new Error('Invalid min parameter');
|
|
316
|
+
if (isNaN(max))
|
|
317
|
+
throw new Error('Invalid max parameter');
|
|
318
|
+
if (max < min) {
|
|
319
|
+
[min, max] = [max, min];
|
|
320
|
+
}
|
|
321
|
+
const { default: def, defaultMin: _dMin, defaultMax: _dMax } = options || {};
|
|
322
|
+
const defaultMin = (_a = _dMin !== null && _dMin !== void 0 ? _dMin : def) !== null && _a !== void 0 ? _a : min;
|
|
323
|
+
const defaultMax = (_b = _dMax !== null && _dMax !== void 0 ? _dMax : def) !== null && _b !== void 0 ? _b : max;
|
|
324
|
+
if (isNaN(num))
|
|
325
|
+
return defaultMin;
|
|
326
|
+
return num < min ? defaultMin : num > max ? defaultMax : num;
|
|
327
|
+
}
|
|
328
|
+
|
|
292
329
|
const mimeMap = {
|
|
293
330
|
application: {
|
|
294
331
|
acrobat: ['pdf'],
|
|
@@ -1287,6 +1324,49 @@ See the Mulan PSL v2 for more details.
|
|
|
1287
1324
|
}
|
|
1288
1325
|
}
|
|
1289
1326
|
|
|
1327
|
+
function _throttle(fn, delay, options) {
|
|
1328
|
+
var _a, _b;
|
|
1329
|
+
const trailing = (_a = options === null || options === void 0 ? void 0 : options.trailing) !== null && _a !== void 0 ? _a : false;
|
|
1330
|
+
const leading = (_b = options === null || options === void 0 ? void 0 : options.leading) !== null && _b !== void 0 ? _b : true;
|
|
1331
|
+
let timer = null;
|
|
1332
|
+
let lastTime = 0;
|
|
1333
|
+
const clearTimer = () => {
|
|
1334
|
+
if (timer) {
|
|
1335
|
+
clearInterval(timer);
|
|
1336
|
+
timer = null;
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
const reset = () => {
|
|
1340
|
+
clearTimer();
|
|
1341
|
+
lastTime = 0;
|
|
1342
|
+
};
|
|
1343
|
+
return Object.assign(function (...args) {
|
|
1344
|
+
const now = Date.now();
|
|
1345
|
+
const timeGap = now - lastTime - delay;
|
|
1346
|
+
if (timeGap >= 0) {
|
|
1347
|
+
lastTime = now;
|
|
1348
|
+
clearTimer();
|
|
1349
|
+
}
|
|
1350
|
+
if (timeGap >= 0 && leading) {
|
|
1351
|
+
fn.apply(this, args);
|
|
1352
|
+
}
|
|
1353
|
+
else if (timeGap >= 0 || (timeGap < 0 && trailing && !timer)) {
|
|
1354
|
+
timer = setTimeout(() => {
|
|
1355
|
+
lastTime = Date.now();
|
|
1356
|
+
timer = null;
|
|
1357
|
+
fn.apply(this, args);
|
|
1358
|
+
}, timeGap >= 0 ? delay : -timeGap);
|
|
1359
|
+
}
|
|
1360
|
+
}, { reset });
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
function debounce(fn, delay, options) {
|
|
1364
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1365
|
+
throw new Error('Invalid delay parameter');
|
|
1366
|
+
}
|
|
1367
|
+
return _throttle(fn, delay, Object.assign({ trailing: true, leading: false }, options));
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1290
1370
|
function _cloneArray(obj, map, cloner, ...args) {
|
|
1291
1371
|
const res = obj.slice();
|
|
1292
1372
|
map.set(obj, res);
|
|
@@ -1551,6 +1631,13 @@ See the Mulan PSL v2 for more details.
|
|
|
1551
1631
|
};
|
|
1552
1632
|
}
|
|
1553
1633
|
|
|
1634
|
+
function throttle(fn, delay, options) {
|
|
1635
|
+
if (!isNumber(delay) || !isFinite(delay) || delay <= 0) {
|
|
1636
|
+
throw new Error('Invalid delay parameter');
|
|
1637
|
+
}
|
|
1638
|
+
return _throttle(fn, delay, Object.assign({ trailing: false, leading: true }, options));
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1554
1641
|
exports._ = _;
|
|
1555
1642
|
exports.acceptableFileName = acceptableFileName;
|
|
1556
1643
|
exports.acceptableFileType = acceptableFileType;
|
|
@@ -1560,8 +1647,10 @@ See the Mulan PSL v2 for more details.
|
|
|
1560
1647
|
exports.caseKebab = caseKebab;
|
|
1561
1648
|
exports.casePascal = casePascal;
|
|
1562
1649
|
exports.caseSnake = caseSnake;
|
|
1650
|
+
exports.clamp = clamp;
|
|
1563
1651
|
exports.compose = compose;
|
|
1564
1652
|
exports.curry = _curryMore;
|
|
1653
|
+
exports.debounce = debounce;
|
|
1565
1654
|
exports.deepClone = deepClone;
|
|
1566
1655
|
exports.fastClone = fastClone;
|
|
1567
1656
|
exports.getAcceptableExtByMIME = getAcceptableExtByMIME;
|
|
@@ -1631,10 +1720,12 @@ See the Mulan PSL v2 for more details.
|
|
|
1631
1720
|
exports.randomIntFloor = randomIntFloor;
|
|
1632
1721
|
exports.randomString = randomString;
|
|
1633
1722
|
exports.range = range;
|
|
1723
|
+
exports.remove = remove;
|
|
1634
1724
|
exports.shuffle = shuffle;
|
|
1635
1725
|
exports.sleep = sleep;
|
|
1636
1726
|
exports.snakeCase = snakeCase;
|
|
1637
1727
|
exports.splitWords = splitWords;
|
|
1728
|
+
exports.throttle = throttle;
|
|
1638
1729
|
exports.titleCase = titleCase;
|
|
1639
1730
|
exports.tryit = tryit;
|
|
1640
1731
|
exports.ulid = ulid;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foreslash",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Foreslash is a Javascript utilities lib which contains plenty of practical functions.",
|
|
5
5
|
"author": "moushu",
|
|
6
6
|
"license": "Mulan PSL v2",
|
|
@@ -27,7 +27,10 @@
|
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"lib",
|
|
30
|
-
"README.md"
|
|
30
|
+
"README.EN.md",
|
|
31
|
+
"CHANGELOG.md",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
31
34
|
],
|
|
32
35
|
"scripts": {
|
|
33
36
|
"dev": "rollup -c -w",
|