From 63f703d06dcdf03476eda4876ae552c54b7ad501 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 21 Sep 2024 20:52:52 -0700 Subject: [PATCH 01/19] feat: adds unit support to ceil and floor functions --- .../embeddedDocs/function/arithmetic/ceil.js | 9 +++-- .../embeddedDocs/function/arithmetic/floor.js | 9 +++-- src/function/arithmetic/ceil.js | 24 +++++++++++++ src/function/arithmetic/floor.js | 24 +++++++++++++ .../function/arithmetic/ceil.test.js | 28 +++++++++++++++ .../function/arithmetic/floor.test.js | 36 ++++++++++++++++--- 6 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/expression/embeddedDocs/function/arithmetic/ceil.js b/src/expression/embeddedDocs/function/arithmetic/ceil.js index ae6afb8f52..ef29722167 100644 --- a/src/expression/embeddedDocs/function/arithmetic/ceil.js +++ b/src/expression/embeddedDocs/function/arithmetic/ceil.js @@ -2,14 +2,19 @@ export const ceilDocs = { name: 'ceil', category: 'Arithmetic', syntax: [ - 'ceil(x)' + 'ceil(x)', + 'ceil(x, n)', + 'ceil(unit, valuelessUnit)', + 'ceil(unit, n, valuelessUnit)' ], description: 'Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.', examples: [ 'ceil(3.2)', 'ceil(3.8)', - 'ceil(-4.2)' + 'ceil(-4.2)', + 'ceil(3.241cm, cm)', + 'ceil(3.241cm, 2, cm)' ], seealso: ['floor', 'fix', 'round'] } diff --git a/src/expression/embeddedDocs/function/arithmetic/floor.js b/src/expression/embeddedDocs/function/arithmetic/floor.js index af78e187e7..ce6c55f22e 100644 --- a/src/expression/embeddedDocs/function/arithmetic/floor.js +++ b/src/expression/embeddedDocs/function/arithmetic/floor.js @@ -2,14 +2,19 @@ export const floorDocs = { name: 'floor', category: 'Arithmetic', syntax: [ - 'floor(x)' + 'floor(x)', + 'floor(x, n)', + 'floor(unit, valuelessUnit)', + 'floor(unit, n, valuelessUnit)' ], description: 'Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.', examples: [ 'floor(3.2)', 'floor(3.8)', - 'floor(-4.2)' + 'floor(-4.2)', + 'floor(3.241cm, cm)', + 'floor(3.241cm, 2, cm)' ], seealso: ['ceil', 'fix', 'round'] } diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 4d4c7a0ea1..48828a94ed 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -67,6 +67,12 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.ceil(c) // returns Complex 4 - 2i * math.ceil(c, 1) // returns Complex 3.3 - 2.7i * + * const unit = math.unit('3.241 cm') + * const cm = math.unit('cm') + * const mm = math.unit('mm') + * math.ceil(unit, 1, cm) // returns Unit 3.3 cm + * math.ceil(unit, 1, mm) // returns Unit 32.5 mm + * * math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4] * math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7] * @@ -122,6 +128,24 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.ceil(n.toNumber()) }, + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + const valueless = x.toNumeric(unit) + return unit.multiply(self(valueless, n)) + }), + + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + + 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + // deep map collection, skip zeros since ceil(0) = 0 + return deepMap(x, (value) => self(value, n, unit), true) + }), + + 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since ceil(0) = 0 return deepMap(x, self, true) diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 4129d5561b..dac6af2cea 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -66,6 +66,12 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.floor(c) // returns Complex 3 - 3i * math.floor(c, 1) // returns Complex 3.2 -2.8i * + * const unit = math.unit('3.241 cm') + * const cm = math.unit('cm') + * const mm = math.unit('mm') + * math.ceil(unit, 1, cm) // returns Unit 3.2 cm + * math.ceil(unit, 1, mm) // returns Unit 32.2 mm + * * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] * @@ -125,6 +131,24 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.floor(n.toNumber()) }, + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + const valueless = x.toNumeric(unit) + return unit.multiply(self(valueless, n)) + }), + + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + + 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + // deep map collection, skip zeros since floor(0) = 0 + return deepMap(x, (value) => self(value, n, unit), true) + }), + + 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since floor(0) = 0 return deepMap(x, self, true) diff --git a/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index e4a40508a2..b9315d29cd 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -148,6 +148,34 @@ describe('ceil', function () { assert.deepStrictEqual(ceil(bignumber(-799999.9999999999)), bignumber(-800000)) }) + it('should ceil units', function () { + assert.deepStrictEqual(ceil(unit('5.01 inch'), unit('inch')), unit('6 inch')) + assert.deepStrictEqual(ceil(unit('3.12345 cm'), 3, unit('cm')), unit('3.124 cm')) + assert.deepStrictEqual(ceil(unit('3.12345 cm'), unit('cm')), unit('4 cm')) + assert.deepStrictEqual(ceil(unit('2 inch'), unit('cm')), unit('6 cm')) + assert.deepStrictEqual(ceil(unit('2 inch'), 1, unit('cm')), unit('5.1 cm')) + + // bignumber values + assert.deepStrictEqual(ceil(unit('3.12345 cm'), bignumber(2), unit('cm')), unit('3.13 cm')) + assert.deepStrictEqual(ceil(unit(bignumber('2'), 'inch'), unit('cm')), unit(bignumber('6'), 'cm')) + assert.deepStrictEqual(ceil(unit(bignumber('2'), 'inch'), bignumber(1), unit('cm')), unit(bignumber('5.1'), 'cm')) + + // first argument is a collection + assert.deepStrictEqual(ceil([unit('2 inch'), unit('3 inch')], unit('cm')), [unit('6 cm'), unit('8 cm')]) + assert.deepStrictEqual(ceil(matrix([unit('2 inch'), unit('3 inch')]), unit('cm')), matrix([unit('6 cm'), unit('8 cm')])) + }) + + it('should throw an error if used with a unit without valueless unit', function () { + assert.throws(function () { ceil(unit('5cm')) }, TypeError, 'Function ceil(unit) not supported') + assert.throws(function () { ceil(unit('5cm'), 2) }, TypeError, 'Function ceil(unit) not supported') + assert.throws(function () { ceil(unit('5cm'), bignumber(2)) }, TypeError, 'Function ceil(unit) not supported') + }) + + it('should throw an error if used with a unit with a second unit that is not valueless', function () { + assert.throws(function () { ceil(unit('2 inch'), 1, unit('10 cm')) }, Error) + assert.throws(function () { ceil(unit('2 inch'), unit('10 cm')) }, Error) + }) + it('should throw an error for units', function () { assert.throws(function () { ceil(unit('5cm')) }, TypeError, 'Function ceil(unit) not supported') }) diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index 36ec8763fa..a8acc77f8b 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -12,7 +12,7 @@ const i = math.i const sparse = math.sparse describe('floor', function () { - it('should round booleans correctly', function () { + it('should floor booleans correctly', function () { assert.strictEqual(floor(true), 1) assert.strictEqual(floor(false), 0) }) @@ -148,6 +148,34 @@ describe('floor', function () { assert.deepStrictEqual(floor(bignumber(-30000.000000000004)), bignumber(-30000)) }) + it('should floor units', function () { + assert.deepStrictEqual(floor(unit('5.99 inch'), unit('inch')), unit('5 inch')) + assert.deepStrictEqual(floor(unit('3.12345 cm'), 3, unit('cm')), unit('3.123 cm')) + assert.deepStrictEqual(floor(unit('3.12345 cm'), unit('cm')), unit('3 cm')) + assert.deepStrictEqual(floor(unit('2 inch'), unit('cm')), unit('5 cm')) + assert.deepStrictEqual(floor(unit('2 inch'), 1, unit('cm')), unit('5 cm')) + + // bignumber values + assert.deepStrictEqual(floor(unit('3.12345 cm'), bignumber(2), unit('cm')), unit('3.12 cm')) + assert.deepStrictEqual(floor(unit(bignumber('2'), 'inch'), unit('cm')), unit(bignumber('5'), 'cm')) + assert.deepStrictEqual(floor(unit(bignumber('2'), 'inch'), bignumber(1), unit('cm')), unit(bignumber('5.0'), 'cm')) + + // first argument is a collection + assert.deepStrictEqual(floor([unit('2 inch'), unit('3 inch')], unit('cm')), [unit('5 cm'), unit('7 cm')]) + assert.deepStrictEqual(floor(matrix([unit('2 inch'), unit('3 inch')]), unit('cm')), matrix([unit('5 cm'), unit('7 cm')])) + }) + + it('should throw an error if used with a unit without valueless unit', function () { + assert.throws(function () { floor(unit('5cm')) }, TypeError, 'Function floor(unit) not supported') + assert.throws(function () { floor(unit('5cm'), 2) }, TypeError, 'Function floor(unit) not supported') + assert.throws(function () { floor(unit('5cm'), bignumber(2)) }, TypeError, 'Function floor(unit) not supported') + }) + + it('should throw an error if used with a unit with a second unit that is not valueless', function () { + assert.throws(function () { floor(unit('2 inch'), 1, unit('10 cm')) }, Error) + assert.throws(function () { floor(unit('2 inch'), unit('10 cm')) }, Error) + }) + it('should throw an error with a unit', function () { assert.throws(function () { floor(unit('5cm')) }, TypeError, 'Function floor(unit) not supported') }) @@ -226,9 +254,9 @@ describe('floor', function () { }) it('should throw an error if requested number of decimals is incorrect', function () { - assert.throws(function () { floor(2.5, 1.5) }, Error, 'Number of decimals in function round must be an integer') - assert.throws(function () { floor(2.5, -2) }, Error, ' Number of decimals in function round must be in the range of 0-15') - assert.throws(function () { floor(2.5, Infinity) }, Error, ' Number of decimals in function round must be in the range of 0-15') + assert.throws(function () { floor(2.5, 1.5) }, Error, 'Number of decimals in function floor must be an integer') + assert.throws(function () { floor(2.5, -2) }, Error, ' Number of decimals in function floor must be in the range of 0-15') + assert.throws(function () { floor(2.5, Infinity) }, Error, ' Number of decimals in function floor must be in the range of 0-15') }) it('should LaTeX floor', function () { From cfa3ba5d571d02f640f9c312899febfc63feed06 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 21 Sep 2024 21:01:16 -0700 Subject: [PATCH 02/19] feat: adds unit support for fix function --- .../embeddedDocs/function/arithmetic/fix.js | 9 ++++-- src/function/arithmetic/fix.js | 18 +++++++++++ .../function/arithmetic/fix.test.js | 32 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/expression/embeddedDocs/function/arithmetic/fix.js b/src/expression/embeddedDocs/function/arithmetic/fix.js index eb6302104e..e324fa9c6e 100644 --- a/src/expression/embeddedDocs/function/arithmetic/fix.js +++ b/src/expression/embeddedDocs/function/arithmetic/fix.js @@ -2,7 +2,10 @@ export const fixDocs = { name: 'fix', category: 'Arithmetic', syntax: [ - 'fix(x)' + 'fix(x)', + 'fix(x, n)', + 'fix(unit, valuelessUnit)', + 'fix(unit, n, valuelessUnit)' ], description: 'Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.', @@ -10,7 +13,9 @@ export const fixDocs = { 'fix(3.2)', 'fix(3.8)', 'fix(-4.2)', - 'fix(-4.8)' + 'fix(-4.8)', + 'fix(3.241cm, cm)', + 'fix(3.241cm, 2, cm)' ], seealso: ['ceil', 'floor', 'round'] } diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index f0e8a39aaa..609de46f4f 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -103,6 +103,24 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return x.s < 0 ? ceil(x, n) : floor(x, n) }, + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + const valueless = x.toNumeric(unit) + return unit.multiply(self(valueless, n)) + }), + + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + + 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + // deep map collection, skip zeros since round(0) = 0 + return deepMap(x, (value) => self(value, n, unit), true) + }), + + 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + + 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since fix(0) = 0 return deepMap(x, self, true) diff --git a/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index 821123cc6b..a0cb6f4d98 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -173,6 +173,38 @@ describe('fix', function () { assert.strictEqual(fix(-799999.9999999999, 3), -800000) }) + it('should fix units', function () { + assert.deepStrictEqual(fix(unit('5.99 inch'), unit('inch')), unit('5 inch')) + assert.deepStrictEqual(fix(unit('3.12345 cm'), 3, unit('cm')), unit('3.123 cm')) + assert.deepStrictEqual(fix(unit('3.12345 cm'), unit('cm')), unit('3 cm')) + assert.deepStrictEqual(fix(unit('2 inch'), unit('cm')), unit('5 cm')) + assert.deepStrictEqual(fix(unit('2 inch'), 1, unit('cm')), unit('5 cm')) + + // bignumber values + assert.deepStrictEqual(fix(unit('3.12345 cm'), bignumber(2), unit('cm')), unit('3.12 cm')) + assert.deepStrictEqual(fix(unit(bignumber('2'), 'inch'), unit('cm')), unit(bignumber('5'), 'cm')) + assert.deepStrictEqual(fix(unit(bignumber('2'), 'inch'), bignumber(1), unit('cm')), unit(bignumber('5.0'), 'cm')) + + // first argument is a collection + assert.deepStrictEqual(fix([unit('2 inch'), unit('3 inch')], unit('cm')), [unit('5 cm'), unit('7 cm')]) + assert.deepStrictEqual(fix(matrix([unit('2 inch'), unit('3 inch')]), unit('cm')), matrix([unit('5 cm'), unit('7 cm')])) + }) + + it('should throw an error if used with a unit without valueless unit', function () { + assert.throws(function () { fix(unit('5cm')) }, TypeError, 'Function fix(unit) not supported') + assert.throws(function () { fix(unit('5cm'), 2) }, TypeError, 'Function fix(unit) not supported') + assert.throws(function () { fix(unit('5cm'), bignumber(2)) }, TypeError, 'Function fix(unit) not supported') + }) + + it('should throw an error if used with a unit with a second unit that is not valueless', function () { + assert.throws(function () { fix(unit('2 inch'), 1, unit('10 cm')) }, Error) + assert.throws(function () { fix(unit('2 inch'), unit('10 cm')) }, Error) + }) + + it('should throw an error with a unit', function () { + assert.throws(function () { fix(unit('5cm')) }, TypeError, 'Function fix(unit) not supported') + }) + it('should throw an error on unit as parameter', function () { // unit assert.throws(function () { fix(unit('5cm')) }, TypeError, 'Function fix(unit) not supported') From c2e67e2178a4fa0be707751716e27308e007d424 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 21 Sep 2024 21:03:02 -0700 Subject: [PATCH 03/19] test: adds negative units test --- test/unit-tests/function/arithmetic/fix.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index a0cb6f4d98..74cf373a34 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -179,6 +179,7 @@ describe('fix', function () { assert.deepStrictEqual(fix(unit('3.12345 cm'), unit('cm')), unit('3 cm')) assert.deepStrictEqual(fix(unit('2 inch'), unit('cm')), unit('5 cm')) assert.deepStrictEqual(fix(unit('2 inch'), 1, unit('cm')), unit('5 cm')) + assert.deepStrictEqual(fix(unit('-1.9 inch'), unit('cm')), unit('-4 cm')) // bignumber values assert.deepStrictEqual(fix(unit('3.12345 cm'), bignumber(2), unit('cm')), unit('3.12 cm')) From aa4c4084a57c19fbdc9fec73a33326c790ef5d6d Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 21 Sep 2024 21:17:35 -0700 Subject: [PATCH 04/19] chore: fixes and adds example to arithmetic files --- src/function/arithmetic/fix.js | 6 ++++++ src/function/arithmetic/floor.js | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 609de46f4f..b1c0f60d9d 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -50,6 +50,12 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * math.fix(c) // returns Complex 3 - 2i * math.fix(c, 1) // returns Complex 3.2 -2.7i * + * const unit = math.unit('3.241 cm') + * const cm = math.unit('cm') + * const mm = math.unit('mm') + * math.fix(unit, 1, cm) // returns Unit 3.2 cm + * math.fix(unit, 1, mm) // returns Unit 32.4 mm + * * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4] * math.fix([3.2, 3.8, -4.7], 1) // returns Array [3.2, 3.8, -4.7] * diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index dac6af2cea..08fa7715a5 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -69,8 +69,8 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * const unit = math.unit('3.241 cm') * const cm = math.unit('cm') * const mm = math.unit('mm') - * math.ceil(unit, 1, cm) // returns Unit 3.2 cm - * math.ceil(unit, 1, mm) // returns Unit 32.2 mm + * math.floor(unit, 1, cm) // returns Unit 3.2 cm + * math.floor(unit, 1, mm) // returns Unit 32.4 mm * * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] From 7e6ad32a11d91d799daa95a83b3e0d0ccce0d76e Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 21 Sep 2024 21:17:48 -0700 Subject: [PATCH 05/19] type: adds typing for functions --- test/typescript-tests/testTypes.ts | 9 +++++ types/index.d.ts | 57 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 735ac1148b..95b1730149 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -649,6 +649,9 @@ Chaining examples expectTypeOf(math.chain([1]).ceil()).toMatchTypeOf< MathJsChain >() + expectTypeOf( + math.chain(math.unit('5.2cm')).ceil(math.unit('cm')) + ).toMatchTypeOf>() // fix expectTypeOf(math.chain(1).fix()).toMatchTypeOf< @@ -657,6 +660,9 @@ Chaining examples expectTypeOf(math.chain([1]).fix()).toMatchTypeOf< MathJsChain >() + expectTypeOf( + math.chain(math.unit('5.2cm')).fix(math.unit('cm')) + ).toMatchTypeOf>() // floor expectTypeOf(math.chain(1).floor()).toMatchTypeOf< @@ -665,6 +671,9 @@ Chaining examples expectTypeOf(math.chain([1]).floor()).toMatchTypeOf< MathJsChain >() + expectTypeOf( + math.chain(math.unit('5.2cm')).floor(math.unit('cm')) + ).toMatchTypeOf>() // round expectTypeOf(math.chain(1).round()).toMatchTypeOf< diff --git a/types/index.d.ts b/types/index.d.ts index 16f4d16192..feb9d5cbe0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4995,6 +4995,25 @@ export interface MathJsChain { this: MathJsChain, n?: number | BigNumber | MathCollection ): MathJsChain + ceil( + this: MathJsChain, + n: U + ): MathJsChain + ceil(this: MathJsChain, unit: Unit): MathJsChain + ceil( + this: MathJsChain, + unit: Unit + ): MathJsChain + ceil( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain + ceil( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain /** * Round a value towards zero. For matrices, the function is evaluated @@ -5005,6 +5024,25 @@ export interface MathJsChain { this: MathJsChain, n?: number | BigNumber | MathCollection ): MathJsChain + fix( + this: MathJsChain, + n: U + ): MathJsChain + fix(this: MathJsChain, unit: Unit): MathJsChain + fix( + this: MathJsChain, + unit: Unit + ): MathJsChain + fix( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain + fix( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain /** * Round a value towards minus infinity. For matrices, the function is @@ -5015,6 +5053,25 @@ export interface MathJsChain { this: MathJsChain, n?: number | BigNumber | MathCollection ): MathJsChain + floor( + this: MathJsChain, + n: U + ): MathJsChain + floor(this: MathJsChain, unit: Unit): MathJsChain + floor( + this: MathJsChain, + unit: Unit + ): MathJsChain + floor( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain + floor( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain /** * Round a value towards the nearest integer. For matrices, the function From 006db55b8597395e65ceb2ddc1b0ebda33dc6888 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Wed, 25 Sep 2024 12:08:01 -0700 Subject: [PATCH 06/19] refactor: combines conditions --- src/function/arithmetic/ceil.js | 13 ++++--------- src/function/arithmetic/fix.js | 15 +++++---------- src/function/arithmetic/floor.js | 13 ++++--------- src/function/arithmetic/round.js | 13 ++++--------- 4 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 48828a94ed..2847f17581 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -128,23 +128,18 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.ceil(n.toNumber()) }, - 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { + if (typeof n !== 'number') n = n.toNumber() const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), - 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), - - 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since ceil(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) }), - 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since ceil(0) = 0 diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index b1c0f60d9d..791e6dabfe 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -109,23 +109,18 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return x.s < 0 ? ceil(x, n) : floor(x, n) }, - 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { + if (typeof n !== 'number') n = n.toNumber() const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), - 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), - - 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { - // deep map collection, skip zeros since round(0) = 0 + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { + // deep map collection, skip zeros since fix(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) }), - 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since fix(0) = 0 diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 08fa7715a5..711bda8600 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -131,23 +131,18 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.floor(n.toNumber()) }, - 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { + if (typeof n !== 'number') n = n.toNumber() const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), - 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), - - 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since floor(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) }), - 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since floor(0) = 0 diff --git a/src/function/arithmetic/round.js b/src/function/arithmetic/round.js index 7e585fb5af..e87f4e973c 100644 --- a/src/function/arithmetic/round.js +++ b/src/function/arithmetic/round.js @@ -147,23 +147,18 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.round(n.toNumber()) }, - 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { + 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { + if (typeof n !== 'number') n = n.toNumber() const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), - 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), - - 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since round(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) }), - 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), - - 'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), + 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)), 'Array | Matrix': typed.referToSelf(self => x => { // deep map collection, skip zeros since round(0) = 0 From 623bafebc69ff90d7f57c53beabb7574049ea997 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Wed, 25 Sep 2024 16:06:22 -0700 Subject: [PATCH 07/19] fix: add missing types. --- test/typescript-tests/testTypes.ts | 6 ++++++ types/index.d.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 95b1730149..4df2c4eb16 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1807,6 +1807,12 @@ Function ceil examples math.fraction(6286, 1000) ]) + // unit input + const numCeiledUnit = math.ceil(math.unit(3.2, 'cm'), math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit, math.unit(4, 'cm')) + const numCeiledUnit1Dec = math.ceil(math.unit(3.21, 'cm'), 1, math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.3, 'cm')) + // @ts-expect-error ... verify ceil(array, array) throws an error (for now) assert.throws(() => math.ceil([3.21, 3.82], [1, 2]), TypeError) } diff --git a/types/index.d.ts b/types/index.d.ts index feb9d5cbe0..1f356e379c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1147,6 +1147,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType ceil(x: MathNumericType, n: U): U + ceil(x: U, unit: Unit): U + ceil(x: Unit, unit: Unit): Unit + ceil(x: Unit, n: number | BigNumber, unit: Unit): Unit + ceil(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards zero. For matrices, the function is evaluated @@ -1160,6 +1164,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType fix(x: MathNumericType, n: U): U + fix(x: U, unit: Unit): U + fix(x: Unit, unit: Unit): Unit + fix(x: Unit, n: number | BigNumber, unit: Unit): Unit + fix(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards minus infinity. For matrices, the function is @@ -1173,6 +1181,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType floor(x: MathNumericType, n: U): U + floor(x: U, unit: Unit): U + floor(x: Unit, unit: Unit): Unit + floor(x: Unit, n: number | BigNumber, unit: Unit): Unit + floor(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards the nearest integer. For matrices, the function From 5ece8e6adb2486aa2cc7b9d2cd09f94318103a05 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Wed, 25 Sep 2024 16:31:15 -0700 Subject: [PATCH 08/19] chore: adds more unit tests --- test/typescript-tests/testTypes.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 4df2c4eb16..3413e2a359 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1786,6 +1786,12 @@ Function ceil examples math.complex(3.3, -2.7) ) + // unit input + const numCeiledUnit = math.ceil(math.unit(3.2, 'cm'), math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit, math.unit(4, 'cm')) + const numCeiledUnit1Dec = math.ceil(math.unit(3.21, 'cm'), 1, math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.3, 'cm')) + // array input assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4]) assert.deepStrictEqual(math.ceil([3.21, 3.82, -4.71], 1), [3.3, 3.9, -4.7]) @@ -1807,12 +1813,6 @@ Function ceil examples math.fraction(6286, 1000) ]) - // unit input - const numCeiledUnit = math.ceil(math.unit(3.2, 'cm'), math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit, math.unit(4, 'cm')) - const numCeiledUnit1Dec = math.ceil(math.unit(3.21, 'cm'), 1, math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.3, 'cm')) - // @ts-expect-error ... verify ceil(array, array) throws an error (for now) assert.throws(() => math.ceil([3.21, 3.82], [1, 2]), TypeError) } @@ -1868,6 +1868,12 @@ Function fix examples math.complex(3.2, -2.7) ) + // unit input + const numCeiledUnit = math.fix(math.unit(3.2, 'cm'), math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit, math.unit(3, 'cm')) + const numCeiledUnit1Dec = math.fix(math.unit(3.1, 'cm'), 1, math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.1, 'cm')) + // array input assert.deepStrictEqual(math.fix([3.2, 3.8, -4.7]), [3, 3, -4]) assert.deepStrictEqual(math.fix([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7]) @@ -1944,6 +1950,12 @@ Function floor examples math.complex(3.2, -2.8) ) + // unit input + const numCeiledUnit = math.floor(math.unit(3.2, 'cm'), math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit, math.unit(3, 'cm')) + const numCeiledUnit1Dec = math.floor(math.unit(3.1, 'cm'), 1, math.unit('cm')) + assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.1, 'cm')) + // array input assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5]) assert.deepStrictEqual(math.floor([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.8]) From b9437ce0824ef9dbf5296e095899037c33689138 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 27 Sep 2024 19:40:20 -0700 Subject: [PATCH 09/19] chore: add tests for round(unit, number,unit) --- test/typescript-tests/testTypes.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 3413e2a359..bc42189793 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -674,6 +674,9 @@ Chaining examples expectTypeOf( math.chain(math.unit('5.2cm')).floor(math.unit('cm')) ).toMatchTypeOf>() + expectTypeOf( + math.chain(math.unit('5.2cm')).round(2, math.unit('cm')) + ).toMatchTypeOf>() // round expectTypeOf(math.chain(1).round()).toMatchTypeOf< From 08ec5ab853d94c630535f43abdb94d31e4efdb80 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 4 Oct 2024 19:40:47 -0700 Subject: [PATCH 10/19] refactor: revert to separate signatures --- src/function/arithmetic/ceil.js | 5 +++-- src/function/arithmetic/fix.js | 5 +++-- src/function/arithmetic/floor.js | 5 +++-- src/function/arithmetic/round.js | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 2847f17581..19971c9907 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -128,12 +128,13 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.ceil(n.toNumber()) }, - 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { - if (typeof n !== 'number') n = n.toNumber() + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since ceil(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 791e6dabfe..222764bf7d 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -109,12 +109,13 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return x.s < 0 ? ceil(x, n) : floor(x, n) }, - 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { - if (typeof n !== 'number') n = n.toNumber() + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since fix(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 711bda8600..026d29119e 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -131,12 +131,13 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.floor(n.toNumber()) }, - 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { - if (typeof n !== 'number') n = n.toNumber() + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since floor(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) diff --git a/src/function/arithmetic/round.js b/src/function/arithmetic/round.js index e87f4e973c..24844656bb 100644 --- a/src/function/arithmetic/round.js +++ b/src/function/arithmetic/round.js @@ -147,12 +147,13 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, return x.round(n.toNumber()) }, - 'Unit, number | BigNumber, Unit': typed.referToSelf(self => function (x, n, unit) { - if (typeof n !== 'number') n = n.toNumber() + 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) { const valueless = x.toNumeric(unit) return unit.multiply(self(valueless, n)) }), + 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), + 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since round(0) = 0 return deepMap(x, (value) => self(value, n, unit), true) From 300237d900327e29599886b14ea66eafcac117f3 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sat, 5 Oct 2024 01:02:10 -0700 Subject: [PATCH 11/19] wip: improve types for rounding functions inputs --- src/function/arithmetic/ceil.js | 7 ++- src/function/arithmetic/fix.js | 9 ++- src/function/arithmetic/floor.js | 7 ++- src/function/arithmetic/round.js | 2 +- test/typescript-tests/testTypes.ts | 80 +++++++++++++++++++++------ types/index.d.ts | 89 ++++++++++++++++++++++++------ 6 files changed, 154 insertions(+), 40 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 19971c9907..339f0dce95 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -50,6 +50,8 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * math.ceil(x) * math.ceil(x, n) + * math.ceil(unit, valuelessUnit) + * math.ceil(unit, n, valuelessUnit) * * Examples: * @@ -80,9 +82,10 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * floor, fix, round * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value + * @param {Unit} [valuelessUnit] A valueless unit + * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value */ return typed('ceil', { number: ceilNumber.signatures.number, diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 222764bf7d..90f59ea80b 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -33,6 +33,8 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * * math.fix(x) * math.fix(x,n) + * math.fix(unit, valuelessUnit) + * math.fix(unit, n, valuelessUnit) * * Examples: * @@ -63,9 +65,10 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * * ceil, floor, round * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value + * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded + * @param {number | BigNumber | Array} [n=0] Number of decimals + * @param {Unit} [valuelessUnit] A valueless unit + * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value */ return typed('fix', { number: fixNumber.signatures.number, diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 026d29119e..449111df7c 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -49,6 +49,8 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * math.floor(x) * math.floor(x, n) + * math.floor(unit, valuelessUnit) + * math.floor(unit, n, valuelessUnit) * * Examples: * @@ -83,9 +85,10 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * ceil, fix, round * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value + * @param {Unit} [valuelessUnit] A valueless unit + * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value */ return typed('floor', { number: floorNumber.signatures.number, diff --git a/src/function/arithmetic/round.js b/src/function/arithmetic/round.js index 24844656bb..1f48fa13e7 100644 --- a/src/function/arithmetic/round.js +++ b/src/function/arithmetic/round.js @@ -71,7 +71,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded * @param {number | BigNumber | Array} [n=0] Number of decimals * @param {Unit} [valuelessUnit] A valueless unit - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value + * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value */ return typed(name, { number: function (x) { diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index bc42189793..e1f0ec5da0 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1790,10 +1790,21 @@ Function ceil examples ) // unit input - const numCeiledUnit = math.ceil(math.unit(3.2, 'cm'), math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit, math.unit(4, 'cm')) - const numCeiledUnit1Dec = math.ceil(math.unit(3.21, 'cm'), 1, math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.3, 'cm')) + const u1 = math.unit(3.2, 'cm') + const u2 = math.unit('cm') + const u3 = math.unit(5.51, 'cm') + const unitArray = [u1, u3] + const unitMatrix = math.matrix(unitArray) + assert.deepStrictEqual(math.ceil(u1, u2), math.unit(4, 'cm')) + assert.deepStrictEqual(math.ceil(u1, 1, u2), math.unit(3.2, 'cm')) + assert.deepStrictEqual(math.ceil(unitArray, 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.6, 'cm') + ]) + assert.deepStrictEqual( + math.ceil(unitMatrix, 1, math.unit('cm')), + math.matrix([math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]) + ) // array input assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4]) @@ -1872,10 +1883,21 @@ Function fix examples ) // unit input - const numCeiledUnit = math.fix(math.unit(3.2, 'cm'), math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit, math.unit(3, 'cm')) - const numCeiledUnit1Dec = math.fix(math.unit(3.1, 'cm'), 1, math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.1, 'cm')) + const u1 = math.unit(3.2, 'cm') + const u2 = math.unit('cm') + const u3 = math.unit(5.51, 'cm') + const unitArray = [u1, u3] + const unitMatrix = math.matrix(unitArray) + assert.deepStrictEqual(math.fix(u1, u2), math.unit(3, 'cm')) + assert.deepStrictEqual(math.fix(u1, 1, u2), math.unit(3.2, 'cm')) + assert.deepStrictEqual(math.fix(unitArray, 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.5, 'cm') + ]) + assert.deepStrictEqual( + math.fix(unitMatrix, 1, math.unit('cm')), + math.matrix([math.unit(3.2, 'cm'), math.unit(5.5, 'cm')]) + ) // array input assert.deepStrictEqual(math.fix([3.2, 3.8, -4.7]), [3, 3, -4]) @@ -1954,10 +1976,21 @@ Function floor examples ) // unit input - const numCeiledUnit = math.floor(math.unit(3.2, 'cm'), math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit, math.unit(3, 'cm')) - const numCeiledUnit1Dec = math.floor(math.unit(3.1, 'cm'), 1, math.unit('cm')) - assert.deepStrictEqual(numCeiledUnit1Dec, math.unit(3.1, 'cm')) + const u1 = math.unit(3.2, 'cm') + const u2 = math.unit('cm') + const u3 = math.unit(5.51, 'cm') + const unitArray = [u1, u3] + const unitMatrix = math.matrix(unitArray) + assert.deepStrictEqual(math.floor(u1, u2), math.unit(3, 'cm')) + assert.deepStrictEqual(math.floor(u1, 1, u2), math.unit(3.2, 'cm')) + assert.deepStrictEqual(math.floor(unitArray, 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.5, 'cm') + ]) + assert.deepStrictEqual( + math.floor(unitMatrix, 1, math.unit('cm')), + math.matrix([math.unit(3.2, 'cm'), math.unit(5.5, 'cm')]) + ) // array input assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5]) @@ -2036,13 +2069,28 @@ Function round examples ) // unit input + const u1 = math.unit(3.2, 'cm') + const u2 = math.unit('cm') + const u3 = math.unit(5.51, 'cm') + const unitArray = [u1, u3] + const unitMatrix = math.matrix(unitArray) + assert.deepStrictEqual(math.round(u1, u2), math.unit(3, 'cm')) + assert.deepStrictEqual(math.round(u1, 1, u2), math.unit(3.2, 'cm')) + assert.deepStrictEqual( + math.round(u1, math.bignumber(1), u2), + math.unit(3.2, 'cm') + ) + assert.deepStrictEqual(math.round(unitArray, 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.5, 'cm') + ]) assert.deepStrictEqual( - math.round(math.unit('5.21 cm'), math.unit('cm')), - math.unit('5 cm') + math.round(unitArray, math.bignumber(1), math.unit('cm')), + [math.unit(3.2, 'cm'), math.unit(5.5, 'cm')] ) assert.deepStrictEqual( - math.round(math.unit('5.21 cm'), 1, math.unit('cm')), - math.unit('5.2 cm') + math.round(unitMatrix, 1, math.unit('cm')), + math.matrix([math.unit(3.2, 'cm'), math.unit(5.5, 'cm')]) ) // array input diff --git a/types/index.d.ts b/types/index.d.ts index 1f356e379c..e0c879a052 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -18,6 +18,9 @@ export type MathCollection = MathArray | Matrix export type MathType = MathScalarType | MathCollection export type MathExpression = string | string[] | MathCollection +export type UnitArray = Unit[] | Unit[][] +export type UnitCollection = UnitArray | UnitMatrix + // eslint-disable-next-line @typescript-eslint/no-explicit-any export type FactoryFunction = (scope: any) => T @@ -751,6 +754,17 @@ export interface MathJsInstance extends MathJsFactory { format?: 'sparse' | 'dense', dataType?: string ): Matrix + /** + * @param data A multi dimensional array + * @param format The Matrix storage format + * @param dataType The Matrix data type + * @returns The created Unit Matrix + */ + matrix( + data: UnitArray, + format?: 'sparse' | 'dense', + dataType?: string + ): UnitMatrix /** * Create a number or convert a string, boolean, or unit to a number. @@ -1147,10 +1161,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType ceil(x: MathNumericType, n: U): U - ceil(x: U, unit: Unit): U + ceil(x: U, unit: Unit): U ceil(x: Unit, unit: Unit): Unit ceil(x: Unit, n: number | BigNumber, unit: Unit): Unit - ceil(x: U, n: number | BigNumber, unit: Unit): U + ceil(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards zero. For matrices, the function is evaluated @@ -1164,10 +1178,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType fix(x: MathNumericType, n: U): U - fix(x: U, unit: Unit): U + fix(x: U, unit: Unit): U fix(x: Unit, unit: Unit): Unit fix(x: Unit, n: number | BigNumber, unit: Unit): Unit - fix(x: U, n: number | BigNumber, unit: Unit): U + fix(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards minus infinity. For matrices, the function is @@ -1181,10 +1195,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType floor(x: MathNumericType, n: U): U - floor(x: U, unit: Unit): U + floor(x: U, unit: Unit): U floor(x: Unit, unit: Unit): Unit floor(x: Unit, n: number | BigNumber, unit: Unit): Unit - floor(x: U, n: number | BigNumber, unit: Unit): U + floor(x: U, n: number | BigNumber, unit: Unit): U /** * Round a value towards the nearest integer. For matrices, the function @@ -1198,10 +1212,10 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType round(x: MathNumericType, n: U): U - round(x: U, unit: Unit): U + round(x: U, unit: Unit): U round(x: Unit, unit: Unit): Unit round(x: Unit, n: number | BigNumber, unit: Unit): Unit - round(x: U, n: number | BigNumber, unit: Unit): U + round(x: U, n: number | BigNumber, unit: Unit): U // End of group of rounding functions @@ -4024,6 +4038,49 @@ export interface MatrixCtor { new (): Matrix } +export interface UnitMatrix + extends Omit< + Matrix, + | 'create' + | 'toArray' + | 'valueOf' + | 'subset' + | 'apply' + | 'set' + | 'resize' + | 'clone' + | 'map' + | 'forEach' + | 'toArray' + | 'valueOf' + | 'swapRows' + > { + create(data: UnitArray, datatype?: string): void + // eslint-disable-next-line @typescript-eslint/no-explicit-any + subset(index: Index, replacement?: any, defaultValue?: any): UnitMatrix + apply( + dim: number, + callback: (array: UnitCollection) => number + ): UnitCollection + // eslint-disable-next-line @typescript-eslint/no-explicit-any + set(index: number[], value: any, defaultValue?: number | string): UnitMatrix + resize(size: MathCollection, defaultValue?: number | string): UnitMatrix + clone(): UnitMatrix + map( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + callback: (a: any, b: number[], c: UnitMatrix) => any, + skipZeros?: boolean + ): UnitMatrix + forEach( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + callback: (a: any, b: number[], c: UnitMatrix) => void, + skipZeros?: boolean + ): void + toArray(): UnitArray + valueOf(): UnitArray + swapRows(i: number, j: number): UnitMatrix +} + // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface BigNumber extends Decimal {} @@ -5012,7 +5069,7 @@ export interface MathJsChain { n: U ): MathJsChain ceil(this: MathJsChain, unit: Unit): MathJsChain - ceil( + ceil( this: MathJsChain, unit: Unit ): MathJsChain @@ -5021,7 +5078,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - ceil( + ceil( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5041,7 +5098,7 @@ export interface MathJsChain { n: U ): MathJsChain fix(this: MathJsChain, unit: Unit): MathJsChain - fix( + fix( this: MathJsChain, unit: Unit ): MathJsChain @@ -5050,7 +5107,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - fix( + fix( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5070,7 +5127,7 @@ export interface MathJsChain { n: U ): MathJsChain floor(this: MathJsChain, unit: Unit): MathJsChain - floor( + floor( this: MathJsChain, unit: Unit ): MathJsChain @@ -5079,7 +5136,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - floor( + floor( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5099,7 +5156,7 @@ export interface MathJsChain { n: U ): MathJsChain round(this: MathJsChain, unit: Unit): MathJsChain - round( + round( this: MathJsChain, unit: Unit ): MathJsChain @@ -5108,7 +5165,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - round( + round( this: MathJsChain, n: number | BigNumber, unit: Unit From c71df170a086340959c76ca72e370b099824dc3e Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sun, 6 Oct 2024 15:05:12 -0700 Subject: [PATCH 12/19] feat: allow matrices to have units --- test/typescript-tests/testTypes.ts | 10 +++ types/index.d.ts | 124 ++++++++++------------------- 2 files changed, 52 insertions(+), 82 deletions(-) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index e1f0ec5da0..dac18579b2 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1794,6 +1794,7 @@ Function ceil examples const u2 = math.unit('cm') const u3 = math.unit(5.51, 'cm') const unitArray = [u1, u3] + const unitMatrix = math.matrix(unitArray) assert.deepStrictEqual(math.ceil(u1, u2), math.unit(4, 'cm')) assert.deepStrictEqual(math.ceil(u1, 1, u2), math.unit(3.2, 'cm')) @@ -1801,11 +1802,20 @@ Function ceil examples math.unit(3.2, 'cm'), math.unit(5.6, 'cm') ]) + assert.deepStrictEqual( math.ceil(unitMatrix, 1, math.unit('cm')), math.matrix([math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]) ) + const array = [u1, u3, 1] + array.pop() + + assert.deepStrictEqual(math.ceil(array as Unit[], 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.6, 'cm') + ]) + // array input assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4]) assert.deepStrictEqual(math.ceil([3.21, 3.82, -4.71], 1), [3.3, 3.9, -4.7]) diff --git a/types/index.d.ts b/types/index.d.ts index e0c879a052..4a2d87aff6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,17 +10,15 @@ export type NoLiteralType = T extends number ? boolean : T -// TODO: introduce generics for MathCollection, MathMatrix, and MathArray export type MathNumericType = number | BigNumber | bigint | Fraction | Complex export type MathScalarType = MathNumericType | Unit -export type MathArray = MathNumericType[] | MathNumericType[][] // TODO: MathArray can also contain Unit -export type MathCollection = MathArray | Matrix +export type MathArray = T[] | T[][] +export type MathCollection = + | MathArray + | Matrix export type MathType = MathScalarType | MathCollection export type MathExpression = string | string[] | MathCollection -export type UnitArray = Unit[] | Unit[][] -export type UnitCollection = UnitArray | UnitMatrix - // eslint-disable-next-line @typescript-eslint/no-explicit-any export type FactoryFunction = (scope: any) => T @@ -754,17 +752,6 @@ export interface MathJsInstance extends MathJsFactory { format?: 'sparse' | 'dense', dataType?: string ): Matrix - /** - * @param data A multi dimensional array - * @param format The Matrix storage format - * @param dataType The Matrix data type - * @returns The created Unit Matrix - */ - matrix( - data: UnitArray, - format?: 'sparse' | 'dense', - dataType?: string - ): UnitMatrix /** * Create a number or convert a string, boolean, or unit to a number. @@ -1161,10 +1148,14 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType ceil(x: MathNumericType, n: U): U - ceil(x: U, unit: Unit): U + ceil>(x: U, unit: Unit): U ceil(x: Unit, unit: Unit): Unit ceil(x: Unit, n: number | BigNumber, unit: Unit): Unit - ceil(x: U, n: number | BigNumber, unit: Unit): U + ceil>( + x: U, + n: number | BigNumber, + unit: Unit + ): U /** * Round a value towards zero. For matrices, the function is evaluated @@ -1178,10 +1169,14 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType fix(x: MathNumericType, n: U): U - fix(x: U, unit: Unit): U + fix>(x: U, unit: Unit): U fix(x: Unit, unit: Unit): Unit fix(x: Unit, n: number | BigNumber, unit: Unit): Unit - fix(x: U, n: number | BigNumber, unit: Unit): U + fix>( + x: U, + n: number | BigNumber, + unit: Unit + ): U /** * Round a value towards minus infinity. For matrices, the function is @@ -1195,10 +1190,14 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType floor(x: MathNumericType, n: U): U - floor(x: U, unit: Unit): U + floor>(x: U, unit: Unit): U floor(x: Unit, unit: Unit): Unit floor(x: Unit, n: number | BigNumber, unit: Unit): Unit - floor(x: U, n: number | BigNumber, unit: Unit): U + floor>( + x: U, + n: number | BigNumber, + unit: Unit + ): U /** * Round a value towards the nearest integer. For matrices, the function @@ -1212,10 +1211,14 @@ export interface MathJsInstance extends MathJsFactory { n?: number | BigNumber ): NoLiteralType round(x: MathNumericType, n: U): U - round(x: U, unit: Unit): U + round>(x: U, unit: Unit): U round(x: Unit, unit: Unit): Unit round(x: Unit, n: number | BigNumber, unit: Unit): Unit - round(x: U, n: number | BigNumber, unit: Unit): U + round>( + x: U, + n: number | BigNumber, + unit: Unit + ): U // End of group of rounding functions @@ -1390,7 +1393,7 @@ export interface MathJsInstance extends MathJsFactory { multiply(x: T, y: T): T multiply(x: Unit, y: Unit): Unit multiply(x: number, y: number): number - multiply(x: MathType, y: MathType): MathType + multiply(x: T, y: U): U | T multiply(...values: T[]): T multiply(...values: MathType[]): MathType @@ -3991,14 +3994,14 @@ export const { varianceTransformDependencies }: Record -export interface Matrix { +export interface Matrix { type: string storage(): string datatype(): string create(data: MathArray, datatype?: string): void density(): number // eslint-disable-next-line @typescript-eslint/no-explicit-any - subset(index: Index, replacement?: any, defaultValue?: any): Matrix + subset(index: Index, replacement?: any, defaultValue?: any): Matrix apply( dim: number, callback: (array: MathCollection) => number @@ -4006,8 +4009,8 @@ export interface Matrix { // eslint-disable-next-line @typescript-eslint/no-explicit-any get(index: number[]): any // eslint-disable-next-line @typescript-eslint/no-explicit-any - set(index: number[], value: any, defaultValue?: number | string): Matrix - resize(size: MathCollection, defaultValue?: number | string): Matrix + set(index: number[], value: any, defaultValue?: number | string): Matrix + resize(size: MathCollection, defaultValue?: number | string): Matrix clone(): Matrix size(): number[] map( @@ -4031,56 +4034,13 @@ export interface Matrix { toJSON(): any // eslint-disable-next-line @typescript-eslint/no-explicit-any diagonal(k?: number | BigNumber): any[] - swapRows(i: number, j: number): Matrix + swapRows(i: number, j: number): Matrix } export interface MatrixCtor { new (): Matrix } -export interface UnitMatrix - extends Omit< - Matrix, - | 'create' - | 'toArray' - | 'valueOf' - | 'subset' - | 'apply' - | 'set' - | 'resize' - | 'clone' - | 'map' - | 'forEach' - | 'toArray' - | 'valueOf' - | 'swapRows' - > { - create(data: UnitArray, datatype?: string): void - // eslint-disable-next-line @typescript-eslint/no-explicit-any - subset(index: Index, replacement?: any, defaultValue?: any): UnitMatrix - apply( - dim: number, - callback: (array: UnitCollection) => number - ): UnitCollection - // eslint-disable-next-line @typescript-eslint/no-explicit-any - set(index: number[], value: any, defaultValue?: number | string): UnitMatrix - resize(size: MathCollection, defaultValue?: number | string): UnitMatrix - clone(): UnitMatrix - map( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - callback: (a: any, b: number[], c: UnitMatrix) => any, - skipZeros?: boolean - ): UnitMatrix - forEach( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - callback: (a: any, b: number[], c: UnitMatrix) => void, - skipZeros?: boolean - ): void - toArray(): UnitArray - valueOf(): UnitArray - swapRows(i: number, j: number): UnitMatrix -} - // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface BigNumber extends Decimal {} @@ -5069,7 +5029,7 @@ export interface MathJsChain { n: U ): MathJsChain ceil(this: MathJsChain, unit: Unit): MathJsChain - ceil( + ceil>( this: MathJsChain, unit: Unit ): MathJsChain @@ -5078,7 +5038,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - ceil( + ceil>( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5098,7 +5058,7 @@ export interface MathJsChain { n: U ): MathJsChain fix(this: MathJsChain, unit: Unit): MathJsChain - fix( + fix>( this: MathJsChain, unit: Unit ): MathJsChain @@ -5107,7 +5067,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - fix( + fix>( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5127,7 +5087,7 @@ export interface MathJsChain { n: U ): MathJsChain floor(this: MathJsChain, unit: Unit): MathJsChain - floor( + floor>( this: MathJsChain, unit: Unit ): MathJsChain @@ -5136,7 +5096,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - floor( + floor>( this: MathJsChain, n: number | BigNumber, unit: Unit @@ -5156,7 +5116,7 @@ export interface MathJsChain { n: U ): MathJsChain round(this: MathJsChain, unit: Unit): MathJsChain - round( + round>( this: MathJsChain, unit: Unit ): MathJsChain @@ -5165,7 +5125,7 @@ export interface MathJsChain { n: number | BigNumber, unit: Unit ): MathJsChain - round( + round>( this: MathJsChain, n: number | BigNumber, unit: Unit From fdeac7630d9fadf0a133cd883acbd7d9cc436339 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sun, 6 Oct 2024 18:36:07 -0700 Subject: [PATCH 13/19] feat: adds type-guards --- src/core/create.js | 6 ++- src/core/function/typed.js | 6 ++- src/entry/typeChecks.js | 4 +- src/utils/is.js | 18 +++++++ test/node-tests/doc.test.js | 2 + test/typescript-tests/testTypes.ts | 82 +++++++++++++++++++++++++----- types/index.d.ts | 25 ++++++--- 7 files changed, 120 insertions(+), 23 deletions(-) diff --git a/src/core/create.js b/src/core/create.js index bb3a7bbe04..d6a2bacfd8 100644 --- a/src/core/create.js +++ b/src/core/create.js @@ -46,7 +46,9 @@ import { isString, isSymbolNode, isUndefined, - isUnit + isUnit, + isUnitArray, + isUnitMatrix } from '../utils/is.js' import { deepFlatten, isLegacyFactory } from '../utils/object.js' import * as emitter from './../utils/emitter.js' @@ -116,7 +118,9 @@ export function create (factories, config) { isUnit, isString, isArray, + isUnitArray, isMatrix, + isUnitMatrix, isCollection, isDenseMatrix, isSparseMatrix, diff --git a/src/core/function/typed.js b/src/core/function/typed.js index 966a1a4f78..e25e928247 100644 --- a/src/core/function/typed.js +++ b/src/core/function/typed.js @@ -79,7 +79,9 @@ import { isString, isSymbolNode, isUndefined, - isUnit + isUnit, + isUnitArray, + isUnitMatrix } from '../../utils/is.js' import { digits } from '../../utils/number.js' @@ -132,7 +134,9 @@ export const createTyped = /* #__PURE__ */ factory('typed', dependencies, functi { name: 'string', test: isString }, { name: 'Chain', test: isChain }, { name: 'Array', test: isArray }, + { name: 'isUnitArray', test: isUnitArray }, { name: 'Matrix', test: isMatrix }, + { name: 'isUnitMatrix', test: isUnitMatrix }, { name: 'DenseMatrix', test: isDenseMatrix }, { name: 'SparseMatrix', test: isSparseMatrix }, { name: 'Range', test: isRange }, diff --git a/src/entry/typeChecks.js b/src/entry/typeChecks.js index caa9629f7e..a5acb20a9a 100644 --- a/src/entry/typeChecks.js +++ b/src/entry/typeChecks.js @@ -42,5 +42,7 @@ export { isResultSet, isSparseMatrix, isSymbolNode, - isUnit + isUnit, + isUnitArray, + isUnitMatrix } from '../utils/is.js' diff --git a/src/utils/is.js b/src/utils/is.js index 5bc6db84ce..125789f638 100644 --- a/src/utils/is.js +++ b/src/utils/is.js @@ -66,10 +66,28 @@ export function isString (x) { export const isArray = Array.isArray +/** + * Test whether a value is a Array or Array of Arrays containing only units + * @param {*} x + * @returns {boolean} isUnitArray + */ +export function isUnitArray (x) { + return (isArray(x) && x.every((v) => (isUnit(v) || (isArray(v) && isUnitArray(v))))) || false +} + export function isMatrix (x) { return (x && x.constructor.prototype.isMatrix === true) || false } +/** + * Test whether a value is a Matrix containing only units + * @param {*} x + * @returns {boolean} isUnitMatrix + */ +export function isUnitMatrix (x) { + return (isMatrix(x) && isUnitArray(x.toArray())) || false +} + /** * Test whether a value is a collection: an Array or Matrix * @param {*} x diff --git a/test/node-tests/doc.test.js b/test/node-tests/doc.test.js index dd73eb8534..933c50fab1 100644 --- a/test/node-tests/doc.test.js +++ b/test/node-tests/doc.test.js @@ -168,7 +168,9 @@ const knownUndocumented = new Set([ 'isUnit', 'isString', 'isArray', + 'isUnitArray', 'isMatrix', + 'isUnitMatrix', 'isCollection', 'isDenseMatrix', 'isSparseMatrix', diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index dac18579b2..f819b0fb73 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -46,7 +46,9 @@ import { UnitPrefix, Node, isSymbolNode, - MathScalarType + MathScalarType, + isUnitArray, + isUnitMatrix } from 'mathjs' import * as assert from 'assert' import { expectTypeOf } from 'expect-type' @@ -1793,9 +1795,17 @@ Function ceil examples const u1 = math.unit(3.2, 'cm') const u2 = math.unit('cm') const u3 = math.unit(5.51, 'cm') + + // unit array input const unitArray = [u1, u3] + const array = [u1, u3, 1] + array.pop() + const array2 = [ + [u1, u3], + [1, 5] + ] + array2.pop() - const unitMatrix = math.matrix(unitArray) assert.deepStrictEqual(math.ceil(u1, u2), math.unit(4, 'cm')) assert.deepStrictEqual(math.ceil(u1, 1, u2), math.unit(3.2, 'cm')) assert.deepStrictEqual(math.ceil(unitArray, 1, math.unit('cm')), [ @@ -1803,18 +1813,59 @@ Function ceil examples math.unit(5.6, 'cm') ]) + // Can assert that the array is a Unit[] + assert.deepStrictEqual(math.ceil(array as Unit[], 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.6, 'cm') + ]) + + // Can assert that the array is a Unit[][] + assert.deepStrictEqual(math.ceil(array2 as Unit[][], 1, math.unit('cm')), [ + [math.unit(3.2, 'cm'), math.unit(5.6, 'cm')] + ]) + + // Can use a type guard to assert that the array is a Unit[] + if (isUnitArray(array)) { + assert.deepStrictEqual(math.ceil(array, 1, math.unit('cm')), [ + math.unit(3.2, 'cm'), + math.unit(5.6, 'cm') + ]) + } + // Can use a type guard to assert that the array is a Unit[][] + if (isUnitArray(array2)) { + assert.deepStrictEqual(math.ceil(array2, 1, math.unit('cm')), [ + [math.unit(3.2, 'cm'), math.unit(5.6, 'cm')] + ]) + } + + // unit matrix input + const unitMatrix = math.matrix(unitArray) + const matrix = math.matrix([u1, u3]) + let matrix2 = math.matrix([ + [u1, u3], + [1, 5] + ]) + + matrix2 = matrix2.subset(math.index([0], [0, 1])) + assert.deepStrictEqual( math.ceil(unitMatrix, 1, math.unit('cm')), math.matrix([math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]) ) - const array = [u1, u3, 1] - array.pop() + // Can assert that the matrix is a Matrix + assert.deepStrictEqual( + math.ceil(matrix as Matrix, 1, math.unit('cm')), + math.matrix([math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]) + ) - assert.deepStrictEqual(math.ceil(array as Unit[], 1, math.unit('cm')), [ - math.unit(3.2, 'cm'), - math.unit(5.6, 'cm') - ]) + // Can use a type guard to assert that the matrix is a Matrix + if (isUnitMatrix(matrix2)) { + assert.deepStrictEqual( + math.ceil(matrix2, 1, math.unit('cm')), + math.matrix([[math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]]) + ) + } // array input assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4]) @@ -1897,7 +1948,7 @@ Function fix examples const u2 = math.unit('cm') const u3 = math.unit(5.51, 'cm') const unitArray = [u1, u3] - const unitMatrix = math.matrix(unitArray) + const unitMatrix = math.matrix(unitArray) assert.deepStrictEqual(math.fix(u1, u2), math.unit(3, 'cm')) assert.deepStrictEqual(math.fix(u1, 1, u2), math.unit(3.2, 'cm')) assert.deepStrictEqual(math.fix(unitArray, 1, math.unit('cm')), [ @@ -1990,7 +2041,7 @@ Function floor examples const u2 = math.unit('cm') const u3 = math.unit(5.51, 'cm') const unitArray = [u1, u3] - const unitMatrix = math.matrix(unitArray) + const unitMatrix = math.matrix(unitArray) assert.deepStrictEqual(math.floor(u1, u2), math.unit(3, 'cm')) assert.deepStrictEqual(math.floor(u1, 1, u2), math.unit(3.2, 'cm')) assert.deepStrictEqual(math.floor(unitArray, 1, math.unit('cm')), [ @@ -2083,7 +2134,7 @@ Function round examples const u2 = math.unit('cm') const u3 = math.unit(5.51, 'cm') const unitArray = [u1, u3] - const unitMatrix = math.matrix(unitArray) + const unitMatrix = math.matrix(unitArray) assert.deepStrictEqual(math.round(u1, u2), math.unit(3, 'cm')) assert.deepStrictEqual(math.round(u1, 1, u2), math.unit(3.2, 'cm')) assert.deepStrictEqual( @@ -2388,7 +2439,9 @@ Factory Test math.isUnit, math.isString, math.isArray, + math.isUnitArray, math.isMatrix, + math.isUnitMatrix, math.isCollection, math.isDenseMatrix, math.isSparseMatrix, @@ -2456,11 +2509,14 @@ Factory Test if (math.isArray(x)) { expectTypeOf(x).toMatchTypeOf() } + if (math.isUnitArray(x)) { + expectTypeOf(x).toMatchTypeOf() + } if (math.isMatrix(x)) { expectTypeOf(x).toMatchTypeOf() } - if (math.isDenseMatrix(x)) { - expectTypeOf(x).toMatchTypeOf() + if (math.isUnitMatrix(x)) { + expectTypeOf(x).toMatchTypeOf>() } if (math.isSparseMatrix(x)) { expectTypeOf(x).toMatchTypeOf() diff --git a/types/index.d.ts b/types/index.d.ts index 4a2d87aff6..1e6c133c6f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -752,6 +752,11 @@ export interface MathJsInstance extends MathJsFactory { format?: 'sparse' | 'dense', dataType?: string ): Matrix + matrix( + data: MathCollection, + format?: 'sparse' | 'dense', + dataType?: string + ): Matrix /** * Create a number or convert a string, boolean, or unit to a number. @@ -3433,8 +3438,12 @@ export interface MathJsInstance extends MathJsFactory { isArray: ArrayConstructor['isArray'] + isUnitArray(x: unknown): x is MathArray + isMatrix(x: unknown): x is Matrix + isUnitMatrix(x: unknown): x is Matrix + // eslint-disable-next-line @typescript-eslint/no-explicit-any isCollection(x: unknown): x is Matrix | any[] @@ -3994,14 +4003,14 @@ export const { varianceTransformDependencies }: Record -export interface Matrix { +export interface Matrix { type: string storage(): string datatype(): string create(data: MathArray, datatype?: string): void density(): number // eslint-disable-next-line @typescript-eslint/no-explicit-any - subset(index: Index, replacement?: any, defaultValue?: any): Matrix + subset(index: Index, replacement?: any, defaultValue?: any): Matrix apply( dim: number, callback: (array: MathCollection) => number @@ -4009,9 +4018,9 @@ export interface Matrix { // eslint-disable-next-line @typescript-eslint/no-explicit-any get(index: number[]): any // eslint-disable-next-line @typescript-eslint/no-explicit-any - set(index: number[], value: any, defaultValue?: number | string): Matrix - resize(size: MathCollection, defaultValue?: number | string): Matrix - clone(): Matrix + set(index: number[], value: any, defaultValue?: number | string): Matrix + resize(size: MathCollection, defaultValue?: number | string): Matrix + clone(): Matrix size(): number[] map( // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -4023,8 +4032,8 @@ export interface Matrix { callback: (a: any, b: number[], c: Matrix) => void, skipZeros?: boolean ): void - toArray(): MathArray - valueOf(): MathArray + toArray(): MathArray + valueOf(): MathArray format( // eslint-disable-next-line @typescript-eslint/no-explicit-any options?: FormatOptions | number | BigNumber | ((value: any) => string) @@ -7301,7 +7310,9 @@ export const { isUnit, isString, isArray, + isUnitArray, isMatrix, + isUnitMatrix, isCollection, isDenseMatrix, isSparseMatrix, From 5f5d3bd1484ec2922a9361f371af8e95df9632f9 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Sun, 6 Oct 2024 19:16:16 -0700 Subject: [PATCH 14/19] chore: small type fixes --- test/typescript-tests/testTypes.ts | 4 ++++ types/index.d.ts | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index f819b0fb73..147378f36e 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1464,6 +1464,9 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices [5, 6, 7, 8] ] + const cde: MathArray = [1] + const def: MathArray = [2] + const Mbcd = math.matrix(bcd) const Mabc = math.matrix(abc) @@ -1476,6 +1479,7 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices const _r2 = math.multiply(a, b) // 1D JS Array + const _r12 = math.multiply(cde, def) // equal 2 const r3 = math.multiply(abc, bcd) const _r31 = r3[1] // By default least promised valid syntax diff --git a/types/index.d.ts b/types/index.d.ts index 1e6c133c6f..ea394df137 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1393,12 +1393,12 @@ export interface MathJsInstance extends MathJsFactory { multiply(x: T, y: MathType): Matrix multiply(x: MathType, y: T): Matrix - multiply(x: T, y: T[]): T - multiply(x: T[], y: T): T - multiply(x: T, y: T): T + multiply(x: T, y: T[]): T + multiply(x: T[], y: T): T + multiply(x: T[], y: T[]): T[] multiply(x: Unit, y: Unit): Unit multiply(x: number, y: number): number - multiply(x: T, y: U): U | T + multiply(x: MathType, y: MathType): MathType multiply(...values: T[]): T multiply(...values: MathType[]): MathType From fd2a75701b7a5da9228e70790a69f06da4975b18 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 11 Oct 2024 13:56:17 -0700 Subject: [PATCH 15/19] revert: matharray changes --- types/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index ea394df137..8b080a261e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1393,9 +1393,9 @@ export interface MathJsInstance extends MathJsFactory { multiply(x: T, y: MathType): Matrix multiply(x: MathType, y: T): Matrix - multiply(x: T, y: T[]): T - multiply(x: T[], y: T): T - multiply(x: T[], y: T[]): T[] + multiply(x: T, y: T[]): T + multiply(x: T[], y: T): T + multiply>(x: T, y: T): T multiply(x: Unit, y: Unit): Unit multiply(x: number, y: number): number multiply(x: MathType, y: MathType): MathType From ef1a80664bf84779aeaa21798023643c87846e80 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 11 Oct 2024 14:42:13 -0700 Subject: [PATCH 16/19] fix: sets default type to MathScalarType to avoid breaking changes --- types/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 8b080a261e..7190ae9e9d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,8 +12,8 @@ export type NoLiteralType = T extends number export type MathNumericType = number | BigNumber | bigint | Fraction | Complex export type MathScalarType = MathNumericType | Unit -export type MathArray = T[] | T[][] -export type MathCollection = +export type MathArray = T[] | T[][] +export type MathCollection = | MathArray | Matrix export type MathType = MathScalarType | MathCollection @@ -4003,7 +4003,7 @@ export const { varianceTransformDependencies }: Record -export interface Matrix { +export interface Matrix { type: string storage(): string datatype(): string From 6a27790dab6d05a7f21a20a7c49097d975d4c03c Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 11 Oct 2024 19:26:20 -0700 Subject: [PATCH 17/19] chore: avoids repetition of generic type --- types/index.d.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 7190ae9e9d..af0479d20b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,10 +12,9 @@ export type NoLiteralType = T extends number export type MathNumericType = number | BigNumber | bigint | Fraction | Complex export type MathScalarType = MathNumericType | Unit -export type MathArray = T[] | T[][] -export type MathCollection = - | MathArray - | Matrix +export type MathGeneric = T +export type MathArray = T[] | T[][] +export type MathCollection = MathArray | Matrix export type MathType = MathScalarType | MathCollection export type MathExpression = string | string[] | MathCollection @@ -4003,7 +4002,7 @@ export const { varianceTransformDependencies }: Record -export interface Matrix { +export interface Matrix { type: string storage(): string datatype(): string From f7066e23436c2668ba020c8f3bddebdb0bf08633 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 11 Oct 2024 19:35:44 -0700 Subject: [PATCH 18/19] revert: removes type guards --- src/core/create.js | 6 +---- src/core/function/typed.js | 6 +---- src/entry/typeChecks.js | 4 +-- src/utils/is.js | 18 ------------- test/node-tests/doc.test.js | 2 -- test/typescript-tests/testTypes.ts | 42 ++---------------------------- types/index.d.ts | 6 ----- 7 files changed, 5 insertions(+), 79 deletions(-) diff --git a/src/core/create.js b/src/core/create.js index d6a2bacfd8..bb3a7bbe04 100644 --- a/src/core/create.js +++ b/src/core/create.js @@ -46,9 +46,7 @@ import { isString, isSymbolNode, isUndefined, - isUnit, - isUnitArray, - isUnitMatrix + isUnit } from '../utils/is.js' import { deepFlatten, isLegacyFactory } from '../utils/object.js' import * as emitter from './../utils/emitter.js' @@ -118,9 +116,7 @@ export function create (factories, config) { isUnit, isString, isArray, - isUnitArray, isMatrix, - isUnitMatrix, isCollection, isDenseMatrix, isSparseMatrix, diff --git a/src/core/function/typed.js b/src/core/function/typed.js index e25e928247..966a1a4f78 100644 --- a/src/core/function/typed.js +++ b/src/core/function/typed.js @@ -79,9 +79,7 @@ import { isString, isSymbolNode, isUndefined, - isUnit, - isUnitArray, - isUnitMatrix + isUnit } from '../../utils/is.js' import { digits } from '../../utils/number.js' @@ -134,9 +132,7 @@ export const createTyped = /* #__PURE__ */ factory('typed', dependencies, functi { name: 'string', test: isString }, { name: 'Chain', test: isChain }, { name: 'Array', test: isArray }, - { name: 'isUnitArray', test: isUnitArray }, { name: 'Matrix', test: isMatrix }, - { name: 'isUnitMatrix', test: isUnitMatrix }, { name: 'DenseMatrix', test: isDenseMatrix }, { name: 'SparseMatrix', test: isSparseMatrix }, { name: 'Range', test: isRange }, diff --git a/src/entry/typeChecks.js b/src/entry/typeChecks.js index a5acb20a9a..caa9629f7e 100644 --- a/src/entry/typeChecks.js +++ b/src/entry/typeChecks.js @@ -42,7 +42,5 @@ export { isResultSet, isSparseMatrix, isSymbolNode, - isUnit, - isUnitArray, - isUnitMatrix + isUnit } from '../utils/is.js' diff --git a/src/utils/is.js b/src/utils/is.js index 125789f638..5bc6db84ce 100644 --- a/src/utils/is.js +++ b/src/utils/is.js @@ -66,28 +66,10 @@ export function isString (x) { export const isArray = Array.isArray -/** - * Test whether a value is a Array or Array of Arrays containing only units - * @param {*} x - * @returns {boolean} isUnitArray - */ -export function isUnitArray (x) { - return (isArray(x) && x.every((v) => (isUnit(v) || (isArray(v) && isUnitArray(v))))) || false -} - export function isMatrix (x) { return (x && x.constructor.prototype.isMatrix === true) || false } -/** - * Test whether a value is a Matrix containing only units - * @param {*} x - * @returns {boolean} isUnitMatrix - */ -export function isUnitMatrix (x) { - return (isMatrix(x) && isUnitArray(x.toArray())) || false -} - /** * Test whether a value is a collection: an Array or Matrix * @param {*} x diff --git a/test/node-tests/doc.test.js b/test/node-tests/doc.test.js index 933c50fab1..dd73eb8534 100644 --- a/test/node-tests/doc.test.js +++ b/test/node-tests/doc.test.js @@ -168,9 +168,7 @@ const knownUndocumented = new Set([ 'isUnit', 'isString', 'isArray', - 'isUnitArray', 'isMatrix', - 'isUnitMatrix', 'isCollection', 'isDenseMatrix', 'isSparseMatrix', diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 147378f36e..cd98daf427 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -46,9 +46,7 @@ import { UnitPrefix, Node, isSymbolNode, - MathScalarType, - isUnitArray, - isUnitMatrix + MathScalarType } from 'mathjs' import * as assert from 'assert' import { expectTypeOf } from 'expect-type' @@ -1801,7 +1799,7 @@ Function ceil examples const u3 = math.unit(5.51, 'cm') // unit array input - const unitArray = [u1, u3] + const unitArray: MathArray = [u1, u3] const array = [u1, u3, 1] array.pop() const array2 = [ @@ -1828,29 +1826,9 @@ Function ceil examples [math.unit(3.2, 'cm'), math.unit(5.6, 'cm')] ]) - // Can use a type guard to assert that the array is a Unit[] - if (isUnitArray(array)) { - assert.deepStrictEqual(math.ceil(array, 1, math.unit('cm')), [ - math.unit(3.2, 'cm'), - math.unit(5.6, 'cm') - ]) - } - // Can use a type guard to assert that the array is a Unit[][] - if (isUnitArray(array2)) { - assert.deepStrictEqual(math.ceil(array2, 1, math.unit('cm')), [ - [math.unit(3.2, 'cm'), math.unit(5.6, 'cm')] - ]) - } - // unit matrix input const unitMatrix = math.matrix(unitArray) const matrix = math.matrix([u1, u3]) - let matrix2 = math.matrix([ - [u1, u3], - [1, 5] - ]) - - matrix2 = matrix2.subset(math.index([0], [0, 1])) assert.deepStrictEqual( math.ceil(unitMatrix, 1, math.unit('cm')), @@ -1863,14 +1841,6 @@ Function ceil examples math.matrix([math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]) ) - // Can use a type guard to assert that the matrix is a Matrix - if (isUnitMatrix(matrix2)) { - assert.deepStrictEqual( - math.ceil(matrix2, 1, math.unit('cm')), - math.matrix([[math.unit(3.2, 'cm'), math.unit(5.6, 'cm')]]) - ) - } - // array input assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4]) assert.deepStrictEqual(math.ceil([3.21, 3.82, -4.71], 1), [3.3, 3.9, -4.7]) @@ -2443,9 +2413,7 @@ Factory Test math.isUnit, math.isString, math.isArray, - math.isUnitArray, math.isMatrix, - math.isUnitMatrix, math.isCollection, math.isDenseMatrix, math.isSparseMatrix, @@ -2513,15 +2481,9 @@ Factory Test if (math.isArray(x)) { expectTypeOf(x).toMatchTypeOf() } - if (math.isUnitArray(x)) { - expectTypeOf(x).toMatchTypeOf() - } if (math.isMatrix(x)) { expectTypeOf(x).toMatchTypeOf() } - if (math.isUnitMatrix(x)) { - expectTypeOf(x).toMatchTypeOf>() - } if (math.isSparseMatrix(x)) { expectTypeOf(x).toMatchTypeOf() } diff --git a/types/index.d.ts b/types/index.d.ts index af0479d20b..1e1ff5fc24 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -3437,12 +3437,8 @@ export interface MathJsInstance extends MathJsFactory { isArray: ArrayConstructor['isArray'] - isUnitArray(x: unknown): x is MathArray - isMatrix(x: unknown): x is Matrix - isUnitMatrix(x: unknown): x is Matrix - // eslint-disable-next-line @typescript-eslint/no-explicit-any isCollection(x: unknown): x is Matrix | any[] @@ -7309,9 +7305,7 @@ export const { isUnit, isString, isArray, - isUnitArray, isMatrix, - isUnitMatrix, isCollection, isDenseMatrix, isSparseMatrix, From ab9287a7470abbf684d1a77d5e3f891816dae007 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah Date: Fri, 18 Oct 2024 09:47:00 -0700 Subject: [PATCH 19/19] fix: reverts accidental removal of isDenseMatrix test --- test/typescript-tests/testTypes.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index cd98daf427..acce2c029e 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1,3 +1,5 @@ +import * as assert from 'assert' +import { expectTypeOf } from 'expect-type' import { AccessorNode, addDependencies, @@ -21,15 +23,19 @@ import { Help, Index, IndexNode, + isSymbolNode, LUDecomposition, MathArray, MathCollection, MathJsChain, MathJsFunctionName, MathNode, + MathNodeCommon, MathNumericType, + MathScalarType, MathType, Matrix, + Node, ObjectNode, OperatorNode, OperatorNodeFn, @@ -41,15 +47,9 @@ import { SimplifyRule, SLUDecomposition, SymbolNode, - MathNodeCommon, Unit, - UnitPrefix, - Node, - isSymbolNode, - MathScalarType + UnitPrefix } from 'mathjs' -import * as assert from 'assert' -import { expectTypeOf } from 'expect-type' // This file serves a dual purpose: // 1) examples of how to use math.js in TypeScript @@ -2484,6 +2484,9 @@ Factory Test if (math.isMatrix(x)) { expectTypeOf(x).toMatchTypeOf() } + if (math.isDenseMatrix(x)) { + expectTypeOf(x).toMatchTypeOf() + } if (math.isSparseMatrix(x)) { expectTypeOf(x).toMatchTypeOf() }