Skip to content

Commit

Permalink
docs:添加使用文档
Browse files Browse the repository at this point in the history
  • Loading branch information
linzb93 committed May 23, 2024
1 parent 6af27db commit 3737ef7
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 15 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# utils

自用的工具包,包含工具函数,含测试用例。
自用的工具包,包含工具函数。

## 使用方法
```js
import { sleep, formatWeekCircle } from '@linzb93/utils';

(async () => {
await sleep(3000);
console.log('3秒后才执行');

formatWeekCircle(['周一','周二','周三','周六']); // 周一~周三、周六
})()
```

请在 Node v18+ 环境下开发。
```shell
git clone https://github.com/linzb93/utils.git
yarn
```
11 changes: 0 additions & 11 deletions packages/core/README.md

This file was deleted.

27 changes: 27 additions & 0 deletions packages/core/lib/binarySearch/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# binarySearch

通过二分查找的方式找到最符合条件的数。
## 使用方法
```js
import { binarySearch } from '@linzb93/utils';

binarySearch({
start: 1,
end: 100,
callback: data => data > 40
}); // 41

```
## API
### binarySearch({start, end, callback})
返回类型`number`, 最接近条件的数。
#### start
Type: `number`

起始值
#### end
Type: `number`
结束值

#### callback(data:number)
返回`boolean`
22 changes: 22 additions & 0 deletions packages/core/lib/cutDecimalSegNumber/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# cutDecimalSegNumber
## 使用方法
截断小数位数,默认2位

```js
import { cutDecimalSegNumber } from '@linzb93/utils';

cutDecimalSegNumber(23.332); // 23.33
cutDecimalSegNumber(23.33283, 3); // 23.332

```
## API
### cutDecimalSegNumber(data, digit?)
截断小数位数。
#### data
Type: `number`

原始值。
#### digit
Type: `number`
Default: 2
截断的位数。
22 changes: 22 additions & 0 deletions packages/core/lib/fakeProgress/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fakeProgress

当服务端没有返回进度条时,可以用`fakeProgress`生成假的进度条。

```js
import { fakeProgress } from '@linzb93/utils';

const obs$ = fakeProgress(pEvent, 5); // pEvent是Promise异步事件。第二个参数是预计执行时间,单位:秒。

obs$.subscribe(progress => {
console.log(progress);
});

obs$.then(() => {
console.log('已完成');
});

obs$.catch(() => {
console.log('报错');
})

```
9 changes: 9 additions & 0 deletions packages/core/lib/fix0/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# fix0
为个位数补0。

```js
import { fix0 } from '@linzb93/utils';

fix0(4); // '04'
fix0(12); // '12'
```
46 changes: 46 additions & 0 deletions packages/core/lib/fixContinuousNumber/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# fixContinuousNumber
生成连续的整数数组。
## 使用方法
```js
import { fixContinuousNumber } from '@linzb93/utils';

fixContinuousNumber(5); // ['0', '1', '2', '3', '4', '5']

fixContinuousNumber({
start: 2,
step: 2,
max: 6,
useFix0: true
}); // ['02', '04', '06']

```
## API
### fixContinuousNumber(max)
#### max
Type: `number`

最大值。列表从0开始生成,步长为1。

### fixContinuousNumber({start, max, step, useFix0})
#### max
Type: `number`

最大值。
#### start
Type: `number`

Default: 1

起始值。
#### step
Type: `number`

Default: 1

步长。
#### useFix0
Type: `boolean`

Default: false

是否补0。
31 changes: 31 additions & 0 deletions packages/core/lib/formatWeekCircle/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# formatWeekCircle
周期格式化。
## 使用方法
```js
import { formatWeekCircle } from '@linzb93/utils';

formatWeekCircle(["周一", "周二", "周三", "周五"]) // "周一~周三、周五"

formatWeekCircle(["周一", "周二", "周三", "周四", "周五", "周六", "周日"], {
allText: "全部"
}); // "全部"
```
## API
### formatWeekCircle(weekList, options?)
#### weekList
Type: `string[]`

星期列表,例如`["周一", "周二", "周三", "周五"]`

#### options
**allText**

Type: `string`

当一周七天都包含的时候,显示的文字。

**separator**

Type: `string`

分隔符,默认用`~`
2 changes: 1 addition & 1 deletion packages/core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export { fixContinuousNumber } from "./fixContinuousNumber/index";
export { isEmptyObject } from "./isEmptyObject/index";
export { isNumberHasMoreThan2Decimal } from "./isNumberHasMoreThan2Decimal/index";
export { fakeProgress } from "./fakeProgress/index";
export { timeIsSmaller, validateTimeOverlay } from "./timeOverlay/index";
export { isTimeOverlay, validateTimeOverlay } from "./timeOverlay/index";
export { timejs } from "./timejs/index";
export { formatWeekCircle } from "./formatWeekCircle/index";
9 changes: 9 additions & 0 deletions packages/core/lib/isEmptyObject/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# isEmptyObject
是否是空对象
```js
import { imEmptyObject } from '@linzb93/utils';

imEmptyObject({}); // true

isEmptyObject({user: 'Li'}); // false
```
7 changes: 7 additions & 0 deletions packages/core/lib/isNumberHasMoreThan2Decimal/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# isNumberHasMoreThan2Decimal
判断一个数是否有超过2位小数
```js
isNumberHasMoreThan2Decimal(23.34); // false

isNumberHasMoreThan2Decimal(23.223); // true
```
18 changes: 18 additions & 0 deletions packages/core/lib/sleep/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# sleep
延迟一定的时间。
## 使用方法
```js
import { sleep } from '@linzb93/utils';

(async () => {
await sleep(3000);
console.log('3秒后执行');
})();
```
## API
### sleep(time)
返回一个`Promise`
#### time
Type: `number`

延迟的时间,单位:秒。
16 changes: 16 additions & 0 deletions packages/core/lib/timeOverlay/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# timeOverlay
判断时间是否有重叠(目前还不支持跨天的)。
```js
import { isTimeOverlay, validateTimeOverlay } from '@linzb93/utils';

isTimeOverlay([
{ startTime: '18:00', endTime: '20:00'},
{ startTime: '18:30', endTime: '20:40'},
]); // true

isTimeOverlay([
{ startTime: '12:00', endTime: '17:00'},
{ startTime: '18:00', endTime: '20:00'},
{ startTime: '18:30', endTime: '20:40'},
]); // true
```
2 changes: 1 addition & 1 deletion packages/core/lib/timeOverlay/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { timejs } from "../timejs/index";
// 时间比大小,时间格式是mm:ss类型的。
export function timeIsSmaller(time0: string, time1: string): boolean {
function timeIsSmaller(time0: string, time1: string): boolean {
return timejs(time0).isBefore(time1);
}

Expand Down
47 changes: 47 additions & 0 deletions packages/core/lib/timejs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# timejs
时间相关的库(不含日期)
## 使用方法
```js
import { timejs } from '@linzb93/utils';

timejs('18:00').isAfter('15:30:00'); // true

timejs('15:00:00').isInRange('14:00:00', '15:30:00'); // true
```
## API
### timejs(time)
返回一个实例。
#### time
Type: `string`

时间格式支持"HH:mm"以及"HH:mm:ss"。

下面都是实例方法。
### isAfter(time)
所选时间是否在传入时间的后面。
#### time
Type: `string`

时间。
### isBefore(time)
所选时间是否在传入时间的前面。
#### time
Type: `string`

时间。
### isSame(time)
所选时间是否和传入时间相等。
#### time
Type: `string`

时间。
### isInRange(startTime, endTime)
所选时间是否在传入时间的中间(含相等)。
#### startTime
Type: `string`

时间。
#### endTime
Type: `string`

时间。
14 changes: 14 additions & 0 deletions packages/core/lib/validate/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# validate
正则表达式验证。
```js
import { validate, regExpMap } from '@linzb93/utils';

validate.isMobile('13533335555'); // true
validate.isMoney('23.213'); // false
validate.weakIsMoney('23.'); // true
```
支持4种正则表达式验证:
* isMobile: 是手机号
* includechinese: 包含中文
* isMoney: 是货币(最多包含2位小数)
* weakIsMoney: 是货币(最多包含2位小数,最后一位可以是`.`号)
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"declaration": true,
"declarationDir": "./types",
"experimentalDecorators": true,
"allowImportingTsExtensions": true,
"esModuleInterop": true,
"moduleResolution": "Bundler",
"isolatedModules": true,
Expand Down

0 comments on commit 3737ef7

Please sign in to comment.