├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .npmignore ├── .vcmrc ├── .vscode └── launch.json ├── CHANGELOG.md ├── README-CN.md ├── README.md ├── docs ├── .nojekyll ├── README.md ├── _coverpage.md ├── _media │ └── icon.svg ├── _navbar.md ├── _sidebar.md ├── array.md ├── date.md ├── fn.md ├── index.html ├── math.md ├── net.md ├── number.md ├── object.md ├── string.md ├── verification.md └── zh-cn │ ├── .nojekyll │ ├── README.md │ ├── _coverpage.md │ ├── _navbar.md │ ├── _sidebar.md │ ├── array.md │ ├── date.md │ ├── fn.md │ ├── index.html │ ├── math.md │ ├── net.md │ ├── number.md │ ├── object.md │ ├── string.md │ └── verification.md ├── gulpfile.js ├── index.js ├── package.json ├── release ├── utils.js └── utils.min.js ├── src ├── __test__ │ ├── import.test.ts │ └── require.test.ts ├── array │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── date │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── fn │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── math │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── net │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── number │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── object │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── string │ ├── __test__ │ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── utils.d.ts ├── utils.ts └── verification │ ├── __test__ │ └── index.test.ts │ ├── index.d.ts │ └── index.ts ├── tsconfig.json └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | release 3 | validate-commit-msg.js 4 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | commonjs: true, 5 | es6: true, 6 | node: true, 7 | jest: true 8 | }, 9 | extends: "eslint:recommended", 10 | parserOptions: { 11 | ecmaVersion: 2016, 12 | sourceType: "module" 13 | }, 14 | parser: "typescript-eslint-parser", 15 | plugins: ["typescript"], 16 | rules: { 17 | "no-undef": "off", 18 | "no-unused-vars": "off", 19 | indent: ["error", 2], 20 | "linebreak-style": ["error", "windows"], 21 | quotes: ["error", "single"], 22 | semi: ["error", "always"] 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | example -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.vcmrc: -------------------------------------------------------------------------------- 1 | { 2 | "types": [ 3 | "feat", 4 | "fix", 5 | "docs", 6 | "style", 7 | "refactor", 8 | "perf", 9 | "test", 10 | "build", 11 | "ci", 12 | "chore", 13 | "revert" 14 | ], 15 | "scope": { 16 | "required": false, 17 | "allowed": [ 18 | "*" 19 | ], 20 | "validate": false, 21 | "multiple": false 22 | }, 23 | "warnOnFail": false, 24 | "maxSubjectLength": 100, 25 | "subjectPattern": ".+", 26 | "subjectPatternErrorMsg": "subject does not match subject pattern!", 27 | "helpMessage": "", 28 | "autoFix": false 29 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "attach", 10 | "name": "Attach by Process ID", 11 | "processId": "${command:PickProcess}", 12 | "port": 9229 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | # 2.1.6 (2018-09-13) 3 | 4 | ## Support Tree-Shaking 5 | ```JS 6 | import { add } from 'windlike-utils/dist/math'; 7 | 8 | add(0.1, 0.2); // 0.3 9 | ``` 10 | 11 | ### New features 12 | 13 | 14 | # 2.1.0 (2018-09-05) 15 | 16 | ### New features 17 | `Function` Module: 18 | - add `debounce` 19 | - add `throttle` 20 | 21 | `Array` Module: 22 | - add `shallowCompare` 23 | - add `deepCompare` 24 | - remove `equal` 25 | 26 | `Object` Module: 27 | - add `shallowCompare` 28 | - add `deepCompare` 29 | - remove `valueEqual` 30 | 31 | `Math` Module: 32 | - add `add`: add the incoming parameters together, and return the result. 33 | ```js 34 | utils.math.add(0.1, 0.2); // 0.3 35 | ``` -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | # Windlike-Utils · [![npm version](https://img.shields.io/npm/v/windlike-utils.svg?style=flat)](https://www.npmjs.com/package/windlike-utils) 2 | 3 | Windlike-Utils是一个基于函数式编程思想开发的一个工具库。 4 | 5 | * **模块化:** Windlike-Utils把工具分为若干模块,如```array```、```object```、```string```等等,方便查找和使用。 6 | * **函数式的:** 每个方法只要输入的参数相同,输出的结果也是唯一的,就像数学里的y=f(x),只要x不变,输出的y也不变,有些脏函数也做延迟输出处理,如```number.random```函数,它返回的是一个产生随机数的函数,而不是一个随机数,以保证输出的唯一性和变量的重用性。 7 | * **不可变的:** 输入的任何实参都不会被改变,而是返回新的结果。 8 | 9 | ## 安装 10 | 11 | ```npm 12 | npm install windlike-utils --save 13 | ``` 14 | 15 | ## [文档](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/) 16 | - [Array](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/array) 17 | - [Date](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/date) 18 | - [Function](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/fn) 19 | - [Math](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/math) 20 | - [Net](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/net) 21 | - [Number](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/number) 22 | - [Object](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/object) 23 | - [String](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/string) 24 | - [Verification](https://mrwindlike.github.io/Windlike-Utils/zh-cn/#/verification) 25 | 26 | ## 贡献 27 | 欢迎大家踊跃提交issues和PR😄 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Windlike-Utils · [![npm version](https://img.shields.io/npm/v/windlike-utils.svg?style=flat)](https://www.npmjs.com/package/windlike-utils) 2 | 3 | Windlike-Utils is a tool library developed based on functional programming ideas. 4 | 5 | - **Modules:** Windlike-Utils divide the tool into several modules which like `array`、`object`、`string` and so on.It can be easily found and used. 6 | - **Functional:** Each function only has the same input parameters, and the output result is unique.Just like `y=f(x)` in mathematics.As long as `x` is unchanged, the output `y` is also unchanged.To ensure the uniqueness of the output and the reusability of the variables, Some dirty functions also do delayed output processing.For examples, `number.random` returns a function which can generate a random number, instead of the result of the random number. 7 | - **Immutable:** Any arguments entered is immutable and new results will be returned. 8 | - **Tree-Shaking:** Support Tree-Shaking. 9 | 10 | ## Install 11 | 12 | ```npm 13 | npm install windlike-utils --save 14 | ``` 15 | 16 | ## Feature 17 | 18 | - Format date: 19 | 20 | ```js 21 | const ms = 837043200000; // 1996-07-11 08:00:00 22 | 23 | utils.date.createFormatDate("YYYY-MM-DD hh:mm:ss w")(ms); // 1996-07-11 08:00:00 Thur. 24 | utils.date.createFormatDate("YY-MM-DD hh:mm:ss W")(ms); // 96-07-11 08:00:00 星期四 25 | ``` 26 | 27 | - Currying 28 | 29 | ```js 30 | const add = (a: number, b: number, c: number): number => a + b + c; 31 | const curryAdd: any = utils.fn.curry(add); 32 | 33 | curryAdd(1, 2, 3); // 6 34 | curryAdd(1, 2)(4); // 7 35 | curryAdd(1)(3)(5); // 9 36 | curryAdd(1)(2, 3); // 6 37 | ``` 38 | 39 | - Parse Url 40 | 41 | ```js 42 | const URL = 'https://github.com/MrWindlike/Windlike-Utils?key=value'; 43 | const result = utils.net.parseUrl(URL); 44 | // { 45 | // url: URL, 46 | // host: 'https://github.com', 47 | // port: 80, 48 | // path: '/MrWindlike/Windlike-Utils', 49 | // params: { 50 | // key: 'value', 51 | // }, 52 | // } 53 | ``` 54 | 55 | ## [DOCS](https://mrwindlike.github.io/Windlike-Utils/#/) 56 | 57 | - [Array](https://mrwindlike.github.io/Windlike-Utils/#/array) 58 | - [Date](https://mrwindlike.github.io/Windlike-Utils/#/date) 59 | - [Function](https://mrwindlike.github.io/Windlike-Utils/#/fn) 60 | - [Math](https://mrwindlike.github.io/Windlike-Utils/#/math) 61 | - [Net](https://mrwindlike.github.io/Windlike-Utils/#/net) 62 | - [Number](https://mrwindlike.github.io/Windlike-Utils/#/number) 63 | - [Object](https://mrwindlike.github.io/Windlike-Utils/#/object) 64 | - [String](https://mrwindlike.github.io/Windlike-Utils/#/string) 65 | - [Verification](https://mrwindlike.github.io/Windlike-Utils/#/verification) 66 | 67 | ## Contribute 68 | 69 | Open an issue or PR.😄 70 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MrWindlike/Windlike-Utils/2c5c10c37b3ae11967a1d5a26d0fc06162394203/docs/.nojekyll -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Windlike-Utils · [![npm version](https://img.shields.io/npm/v/windlike-utils.svg?style=flat)](https://www.npmjs.com/package/windlike-utils) 2 | 3 | Windlike-Utils is a tool library developed based on functional programming ideas. 4 | 5 | - **Modules:** Windlike-Utils divide the tool into several modules which like `array`、`object`、`string` and so on.It can be easily found and used. 6 | - **Functional:** Each function only has the same input parameters, and the output result is unique.Just like `y=f(x)` in mathematics.As long as `x` is unchanged, the output `y` is also unchanged.To ensure the uniqueness of the output and the reusability of the variables, Some dirty functions also do delayed output processing.For examples, `number.random` returns a function which can generate a random number, instead of the result of the random number. 7 | - **Immutable:** Any arguments entered is immutable and new results will be returned. 8 | 9 | ## Install 10 | 11 | ```npm 12 | npm install windlike-utils --save 13 | ``` 14 | 15 | ## Feature 16 | 17 | - Format date: 18 | 19 | ```js 20 | const ms = 837043200000; // 1996-07-11 08:00:00 21 | 22 | utils.date.createFormatDate("YYYY-MM-DD hh:mm:ss w")(ms); // 1996-07-11 08:00:00 Thur. 23 | utils.date.createFormatDate("YY-MM-DD hh:mm:ss W")(ms); // 96-07-11 08:00:00 星期四 24 | ``` 25 | 26 | - Currying 27 | 28 | ```js 29 | const add = (a: number, b: number, c: number): number => a + b + c; 30 | const curryAdd: any = utils.fn.curry(add); 31 | 32 | curryAdd(1, 2, 3); // 6 33 | curryAdd(1, 2)(4); // 7 34 | curryAdd(1)(3)(5); // 9 35 | curryAdd(1)(2, 3); // 6 36 | ``` 37 | 38 | - Parse Url 39 | 40 | ```js 41 | const URL = 'https://github.com/MrWindlike/Windlike-Utils?key=value'; 42 | const result = utils.net.parseUrl(URL); 43 | // { 44 | // url: URL, 45 | // host: 'https://github.com', 46 | // port: 80, 47 | // path: '/MrWindlike/Windlike-Utils', 48 | // params: { 49 | // key: 'value', 50 | // }, 51 | // } 52 | ``` 53 | 54 | 55 | # 2.1.6 (2018-09-13) 56 | 57 | ## Support Tree-Shaking 58 | ```JS 59 | import { add } from 'windlike-utils/dist/math'; 60 | 61 | add(0.1, 0.2); // 0.3 62 | ``` 63 | 64 | 65 | # 2.1.0 (2018-09-05) 66 | 67 | ### New features 68 | `Function` Module: 69 | - add `debounce` 70 | - add `throttle` 71 | 72 | `Array` Module: 73 | - add `shallowCompare` 74 | - add `deepCompare` 75 | - remove `equal` 76 | 77 | `Object` Module: 78 | - add `shallowCompare` 79 | - add `deepCompare` 80 | - remove `valueEqual` 81 | 82 | `Math` Module: 83 | - add `add`: add the incoming parameters together, and return the result. 84 | ```js 85 | utils.math.add(0.1, 0.2); // 0.3 86 | ``` 87 | 88 | ## Contribute 89 | 90 | Open an issue or PR.😄 91 | -------------------------------------------------------------------------------- /docs/_coverpage.md: -------------------------------------------------------------------------------- 1 | ![logo](_media/icon.svg) 2 | 3 | # Windlike-Utils 2.1.0 4 | 5 | > A tool library developed based on functional programming ideas. 6 | 7 | - Friendly editor prompt support 8 | - Simple & Convenience 9 | - Several Modules 10 | 11 | 12 | [GitHub](https://github.com/MrWindlike/Windlike-Utils) 13 | [Get Started](#windlike-utils-middot-) -------------------------------------------------------------------------------- /docs/_media/icon.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/_navbar.md: -------------------------------------------------------------------------------- 1 | - Translations 2 | - [:uk: English](/) 3 | - [:cn: 中文](/zh-cn/) -------------------------------------------------------------------------------- /docs/_sidebar.md: -------------------------------------------------------------------------------- 1 | * [Windlike-Utils](README.md) 2 | * API 3 | * [Array (new)](array.md) 4 | * [Date](date.md) 5 | * [Function (new)](fn.md) 6 | * [Math (new)](math.md) 7 | * [Net](net.md) 8 | * [Number](number.md) 9 | * [Object (new)](object.md) 10 | * [String](string.md) 11 | * [Verification](verification.md) 12 | * [Contribute](contribute.md) 13 | -------------------------------------------------------------------------------- /docs/array.md: -------------------------------------------------------------------------------- 1 | # Array 2 | ```js 3 | interface ArrayModule { 4 | compareLength: (firstArray: any[], secondArray: any[]) => number; 5 | equal: (firstArray: T[], secondArray: T[]) => boolean; 6 | deleteItem: (array: T[], value: T) => T[]; 7 | deleteItems: (array: T[], value: T) => T[]; 8 | deleteItemsExcept: (array: T[], exceptArray: T[]) => T[]; 9 | map: (fn: any) => (array: T[]) => T[]; 10 | } 11 | ``` 12 | 13 | ## compareLength 14 | #### Describe 15 | Compare the length of two arrays. 16 | ```js 17 | (firstArray: any[], secondArray: any[]) => number; 18 | ``` 19 | 20 | #### Arguments 21 | - firstArray(any[]) 22 | - secondArray(any[]) 23 | 24 | #### Returns 25 | (number): The difference between the length of the first array and the length of the second array. 26 | 27 | #### Example 28 | ```js 29 | const firstArray = [1, 2, 3, {}]; 30 | const secondArray = [{}, 3, 2, 1]; 31 | const result: number = utils.array.compareLength(firstArray, secondArray); // 0 32 | ``` 33 | 34 | ## shallowCompare 35 | #### Describe 36 | Compare the values of two arrays. 37 | ```js 38 | (firstArray: T[], secondArray: T[]) => boolean; 39 | ``` 40 | 41 | #### Arguments 42 | - firstArray(any[]) 43 | - secondArray(any[]) 44 | 45 | #### Returns 46 | (boolean) 47 | 48 | #### Example 49 | ```js 50 | utils.array.shallowCompare([1, 2], [1, 2]); // true 51 | utils.array.shallowCompare([1, 2, { key: 'value' }], [1, 2, { key: 'value' }]); // false 52 | ``` 53 | 54 | ## deepCompare 55 | #### Describe 56 | Compare the values of two arrays. 57 | ```js 58 | (firstArray: T[], secondArray: T[]) => boolean; 59 | ``` 60 | 61 | #### Arguments 62 | - firstArray(any[]) 63 | - secondArray(any[]) 64 | 65 | #### Returns 66 | (boolean) 67 | 68 | #### Example 69 | ```js 70 | utils.array.shallowCompare([1, 2], [1, 2]); // true 71 | utils.array.shallowCompare([1, 2, { key: 'value' }], [1, 2, { key: 'value' }]); // true 72 | ``` 73 | 74 | ## deleteItem 75 | #### Describe 76 | Delete the first item in the array equal to the value and return a new array. 77 | ```js 78 | (array: T[], value: T) => T[]; 79 | ``` 80 | 81 | #### Arguments 82 | - array(T[]) 83 | - value(T) 84 | 85 | #### Returns 86 | (T[]) 87 | 88 | #### Example 89 | ```js 90 | const array = [1, 2, 3]; 91 | 92 | utils.array.deleteItem(array, 2); // [1, 3] 93 | ``` 94 | 95 | ## deleteItems 96 | #### Describe 97 | Delete the all items in the array equal to the value and return a new array. 98 | ```js 99 | (array: T[], value: T) => T[]; 100 | ``` 101 | 102 | #### Arguments 103 | - array(T[]) 104 | - value(T) 105 | 106 | #### Returns 107 | (T[]) 108 | 109 | #### Example 110 | ```js 111 | const array = [1, 9, 9, 6]; 112 | 113 | utils.array.deleteItems(array, 9); // [1, 6] 114 | ``` 115 | 116 | ## deleteItemsExcept 117 | #### Describe 118 | Delete values that are not in another array in the array. 119 | ```js 120 | (array: T[], exceptArray: T[]) => T[]; 121 | ``` 122 | 123 | #### Arguments 124 | - array(T[]) 125 | - exceptArray(T[]) 126 | 127 | #### Returns 128 | (T[]) 129 | 130 | #### Example 131 | ```js 132 | const array = [1, '9', 9, 6]; 133 | 134 | utils.array.deleteItemsExcept(array, [1, 2, 3]); // [1] 135 | utils.array.deleteItemsExcept(array, [1, 9, 3]); // [1, 9] 136 | utils.array.deleteItemsExcept(array, [1, '9', 6]); // [1, '9', 6] 137 | ``` 138 | 139 | ## map 140 | #### Describe 141 | ```js 142 | (fn: any) => (array: T[]) => T[] 143 | ``` 144 | 145 | #### Arguments 146 | - fn: callback function 147 | 148 | #### Returns 149 | (```(array: T[]) => T[]```) 150 | 151 | #### Example 152 | ```js 153 | const plusOne = (value) => 1 + value; 154 | const array = [1, 9, 9, 6]; 155 | const plusOneMap = utils.array.map(plusOne); 156 | 157 | plusOneMap(array); // [2, 10, 10, 7] 158 | ``` 159 | -------------------------------------------------------------------------------- /docs/date.md: -------------------------------------------------------------------------------- 1 | # Date 2 | ```js 3 | interface DateModule { 4 | createFormatDate: (format: string) => (date: number) => string; 5 | } 6 | ``` 7 | 8 | ## createFormatDate 9 | #### Describe 10 | Create a function to format date. 11 | ```js 12 | (format: string) => (date: number) => string; 13 | ``` 14 | 15 | #### Arguments 16 | - format(string): Y: Year;M: Month;D: Day;h: Hour;m: Minute;s: Second;w: Week(en);W: Week(cn) 17 | 18 | #### Returns 19 | (```(ms: number) => string;```) 20 | 21 | #### Example 22 | ```js 23 | const ms = 837043200000; // 1996-07-11 08:00:00 24 | 25 | utils.date.createFormatDate('YYYY-MM-DD hh:mm:ss w')(ms); // 1996-07-11 08:00:00 Thur. 26 | utils.date.createFormatDate('YY-MM-DD hh:mm:ss W')(ms); // 96-07-11 08:00:00 星期四 27 | ``` -------------------------------------------------------------------------------- /docs/fn.md: -------------------------------------------------------------------------------- 1 | # Fn 2 | ```js 3 | interface FunctionModule { 4 | curry: (fn: (...params: any[]) => Return) => CurryFunction; 5 | compose: (...fn: any[]) => (...params: any[]) => Return; 6 | } 7 | 8 | interface CurryFunction { 9 | (...newParams: any[]): Return | CurryFunction 10 | } 11 | ``` 12 | 13 | ## curry 14 | #### Describe 15 | ```js 16 | interface CurryFunction { 17 | (...newParams: any[]): Return | CurryFunction 18 | } 19 | 20 | (fn: (...params: any[]) => Return) => CurryFunction; 21 | ``` 22 | 23 | #### Arguments 24 | - fn(```(...params: any[]) => Return) => CurryFunction```) 25 | 26 | #### Returns 27 | (```CurryFunction```) 28 | 29 | #### Example 30 | ```js 31 | const add = (a: number, b: number, c: number): number => a + b + c; 32 | const curryAdd: any = utils.fn.curry(add); 33 | 34 | curryAdd(1, 2, 3); // 6 35 | curryAdd(1, 2)(4); // 7 36 | curryAdd(1)(3)(5); // 9 37 | curryAdd(1)(2, 3); // 6 38 | ``` 39 | 40 | ## compose 41 | #### Describe 42 | Compose the functions from right to left. 43 | ```js 44 | (...fn: any[]) => (...params: any[]) => Return; 45 | ``` 46 | 47 | #### Arguments 48 | - ...fn(any[]) 49 | 50 | #### Returns 51 | (```(...params: any[]) => Return```) 52 | 53 | #### Example 54 | ```js 55 | const plusOne = (num) => num * 1 + 1; 56 | const double = (num) => num * 2; 57 | const plusOneAndDouble = utils.fn.compose(double, plusOne); // 先加一后乘二 58 | 59 | plusOneAndDouble(1); // 4 60 | plusOneAndDouble(2); // 6 61 | ``` 62 | 63 | ## debounce 64 | #### Describe 65 | Change multiple consecutive executions over a period of time. 66 | ```js 67 | ( 68 | fn: (...params: any[]) => Return, 69 | wait: number, 70 | immediate: boolean 71 | ) => Executor; 72 | ``` 73 | 74 | #### Arguments 75 | - fn 76 | - wait 77 | - immediate: is execute immediately? 78 | 79 | #### Returns 80 | ``` 81 | interface Executor { 82 | execute: (...params: any[]) => (Return | null); 83 | result: Return; 84 | } 85 | ``` 86 | `Executor` 87 | 88 | #### Example 89 | ```js 90 | const executor = utils.fn.debounce( 91 | fetch, 92 | 300, 93 | false 94 | ); 95 | 96 | input.addEvetListener('input', function(e) { 97 | const result = executor.execute('/api'); 98 | }); 99 | 100 | const result = excutor.result; // get current result 101 | ``` 102 | 103 | ## throttle 104 | #### Describe 105 | Reduce the frequency of execution 106 | 107 | ```js 108 | interface ThrottleOptions { 109 | isExecuteAtStart: boolean; // default is true 110 | isExecuteAtEnd: boolean; // default is true 111 | } 112 | 113 | ( 114 | fn: (...params: any[]) => Return, 115 | wait: number, 116 | options?: ThrottleOptions 117 | ) => Executor; 118 | ``` 119 | 120 | #### Arguments 121 | - fn 122 | - wait 123 | - options? 124 | 125 | #### Returns 126 | ``` 127 | interface Executor { 128 | execute: (...params: any[]) => (Return | null); 129 | result: Return; 130 | } 131 | ``` 132 | `Executor` 133 | 134 | #### Example 135 | ```js 136 | const executor = utils.fn.throttle( 137 | fetch, 138 | 300, 139 | { 140 | isExecuteAtStart: true, 141 | isExecuteAtEnd: false, 142 | } 143 | ); 144 | 145 | input.addEvetListener('click', function(e) { 146 | const result = executor.execute('/api'); 147 | }); 148 | 149 | const result = excutor.result; // get current result 150 | ``` -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Windlike-Utils 7 | 8 | 9 | 10 | 11 | 16 | 17 | 18 | 22 |
Loading...
23 | 24 | 32 | 34 | 35 | -------------------------------------------------------------------------------- /docs/math.md: -------------------------------------------------------------------------------- 1 | # Math 2 | ```js 3 | interface MathModule { 4 | createSin: (height: number, width: number, offset: number) => (x: number) => number; 5 | createGetPointOnCircle: (radius: number, offsetX: number, offsetY: number) => (radian: number) => Point; 6 | } 7 | 8 | interface Point { 9 | x: number; 10 | y: number; 11 | } 12 | ``` 13 | 14 | ## createSin 15 | #### Describe 16 | y=f(x)=height \* sin(width \* x + offset)。 17 | ```js 18 | (height: number, width: number, offset: number) => (x: number) => number; 19 | ``` 20 | 21 | #### Arguments 22 | - height(number) 23 | - width(number) 24 | - offset(number) 25 | 26 | #### Returns 27 | (```(x: number) => number```) 28 | 29 | #### Example 30 | ```js 31 | const sin = utils.math.createSin(2, 2, Math.PI / 2); 32 | 33 | sin(Math.PI / 2); // -2 34 | ``` 35 | 36 | ## createGetPointOnCircle 37 | #### Describe 38 | Create a function to get the point on the circle. 39 | ```js 40 | interface Point { 41 | x: number; 42 | y: number; 43 | } 44 | 45 | (radius: number, offsetX: number, offsetY: number) => (radian: number) => Point; 46 | ``` 47 | 48 | #### Arguments 49 | - radius(number) 50 | - offsetX(number) 51 | - offsetY(number) 52 | 53 | #### Returns 54 | (```(radian: number) => Point```) 55 | 56 | #### Example 57 | ```js 58 | const getPointOnCircle = utils.math.createGetPointOnCircle(4, 10, 5); 59 | 60 | getPointOnCircle(0); // { x: 14, y: 5 } 61 | ``` 62 | 63 | ## add 64 | #### Describe 65 | add the incoming parameters together, and return the result. 66 | ```js 67 | (...numbers: number[])=> number; 68 | ``` 69 | 70 | #### Arguments 71 | - ...numbers 72 | 73 | #### Returns 74 | (```number```) 75 | 76 | #### Example 77 | ```js 78 | utils.math.add(0.1, 0.2); // 0.3 79 | ``` 80 | -------------------------------------------------------------------------------- /docs/net.md: -------------------------------------------------------------------------------- 1 | # Net 2 | ```js 3 | interface NetModule { 4 | parseParams: (locationSearch: string) => object; 5 | parseUrl: (url: string) => UrlInfoObject; 6 | } 7 | 8 | interface UrlInfoObject { 9 | url: string 10 | host: string 11 | port: number 12 | path: string 13 | params: object 14 | } 15 | ``` 16 | 17 | ## parseParams 18 | #### Describe 19 | ```js 20 | (locationSearch: string) => object; 21 | ``` 22 | 23 | #### Arguments 24 | - locationSearch(string) 25 | 26 | #### Returns 27 | (object) 28 | 29 | #### Example 30 | ```js 31 | const SEARCH = '?key=value&search=windlike'; 32 | 33 | utils.net.parseParams(SEARCH); // { key: 'value', search: 'windlike' } 34 | ``` 35 | 36 | ## parseUrl 37 | Parse URL string to object. 38 | #### Describe 39 | ```js 40 | interface UrlInfoObject { 41 | url: string 42 | host: string 43 | port: number 44 | path: string 45 | params: object 46 | } 47 | 48 | (url: string) => UrlInfoObject; 49 | ``` 50 | 51 | #### Arguments 52 | - url(string) 53 | 54 | #### Returns 55 | (UrlInfoObject) 56 | 57 | #### Example 58 | ```js 59 | const URL = 'https://github.com/MrWindlike/Windlike-Utils?key=value'; 60 | 61 | utils.net.parseUrl(URL); 62 | // { 63 | // url: URL, 64 | // host: 'https://github.com', 65 | // port: 80, 66 | // path: '/MrWindlike/Windlike-Utils', 67 | // params: { 68 | // key: 'value', 69 | // }, 70 | // } 71 | ``` -------------------------------------------------------------------------------- /docs/number.md: -------------------------------------------------------------------------------- 1 | # Number 2 | ```js 3 | interface NumberModule { 4 | createRandomFunction: (min: number, max: number, float: boolean) => () => number; 5 | } 6 | ``` 7 | 8 | ## createRandomFunction 9 | #### Describe 10 | Create a function to generate a random number. 11 | ```js 12 | (min: number, max: number, float: boolean) => () => number; 13 | ``` 14 | 15 | #### Arguments 16 | - min(number) 17 | - max(number) 18 | - float(boolean) 19 | 20 | #### Returns 21 | (```() => number```) 22 | 23 | #### Example 24 | ```js 25 | const createRandom = utils.number.createRandomFunction(100, 1, true); 26 | 27 | createRandom(); 28 | ``` -------------------------------------------------------------------------------- /docs/object.md: -------------------------------------------------------------------------------- 1 | # Object 2 | ```js 3 | interface ObjectModule { 4 | valueEqual: (firstObj: AnyObject, secondObj: AnyObject) => boolean 5 | } 6 | ``` 7 | 8 | ## shallowCompare 9 | #### Describe 10 | Determine if the values of two objects are equal. 11 | ```js 12 | (firstObj: AnyObject, secondObj: AnyObject) => boolean; 13 | ``` 14 | 15 | #### Arguments 16 | - firstObj(object) 17 | - secondObj(object) 18 | 19 | #### Returns 20 | (boolean) 21 | 22 | #### Example 23 | ```js 24 | utils.object.shallowCompare( 25 | { 26 | a: 1, 27 | b: 2, 28 | }, 29 | { 30 | b: 2, 31 | a: 1, 32 | }, 33 | ); // true 34 | 35 | utils.object.shallowCompare( 36 | { 37 | a: 1, 38 | child: { 39 | key: 'value' 40 | }, 41 | b: 2, 42 | obj: {}, 43 | }, 44 | { 45 | b: 2, 46 | obj: {}, 47 | a: 1, 48 | child: { 49 | key: 'value' 50 | }, 51 | }, 52 | ); // false 53 | 54 | ``` 55 | 56 | ## deepCompare 57 | #### Describe 58 | Determine if the values of two objects are equal. 59 | ```js 60 | (firstObj: AnyObject, secondObj: AnyObject) => boolean; 61 | ``` 62 | 63 | #### Arguments 64 | - firstObj(object) 65 | - secondObj(object) 66 | 67 | #### Returns 68 | (boolean) 69 | 70 | #### Example 71 | ```js 72 | utils.object.deepCompare( 73 | { 74 | a: 1, 75 | child: { 76 | key: 'value' 77 | }, 78 | b: 2, 79 | obj: {}, 80 | }, 81 | { 82 | b: 2, 83 | obj: {}, 84 | a: 1, 85 | child: { 86 | key: 'value' 87 | }, 88 | }, 89 | ); // true 90 | 91 | ``` 92 | 93 | ## has 94 | #### Describe 95 | Determine if the object has the key. 96 | ```js 97 | (object: AnyObject, key: string) => boolean; 98 | ``` 99 | 100 | #### Arguments 101 | - object(object) 102 | - key(string) 103 | 104 | #### Returns 105 | (boolean) 106 | 107 | #### Example 108 | ```js 109 | const object = { key: 'value', nonexistent: undefined }; 110 | 111 | utils.object.has(object, 'key'); // true 112 | utils.object.has(object, 'nonexistent'); // false 113 | ``` -------------------------------------------------------------------------------- /docs/string.md: -------------------------------------------------------------------------------- 1 | # String 2 | ```js 3 | interface StringModule { 4 | replace: (match: RegExp | string) => (str: string, substitute: any) => string; 5 | split: (char: string | RegExp) => (str: string) => string[]; 6 | match: (regexp: RegExp) => (str: string) => string[]; 7 | } 8 | ``` 9 | 10 | ## replace 11 | #### Describe 12 | ```js 13 | (match: RegExp | string) => (str: string, substitute: any) => string; 14 | ``` 15 | 16 | #### Arguments 17 | - match(RegExp | string) 18 | 19 | #### Returns 20 | (```(str: string, substitute: any) => string```) 21 | 22 | #### Example 23 | ```js 24 | const str = 'I\'m Windlike.'; 25 | const replaceWindlike = utils.string.replace('Windlike'); 26 | 27 | replaceWindlike(str, 'I'); // I'm I. 28 | ``` 29 | 30 | ## split 31 | #### Describe 32 | ```js 33 | (char: string | RegExp) => (str: string) => string[]; 34 | ``` 35 | 36 | #### Arguments 37 | - char(RegExp | string): 分割符 38 | 39 | #### Returns 40 | (```(str: string) => string[]```) 41 | 42 | #### Example 43 | ```js 44 | const str = 'I And You'; 45 | const splitSpace = utils.string.split(' '); 46 | 47 | splitSpace(str); // ['I', 'And', 'You'] 48 | ``` 49 | 50 | ## match 51 | #### Describe 52 | ```js 53 | (regexp: RegExp) => (str: string) => string[]; 54 | ``` 55 | 56 | #### Arguments 57 | - regexp(RegExp) 58 | 59 | #### Returns 60 | (```(str: string) => string[]```) 61 | 62 | #### Example 63 | ```js 64 | const re = /[\w]+/g; 65 | const str = 'I am a string.'; 66 | const matchWork = utils.string.match(re); 67 | 68 | matchWork(str); // ['I', 'am', 'a', 'string'] 69 | ``` -------------------------------------------------------------------------------- /docs/verification.md: -------------------------------------------------------------------------------- 1 | # Verification 2 | ```js 3 | interface VerificationModule { 4 | readonly _phoneRE: RegExp; 5 | readonly _emailRE: RegExp; 6 | 7 | checkRe: (re: RegExp) => (checkedStr: string) => boolean; 8 | checkLength: (min: number, max: number) => (str: string) => boolean; 9 | check: (checkType: CheckType) => (checkedStr: string) => boolean; 10 | [prop: string]: any 11 | } 12 | 13 | type CheckType = 'phone' | 'email'; 14 | ``` 15 | 16 | ## checkRe 17 | #### Describe 18 | ```js 19 | (re: RegExp) => (checkedStr: string) => boolean; 20 | ``` 21 | 22 | #### Arguments 23 | - re(RegExp) 24 | 25 | #### Returns 26 | (```(checkedStr: string) => boolean```) 27 | 28 | #### Example 29 | ```js 30 | const checkFunction = utils.verification.checkRe(/<[\s\S]*?(script)[\s\S]*?>/); 31 | 32 | checkFunction(''); // true 33 | checkFunction(''); // true 34 | checkFunction(' 32 | 34 | 35 | -------------------------------------------------------------------------------- /docs/zh-cn/math.md: -------------------------------------------------------------------------------- 1 | # Math 2 | ```js 3 | interface MathModule { 4 | createSin: (height: number, width: number, offset: number) => (x: number) => number; 5 | createGetPointOnCircle: (radius: number, offsetX: number, offsetY: number) => (radian: number) => Point; 6 | } 7 | 8 | interface Point { 9 | x: number; 10 | y: number; 11 | } 12 | ``` 13 | 14 | ## createSin 15 | #### Describe 16 | 创建sin函数,y=f(x)=height \* sin(width \* x + offset)。 17 | ```js 18 | (height: number, width: number, offset: number) => (x: number) => number; 19 | ``` 20 | 21 | #### Arguments 22 | - height(number) 23 | - width(number) 24 | - offset(number) 25 | 26 | #### Returns 27 | (```(x: number) => number```): 传入参数返回计算结果的函数。 28 | 29 | #### Example 30 | ```js 31 | const sin = utils.math.createSin(2, 2, Math.PI / 2); 32 | 33 | sin(Math.PI / 2); // -2 34 | ``` 35 | 36 | ## createGetPointOnCircle 37 | #### Describe 38 | 创建获取点在圆上的位置的函数。 39 | ```js 40 | interface Point { 41 | x: number; 42 | y: number; 43 | } 44 | 45 | (radius: number, offsetX: number, offsetY: number) => (radian: number) => Point; 46 | ``` 47 | 48 | #### Arguments 49 | - radius(number): 半径 50 | - offsetX(number): X轴偏移 51 | - offsetY(number): Y轴偏移 52 | 53 | #### Returns 54 | (```(radian: number) => Point```): 传入弧度返回计算结果的函数。 55 | 56 | #### Example 57 | ```js 58 | const getPointOnCircle = utils.math.createGetPointOnCircle(4, 10, 5); 59 | 60 | getPointOnCircle(0); // { x: 14, y: 5 } 61 | ``` 62 | -------------------------------------------------------------------------------- /docs/zh-cn/net.md: -------------------------------------------------------------------------------- 1 | # Net 2 | ```js 3 | interface NetModule { 4 | parseParams: (locationSearch: string) => object; 5 | parseUrl: (url: string) => UrlInfoObject; 6 | } 7 | 8 | interface UrlInfoObject { 9 | url: string 10 | host: string 11 | port: number 12 | path: string 13 | params: object 14 | } 15 | ``` 16 | 17 | ## parseParams 18 | #### Describe 19 | 解析地址参数字符串为对象的函数。 20 | ```js 21 | (locationSearch: string) => object; 22 | ``` 23 | 24 | #### Arguments 25 | - locationSearch(string): 参数字符串,如'?key=value&search=windlike'。 26 | 27 | #### Returns 28 | (object): 参数对象。 29 | 30 | #### Example 31 | ```js 32 | const SEARCH = '?key=value&search=windlike'; 33 | 34 | utils.net.parseParams(SEARCH); // { key: 'value', search: 'windlike' } 35 | ``` 36 | 37 | ## parseUrl 38 | #### Describe 39 | 解析地址的函数。 40 | ```js 41 | interface UrlInfoObject { 42 | url: string 43 | host: string 44 | port: number 45 | path: string 46 | params: object 47 | } 48 | 49 | (url: string) => UrlInfoObject; 50 | ``` 51 | 52 | #### Arguments 53 | - url(string): 地址。 54 | 55 | #### Returns 56 | (UrlInfoObject): 含有地址信息的对象。 57 | 58 | #### Example 59 | ```js 60 | const URL = 'https://github.com/MrWindlike/Windlike-Utils?key=value'; 61 | 62 | utils.net.parseUrl(URL); 63 | // { 64 | // url: URL, 65 | // host: 'https://github.com', 66 | // port: 80, 67 | // path: '/MrWindlike/Windlike-Utils', 68 | // params: { 69 | // key: 'value', 70 | // }, 71 | // } 72 | ``` -------------------------------------------------------------------------------- /docs/zh-cn/number.md: -------------------------------------------------------------------------------- 1 | # Number 2 | ```js 3 | interface NumberModule { 4 | createRandomFunction: (min: number, max: number, float: boolean) => () => number; 5 | } 6 | ``` 7 | 8 | ## createRandomFunction 9 | #### Describe 10 | 创建产生随机数函数的函数。 11 | ```js 12 | (min: number, max: number, float: boolean) => () => number; 13 | ``` 14 | 15 | #### Arguments 16 | - min(number) 17 | - max(number) 18 | - float(boolean): 是否有小数 19 | 20 | #### Returns 21 | (```() => number```): 产生随机数的函数。 22 | 23 | #### Example 24 | ```js 25 | const createRandom = utils.number.createRandomFunction(100, 1, true); 26 | 27 | createRandom(); 28 | ``` -------------------------------------------------------------------------------- /docs/zh-cn/object.md: -------------------------------------------------------------------------------- 1 | # Object 2 | ```js 3 | interface ObjectModule { 4 | valueEqual: (firstObj: AnyObject, secondObj: AnyObject) => boolean 5 | } 6 | ``` 7 | 8 | ## valueEqual 9 | #### Describe 10 | 判断两个对象的值是否相等,会遍历子对象。 11 | ```js 12 | (firstObj: AnyObject, secondObj: AnyObject) => boolean; 13 | ``` 14 | 15 | #### Arguments 16 | - firstObj(object) 17 | - secondObj(object) 18 | 19 | #### Returns 20 | (boolean) 21 | 22 | #### Example 23 | ```js 24 | utils.object.valueEqual( 25 | { 26 | a: 1, 27 | child: { 28 | key: 'value' 29 | }, 30 | b: 2, 31 | obj: {}, 32 | }, 33 | { 34 | b: 2, 35 | obj: {}, 36 | a: 1, 37 | child: { 38 | key: 'value' 39 | }, 40 | }, 41 | ); // true 42 | utils.object.valueEqual( 43 | { 44 | a: 1, 45 | b: 2, 46 | }, 47 | { 48 | a: 1, 49 | b: '2', 50 | } 51 | ); // false 52 | 53 | ``` 54 | 55 | ## has 56 | #### Describe 57 | 判断一个对象是否有该属性 58 | ```js 59 | (object: AnyObject, key: string) => boolean; 60 | ``` 61 | 62 | #### Arguments 63 | - object(object) 64 | - key(string) 65 | 66 | #### Returns 67 | (boolean) 68 | 69 | #### Example 70 | ```js 71 | const object = { key: 'value', nonexistent: undefined }; 72 | 73 | utils.object.has(object, 'key'); // true 74 | utils.object.has(object, 'nonexistent'); // false 75 | ``` 76 | -------------------------------------------------------------------------------- /docs/zh-cn/string.md: -------------------------------------------------------------------------------- 1 | # String 2 | ```js 3 | interface StringModule { 4 | replace: (match: RegExp | string) => (str: string, substitute: any) => string; 5 | split: (char: string | RegExp) => (str: string) => string[]; 6 | match: (regexp: RegExp) => (str: string) => string[]; 7 | } 8 | ``` 9 | 10 | ## replace 11 | #### Describe 12 | 对原生```replace```进行封装过的函数。 13 | ```js 14 | (match: RegExp | string) => (str: string, substitute: any) => string; 15 | ``` 16 | 17 | #### Arguments 18 | - match(RegExp | string): 匹配符 19 | 20 | #### Returns 21 | (```(str: string, substitute: any) => string```): 传入原字符串和代替字符串,返回新字符串的函数。 22 | 23 | #### Example 24 | ```js 25 | const str = 'I\'m Windlike.'; 26 | const replaceWindlike = utils.string.replace('Windlike'); 27 | 28 | replaceWindlike(str, 'I'); // I'm I. 29 | ``` 30 | 31 | ## split 32 | #### Describe 33 | 对原生```split```进行封装过的函数。 34 | ```js 35 | (char: string | RegExp) => (str: string) => string[]; 36 | ``` 37 | 38 | #### Arguments 39 | - char(RegExp | string): 分割符 40 | 41 | #### Returns 42 | (```(str: string) => string[]```): 传入原字符串,返回分割后的数组的函数。 43 | 44 | #### Example 45 | ```js 46 | const str = 'I And You'; 47 | const splitSpace = utils.string.split(' '); 48 | 49 | splitSpace(str); // ['I', 'And', 'You'] 50 | ``` 51 | 52 | ## match 53 | #### Describe 54 | 对原生```match```进行封装过的函数。 55 | ```js 56 | (regexp: RegExp) => (str: string) => string[]; 57 | ``` 58 | 59 | #### Arguments 60 | - regexp(RegExp): 正则表达式 61 | 62 | #### Returns 63 | (```(str: string) => string[]```): 传入原字符串,返回匹配到的字符串数组的函数。 64 | 65 | #### Example 66 | ```js 67 | const re = /[\w]+/g; 68 | const str = 'I am a string.'; 69 | const matchWork = utils.string.match(re); 70 | 71 | matchWork(str); // ['I', 'am', 'a', 'string'] 72 | ``` -------------------------------------------------------------------------------- /docs/zh-cn/verification.md: -------------------------------------------------------------------------------- 1 | # Verification 2 | ```js 3 | interface VerificationModule { 4 | readonly _phoneRE: RegExp; 5 | readonly _emailRE: RegExp; 6 | 7 | checkRe: (re: RegExp) => (checkedStr: string) => boolean; 8 | checkLength: (min: number, max: number) => (str: string) => boolean; 9 | check: (checkType: CheckType) => (checkedStr: string) => boolean; 10 | [prop: string]: any 11 | } 12 | 13 | type CheckType = 'phone' | 'email'; 14 | ``` 15 | 16 | ## checkRe 17 | #### Describe 18 | 创建检测字符串是否匹配正则表达式的函数。 19 | ```js 20 | (re: RegExp) => (checkedStr: string) => boolean; 21 | ``` 22 | 23 | #### Arguments 24 | - re(RegExp) 25 | 26 | #### Returns 27 | (```(checkedStr: string) => boolean```): 判断是否匹配正则的函数。 28 | 29 | #### Example 30 | ```js 31 | const checkFunction = utils.verification.checkRe(/<[\s\S]*?(script)[\s\S]*?>/); 32 | 33 | checkFunction(''); // true 34 | checkFunction(''); // true 35 | checkFunction('')).toBeTruthy(); 12 | expect(checkFunction('')).toBeTruthy(); 13 | expect(checkFunction('