@mrmartineau/kit 0.1.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 +325 -40
- package/package.json +46 -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 -1
- 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 -0
- package/src/utils/formatters/index.ts +24 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberBase.test.ts +55 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberCurrency.test.ts +55 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimal.test.ts +63 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimalForceDecimalPlaces.test.ts +29 -0
- package/src/utils/formatters/numbers/formatNumberAccounting.ts +27 -0
- package/src/utils/formatters/numbers/formatNumberBase.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberBytes.ts +29 -0
- package/src/utils/formatters/numbers/formatNumberCompact.ts +19 -0
- package/src/utils/formatters/numbers/formatNumberCurrency.ts +25 -0
- package/src/utils/formatters/numbers/formatNumberDecimal.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberDecimalForceDecimalPlaces.ts +23 -0
- package/src/utils/formatters/numbers/formatNumberOrdinal.ts +22 -0
- package/src/utils/formatters/numbers/formatNumberPercent.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberRange.ts +22 -0
- package/src/utils/formatters/numbers/formatNumberSignDisplay.ts +24 -0
- package/src/utils/formatters/strings/formatInitials.ts +17 -0
- package/src/utils/formatters/strings/formatList.test.ts +48 -0
- package/src/utils/formatters/strings/formatList.ts +10 -0
- package/src/utils/formatters/strings/maskString.ts +27 -0
- package/src/utils/formatters/strings/normalizeWhitespace.ts +8 -0
- package/src/utils/formatters/strings/pluralize.ts +11 -0
- package/src/utils/formatters/strings/slugify.ts +12 -0
- package/src/utils/formatters/strings/splitWords.ts +12 -0
- package/src/utils/formatters/strings/stripDiacritics.ts +7 -0
- package/src/utils/formatters/strings/toCamelCase.ts +17 -0
- package/src/utils/formatters/strings/toKebabCase.ts +11 -0
- package/src/utils/formatters/strings/toPascalCase.ts +14 -0
- package/src/utils/formatters/strings/toSnakeCase.ts +11 -0
- package/src/utils/formatters/strings/toStartCase.test.ts +40 -0
- package/src/utils/formatters/strings/toStartCase.ts +21 -0
- package/src/utils/formatters/strings/truncate.ts +25 -0
- package/src/utils/index.ts +2 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_DECIMAL_PLACES = 2
|
|
@@ -0,0 +1,24 @@
|
|
|
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'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberBase } from '../formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
describe('formatNumberBase', () => {
|
|
5
|
+
it('should throw if no value provided', () => {
|
|
6
|
+
// @ts-expect-error - Testing for invalid input
|
|
7
|
+
expect(() => formatNumberBase()).toThrowError('formatNumber: Value is not defined')
|
|
8
|
+
// @ts-expect-error - Testing for invalid input
|
|
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
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should throw if value is invalid', () => {
|
|
14
|
+
// @ts-expect-error - Testing for invalid input
|
|
15
|
+
expect(() => formatNumberBase(false)).toThrowError('formatNumber: Invalid number format')
|
|
16
|
+
// @ts-expect-error - Testing for invalid input
|
|
17
|
+
expect(() => formatNumberBase([false])).toThrowError('formatNumber: Invalid number format')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should throw if string passed is invalid', () => {
|
|
21
|
+
expect(() => formatNumberBase('-')).toThrowError('formatNumber: Invalid number format')
|
|
22
|
+
})
|
|
23
|
+
|
|
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
|
+
})
|
|
28
|
+
|
|
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
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should format a number to 0 decimal places', () => {
|
|
35
|
+
expect(
|
|
36
|
+
formatNumberBase(123456.789, {
|
|
37
|
+
maximumFractionDigits: 0,
|
|
38
|
+
}),
|
|
39
|
+
).toBe('123,457')
|
|
40
|
+
expect(
|
|
41
|
+
formatNumberBase(0, {
|
|
42
|
+
maximumFractionDigits: 0,
|
|
43
|
+
}),
|
|
44
|
+
).toBe('0')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should format a number based on the options passed', () => {
|
|
48
|
+
expect(
|
|
49
|
+
formatNumberBase(123456.789, {
|
|
50
|
+
style: 'currency',
|
|
51
|
+
currency: 'EUR',
|
|
52
|
+
}),
|
|
53
|
+
).toBe('€123,456.79')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
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', () => {
|
|
24
|
+
expect(
|
|
25
|
+
formatNumberCurrency(1.789, 'GBP', undefined, {
|
|
26
|
+
maximumFractionDigits: 1,
|
|
27
|
+
}),
|
|
28
|
+
).toBe('£1.8')
|
|
29
|
+
expect(
|
|
30
|
+
formatNumberCurrency(12.789, 'GBP', undefined, {
|
|
31
|
+
maximumFractionDigits: 1,
|
|
32
|
+
}),
|
|
33
|
+
).toBe('£12.8')
|
|
34
|
+
expect(
|
|
35
|
+
formatNumberCurrency(123.789, 'GBP', undefined, {
|
|
36
|
+
maximumFractionDigits: 1,
|
|
37
|
+
}),
|
|
38
|
+
).toBe('£123.8')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should throw if passing an invalid value', () => {
|
|
42
|
+
expect(() => formatNumberCurrency('-', 'GBP')).toThrowError(
|
|
43
|
+
'formatNumber: Invalid number format',
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should throw if passing an invalid currency code', () => {
|
|
48
|
+
expect(() => formatNumberCurrency(1.789, 'DARE')).toThrowError()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should throw if missing a currency code', () => {
|
|
52
|
+
// @ts-expect-error - Testing for invalid input
|
|
53
|
+
expect(() => formatNumberCurrency(1.789)).toThrowError()
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberDecimal } from '../formatNumberDecimal.js'
|
|
3
|
+
|
|
4
|
+
describe('formatNumberDecimal', () => {
|
|
5
|
+
it('should throw if no value provided', () => {
|
|
6
|
+
// @ts-expect-error - Testing for invalid input
|
|
7
|
+
expect(() => formatNumberDecimal()).toThrowError('formatNumber: Value is not defined')
|
|
8
|
+
// @ts-expect-error - Testing for invalid input
|
|
9
|
+
expect(() => formatNumberDecimal(undefined)).toThrowError('formatNumber: Value is not defined')
|
|
10
|
+
// @ts-expect-error - Testing for invalid input
|
|
11
|
+
expect(() => formatNumberDecimal(null)).toThrowError('formatNumber: Value is not defined')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should throw if value is invalid', () => {
|
|
15
|
+
// @ts-expect-error - Testing for invalid input
|
|
16
|
+
expect(() => formatNumberDecimal(false)).toThrowError('formatNumber: Invalid number format')
|
|
17
|
+
// @ts-expect-error - Testing for invalid input
|
|
18
|
+
expect(() => formatNumberDecimal([false])).toThrowError('formatNumber: Invalid number format')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should throw if string passed is invalid', () => {
|
|
22
|
+
expect(() => formatNumberDecimal('-')).toThrowError('formatNumber: Invalid number format')
|
|
23
|
+
})
|
|
24
|
+
|
|
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
|
+
})
|
|
29
|
+
|
|
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
|
+
})
|
|
34
|
+
|
|
35
|
+
it('should format a number to 1 decimal place', () => {
|
|
36
|
+
expect(formatNumberDecimal(123456.789, 1)).toBe('123,456.8')
|
|
37
|
+
})
|
|
38
|
+
|
|
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
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should format a number to 3 decimal places', () => {
|
|
45
|
+
expect(formatNumberDecimal(123456.789, 3)).toBe('123,456.789')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should format a number to 4 decimal places', () => {
|
|
49
|
+
expect(formatNumberDecimal(123456.789, 4)).toBe('123,456.789')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should format non-standard numbers', () => {
|
|
53
|
+
expect(formatNumberDecimal(6.938893903907228e-18, 2)).toBe('0')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should format with custom number format options', () => {
|
|
57
|
+
expect(
|
|
58
|
+
formatNumberDecimal(1.789, 3, {
|
|
59
|
+
maximumFractionDigits: 1, // this will override the decimalCount parameter
|
|
60
|
+
}),
|
|
61
|
+
).toBe('1.8')
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatNumberDecimalForceDecimalPlaces } from '../formatNumberDecimalForceDecimalPlaces.js'
|
|
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
|
+
})
|
|
8
|
+
|
|
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
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should format a number to 3 decimal places', () => {
|
|
15
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 3)).toBe('123,456.789')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should format a number to 4 decimal places', () => {
|
|
19
|
+
expect(formatNumberDecimalForceDecimalPlaces(123456.789, 4)).toBe('123,456.7890')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should format non-standard numbers', () => {
|
|
23
|
+
expect(formatNumberDecimalForceDecimalPlaces(6.938893903907228e-18, 2)).toBe('0.00')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('should format with custom number format options', () => {
|
|
27
|
+
expect(formatNumberDecimalForceDecimalPlaces(1.789, 3)).toBe('1.789')
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number as currency in accounting style: negative values are wrapped
|
|
6
|
+
* in parentheses (e.g. `-100` → `(£100.00)`).
|
|
7
|
+
* @param value - The number to format.
|
|
8
|
+
* @param currency - The currency code e.g. `USD`, `GBP`, `EUR`.
|
|
9
|
+
* @param decimalCount - Number of decimal places. Default 2.
|
|
10
|
+
* @param options - Additional, optional `Intl.NumberFormatOptions`.
|
|
11
|
+
*/
|
|
12
|
+
export const formatNumberAccounting = (
|
|
13
|
+
value: number | string,
|
|
14
|
+
currency: string,
|
|
15
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
16
|
+
options?: Intl.NumberFormatOptions,
|
|
17
|
+
) => {
|
|
18
|
+
return formatNumberBase(value, {
|
|
19
|
+
minimumFractionDigits: decimalCount,
|
|
20
|
+
maximumFractionDigits: decimalCount,
|
|
21
|
+
...options,
|
|
22
|
+
style: 'currency',
|
|
23
|
+
currency,
|
|
24
|
+
currencyDisplay: 'narrowSymbol',
|
|
25
|
+
currencySign: 'accounting',
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const formatNumberBase = (
|
|
2
|
+
value: string | number,
|
|
3
|
+
options?: Intl.NumberFormatOptions,
|
|
4
|
+
): string => {
|
|
5
|
+
if (value === undefined || value === '' || value === null) {
|
|
6
|
+
throw TypeError('formatNumber: Value is not defined')
|
|
7
|
+
}
|
|
8
|
+
if (typeof value === 'boolean' || typeof value === 'object') {
|
|
9
|
+
throw Error('formatNumber: Invalid number format')
|
|
10
|
+
}
|
|
11
|
+
|
|
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
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return new Intl.NumberFormat('en-GB', options).format(value)
|
|
21
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberDecimal } from './formatNumberDecimal.js'
|
|
3
|
+
|
|
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
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Formats a byte count as a human-readable string (e.g. `1500` → `1.5 KB`).
|
|
9
|
+
* @param bytes - The byte count.
|
|
10
|
+
* @param binary - If `true`, use 1024-based units (KiB, MiB, …). Default `false`.
|
|
11
|
+
* @param decimalCount - Maximum decimal places. Default 2.
|
|
12
|
+
*/
|
|
13
|
+
export const formatNumberBytes = (
|
|
14
|
+
bytes: number,
|
|
15
|
+
binary: boolean = false,
|
|
16
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
17
|
+
) => {
|
|
18
|
+
if (!Number.isFinite(bytes)) {
|
|
19
|
+
throw new Error('formatNumberBytes: Invalid byte count')
|
|
20
|
+
}
|
|
21
|
+
if (bytes === 0) return '0 B'
|
|
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 / base ** exponent
|
|
28
|
+
return `${formatNumberDecimal(value, decimalCount)} ${units[exponent]}`
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Formats a number in compact notation (e.g. `1200` → `1.2K`).
|
|
5
|
+
* @param value - The number to format.
|
|
6
|
+
* @param display - `"short"` (default) or `"long"` (e.g. `1.2 thousand`).
|
|
7
|
+
* @param options - Additional, optional `Intl.NumberFormatOptions`.
|
|
8
|
+
*/
|
|
9
|
+
export const formatNumberCompact = (
|
|
10
|
+
value: number | string,
|
|
11
|
+
display: 'short' | 'long' = 'short',
|
|
12
|
+
options?: Intl.NumberFormatOptions,
|
|
13
|
+
) => {
|
|
14
|
+
return formatNumberBase(value, {
|
|
15
|
+
notation: 'compact',
|
|
16
|
+
compactDisplay: display,
|
|
17
|
+
...options,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number as currency.
|
|
6
|
+
* @param value - The number to format.
|
|
7
|
+
* @param currency - The currency code e.g. USD, GBP, EUR, etc.
|
|
8
|
+
* @param decimalCount - Optional. The number of decimal places to display. Default is 2.
|
|
9
|
+
* @param options - Additional, optional options for formatting the number.
|
|
10
|
+
* @returns The formatted currency as a string.
|
|
11
|
+
*/
|
|
12
|
+
export const formatNumberCurrency = (
|
|
13
|
+
value: number | string,
|
|
14
|
+
currency: string,
|
|
15
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
16
|
+
options?: Intl.NumberFormatOptions,
|
|
17
|
+
) => {
|
|
18
|
+
return formatNumberBase(value, {
|
|
19
|
+
maximumFractionDigits: decimalCount,
|
|
20
|
+
...options,
|
|
21
|
+
style: 'currency',
|
|
22
|
+
currencyDisplay: 'narrowSymbol',
|
|
23
|
+
currency,
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number with decimal places.
|
|
6
|
+
* @param value - The number to format.
|
|
7
|
+
* @param decimalCount - Optional. The number of decimal places to display. Default is 2.
|
|
8
|
+
* @param options - Additional, optional options for formatting the number.
|
|
9
|
+
* @returns The formatted number as a string.
|
|
10
|
+
*/
|
|
11
|
+
export const formatNumberDecimal = (
|
|
12
|
+
value: number | string,
|
|
13
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
14
|
+
options?: Intl.NumberFormatOptions,
|
|
15
|
+
) => {
|
|
16
|
+
return formatNumberBase(value, {
|
|
17
|
+
style: 'decimal',
|
|
18
|
+
maximumFractionDigits: decimalCount,
|
|
19
|
+
...options,
|
|
20
|
+
})
|
|
21
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberDecimal } from './formatNumberDecimal.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number with decimal places.
|
|
6
|
+
* @param value - The number to format.
|
|
7
|
+
* @param decimalCount - The number of decimal places to display. Default is 2.
|
|
8
|
+
* @param options - Additional, optional options for formatting the number.
|
|
9
|
+
* @returns The formatted number as a string.
|
|
10
|
+
*/
|
|
11
|
+
export const formatNumberDecimalForceDecimalPlaces = (
|
|
12
|
+
value: number,
|
|
13
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
14
|
+
options?: Intl.NumberFormatOptions,
|
|
15
|
+
) => {
|
|
16
|
+
return formatNumberDecimal(value, decimalCount, {
|
|
17
|
+
...options,
|
|
18
|
+
// The `Intl.NumberFormat` constructor that `formatNumberDecimal` uses trims
|
|
19
|
+
// decimal places if possible so I have ensured that the number of decimal
|
|
20
|
+
// places will be kept no matter what here.
|
|
21
|
+
minimumFractionDigits: decimalCount,
|
|
22
|
+
})
|
|
23
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const ORDINAL_SUFFIXES: Record<Intl.LDMLPluralRule, string> = {
|
|
2
|
+
zero: 'th',
|
|
3
|
+
one: 'st',
|
|
4
|
+
two: 'nd',
|
|
5
|
+
few: 'rd',
|
|
6
|
+
many: 'th',
|
|
7
|
+
other: 'th',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Formats an integer with its English ordinal suffix (e.g. `1` → `1st`).
|
|
12
|
+
* @param value - The integer to format.
|
|
13
|
+
* @param locale - Optional. Defaults to `"en-GB"`.
|
|
14
|
+
*/
|
|
15
|
+
export const formatNumberOrdinal = (value: number, locale: string = 'en-GB') => {
|
|
16
|
+
if (!Number.isInteger(value)) {
|
|
17
|
+
throw new Error('formatNumberOrdinal: Value must be an integer')
|
|
18
|
+
}
|
|
19
|
+
const rules = new Intl.PluralRules(locale, { type: 'ordinal' })
|
|
20
|
+
const suffix = ORDINAL_SUFFIXES[rules.select(value)]
|
|
21
|
+
return `${value}${suffix}`
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number as a percentage. The value is expected to already be in
|
|
6
|
+
* decimal form (e.g. `0.42` → `42%`).
|
|
7
|
+
* @param value - The number to format.
|
|
8
|
+
* @param decimalCount - Optional. Maximum decimal places. Default 2.
|
|
9
|
+
* @param options - Additional, optional `Intl.NumberFormatOptions`.
|
|
10
|
+
*/
|
|
11
|
+
export const formatNumberPercent = (
|
|
12
|
+
value: number | string,
|
|
13
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
14
|
+
options?: Intl.NumberFormatOptions,
|
|
15
|
+
) => {
|
|
16
|
+
return formatNumberBase(value, {
|
|
17
|
+
style: 'percent',
|
|
18
|
+
maximumFractionDigits: decimalCount,
|
|
19
|
+
...options,
|
|
20
|
+
})
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a numeric range using `Intl.NumberFormat#formatRange`
|
|
3
|
+
* (e.g. `1`, `5` → `1–5`).
|
|
4
|
+
* @param min - Lower bound.
|
|
5
|
+
* @param max - Upper bound.
|
|
6
|
+
* @param options - Additional, optional `Intl.NumberFormatOptions`.
|
|
7
|
+
* @param locale - Optional. Defaults to `"en-GB"`.
|
|
8
|
+
*/
|
|
9
|
+
export const formatNumberRange = (
|
|
10
|
+
min: number,
|
|
11
|
+
max: number,
|
|
12
|
+
options?: Intl.NumberFormatOptions,
|
|
13
|
+
locale: string = 'en-GB',
|
|
14
|
+
) => {
|
|
15
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
16
|
+
throw new Error('formatNumberRange: Invalid range')
|
|
17
|
+
}
|
|
18
|
+
if (min > max) {
|
|
19
|
+
throw new RangeError('formatNumberRange: min must be <= max')
|
|
20
|
+
}
|
|
21
|
+
return new Intl.NumberFormat(locale, options).formatRange(min, max)
|
|
22
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
|
|
2
|
+
import { formatNumberBase } from './formatNumberBase.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number with an explicit sign for non-zero values (e.g. `5` → `+5`,
|
|
6
|
+
* `-5` → `-5`, `0` → `0`). Pass `signDisplay: "always"` to also sign zero.
|
|
7
|
+
* @param value - The number to format.
|
|
8
|
+
* @param decimalCount - Maximum decimal places. Default 2.
|
|
9
|
+
* @param signDisplay - `Intl.NumberFormatOptions['signDisplay']`. Default `"exceptZero"`.
|
|
10
|
+
* @param options - Additional, optional `Intl.NumberFormatOptions`.
|
|
11
|
+
*/
|
|
12
|
+
export const formatNumberSignDisplay = (
|
|
13
|
+
value: number | string,
|
|
14
|
+
decimalCount: number = DEFAULT_DECIMAL_PLACES,
|
|
15
|
+
signDisplay: Intl.NumberFormatOptions['signDisplay'] = 'exceptZero',
|
|
16
|
+
options?: Intl.NumberFormatOptions,
|
|
17
|
+
) => {
|
|
18
|
+
return formatNumberBase(value, {
|
|
19
|
+
style: 'decimal',
|
|
20
|
+
maximumFractionDigits: decimalCount,
|
|
21
|
+
signDisplay,
|
|
22
|
+
...options,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the initials for a name.
|
|
3
|
+
* @param name - The full name string.
|
|
4
|
+
* @param maxLength - Maximum number of initials to return. Default 2.
|
|
5
|
+
* @example formatInitials("John Smith") // "JS"
|
|
6
|
+
* @example formatInitials("Mary Jane Watson", 3) // "MJW"
|
|
7
|
+
*/
|
|
8
|
+
export function formatInitials(name: string, maxLength: number = 2): string {
|
|
9
|
+
if (!name) return ''
|
|
10
|
+
return name
|
|
11
|
+
.trim()
|
|
12
|
+
.split(/\s+/)
|
|
13
|
+
.map((part) => part.charAt(0).toUpperCase())
|
|
14
|
+
.filter(Boolean)
|
|
15
|
+
.slice(0, maxLength)
|
|
16
|
+
.join('')
|
|
17
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { formatList } from './formatList'
|
|
3
|
+
|
|
4
|
+
describe('formatList', () => {
|
|
5
|
+
it('formats list with default options (long conjunction)', () => {
|
|
6
|
+
const input = ['apple', 'banana', 'orange']
|
|
7
|
+
expect(formatList(input)).toBe('apple, banana, and orange')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('formats list with disjunction type', () => {
|
|
11
|
+
const input = ['apple', 'banana', 'orange']
|
|
12
|
+
expect(formatList(input, { type: 'disjunction' })).toBe('apple, banana, or orange')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('formats list with unit type', () => {
|
|
16
|
+
const input = ['apple', 'banana', 'orange']
|
|
17
|
+
expect(formatList(input, { type: 'unit' })).toBe('apple, banana, orange')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('formats list with short style', () => {
|
|
21
|
+
const input = ['apple', 'banana', 'orange']
|
|
22
|
+
expect(formatList(input, { style: 'short', type: 'conjunction' })).toBe(
|
|
23
|
+
'apple, banana, & orange',
|
|
24
|
+
)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('formats list with narrow style', () => {
|
|
28
|
+
const input = ['apple', 'banana', 'orange']
|
|
29
|
+
expect(formatList(input, { style: 'narrow', type: 'conjunction' })).toBe(
|
|
30
|
+
'apple, banana, orange',
|
|
31
|
+
)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('handles single item list', () => {
|
|
35
|
+
const input = ['apple']
|
|
36
|
+
expect(formatList(input)).toBe('apple')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('handles two item list', () => {
|
|
40
|
+
const input = ['apple', 'banana']
|
|
41
|
+
expect(formatList(input)).toBe('apple and banana')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('handles empty list', () => {
|
|
45
|
+
const input: string[] = []
|
|
46
|
+
expect(formatList(input)).toBe('')
|
|
47
|
+
})
|
|
48
|
+
})
|