From ff4dda8da72100e48a7a4a11d4c86f65e4bac7da Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sat, 26 Oct 2024 15:20:41 +0200 Subject: [PATCH 1/3] fix: docs for decimal and faster rounding alg used --- src/common/data/decimal.ts | 7 +++++++ src/common/data/rounding.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/common/data/decimal.ts b/src/common/data/decimal.ts index 9c9ee9e2..2a4a1276 100644 --- a/src/common/data/decimal.ts +++ b/src/common/data/decimal.ts @@ -1,6 +1,10 @@ export type DecimalValue = number export type DecimalInput = number | string | DecimalValue +/** + * The float returned is guaraneteed to have only the `decimalPlaces` length, + * no rounding errors or float arithmetic artefacts. + */ export function decimal( value: DecimalInput, decimalPlaces = 2, @@ -8,6 +12,7 @@ export function decimal( return +(+value).toFixed(decimalPlaces) } +/** `123` becomes `1.23` */ export function decimalFromCents( value: DecimalInput, decimalPlaces = 2, @@ -15,6 +20,7 @@ export function decimalFromCents( return +(+value / 10 ** decimalPlaces).toFixed(decimalPlaces) } +/** `1.23` becomes `123` */ export function decimalToCents( value: DecimalInput, decimalPlaces = 2, @@ -22,6 +28,7 @@ export function decimalToCents( return Math.round(+value * 10 ** decimalPlaces) } +/** `1.23` becomes `23` */ export function decimalCentsPart( value: DecimalInput, decimalPlaces = 2, diff --git a/src/common/data/rounding.ts b/src/common/data/rounding.ts index f91f7b47..e3ba0e31 100644 --- a/src/common/data/rounding.ts +++ b/src/common/data/rounding.ts @@ -1,6 +1,6 @@ // From https://v2.dinerojs.com/docs/api/formatting/to-unit MIT -import { arraySum } from './array' +import { sum } from './math' export type RoundingMode = (value: number) => number @@ -73,7 +73,7 @@ export function roundArrayOfNumbersToMatchSum(ipt: number[], max = 100, decimalP const out: number[] = [] out.fill(0, length) - const total = arraySum(iptPercents) + const total = sum(iptPercents) if (total !== 0) { const powDecimalPlaces = 10 ** decimalPlaces From 63a2a081b888715f614382703658fac10c928385 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Tue, 29 Oct 2024 09:28:05 +0100 Subject: [PATCH 2/3] fix: BigInt64Array optional --- src/common/data/is.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/common/data/is.ts b/src/common/data/is.ts index 84c455fa..3a11976d 100644 --- a/src/common/data/is.ts +++ b/src/common/data/is.ts @@ -25,17 +25,17 @@ export function isFunction(obj: unknown): obj is Function { } export function isBinaryArray(obj: unknown): obj is T { - return obj instanceof Uint8Array - || obj instanceof Uint8ClampedArray - || obj instanceof Uint16Array - || obj instanceof Uint32Array - || obj instanceof Int8Array - || obj instanceof Int16Array - || obj instanceof Int32Array - || obj instanceof Float32Array - || obj instanceof Float64Array - || obj instanceof BigInt64Array - || obj instanceof BigUint64Array + return (typeof Uint8Array !== 'undefined' && obj instanceof Uint8Array) + || (typeof Uint8ClampedArray !== 'undefined' && obj instanceof Uint8ClampedArray) + || (typeof Uint16Array !== 'undefined' && obj instanceof Uint16Array) + || (typeof Uint32Array !== 'undefined' && obj instanceof Uint32Array) + || (typeof Int8Array !== 'undefined' && obj instanceof Int8Array) + || (typeof Int16Array !== 'undefined' && obj instanceof Int16Array) + || (typeof Int32Array !== 'undefined' && obj instanceof Int32Array) + || (typeof Float32Array !== 'undefined' && obj instanceof Float32Array) + || (typeof Float64Array !== 'undefined' && obj instanceof Float64Array) + || (typeof BigInt64Array !== 'undefined' && obj instanceof BigInt64Array) + || (typeof BigUint64Array !== 'undefined' && obj instanceof BigUint64Array) } /** Something like number, string, boolean */ From b257b00dc3668dc12170faec64843e192db86aee Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Tue, 29 Oct 2024 09:28:09 +0100 Subject: [PATCH 3/3] 0.25.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 33639b89..6c10f89e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.25.4", + "version": "0.25.5", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",