From f7f659ddc3fd22bd446878af6fd0cdb7ee221165 Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Fri, 13 Sep 2024 13:56:37 +1000 Subject: [PATCH] Remove decimal.js-light dependency (#1309) --- .changeset/fast-eagles-change.md | 5 ++++ packages/keystatic/package.json | 1 - .../src/form/fields/number/isAtStep.test.ts | 28 +++++++++++++++++++ .../src/form/fields/number/validateNumber.tsx | 20 +++++++++++-- pnpm-lock.yaml | 7 ----- 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 .changeset/fast-eagles-change.md create mode 100644 packages/keystatic/src/form/fields/number/isAtStep.test.ts diff --git a/.changeset/fast-eagles-change.md b/.changeset/fast-eagles-change.md new file mode 100644 index 000000000..012d58385 --- /dev/null +++ b/.changeset/fast-eagles-change.md @@ -0,0 +1,5 @@ +--- +'@keystatic/core': patch +--- + +Remove unnecessary dependency diff --git a/packages/keystatic/package.json b/packages/keystatic/package.json index 5597ea964..348307f5c 100644 --- a/packages/keystatic/package.json +++ b/packages/keystatic/package.json @@ -141,7 +141,6 @@ "@urql/exchange-graphcache": "^7.1.2", "@urql/exchange-persisted": "^4.3.0", "cookie": "^0.5.0", - "decimal.js-light": "^2.5.1", "emery": "^1.4.1", "escape-string-regexp": "^4.0.0", "fast-deep-equal": "^3.1.3", diff --git a/packages/keystatic/src/form/fields/number/isAtStep.test.ts b/packages/keystatic/src/form/fields/number/isAtStep.test.ts new file mode 100644 index 000000000..f004e8981 --- /dev/null +++ b/packages/keystatic/src/form/fields/number/isAtStep.test.ts @@ -0,0 +1,28 @@ +import { isAtStep } from './validateNumber'; +import { test, expect } from '@jest/globals'; + +for (const [value, step] of [ + [5, 1], + [5, 0.1], + [5, 0.5], + [5, 5e-3], + [5.1, 0.1], + [5.1, 1e-6], + [5.1, 1e-7], + [5.1, 1e-200], + [300, 3e-200], +]) { + test(`value: ${value}, step: ${step} should be true`, () => { + expect(isAtStep(value, step)).toBe(true); + }); +} + +for (const [value, step] of [ + [5.1, 1], + [5.1, 0.5], + [5.1, 3e-200], +]) { + test(`value: ${value}, step: ${step} should be false`, () => { + expect(isAtStep(value, step)).toBe(false); + }); +} diff --git a/packages/keystatic/src/form/fields/number/validateNumber.tsx b/packages/keystatic/src/form/fields/number/validateNumber.tsx index 09afd036b..7bb075f18 100644 --- a/packages/keystatic/src/form/fields/number/validateNumber.tsx +++ b/packages/keystatic/src/form/fields/number/validateNumber.tsx @@ -1,5 +1,3 @@ -import Decimal from 'decimal.js-light'; - export function validateNumber( validation: | { @@ -31,9 +29,25 @@ export function validateNumber( if ( step !== undefined && validation?.validateStep !== undefined && - new Decimal(value).mod(new Decimal(step)).toNumber() !== 0 + !isAtStep(value, step) ) { return `${label} must be a multiple of ${step}`; } } } + +function decimalPlaces(value: number) { + const stringified = value.toString(); + const indexOfDecimal = stringified.indexOf('.'); + if (indexOfDecimal === -1) { + const indexOfE = stringified.indexOf('e-'); + return indexOfE === -1 ? 0 : parseInt(stringified.slice(indexOfE + 2)); + } + return stringified.length - indexOfDecimal - 1; +} + +export function isAtStep(value: number, step: number) { + const dc = Math.max(decimalPlaces(step), decimalPlaces(value)); + const base = Math.pow(10, dc); + return (value * base) % (step * base) === 0; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index afd1f22ed..bbab4bf2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1204,9 +1204,6 @@ importers: cookie: specifier: ^0.5.0 version: 0.5.0 - decimal.js-light: - specifier: ^2.5.1 - version: 2.5.1 emery: specifier: ^1.4.1 version: 1.4.2 @@ -15097,10 +15094,6 @@ packages: engines: {node: '>=0.10.0'} dev: false - /decimal.js-light@2.5.1: - resolution: {integrity: sha512-qIMFpTMZmny+MMIitAB6D7iVPEorVw6YQRWkvarTkT4tBeSLLiHzcwj6q0MmYSFCiVpiqPJTJEYIrpcPzVEIvg==} - dev: false - /decimal.js@10.4.3: resolution: {integrity: sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==} dev: false