Skip to content

Commit

Permalink
Merge branch 'release/v0.25.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Oct 29, 2024
2 parents 2f91118 + b257b00 commit 290d9fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.25.4",
"version": "0.25.5",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
7 changes: 7 additions & 0 deletions src/common/data/decimal.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
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,
): DecimalValue {
return +(+value).toFixed(decimalPlaces)
}

/** `123` becomes `1.23` */
export function decimalFromCents(
value: DecimalInput,
decimalPlaces = 2,
): DecimalValue {
return +(+value / 10 ** decimalPlaces).toFixed(decimalPlaces)
}

/** `1.23` becomes `123` */
export function decimalToCents(
value: DecimalInput,
decimalPlaces = 2,
): number {
return Math.round(+value * 10 ** decimalPlaces)
}

/** `1.23` becomes `23` */
export function decimalCentsPart(
value: DecimalInput,
decimalPlaces = 2,
Expand Down
22 changes: 11 additions & 11 deletions src/common/data/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ export function isFunction(obj: unknown): obj is Function {
}

export function isBinaryArray<T>(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 */
Expand Down
4 changes: 2 additions & 2 deletions src/common/data/rounding.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 290d9fa

Please sign in to comment.