-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
289 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
截断的位数。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('报错'); | ||
}) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
||
分隔符,默认用`~`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
||
延迟的时间,单位:秒。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
||
时间。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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位小数,最后一位可以是`.`号) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters