@mrmartineau/kit 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +284 -67
- package/package.json +40 -17
- package/src/biome/biome.json +37 -7
- package/src/biome/strict.json +108 -0
- package/src/eslint/index.ts +49 -13
- package/src/index.ts +2 -2
- package/src/oxfmt/.oxfmtrc.json +5 -1
- package/src/oxlint/.oxlintrc.json +12 -3
- package/src/oxlint/strict.json +61 -0
- package/src/prettier/astro.js +31 -0
- package/src/prettier/astro.test.ts +48 -0
- package/src/sounds/index.ts +156 -156
- package/src/utils/checks/checks.test.ts +162 -0
- package/src/utils/checks/collections.ts +63 -0
- package/src/utils/checks/index.ts +17 -0
- package/src/utils/checks/isEmpty.ts +15 -0
- package/src/utils/checks/nullish.ts +26 -0
- package/src/utils/checks/numbers.ts +31 -0
- package/src/utils/checks/objects.ts +38 -0
- package/src/utils/checks/primitives.ts +41 -0
- package/src/utils/formatters/constants.ts +1 -1
- package/src/utils/formatters/index.ts +24 -24
- package/src/utils/formatters/numbers/__tests__/formatNumberBase.test.ts +33 -46
- package/src/utils/formatters/numbers/__tests__/formatNumberCurrency.test.ts +42 -43
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimal.test.ts +43 -56
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimalForceDecimalPlaces.test.ts +23 -34
- package/src/utils/formatters/numbers/formatNumberAccounting.ts +7 -7
- package/src/utils/formatters/numbers/formatNumberBase.ts +11 -11
- package/src/utils/formatters/numbers/formatNumberBytes.ts +13 -13
- package/src/utils/formatters/numbers/formatNumberCompact.ts +5 -5
- package/src/utils/formatters/numbers/formatNumberCurrency.ts +6 -6
- package/src/utils/formatters/numbers/formatNumberDecimal.ts +5 -5
- package/src/utils/formatters/numbers/formatNumberDecimalForceDecimalPlaces.ts +4 -4
- package/src/utils/formatters/numbers/formatNumberOrdinal.ts +13 -16
- package/src/utils/formatters/numbers/formatNumberPercent.ts +5 -5
- package/src/utils/formatters/numbers/formatNumberRange.ts +5 -5
- package/src/utils/formatters/numbers/formatNumberSignDisplay.ts +6 -6
- package/src/utils/formatters/strings/formatInitials.ts +2 -2
- package/src/utils/formatters/strings/formatList.test.ts +48 -51
- package/src/utils/formatters/strings/formatList.ts +4 -4
- package/src/utils/formatters/strings/maskString.ts +12 -15
- package/src/utils/formatters/strings/normalizeWhitespace.ts +1 -1
- package/src/utils/formatters/strings/pluralize.ts +3 -7
- package/src/utils/formatters/strings/slugify.ts +3 -3
- package/src/utils/formatters/strings/splitWords.ts +5 -5
- package/src/utils/formatters/strings/stripDiacritics.ts +1 -1
- package/src/utils/formatters/strings/toCamelCase.ts +7 -7
- package/src/utils/formatters/strings/toKebabCase.ts +2 -2
- package/src/utils/formatters/strings/toPascalCase.ts +4 -4
- package/src/utils/formatters/strings/toSnakeCase.ts +2 -2
- package/src/utils/formatters/strings/toStartCase.test.ts +40 -45
- package/src/utils/formatters/strings/toStartCase.ts +4 -4
- package/src/utils/formatters/strings/truncate.ts +11 -15
- package/src/utils/index.ts +2 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `true` for anything `typeof` calls `'object'`, except `null`: arrays,
|
|
3
|
+
* dates, maps, class instances and plain objects.
|
|
4
|
+
* @example isObjectLike([]) // true
|
|
5
|
+
*/
|
|
6
|
+
export function isObjectLike(value: unknown): value is object {
|
|
7
|
+
return typeof value === 'object' && value !== null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `true` for a non-null object that isn't an array. Dates, maps and class
|
|
12
|
+
* instances pass; use `isPlainObject` to rule them out.
|
|
13
|
+
* @example isObject({ a: 1 }) // true
|
|
14
|
+
* @example isObject([]) // false
|
|
15
|
+
*/
|
|
16
|
+
export function isObject(value: unknown): value is Record<string, unknown> {
|
|
17
|
+
return isObjectLike(value) && !Array.isArray(value)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `true` only for `{}` literals and `Object.create(null)`: no arrays, dates,
|
|
22
|
+
* maps or class instances. Use it before going through an object's keys.
|
|
23
|
+
* @example isPlainObject(new Date()) // false
|
|
24
|
+
*/
|
|
25
|
+
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
26
|
+
if (!isObjectLike(value)) return false
|
|
27
|
+
const proto = Object.getPrototypeOf(value)
|
|
28
|
+
return proto === Object.prototype || proto === null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `true` if the value is an object with this key as its own property.
|
|
33
|
+
* Inherited keys like `toString` don't count.
|
|
34
|
+
* @example hasKey({ id: 1 }, 'id') // true
|
|
35
|
+
*/
|
|
36
|
+
export function hasKey<K extends PropertyKey>(value: unknown, key: K): value is Record<K, unknown> {
|
|
37
|
+
return isObjectLike(value) && Object.hasOwn(value, key)
|
|
38
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** A value that isn't an object or a function. */
|
|
2
|
+
export type Primitive = string | number | bigint | boolean | symbol | null | undefined
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `true` for strings. Doesn't match `new String()` wrapper objects.
|
|
6
|
+
* @example isString('a') // true
|
|
7
|
+
*/
|
|
8
|
+
export function isString(value: unknown): value is string {
|
|
9
|
+
return typeof value === 'string'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @example isBoolean(false) // true */
|
|
13
|
+
export function isBoolean(value: unknown): value is boolean {
|
|
14
|
+
return typeof value === 'boolean'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @example isBigInt(10n) // true */
|
|
18
|
+
export function isBigInt(value: unknown): value is bigint {
|
|
19
|
+
return typeof value === 'bigint'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @example isSymbol(Symbol('id')) // true */
|
|
23
|
+
export function isSymbol(value: unknown): value is symbol {
|
|
24
|
+
return typeof value === 'symbol'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `true` for functions, including classes and async functions.
|
|
29
|
+
* @example isFunction(() => {}) // true
|
|
30
|
+
*/
|
|
31
|
+
export function isFunction(value: unknown): value is (...args: unknown[]) => unknown {
|
|
32
|
+
return typeof value === 'function'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* `true` for anything that isn't an object or a function, including `null`.
|
|
37
|
+
* @example isPrimitive(null) // true
|
|
38
|
+
*/
|
|
39
|
+
export function isPrimitive(value: unknown): value is Primitive {
|
|
40
|
+
return value === null || (typeof value !== 'object' && typeof value !== 'function')
|
|
41
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const DEFAULT_DECIMAL_PLACES = 2
|
|
1
|
+
export const DEFAULT_DECIMAL_PLACES = 2
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
export { formatNumberAccounting } from
|
|
2
|
-
export { formatNumberBase } from
|
|
3
|
-
export { formatNumberBytes } from
|
|
4
|
-
export { formatNumberCompact } from
|
|
5
|
-
export { formatNumberCurrency } from
|
|
6
|
-
export { formatNumberDecimal } from
|
|
7
|
-
export { formatNumberDecimalForceDecimalPlaces } from
|
|
8
|
-
export { formatNumberOrdinal } from
|
|
9
|
-
export { formatNumberPercent } from
|
|
10
|
-
export { formatNumberRange } from
|
|
11
|
-
export { formatNumberSignDisplay } from
|
|
12
|
-
export { formatInitials } from
|
|
13
|
-
export { formatList } from
|
|
14
|
-
export { maskString } from
|
|
15
|
-
export { normalizeWhitespace } from
|
|
16
|
-
export { pluralize } from
|
|
17
|
-
export { slugify } from
|
|
18
|
-
export { stripDiacritics } from
|
|
19
|
-
export { toCamelCase } from
|
|
20
|
-
export { toKebabCase } from
|
|
21
|
-
export { toPascalCase } from
|
|
22
|
-
export { toSnakeCase } from
|
|
23
|
-
export { toStartCase } from
|
|
24
|
-
export { truncate } from
|
|
1
|
+
export { formatNumberAccounting } from './numbers/formatNumberAccounting.js'
|
|
2
|
+
export { formatNumberBase } from './numbers/formatNumberBase.js'
|
|
3
|
+
export { formatNumberBytes } from './numbers/formatNumberBytes.js'
|
|
4
|
+
export { formatNumberCompact } from './numbers/formatNumberCompact.js'
|
|
5
|
+
export { formatNumberCurrency } from './numbers/formatNumberCurrency.js'
|
|
6
|
+
export { formatNumberDecimal } from './numbers/formatNumberDecimal.js'
|
|
7
|
+
export { formatNumberDecimalForceDecimalPlaces } from './numbers/formatNumberDecimalForceDecimalPlaces.js'
|
|
8
|
+
export { formatNumberOrdinal } from './numbers/formatNumberOrdinal.js'
|
|
9
|
+
export { formatNumberPercent } from './numbers/formatNumberPercent.js'
|
|
10
|
+
export { formatNumberRange } from './numbers/formatNumberRange.js'
|
|
11
|
+
export { formatNumberSignDisplay } from './numbers/formatNumberSignDisplay.js'
|
|
12
|
+
export { formatInitials } from './strings/formatInitials.js'
|
|
13
|
+
export { formatList } from './strings/formatList.js'
|
|
14
|
+
export { maskString } from './strings/maskString.js'
|
|
15
|
+
export { normalizeWhitespace } from './strings/normalizeWhitespace.js'
|
|
16
|
+
export { pluralize } from './strings/pluralize.js'
|
|
17
|
+
export { slugify } from './strings/slugify.js'
|
|
18
|
+
export { stripDiacritics } from './strings/stripDiacritics.js'
|
|
19
|
+
export { toCamelCase } from './strings/toCamelCase.js'
|
|
20
|
+
export { toKebabCase } from './strings/toKebabCase.js'
|
|
21
|
+
export { toPascalCase } from './strings/toPascalCase.js'
|
|
22
|
+
export { toSnakeCase } from './strings/toSnakeCase.js'
|
|
23
|
+
export { toStartCase } from './strings/toStartCase.js'
|
|
24
|
+
export { truncate } from './strings/truncate.js'
|
|
@@ -1,68 +1,55 @@
|
|
|
1
|
-
import { describe, expect, it } from
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberBase } from '../formatNumberBase.js'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe("formatNumberBase", () => {
|
|
6
|
-
it("should throw if no value provided", () => {
|
|
4
|
+
describe('formatNumberBase', () => {
|
|
5
|
+
it('should throw if no value provided', () => {
|
|
7
6
|
// @ts-expect-error - Testing for invalid input
|
|
8
|
-
expect(() => formatNumberBase()).toThrowError(
|
|
9
|
-
"formatNumber: Value is not defined",
|
|
10
|
-
);
|
|
7
|
+
expect(() => formatNumberBase()).toThrowError('formatNumber: Value is not defined')
|
|
11
8
|
// @ts-expect-error - Testing for invalid input
|
|
12
|
-
expect(() => formatNumberBase(undefined)).toThrowError(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
expect(() => formatNumberBase(null)).toThrowError(
|
|
16
|
-
"formatNumber: Value is not defined",
|
|
17
|
-
);
|
|
18
|
-
});
|
|
9
|
+
expect(() => formatNumberBase(undefined)).toThrowError('formatNumber: Value is not defined') // @ts-expect-error - Testing for invalid input
|
|
10
|
+
expect(() => formatNumberBase(null)).toThrowError('formatNumber: Value is not defined')
|
|
11
|
+
})
|
|
19
12
|
|
|
20
|
-
it(
|
|
13
|
+
it('should throw if value is invalid', () => {
|
|
21
14
|
// @ts-expect-error - Testing for invalid input
|
|
22
|
-
expect(() => formatNumberBase(false)).toThrowError(
|
|
23
|
-
"formatNumber: Invalid number format",
|
|
24
|
-
);
|
|
15
|
+
expect(() => formatNumberBase(false)).toThrowError('formatNumber: Invalid number format')
|
|
25
16
|
// @ts-expect-error - Testing for invalid input
|
|
26
|
-
expect(() => formatNumberBase([false])).toThrowError(
|
|
27
|
-
|
|
28
|
-
);
|
|
29
|
-
});
|
|
17
|
+
expect(() => formatNumberBase([false])).toThrowError('formatNumber: Invalid number format')
|
|
18
|
+
})
|
|
30
19
|
|
|
31
|
-
it(
|
|
32
|
-
expect(() => formatNumberBase(
|
|
33
|
-
|
|
34
|
-
);
|
|
35
|
-
});
|
|
20
|
+
it('should throw if string passed is invalid', () => {
|
|
21
|
+
expect(() => formatNumberBase('-')).toThrowError('formatNumber: Invalid number format')
|
|
22
|
+
})
|
|
36
23
|
|
|
37
|
-
it(
|
|
38
|
-
expect(formatNumberBase(123456.789456456)).toBe(
|
|
39
|
-
expect(formatNumberBase(0.0)).toBe(
|
|
40
|
-
})
|
|
24
|
+
it('should used the Intl.NumberFormat defaults and format a number to the default number of decimal places (3) when no options are passed', () => {
|
|
25
|
+
expect(formatNumberBase(123456.789456456)).toBe('123,456.789')
|
|
26
|
+
expect(formatNumberBase(0.0)).toBe('0')
|
|
27
|
+
})
|
|
41
28
|
|
|
42
|
-
it(
|
|
43
|
-
expect(formatNumberBase(123456.789456456, {})).toBe(
|
|
44
|
-
expect(formatNumberBase(0, {})).toBe(
|
|
45
|
-
})
|
|
29
|
+
it('should used the Intl.NumberFormat defaults and format a number to the default number of decimal places (3) when an empty options object is passed', () => {
|
|
30
|
+
expect(formatNumberBase(123456.789456456, {})).toBe('123,456.789')
|
|
31
|
+
expect(formatNumberBase(0, {})).toBe('0')
|
|
32
|
+
})
|
|
46
33
|
|
|
47
|
-
it(
|
|
34
|
+
it('should format a number to 0 decimal places', () => {
|
|
48
35
|
expect(
|
|
49
36
|
formatNumberBase(123456.789, {
|
|
50
37
|
maximumFractionDigits: 0,
|
|
51
38
|
}),
|
|
52
|
-
).toBe(
|
|
39
|
+
).toBe('123,457')
|
|
53
40
|
expect(
|
|
54
41
|
formatNumberBase(0, {
|
|
55
42
|
maximumFractionDigits: 0,
|
|
56
43
|
}),
|
|
57
|
-
).toBe(
|
|
58
|
-
})
|
|
44
|
+
).toBe('0')
|
|
45
|
+
})
|
|
59
46
|
|
|
60
|
-
it(
|
|
47
|
+
it('should format a number based on the options passed', () => {
|
|
61
48
|
expect(
|
|
62
49
|
formatNumberBase(123456.789, {
|
|
63
|
-
style:
|
|
64
|
-
currency:
|
|
50
|
+
style: 'currency',
|
|
51
|
+
currency: 'EUR',
|
|
65
52
|
}),
|
|
66
|
-
).toBe(
|
|
67
|
-
})
|
|
68
|
-
})
|
|
53
|
+
).toBe('€123,456.79')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -1,56 +1,55 @@
|
|
|
1
|
-
import { describe, expect, it } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
expect(formatNumberCurrency(
|
|
8
|
-
expect(formatNumberCurrency(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
expect(formatNumberCurrency(
|
|
14
|
-
expect(formatNumberCurrency(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
expect(formatNumberCurrency(
|
|
20
|
-
expect(formatNumberCurrency(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
it("should format a number to GBP currency with custom number format options", () => {
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberCurrency } from '../formatNumberCurrency.js'
|
|
3
|
+
|
|
4
|
+
describe('formatNumberCurrency', () => {
|
|
5
|
+
it('should format a number to GBP currency', () => {
|
|
6
|
+
expect(formatNumberCurrency(1.789, 'GBP')).toBe('£1.79')
|
|
7
|
+
expect(formatNumberCurrency(12.789, 'GBP')).toBe('£12.79')
|
|
8
|
+
expect(formatNumberCurrency(123.789, 'GBP')).toBe('£123.79')
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should format a number to USD currency', () => {
|
|
12
|
+
expect(formatNumberCurrency(1.789, 'USD')).toBe('$1.79')
|
|
13
|
+
expect(formatNumberCurrency(12.789, 'USD')).toBe('$12.79')
|
|
14
|
+
expect(formatNumberCurrency(123.789, 'USD')).toBe('$123.79')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should format a number to USD currency with specific number of decimal places', () => {
|
|
18
|
+
expect(formatNumberCurrency(1.789, 'USD', 1)).toBe('$1.8')
|
|
19
|
+
expect(formatNumberCurrency(12.789, 'USD', 2)).toBe('$12.79')
|
|
20
|
+
expect(formatNumberCurrency(123.789, 'USD', 5)).toBe('$123.789')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should format a number to GBP currency with custom number format options', () => {
|
|
25
24
|
expect(
|
|
26
|
-
formatNumberCurrency(1.789,
|
|
25
|
+
formatNumberCurrency(1.789, 'GBP', undefined, {
|
|
27
26
|
maximumFractionDigits: 1,
|
|
28
27
|
}),
|
|
29
|
-
).toBe(
|
|
28
|
+
).toBe('£1.8')
|
|
30
29
|
expect(
|
|
31
|
-
formatNumberCurrency(12.789,
|
|
30
|
+
formatNumberCurrency(12.789, 'GBP', undefined, {
|
|
32
31
|
maximumFractionDigits: 1,
|
|
33
32
|
}),
|
|
34
|
-
).toBe(
|
|
33
|
+
).toBe('£12.8')
|
|
35
34
|
expect(
|
|
36
|
-
formatNumberCurrency(123.789,
|
|
35
|
+
formatNumberCurrency(123.789, 'GBP', undefined, {
|
|
37
36
|
maximumFractionDigits: 1,
|
|
38
37
|
}),
|
|
39
|
-
).toBe(
|
|
40
|
-
})
|
|
38
|
+
).toBe('£123.8')
|
|
39
|
+
})
|
|
41
40
|
|
|
42
|
-
it(
|
|
43
|
-
expect(() => formatNumberCurrency(
|
|
44
|
-
|
|
45
|
-
)
|
|
46
|
-
})
|
|
41
|
+
it('should throw if passing an invalid value', () => {
|
|
42
|
+
expect(() => formatNumberCurrency('-', 'GBP')).toThrowError(
|
|
43
|
+
'formatNumber: Invalid number format',
|
|
44
|
+
)
|
|
45
|
+
})
|
|
47
46
|
|
|
48
|
-
it(
|
|
49
|
-
expect(() => formatNumberCurrency(1.789,
|
|
50
|
-
})
|
|
47
|
+
it('should throw if passing an invalid currency code', () => {
|
|
48
|
+
expect(() => formatNumberCurrency(1.789, 'DARE')).toThrowError()
|
|
49
|
+
})
|
|
51
50
|
|
|
52
|
-
it(
|
|
51
|
+
it('should throw if missing a currency code', () => {
|
|
53
52
|
// @ts-expect-error - Testing for invalid input
|
|
54
|
-
expect(() => formatNumberCurrency(1.789)).toThrowError()
|
|
55
|
-
})
|
|
56
|
-
})
|
|
53
|
+
expect(() => formatNumberCurrency(1.789)).toThrowError()
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -1,76 +1,63 @@
|
|
|
1
|
-
import { describe, expect, it } from
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberDecimal } from '../formatNumberDecimal.js'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe("formatNumberDecimal", () => {
|
|
6
|
-
it("should throw if no value provided", () => {
|
|
4
|
+
describe('formatNumberDecimal', () => {
|
|
5
|
+
it('should throw if no value provided', () => {
|
|
7
6
|
// @ts-expect-error - Testing for invalid input
|
|
8
|
-
expect(() => formatNumberDecimal()).toThrowError(
|
|
9
|
-
"formatNumber: Value is not defined",
|
|
10
|
-
);
|
|
7
|
+
expect(() => formatNumberDecimal()).toThrowError('formatNumber: Value is not defined')
|
|
11
8
|
// @ts-expect-error - Testing for invalid input
|
|
12
|
-
expect(() => formatNumberDecimal(undefined)).toThrowError(
|
|
13
|
-
"formatNumber: Value is not defined",
|
|
14
|
-
);
|
|
9
|
+
expect(() => formatNumberDecimal(undefined)).toThrowError('formatNumber: Value is not defined')
|
|
15
10
|
// @ts-expect-error - Testing for invalid input
|
|
16
|
-
expect(() => formatNumberDecimal(null)).toThrowError(
|
|
17
|
-
|
|
18
|
-
);
|
|
19
|
-
});
|
|
11
|
+
expect(() => formatNumberDecimal(null)).toThrowError('formatNumber: Value is not defined')
|
|
12
|
+
})
|
|
20
13
|
|
|
21
|
-
it(
|
|
14
|
+
it('should throw if value is invalid', () => {
|
|
22
15
|
// @ts-expect-error - Testing for invalid input
|
|
23
|
-
expect(() => formatNumberDecimal(false)).toThrowError(
|
|
24
|
-
"formatNumber: Invalid number format",
|
|
25
|
-
);
|
|
16
|
+
expect(() => formatNumberDecimal(false)).toThrowError('formatNumber: Invalid number format')
|
|
26
17
|
// @ts-expect-error - Testing for invalid input
|
|
27
|
-
expect(() => formatNumberDecimal([false])).toThrowError(
|
|
28
|
-
|
|
29
|
-
);
|
|
30
|
-
});
|
|
18
|
+
expect(() => formatNumberDecimal([false])).toThrowError('formatNumber: Invalid number format')
|
|
19
|
+
})
|
|
31
20
|
|
|
32
|
-
it(
|
|
33
|
-
expect(() => formatNumberDecimal(
|
|
34
|
-
|
|
35
|
-
);
|
|
36
|
-
});
|
|
21
|
+
it('should throw if string passed is invalid', () => {
|
|
22
|
+
expect(() => formatNumberDecimal('-')).toThrowError('formatNumber: Invalid number format')
|
|
23
|
+
})
|
|
37
24
|
|
|
38
|
-
it(
|
|
39
|
-
expect(formatNumberDecimal(123456.789)).toBe(
|
|
40
|
-
expect(formatNumberDecimal(0)).toBe(
|
|
41
|
-
})
|
|
25
|
+
it('should format a number to the default number of decimal places (2)', () => {
|
|
26
|
+
expect(formatNumberDecimal(123456.789)).toBe('123,456.79')
|
|
27
|
+
expect(formatNumberDecimal(0)).toBe('0')
|
|
28
|
+
})
|
|
42
29
|
|
|
43
|
-
it(
|
|
44
|
-
expect(formatNumberDecimal(123456.789, 0)).toBe(
|
|
45
|
-
expect(formatNumberDecimal(0, 0)).toBe(
|
|
46
|
-
})
|
|
30
|
+
it('should format a number to 0 decimal places', () => {
|
|
31
|
+
expect(formatNumberDecimal(123456.789, 0)).toBe('123,457')
|
|
32
|
+
expect(formatNumberDecimal(0, 0)).toBe('0')
|
|
33
|
+
})
|
|
47
34
|
|
|
48
|
-
it(
|
|
49
|
-
expect(formatNumberDecimal(123456.789, 1)).toBe(
|
|
50
|
-
})
|
|
35
|
+
it('should format a number to 1 decimal place', () => {
|
|
36
|
+
expect(formatNumberDecimal(123456.789, 1)).toBe('123,456.8')
|
|
37
|
+
})
|
|
51
38
|
|
|
52
|
-
it(
|
|
53
|
-
expect(formatNumberDecimal(123456.789, 2)).toBe(
|
|
54
|
-
expect(formatNumberDecimal(0.1, 2)).toBe(
|
|
55
|
-
})
|
|
39
|
+
it('should format a number to 2 decimal places', () => {
|
|
40
|
+
expect(formatNumberDecimal(123456.789, 2)).toBe('123,456.79')
|
|
41
|
+
expect(formatNumberDecimal(0.1, 2)).toBe('0.1')
|
|
42
|
+
})
|
|
56
43
|
|
|
57
|
-
it(
|
|
58
|
-
expect(formatNumberDecimal(123456.789, 3)).toBe(
|
|
59
|
-
})
|
|
44
|
+
it('should format a number to 3 decimal places', () => {
|
|
45
|
+
expect(formatNumberDecimal(123456.789, 3)).toBe('123,456.789')
|
|
46
|
+
})
|
|
60
47
|
|
|
61
|
-
it(
|
|
62
|
-
expect(formatNumberDecimal(123456.789, 4)).toBe(
|
|
63
|
-
})
|
|
48
|
+
it('should format a number to 4 decimal places', () => {
|
|
49
|
+
expect(formatNumberDecimal(123456.789, 4)).toBe('123,456.789')
|
|
50
|
+
})
|
|
64
51
|
|
|
65
|
-
it(
|
|
66
|
-
expect(formatNumberDecimal(6.938893903907228e-18, 2)).toBe(
|
|
67
|
-
})
|
|
52
|
+
it('should format non-standard numbers', () => {
|
|
53
|
+
expect(formatNumberDecimal(6.938893903907228e-18, 2)).toBe('0')
|
|
54
|
+
})
|
|
68
55
|
|
|
69
|
-
it(
|
|
56
|
+
it('should format with custom number format options', () => {
|
|
70
57
|
expect(
|
|
71
58
|
formatNumberDecimal(1.789, 3, {
|
|
72
59
|
maximumFractionDigits: 1, // this will override the decimalCount parameter
|
|
73
60
|
}),
|
|
74
|
-
).toBe(
|
|
75
|
-
})
|
|
76
|
-
})
|
|
61
|
+
).toBe('1.8')
|
|
62
|
+
})
|
|
63
|
+
})
|
package/src/utils/formatters/numbers/__tests__/formatNumberDecimalForceDecimalPlaces.test.ts
CHANGED
|
@@ -1,40 +1,29 @@
|
|
|
1
|
-
import { describe, expect, it } from
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberDecimalForceDecimalPlaces } from '../formatNumberDecimalForceDecimalPlaces.js'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
describe('formattedNumberDecimalForceDecimalPlaces', () => {
|
|
5
|
+
it('should format a number to 1 decimal place', () => {
|
|
6
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 1)).toBe('123,456.8')
|
|
7
|
+
})
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
expect(formatNumberDecimalForceDecimalPlaces(
|
|
8
|
-
|
|
9
|
-
);
|
|
10
|
-
});
|
|
9
|
+
it('should format a number to 2 decimal places', () => {
|
|
10
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 2)).toBe('123,456.79')
|
|
11
|
+
expect(formatNumberDecimalForceDecimalPlaces(0.1, 2)).toBe('0.10')
|
|
12
|
+
})
|
|
11
13
|
|
|
12
|
-
it(
|
|
13
|
-
expect(formatNumberDecimalForceDecimalPlaces(123456.789,
|
|
14
|
-
|
|
15
|
-
);
|
|
16
|
-
expect(formatNumberDecimalForceDecimalPlaces(0.1, 2)).toBe("0.10");
|
|
17
|
-
});
|
|
14
|
+
it('should format a number to 3 decimal places', () => {
|
|
15
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 3)).toBe('123,456.789')
|
|
16
|
+
})
|
|
18
17
|
|
|
19
|
-
it(
|
|
20
|
-
expect(formatNumberDecimalForceDecimalPlaces(123456.789,
|
|
21
|
-
|
|
22
|
-
);
|
|
23
|
-
});
|
|
18
|
+
it('should format a number to 4 decimal places', () => {
|
|
19
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 4)).toBe('123,456.7890')
|
|
20
|
+
})
|
|
24
21
|
|
|
25
|
-
it(
|
|
26
|
-
expect(formatNumberDecimalForceDecimalPlaces(
|
|
27
|
-
|
|
28
|
-
);
|
|
29
|
-
});
|
|
22
|
+
it('should format non-standard numbers', () => {
|
|
23
|
+
expect(formatNumberDecimalForceDecimalPlaces(6.938893903907228e-18, 2)).toBe('0.00')
|
|
24
|
+
})
|
|
30
25
|
|
|
31
|
-
it(
|
|
32
|
-
expect(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("should format with custom number format options", () => {
|
|
38
|
-
expect(formatNumberDecimalForceDecimalPlaces(1.789, 3)).toBe("1.789");
|
|
39
|
-
});
|
|
40
|
-
});
|
|
26
|
+
it('should format with custom number format options', () => {
|
|
27
|
+
expect(formatNumberDecimalForceDecimalPlaces(1.789, 3)).toBe('1.789')
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_DECIMAL_PLACES } from
|
|
2
|
-
import { formatNumberBase } from
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Formats a number as currency in accounting style: negative values are wrapped
|
|
@@ -19,9 +19,9 @@ export const formatNumberAccounting = (
|
|
|
19
19
|
minimumFractionDigits: decimalCount,
|
|
20
20
|
maximumFractionDigits: decimalCount,
|
|
21
21
|
...options,
|
|
22
|
-
style:
|
|
22
|
+
style: 'currency',
|
|
23
23
|
currency,
|
|
24
|
-
currencyDisplay:
|
|
25
|
-
currencySign:
|
|
26
|
-
})
|
|
27
|
-
}
|
|
24
|
+
currencyDisplay: 'narrowSymbol',
|
|
25
|
+
currencySign: 'accounting',
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -2,20 +2,20 @@ export const formatNumberBase = (
|
|
|
2
2
|
value: string | number,
|
|
3
3
|
options?: Intl.NumberFormatOptions,
|
|
4
4
|
): string => {
|
|
5
|
-
if (value
|
|
6
|
-
throw TypeError(
|
|
5
|
+
if (value === undefined || value === '' || value === null) {
|
|
6
|
+
throw TypeError('formatNumber: Value is not defined')
|
|
7
7
|
}
|
|
8
|
-
if (typeof value ===
|
|
9
|
-
throw Error(
|
|
8
|
+
if (typeof value === 'boolean' || typeof value === 'object') {
|
|
9
|
+
throw Error('formatNumber: Invalid number format')
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
if (typeof value ===
|
|
13
|
-
const trimmed = value.trim()
|
|
14
|
-
value = trimmed ===
|
|
15
|
-
if (isNaN(value)) {
|
|
16
|
-
throw new Error(
|
|
12
|
+
if (typeof value === 'string') {
|
|
13
|
+
const trimmed = value.trim()
|
|
14
|
+
value = trimmed === '' ? NaN : Number(trimmed)
|
|
15
|
+
if (Number.isNaN(value)) {
|
|
16
|
+
throw new Error('formatNumber: Invalid number format')
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
return new Intl.NumberFormat(
|
|
21
|
-
}
|
|
20
|
+
return new Intl.NumberFormat('en-GB', options).format(value)
|
|
21
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DEFAULT_DECIMAL_PLACES } from
|
|
2
|
-
import { formatNumberDecimal } from
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberDecimal } from './formatNumberDecimal.js'
|
|
3
3
|
|
|
4
|
-
const DECIMAL_UNITS = [
|
|
5
|
-
const BINARY_UNITS = [
|
|
4
|
+
const DECIMAL_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'] as const
|
|
5
|
+
const BINARY_UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'] as const
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Formats a byte count as a human-readable string (e.g. `1500` → `1.5 KB`).
|
|
@@ -16,14 +16,14 @@ export const formatNumberBytes = (
|
|
|
16
16
|
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
17
17
|
) => {
|
|
18
18
|
if (!Number.isFinite(bytes)) {
|
|
19
|
-
throw new Error(
|
|
19
|
+
throw new Error('formatNumberBytes: Invalid byte count')
|
|
20
20
|
}
|
|
21
|
-
if (bytes === 0) return
|
|
21
|
+
if (bytes === 0) return '0 B'
|
|
22
22
|
|
|
23
|
-
const base = binary ? 1024 : 1000
|
|
24
|
-
const units = binary ? BINARY_UNITS : DECIMAL_UNITS
|
|
25
|
-
const rawExponent = Math.floor(Math.log(Math.abs(bytes)) / Math.log(base))
|
|
26
|
-
const exponent = Math.min(Math.max(rawExponent, 0), units.length - 1)
|
|
27
|
-
const value = bytes /
|
|
28
|
-
return `${formatNumberDecimal(value, decimalCount)} ${units[exponent]}
|
|
29
|
-
}
|
|
23
|
+
const base = binary ? 1024 : 1000
|
|
24
|
+
const units = binary ? BINARY_UNITS : DECIMAL_UNITS
|
|
25
|
+
const rawExponent = Math.floor(Math.log(Math.abs(bytes)) / Math.log(base))
|
|
26
|
+
const exponent = Math.min(Math.max(rawExponent, 0), units.length - 1)
|
|
27
|
+
const value = bytes / base ** exponent
|
|
28
|
+
return `${formatNumberDecimal(value, decimalCount)} ${units[exponent]}`
|
|
29
|
+
}
|