Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀v1.4.2 四值法拆分 #23

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 🚀v1.4.2 四值法拆分
➕: fourValueSplit 四值法拆分

### 🚀v1.4.1 递归调用增加父节点参数
➕: recursive 递归调用增加父节点参数

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
🏃‍包含一些常用方法例如对象深克隆/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节...持续更新整合
🏃‍包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节...持续更新整合

![action](https://img.shields.io/github/actions/workflow/status/linyisonger/3r.Tool/cd.yml)![npm](https://img.shields.io/npm/dw/@3r/tool)[![Coverage Status](https://coveralls.io/repos/github/linyisonger/3r.Tool/badge.svg?branch=master)](https://coveralls.io/github/linyisonger/3r.Tool?branch=master)![release](https://img.shields.io/librariesio/release/npm/@3r/tool)![npm](https://img.shields.io/npm/v/@3r/tool)![sourcerank](https://img.shields.io/librariesio/sourcerank/npm/@3r/tool)![NPM](https://img.shields.io/npm/l/@3r/tool)[![Code Climate](https://codeclimate.com/github/linyisonger/3r.Tool/badges/gpa.svg)](https://codeclimate.com/github/linyisonger/3r.Tool)[![Test Coverage](https://codeclimate.com/github/linyisonger/3r.Tool/badges/coverage.svg)](https://codeclimate.com/github/linyisonger/3r.Tool/coverage)[![docs](https://img.shields.io/badge/docs-info-blue)](https://linyisonger.github.io/3r.Tool/)
![action](https://img.shields.io/github/actions/workflow/status/linyisonger/3r.Tool/cd.yml)![npm](https://img.shields.io/npm/dw/@3r/tool)[![Coverage Status](https://coveralls.io/repos/github/linyisonger/3r.Tool/badge.svg?branch=master)](https://coveralls.io/github/linyisonger/3r.Tool?branch=master)![release](https://img.shields.io/librariesio/release/npm/@3r/tool)![npm](https://img.shields.io/npm/v/@3r/tool)![sourcerank](https://img.shields.io/librariesio/sourcerank/npm/@3r/tool)![NPM](https://img.shields.io/npm/l/@3r/tool)[![Code Climate](https://codeclimate.com/github/linyisonger/3r.Tool/badges/gpa.svg)](https://codeclimate.com/github/linyisonger/3r.Tool)[![Test Coverage](https://codeclimate.com/github/linyisonger/3r.Tool/badges/coverage.svg)](https://codeclimate.com/github/linyisonger/3r.Tool/coverage)[![codecov](https://codecov.io/gh/linyisonger/3r.Tool/graph/badge.svg?token=0VLDX7ON0N)](https://codecov.io/gh/linyisonger/3r.Tool)[![docs](https://img.shields.io/badge/docs-info-blue)](https://linyisonger.github.io/3r.Tool/)

#### 如何使用工具包 ?

Expand Down Expand Up @@ -110,7 +110,8 @@ console.log('蛇形命名法 -> 小驼峰命名法', Convertor.snakeCaseToLowerC
console.log('驼峰命名法 -> 蛇形命名法', Convertor.camelcaseToSnakeCase('LowerCaseWithUnderscores'));
console.log('通过日期获取星座', Convertor.getConstellationByDate('09/14'));
console.log('身份证号解析', '230504199607116664'.citizenIdentificationNumberParse);
console.log('字节转换',Convertor.byteFormat(1099511627776,2));
console.log('字节转换', Convertor.byteFormat(1099511627776, 2));
console.log('四值法拆分', Convertor.fourValueSplit(1));

```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@3r/tool",
"version": "1.4.1",
"version": "1.4.2",
"description": "🏃‍包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节...持续更新整合",
"main": "index.js",
"type": "module",
Expand Down
19 changes: 19 additions & 0 deletions src/lib/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,25 @@ export class Convertor {
}
return (byte / Convertor.byteDifferenceValue[Convertor.byteDifferenceValue.length - 1]).toFixed(fractionDigits) + units[units.length - 1]
}

/**
* 四值法拆分
* 1 => [1,1,1,1]
* [1] => [1,1,1,1]
* [1,2] => [1,2,1,2]
* [1,2,3]=> [1,2,3,2]
* [1,2,3,4] => [1,2,3,4]
* @param val 输入值
*/
static fourValueSplit(val: number | number[]): number[] {
if (Array.isArray(val)) {
if (val.length === 4) return val
if (val.length === 3) return val.concat([val[1]])
if (val.length === 2) return val.concat([val[0], val[1]])
if (val.length === 1) return [val[0], val[0], val[0], val[0]]
throw Error('array length error .')
} else return [val, val, val, val]
}
}

[
Expand Down
12 changes: 10 additions & 2 deletions test/convertor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ let run = function () {
console.log('驼峰命名法 -> 蛇形命名法', Convertor.camelcaseToSnakeCase('LowerCaseWithUnderscores'));
console.log('通过日期获取星座', Convertor.getConstellationByDate('09/14'));
console.log('身份证号解析', '230504199607116664'.citizenIdentificationNumberParse);
console.log('字节转换',Convertor.byteFormat(1099511627776,2));
console.log('字节转换', Convertor.byteFormat(1099511627776, 2));
console.log('四值法拆分', Convertor.fourValueSplit(1));
}
try {
describe('转换模块', function () {
Expand Down Expand Up @@ -169,7 +170,14 @@ try {
expect(byte.byteFormat(2)).toEqual('12.06KB')
expect(Convertor.byteFormat('1024', 2)).toEqual('1.00KB')
})

it('四值法拆分', function () {
expect(Convertor.fourValueSplit(1)).toEqual([1, 1, 1, 1])
expect(Convertor.fourValueSplit([1])).toEqual([1, 1, 1, 1])
expect(Convertor.fourValueSplit([1, 2])).toEqual([1, 2, 1, 2])
expect(Convertor.fourValueSplit([1, 2, 3])).toEqual([1, 2, 3, 2])
expect(Convertor.fourValueSplit([1, 2, 3, 4])).toEqual([1, 2, 3, 4])
expect(() => Convertor.fourValueSplit([1, 2, 3, 4, 5])).toThrow()
})
})
} catch (error) {
// describe is not defined 无需理会 调用方式不一致
Expand Down