Skip to content

Commit

Permalink
Merge pull request #24 from White-Dews/master
Browse files Browse the repository at this point in the history
🚀v1.4.3 在可及的范围内
  • Loading branch information
linyisonger authored Nov 27, 2023
2 parents 97721a3 + eec4722 commit 6d3376a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 🚀v1.4.3 在可及的范围内
➕: inRange 在可及的范围内

### 🚀v1.4.2 四值法拆分
➕: fourValueSplit 四值法拆分

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.2",
"version": "1.4.3",
"description": "🏃‍包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节...持续更新整合",
"main": "index.js",
"type": "module",
Expand Down
39 changes: 39 additions & 0 deletions src/lib/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,43 @@ export class Maths {
static pythagorasTheorem(a: number, b: number) {
return Math.sqrt(a * a + b * b)
}

/**
* 在可及的范围内
* @param val 值
* @param min 最小
* @param max 最大
* @returns 范围内值
*/
static inRange(val: number, min: number, max: number) {
if (min > val) return min
if (max < val) return max
return val
}
}

[
{ name: 'inRange', prototype: Number.prototype, type: 'method' }
].forEach(item => {
Object.defineProperty(item.prototype, item.name, {
get: function () {
const that = this as any
return function () {
return (Maths as any)[item.name].apply(that, [that].concat(Object.values(arguments)))
}
}
})
})

declare global {
interface Number {
/**
* 在可及的范围内
* @param min 最小
* @param max 最大
* @returns 范围内值
*/
inRange(min: number, max: number): number;
}

}
8 changes: 8 additions & 0 deletions test/maths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let run = function () {
console.log('数组 通过下标改变位置 从3的位置移到1的位置', Maths.interchange([1, 2, 3, 4], 3, 1));
console.log('阶乘 10!', Maths.iterationFactorial(10))
console.log('勾股定理', Maths.pythagorasTheorem(3, 4))
console.log('在可及的范围内', Maths.inRange(4, 1, 5))
}

try {
Expand Down Expand Up @@ -79,6 +80,13 @@ try {
it('勾股定理', function () {
expect(Maths.pythagorasTheorem(3, 4)).toEqual(5)
})
it('在可及的范围内', function () {
let val = 10;
expect(val.inRange(1, 10)).toEqual(val)
expect(val.inRange(1, 9)).toEqual(9)
expect(val.inRange(11, 91)).toEqual(11)
expect(val.inRange(10, 91)).toEqual(10)
})
})
} catch (error) {
// describe is not defined 无需理会 调用方式不一致
Expand Down

0 comments on commit 6d3376a

Please sign in to comment.