From 90253dfebc55016912fb43a8598f77492cee9cf3 Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Tue, 28 May 2024 11:54:38 -0400 Subject: [PATCH 1/3] add more fns --- src/__swaps__/safe-math/SafeMath.ts | 95 +++++++++++++++++++ .../safe-math/__tests__/SafeMath.test.ts | 34 +++++++ 2 files changed, 129 insertions(+) diff --git a/src/__swaps__/safe-math/SafeMath.ts b/src/__swaps__/safe-math/SafeMath.ts index c98ea7536fa..66ebbac8e04 100644 --- a/src/__swaps__/safe-math/SafeMath.ts +++ b/src/__swaps__/safe-math/SafeMath.ts @@ -155,6 +155,25 @@ export function powWorklet(base: string, exponent: string): string { return formatResultWorklet(result); } +// Logarithm base 10 function +export function log10Worklet(num: string | number): string { + 'worklet'; + const numStr = typeof num === 'number' ? num.toString() : num; + + if (!isNumberStringWorklet(numStr)) { + throw new Error('Arguments must be a numeric string or number'); + } + if (isZeroWorklet(numStr)) { + throw new Error('Argument must be greater than 0'); + } + + const [bigIntNum, decimalPlaces] = removeDecimalWorklet(numStr); + const scaledBigIntNum = scaleUpWorklet(bigIntNum, decimalPlaces); + const result = Math.log10(Number(scaledBigIntNum)) - 20; // Adjust the scale factor for log10 + const resultBigInt = BigInt(result * 10 ** 20); + return formatResultWorklet(resultBigInt); +} + // Equality function export function equalWorklet(num1: string, num2: string): boolean { 'worklet'; @@ -219,3 +238,79 @@ export function lessThanOrEqualToWorklet(num1: string, num2: string): boolean { const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 <= scaledBigInt2; } + +// toFixed function +export function toFixedWorklet(num: string | number, decimalPlaces: number): string { + 'worklet'; + const numStr = typeof num === 'number' ? num.toString() : num; + + if (!isNumberStringWorklet(numStr)) { + throw new Error('Argument must be a numeric string or number'); + } + + const [bigIntNum, numDecimalPlaces] = removeDecimalWorklet(numStr); + const scaledBigIntNum = scaleUpWorklet(bigIntNum, numDecimalPlaces); + + const scaleFactor = BigInt(10) ** BigInt(20 - decimalPlaces); + const roundedBigInt = ((scaledBigIntNum + scaleFactor / BigInt(2)) / scaleFactor) * scaleFactor; + + const resultStr = roundedBigInt.toString().padStart(20 + 1, '0'); // SCALE_FACTOR decimal places + at least 1 integer place + const integerPart = resultStr.slice(0, -20) || '0'; + const fractionalPart = resultStr.slice(-20, -20 + decimalPlaces).padEnd(decimalPlaces, '0'); + + return `${integerPart}.${fractionalPart}`; +} + +// Ceil function +export function ceilWorklet(num: string | number): string { + 'worklet'; + const numStr = typeof num === 'number' ? num.toString() : num; + + if (!isNumberStringWorklet(numStr)) { + throw new Error('Argument must be a numeric string or number'); + } + + const [bigIntNum, decimalPlaces] = removeDecimalWorklet(numStr); + const scaledBigIntNum = scaleUpWorklet(bigIntNum, decimalPlaces); + + const scaleFactor = BigInt(10) ** BigInt(20); + const ceilBigInt = ((scaledBigIntNum + scaleFactor - BigInt(1)) / scaleFactor) * scaleFactor; + + return formatResultWorklet(ceilBigInt); +} + +// Floor function +export function floorWorklet(num: string | number): string { + 'worklet'; + const numStr = typeof num === 'number' ? num.toString() : num; + + if (!isNumberStringWorklet(numStr)) { + throw new Error('Argument must be a numeric string or number'); + } + + const [bigIntNum, decimalPlaces] = removeDecimalWorklet(numStr); + const scaledBigIntNum = scaleUpWorklet(bigIntNum, decimalPlaces); + + const scaleFactor = BigInt(10) ** BigInt(20); + const floorBigInt = (scaledBigIntNum / scaleFactor) * scaleFactor; + + return formatResultWorklet(floorBigInt); +} + +// Round function +export function roundWorklet(num: string | number): string { + 'worklet'; + const numStr = typeof num === 'number' ? num.toString() : num; + + if (!isNumberStringWorklet(numStr)) { + throw new Error('Argument must be a numeric string or number'); + } + + const [bigIntNum, decimalPlaces] = removeDecimalWorklet(numStr); + const scaledBigIntNum = scaleUpWorklet(bigIntNum, decimalPlaces); + + const scaleFactor = BigInt(10) ** BigInt(20); + const roundBigInt = ((scaledBigIntNum + scaleFactor / BigInt(2)) / scaleFactor) * scaleFactor; + + return formatResultWorklet(roundBigInt); +} diff --git a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts index a80196afeec..ddb291948c8 100644 --- a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts +++ b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts @@ -1,16 +1,21 @@ import BigNumber from 'bignumber.js'; import { + ceilWorklet, divWorklet, equalWorklet, + floorWorklet, greaterThanOrEqualToWorklet, greaterThanWorklet, lessThanOrEqualToWorklet, lessThanWorklet, + log10Worklet, modWorklet, mulWorklet, powWorklet, + roundWorklet, subWorklet, sumWorklet, + toFixedWorklet, } from '../SafeMath'; const RESULTS = { @@ -20,11 +25,16 @@ const RESULTS = { div: '325.56878986395199044836', mod: '2172.345', pow: '1546106588588.369025', + log10: '0.30102999566398124032', + toFixed: '1243425.35', + ceil: '1243426', + floor: '1243425', }; const VALUE_A = '1243425.345'; const VALUE_B = '3819.24'; const VALUE_C = '2'; +const VALUE_D = '1243425.745'; const NEGATIVE_VALUE = '-2412.12'; const ZERO = '0'; const ONE = '1'; @@ -86,6 +96,13 @@ describe('SafeMath', () => { expect(powWorklet(VALUE_A, VALUE_C)).toBe(RESULTS.pow); }); + test('log10Worklet', () => { + expect(() => log10Worklet(NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => log10Worklet(ZERO)).toThrow('Argument must be greater than 0'); + expect(log10Worklet(VALUE_C)).toBe(RESULTS.log10); + expect(log10Worklet(Number(VALUE_C))).toBe(RESULTS.log10); + }); + test('equalWorklet', () => { expect(() => equalWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); expect(() => equalWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); @@ -130,6 +147,23 @@ describe('SafeMath', () => { expect(lessThanOrEqualToWorklet(VALUE_A, VALUE_A)).toBe(true); expect(lessThanOrEqualToWorklet(NEGATIVE_VALUE, VALUE_A)).toBe(true); }); + + test('toFixedWorklet', () => { + expect(toFixedWorklet(VALUE_A, 2)).toBe(RESULTS.toFixed); + }); + + test('ceilWorklet', () => { + expect(ceilWorklet(VALUE_A)).toBe(RESULTS.ceil); + }); + + test('floorWorklet', () => { + expect(floorWorklet(VALUE_A)).toBe(RESULTS.floor); + }); + + test('roundWorklet', () => { + expect(roundWorklet(VALUE_A)).toBe(RESULTS.floor); + expect(roundWorklet(VALUE_D)).toBe(RESULTS.ceil); + }); }); describe('BigNumber', () => { From cdc3da188af0ae4a4073099daf57e92f9c0d9664 Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Tue, 28 May 2024 14:31:03 -0400 Subject: [PATCH 2/3] accept string or number --- src/__swaps__/safe-math/SafeMath.ts | 163 +++++++++++------- .../safe-math/__tests__/SafeMath.test.ts | 28 ++- 2 files changed, 128 insertions(+), 63 deletions(-) diff --git a/src/__swaps__/safe-math/SafeMath.ts b/src/__swaps__/safe-math/SafeMath.ts index 66ebbac8e04..e6a2c686bba 100644 --- a/src/__swaps__/safe-math/SafeMath.ts +++ b/src/__swaps__/safe-math/SafeMath.ts @@ -40,21 +40,30 @@ const formatResultWorklet = (result: bigint): string => { return isNegative ? `-${formattedResult}` : formattedResult; }; +// Helper function to handle string and number input types +const toStringWorklet = (value: string | number): string => { + 'worklet'; + return typeof value === 'number' ? value.toString() : value; +}; + // Sum function -export function sumWorklet(num1: string, num2: string): string { +export function sumWorklet(num1: string | number, num2: string | number): string { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(num1)) { - return num2; + if (isZeroWorklet(num1Str)) { + return num2Str; } - if (isZeroWorklet(num2)) { - return num1; + if (isZeroWorklet(num2Str)) { + return num1Str; } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); const result = scaledBigInt1 + scaledBigInt2; @@ -62,18 +71,21 @@ export function sumWorklet(num1: string, num2: string): string { } // Subtract function -export function subWorklet(num1: string, num2: string): string { +export function subWorklet(num1: string | number, num2: string | number): string { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(num2)) { - return num1; + if (isZeroWorklet(num2Str)) { + return num1Str; } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); const result = scaledBigInt1 - scaledBigInt2; @@ -81,16 +93,19 @@ export function subWorklet(num1: string, num2: string): string { } // Multiply function -export function mulWorklet(num1: string, num2: string): string { +export function mulWorklet(num1: string | number, num2: string | number): string { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(num1) || isZeroWorklet(num2)) { + if (isZeroWorklet(num1Str) || isZeroWorklet(num2Str)) { return '0'; } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); const result = (scaledBigInt1 * scaledBigInt2) / BigInt(10) ** BigInt(20); @@ -98,19 +113,22 @@ export function mulWorklet(num1: string, num2: string): string { } // Divide function -export function divWorklet(num1: string, num2: string): string { +export function divWorklet(num1: string | number, num2: string | number): string { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(num2)) { + if (isZeroWorklet(num2Str)) { throw new Error('Division by zero'); } - if (isZeroWorklet(num1)) { + if (isZeroWorklet(num1Str)) { return '0'; } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); const result = (scaledBigInt1 * BigInt(10) ** BigInt(20)) / scaledBigInt2; @@ -118,19 +136,22 @@ export function divWorklet(num1: string, num2: string): string { } // Modulus function -export function modWorklet(num1: string, num2: string): string { +export function modWorklet(num1: string | number, num2: string | number): string { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(num2)) { + if (isZeroWorklet(num2Str)) { throw new Error('Division by zero'); } - if (isZeroWorklet(num1)) { + if (isZeroWorklet(num1Str)) { return '0'; } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); const result = scaledBigInt1 % scaledBigInt2; @@ -138,27 +159,30 @@ export function modWorklet(num1: string, num2: string): string { } // Power function -export function powWorklet(base: string, exponent: string): string { +export function powWorklet(base: string | number, exponent: string | number): string { 'worklet'; - if (!isNumberStringWorklet(base) || !isNumberStringWorklet(exponent)) { + const baseStr = toStringWorklet(base); + const exponentStr = toStringWorklet(exponent); + + if (!isNumberStringWorklet(baseStr) || !isNumberStringWorklet(exponentStr)) { throw new Error('Arguments must be a numeric string'); } - if (isZeroWorklet(base)) { + if (isZeroWorklet(baseStr)) { return '0'; } - if (isZeroWorklet(exponent)) { + if (isZeroWorklet(exponentStr)) { return '1'; } - const [bigIntBase, decimalPlaces] = removeDecimalWorklet(base); + const [bigIntBase, decimalPlaces] = removeDecimalWorklet(baseStr); const scaledBigIntBase = scaleUpWorklet(bigIntBase, decimalPlaces); - const result = scaledBigIntBase ** BigInt(exponent) / BigInt(10) ** BigInt(20); + const result = scaledBigIntBase ** BigInt(exponentStr) / BigInt(10) ** BigInt(20); return formatResultWorklet(result); } // Logarithm base 10 function export function log10Worklet(num: string | number): string { 'worklet'; - const numStr = typeof num === 'number' ? num.toString() : num; + const numStr = toStringWorklet(num); if (!isNumberStringWorklet(numStr)) { throw new Error('Arguments must be a numeric string or number'); @@ -175,65 +199,80 @@ export function log10Worklet(num: string | number): string { } // Equality function -export function equalWorklet(num1: string, num2: string): boolean { +export function equalWorklet(num1: string | number, num2: string | number): boolean { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 === scaledBigInt2; } // Greater than function -export function greaterThanWorklet(num1: string, num2: string): boolean { +export function greaterThanWorklet(num1: string | number, num2: string | number): boolean { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 > scaledBigInt2; } // Greater than or equal to function -export function greaterThanOrEqualToWorklet(num1: string, num2: string): boolean { +export function greaterThanOrEqualToWorklet(num1: string | number, num2: string | number): boolean { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 >= scaledBigInt2; } // Less than function -export function lessThanWorklet(num1: string, num2: string): boolean { +export function lessThanWorklet(num1: string | number, num2: string | number): boolean { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 < scaledBigInt2; } // Less than or equal to function -export function lessThanOrEqualToWorklet(num1: string, num2: string): boolean { +export function lessThanOrEqualToWorklet(num1: string | number, num2: string | number): boolean { 'worklet'; - if (!isNumberStringWorklet(num1) || !isNumberStringWorklet(num2)) { + const num1Str = toStringWorklet(num1); + const num2Str = toStringWorklet(num2); + + if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { throw new Error('Arguments must be a numeric string'); } - const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1); - const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2); + const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); + const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); const scaledBigInt1 = scaleUpWorklet(bigInt1, decimalPlaces1); const scaledBigInt2 = scaleUpWorklet(bigInt2, decimalPlaces2); return scaledBigInt1 <= scaledBigInt2; @@ -242,7 +281,7 @@ export function lessThanOrEqualToWorklet(num1: string, num2: string): boolean { // toFixed function export function toFixedWorklet(num: string | number, decimalPlaces: number): string { 'worklet'; - const numStr = typeof num === 'number' ? num.toString() : num; + const numStr = toStringWorklet(num); if (!isNumberStringWorklet(numStr)) { throw new Error('Argument must be a numeric string or number'); @@ -264,7 +303,7 @@ export function toFixedWorklet(num: string | number, decimalPlaces: number): str // Ceil function export function ceilWorklet(num: string | number): string { 'worklet'; - const numStr = typeof num === 'number' ? num.toString() : num; + const numStr = toStringWorklet(num); if (!isNumberStringWorklet(numStr)) { throw new Error('Argument must be a numeric string or number'); @@ -282,7 +321,7 @@ export function ceilWorklet(num: string | number): string { // Floor function export function floorWorklet(num: string | number): string { 'worklet'; - const numStr = typeof num === 'number' ? num.toString() : num; + const numStr = toStringWorklet(num); if (!isNumberStringWorklet(numStr)) { throw new Error('Argument must be a numeric string or number'); @@ -300,7 +339,7 @@ export function floorWorklet(num: string | number): string { // Round function export function roundWorklet(num: string | number): string { 'worklet'; - const numStr = typeof num === 'number' ? num.toString() : num; + const numStr = toStringWorklet(num); if (!isNumberStringWorklet(numStr)) { throw new Error('Argument must be a numeric string or number'); diff --git a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts index ddb291948c8..fe279cccf42 100644 --- a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts +++ b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts @@ -48,6 +48,8 @@ describe('SafeMath', () => { expect(sumWorklet(VALUE_A, ZERO)).toBe(VALUE_A); expect(sumWorklet(ZERO, VALUE_B)).toBe(VALUE_B); expect(sumWorklet(VALUE_A, VALUE_B)).toBe(RESULTS.sum); + expect(sumWorklet(Number(VALUE_A), VALUE_B)).toBe(RESULTS.sum); + expect(sumWorklet(VALUE_A, Number(VALUE_B))).toBe(RESULTS.sum); }); test('subWorklet', () => { @@ -58,6 +60,8 @@ describe('SafeMath', () => { expect(subWorklet(ZERO, VALUE_B)).toBe(`-${VALUE_B}`); expect(subWorklet(NEGATIVE_VALUE, ZERO)).toBe(NEGATIVE_VALUE); expect(subWorklet(VALUE_A, VALUE_B)).toBe(RESULTS.sub); + expect(subWorklet(Number(VALUE_A), VALUE_B)).toBe(RESULTS.sub); + expect(subWorklet(VALUE_A, Number(VALUE_B))).toBe(RESULTS.sub); }); test('mulWorklet', () => { @@ -67,6 +71,8 @@ describe('SafeMath', () => { expect(mulWorklet(VALUE_A, ZERO)).toBe(ZERO); expect(mulWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(mulWorklet(VALUE_A, VALUE_B)).toBe(RESULTS.mul); + expect(mulWorklet(Number(VALUE_A), VALUE_B)).toBe(RESULTS.mul); + expect(mulWorklet(VALUE_A, Number(VALUE_B))).toBe(RESULTS.mul); }); test('divWorklet', () => { @@ -76,6 +82,8 @@ describe('SafeMath', () => { expect(() => divWorklet(VALUE_A, ZERO)).toThrow('Division by zero'); expect(divWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(divWorklet(VALUE_A, VALUE_B)).toBe(RESULTS.div); + expect(divWorklet(Number(VALUE_A), VALUE_B)).toBe(RESULTS.div); + expect(divWorklet(VALUE_A, Number(VALUE_B))).toBe(RESULTS.div); }); test('modWorklet', () => { @@ -85,6 +93,8 @@ describe('SafeMath', () => { expect(() => modWorklet(VALUE_A, ZERO)).toThrow('Division by zero'); expect(modWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(modWorklet(VALUE_A, VALUE_B)).toBe(RESULTS.mod); + expect(modWorklet(Number(VALUE_A), VALUE_B)).toBe(RESULTS.mod); + expect(modWorklet(VALUE_A, Number(VALUE_B))).toBe(RESULTS.mod); }); test('powWorklet', () => { @@ -92,8 +102,9 @@ describe('SafeMath', () => { expect(() => powWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); expect(powWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(powWorklet(VALUE_A, ZERO)).toBe(ONE); - expect(powWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(powWorklet(VALUE_A, VALUE_C)).toBe(RESULTS.pow); + expect(powWorklet(Number(VALUE_A), VALUE_C)).toBe(RESULTS.pow); + expect(powWorklet(VALUE_A, Number(VALUE_C))).toBe(RESULTS.pow); }); test('log10Worklet', () => { @@ -110,6 +121,8 @@ describe('SafeMath', () => { expect(equalWorklet(VALUE_A, VALUE_A)).toBe(true); expect(equalWorklet(VALUE_A, VALUE_B)).toBe(false); expect(equalWorklet(NEGATIVE_VALUE, NEGATIVE_VALUE)).toBe(true); + expect(equalWorklet(Number(VALUE_A), VALUE_A)).toBe(true); + expect(equalWorklet(VALUE_A, Number(VALUE_A))).toBe(true); }); test('greaterThanWorklet', () => { @@ -119,6 +132,8 @@ describe('SafeMath', () => { expect(greaterThanWorklet(VALUE_B, VALUE_A)).toBe(false); expect(greaterThanWorklet(VALUE_A, VALUE_A)).toBe(false); expect(greaterThanWorklet(NEGATIVE_VALUE, VALUE_A)).toBe(false); + expect(greaterThanWorklet(Number(VALUE_A), VALUE_B)).toBe(true); + expect(greaterThanWorklet(VALUE_A, Number(VALUE_B))).toBe(true); }); test('greaterThanOrEqualToWorklet', () => { @@ -128,6 +143,8 @@ describe('SafeMath', () => { expect(greaterThanOrEqualToWorklet(VALUE_B, VALUE_A)).toBe(false); expect(greaterThanOrEqualToWorklet(VALUE_A, VALUE_A)).toBe(true); expect(greaterThanOrEqualToWorklet(NEGATIVE_VALUE, VALUE_A)).toBe(false); + expect(greaterThanOrEqualToWorklet(Number(VALUE_A), VALUE_B)).toBe(true); + expect(greaterThanOrEqualToWorklet(VALUE_A, Number(VALUE_B))).toBe(true); }); test('lessThanWorklet', () => { @@ -137,6 +154,8 @@ describe('SafeMath', () => { expect(lessThanWorklet(VALUE_B, VALUE_A)).toBe(true); expect(lessThanWorklet(VALUE_A, VALUE_A)).toBe(false); expect(lessThanWorklet(NEGATIVE_VALUE, VALUE_A)).toBe(true); + expect(lessThanWorklet(Number(VALUE_A), VALUE_B)).toBe(false); + expect(lessThanWorklet(VALUE_A, Number(VALUE_B))).toBe(false); }); test('lessThanOrEqualToWorklet', () => { @@ -146,23 +165,30 @@ describe('SafeMath', () => { expect(lessThanOrEqualToWorklet(VALUE_B, VALUE_A)).toBe(true); expect(lessThanOrEqualToWorklet(VALUE_A, VALUE_A)).toBe(true); expect(lessThanOrEqualToWorklet(NEGATIVE_VALUE, VALUE_A)).toBe(true); + expect(lessThanOrEqualToWorklet(Number(VALUE_A), VALUE_B)).toBe(false); + expect(lessThanOrEqualToWorklet(VALUE_A, Number(VALUE_B))).toBe(false); }); test('toFixedWorklet', () => { expect(toFixedWorklet(VALUE_A, 2)).toBe(RESULTS.toFixed); + expect(toFixedWorklet(Number(VALUE_A), 2)).toBe(RESULTS.toFixed); }); test('ceilWorklet', () => { expect(ceilWorklet(VALUE_A)).toBe(RESULTS.ceil); + expect(ceilWorklet(Number(VALUE_A))).toBe(RESULTS.ceil); }); test('floorWorklet', () => { expect(floorWorklet(VALUE_A)).toBe(RESULTS.floor); + expect(floorWorklet(Number(VALUE_A))).toBe(RESULTS.floor); }); test('roundWorklet', () => { expect(roundWorklet(VALUE_A)).toBe(RESULTS.floor); expect(roundWorklet(VALUE_D)).toBe(RESULTS.ceil); + expect(roundWorklet(Number(VALUE_A))).toBe(RESULTS.floor); + expect(roundWorklet(Number(VALUE_D))).toBe(RESULTS.ceil); }); }); From dd2c89317129a8576519d672051458e50a8f509d Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Tue, 28 May 2024 14:32:23 -0400 Subject: [PATCH 3/3] update errors --- src/__swaps__/safe-math/SafeMath.ts | 22 ++++----- .../safe-math/__tests__/SafeMath.test.ts | 46 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/__swaps__/safe-math/SafeMath.ts b/src/__swaps__/safe-math/SafeMath.ts index e6a2c686bba..4d0eb500112 100644 --- a/src/__swaps__/safe-math/SafeMath.ts +++ b/src/__swaps__/safe-math/SafeMath.ts @@ -53,7 +53,7 @@ export function sumWorklet(num1: string | number, num2: string | number): string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(num1Str)) { return num2Str; @@ -77,7 +77,7 @@ export function subWorklet(num1: string | number, num2: string | number): string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(num2Str)) { @@ -99,7 +99,7 @@ export function mulWorklet(num1: string | number, num2: string | number): string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(num1Str) || isZeroWorklet(num2Str)) { return '0'; @@ -119,7 +119,7 @@ export function divWorklet(num1: string | number, num2: string | number): string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(num2Str)) { throw new Error('Division by zero'); @@ -142,7 +142,7 @@ export function modWorklet(num1: string | number, num2: string | number): string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(num2Str)) { throw new Error('Division by zero'); @@ -165,7 +165,7 @@ export function powWorklet(base: string | number, exponent: string | number): st const exponentStr = toStringWorklet(exponent); if (!isNumberStringWorklet(baseStr) || !isNumberStringWorklet(exponentStr)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } if (isZeroWorklet(baseStr)) { return '0'; @@ -205,7 +205,7 @@ export function equalWorklet(num1: string | number, num2: string | number): bool const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); @@ -221,7 +221,7 @@ export function greaterThanWorklet(num1: string | number, num2: string | number) const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); @@ -237,7 +237,7 @@ export function greaterThanOrEqualToWorklet(num1: string | number, num2: string const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); @@ -253,7 +253,7 @@ export function lessThanWorklet(num1: string | number, num2: string | number): b const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); @@ -269,7 +269,7 @@ export function lessThanOrEqualToWorklet(num1: string | number, num2: string | n const num2Str = toStringWorklet(num2); if (!isNumberStringWorklet(num1Str) || !isNumberStringWorklet(num2Str)) { - throw new Error('Arguments must be a numeric string'); + throw new Error('Arguments must be a numeric string or number'); } const [bigInt1, decimalPlaces1] = removeDecimalWorklet(num1Str); const [bigInt2, decimalPlaces2] = removeDecimalWorklet(num2Str); diff --git a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts index fe279cccf42..2db42ca5f90 100644 --- a/src/__swaps__/safe-math/__tests__/SafeMath.test.ts +++ b/src/__swaps__/safe-math/__tests__/SafeMath.test.ts @@ -42,8 +42,8 @@ const NON_NUMERIC_STRING = 'abc'; describe('SafeMath', () => { test('sumWorklet', () => { - expect(() => sumWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => sumWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => sumWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => sumWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(sumWorklet(ZERO, ZERO)).toBe(ZERO); expect(sumWorklet(VALUE_A, ZERO)).toBe(VALUE_A); expect(sumWorklet(ZERO, VALUE_B)).toBe(VALUE_B); @@ -53,8 +53,8 @@ describe('SafeMath', () => { }); test('subWorklet', () => { - expect(() => subWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => subWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => subWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => subWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(subWorklet(ZERO, ZERO)).toBe(ZERO); expect(subWorklet(VALUE_A, ZERO)).toBe(VALUE_A); expect(subWorklet(ZERO, VALUE_B)).toBe(`-${VALUE_B}`); @@ -65,8 +65,8 @@ describe('SafeMath', () => { }); test('mulWorklet', () => { - expect(() => mulWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => mulWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => mulWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => mulWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(mulWorklet(ZERO, ZERO)).toBe(ZERO); expect(mulWorklet(VALUE_A, ZERO)).toBe(ZERO); expect(mulWorklet(ZERO, VALUE_B)).toBe(ZERO); @@ -76,8 +76,8 @@ describe('SafeMath', () => { }); test('divWorklet', () => { - expect(() => divWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => divWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => divWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => divWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(() => divWorklet(ZERO, ZERO)).toThrow('Division by zero'); expect(() => divWorklet(VALUE_A, ZERO)).toThrow('Division by zero'); expect(divWorklet(ZERO, VALUE_B)).toBe(ZERO); @@ -87,8 +87,8 @@ describe('SafeMath', () => { }); test('modWorklet', () => { - expect(() => modWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => modWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => modWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => modWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(() => modWorklet(ZERO, ZERO)).toThrow('Division by zero'); expect(() => modWorklet(VALUE_A, ZERO)).toThrow('Division by zero'); expect(modWorklet(ZERO, VALUE_B)).toBe(ZERO); @@ -98,8 +98,8 @@ describe('SafeMath', () => { }); test('powWorklet', () => { - expect(() => powWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => powWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => powWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => powWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(powWorklet(ZERO, VALUE_B)).toBe(ZERO); expect(powWorklet(VALUE_A, ZERO)).toBe(ONE); expect(powWorklet(VALUE_A, VALUE_C)).toBe(RESULTS.pow); @@ -108,15 +108,15 @@ describe('SafeMath', () => { }); test('log10Worklet', () => { - expect(() => log10Worklet(NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => log10Worklet(NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(() => log10Worklet(ZERO)).toThrow('Argument must be greater than 0'); expect(log10Worklet(VALUE_C)).toBe(RESULTS.log10); expect(log10Worklet(Number(VALUE_C))).toBe(RESULTS.log10); }); test('equalWorklet', () => { - expect(() => equalWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => equalWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => equalWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => equalWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(equalWorklet(ZERO, ZERO)).toBe(true); expect(equalWorklet(VALUE_A, VALUE_A)).toBe(true); expect(equalWorklet(VALUE_A, VALUE_B)).toBe(false); @@ -126,8 +126,8 @@ describe('SafeMath', () => { }); test('greaterThanWorklet', () => { - expect(() => greaterThanWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => greaterThanWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => greaterThanWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => greaterThanWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(greaterThanWorklet(VALUE_A, VALUE_B)).toBe(true); expect(greaterThanWorklet(VALUE_B, VALUE_A)).toBe(false); expect(greaterThanWorklet(VALUE_A, VALUE_A)).toBe(false); @@ -137,8 +137,8 @@ describe('SafeMath', () => { }); test('greaterThanOrEqualToWorklet', () => { - expect(() => greaterThanOrEqualToWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => greaterThanOrEqualToWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => greaterThanOrEqualToWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => greaterThanOrEqualToWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(greaterThanOrEqualToWorklet(VALUE_A, VALUE_B)).toBe(true); expect(greaterThanOrEqualToWorklet(VALUE_B, VALUE_A)).toBe(false); expect(greaterThanOrEqualToWorklet(VALUE_A, VALUE_A)).toBe(true); @@ -148,8 +148,8 @@ describe('SafeMath', () => { }); test('lessThanWorklet', () => { - expect(() => lessThanWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => lessThanWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => lessThanWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => lessThanWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(lessThanWorklet(VALUE_A, VALUE_B)).toBe(false); expect(lessThanWorklet(VALUE_B, VALUE_A)).toBe(true); expect(lessThanWorklet(VALUE_A, VALUE_A)).toBe(false); @@ -159,8 +159,8 @@ describe('SafeMath', () => { }); test('lessThanOrEqualToWorklet', () => { - expect(() => lessThanOrEqualToWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string'); - expect(() => lessThanOrEqualToWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string'); + expect(() => lessThanOrEqualToWorklet(NON_NUMERIC_STRING, VALUE_B)).toThrow('Arguments must be a numeric string or number'); + expect(() => lessThanOrEqualToWorklet(VALUE_A, NON_NUMERIC_STRING)).toThrow('Arguments must be a numeric string or number'); expect(lessThanOrEqualToWorklet(VALUE_A, VALUE_B)).toBe(false); expect(lessThanOrEqualToWorklet(VALUE_B, VALUE_A)).toBe(true); expect(lessThanOrEqualToWorklet(VALUE_A, VALUE_A)).toBe(true);