@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.
Files changed (54) hide show
  1. package/README.md +284 -67
  2. package/package.json +40 -17
  3. package/src/biome/biome.json +37 -7
  4. package/src/biome/strict.json +108 -0
  5. package/src/eslint/index.ts +49 -13
  6. package/src/index.ts +2 -2
  7. package/src/oxfmt/.oxfmtrc.json +5 -1
  8. package/src/oxlint/.oxlintrc.json +12 -3
  9. package/src/oxlint/strict.json +61 -0
  10. package/src/prettier/astro.js +31 -0
  11. package/src/prettier/astro.test.ts +48 -0
  12. package/src/sounds/index.ts +156 -156
  13. package/src/utils/checks/checks.test.ts +162 -0
  14. package/src/utils/checks/collections.ts +63 -0
  15. package/src/utils/checks/index.ts +17 -0
  16. package/src/utils/checks/isEmpty.ts +15 -0
  17. package/src/utils/checks/nullish.ts +26 -0
  18. package/src/utils/checks/numbers.ts +31 -0
  19. package/src/utils/checks/objects.ts +38 -0
  20. package/src/utils/checks/primitives.ts +41 -0
  21. package/src/utils/formatters/constants.ts +1 -1
  22. package/src/utils/formatters/index.ts +24 -24
  23. package/src/utils/formatters/numbers/__tests__/formatNumberBase.test.ts +33 -46
  24. package/src/utils/formatters/numbers/__tests__/formatNumberCurrency.test.ts +42 -43
  25. package/src/utils/formatters/numbers/__tests__/formatNumberDecimal.test.ts +43 -56
  26. package/src/utils/formatters/numbers/__tests__/formatNumberDecimalForceDecimalPlaces.test.ts +23 -34
  27. package/src/utils/formatters/numbers/formatNumberAccounting.ts +7 -7
  28. package/src/utils/formatters/numbers/formatNumberBase.ts +11 -11
  29. package/src/utils/formatters/numbers/formatNumberBytes.ts +13 -13
  30. package/src/utils/formatters/numbers/formatNumberCompact.ts +5 -5
  31. package/src/utils/formatters/numbers/formatNumberCurrency.ts +6 -6
  32. package/src/utils/formatters/numbers/formatNumberDecimal.ts +5 -5
  33. package/src/utils/formatters/numbers/formatNumberDecimalForceDecimalPlaces.ts +4 -4
  34. package/src/utils/formatters/numbers/formatNumberOrdinal.ts +13 -16
  35. package/src/utils/formatters/numbers/formatNumberPercent.ts +5 -5
  36. package/src/utils/formatters/numbers/formatNumberRange.ts +5 -5
  37. package/src/utils/formatters/numbers/formatNumberSignDisplay.ts +6 -6
  38. package/src/utils/formatters/strings/formatInitials.ts +2 -2
  39. package/src/utils/formatters/strings/formatList.test.ts +48 -51
  40. package/src/utils/formatters/strings/formatList.ts +4 -4
  41. package/src/utils/formatters/strings/maskString.ts +12 -15
  42. package/src/utils/formatters/strings/normalizeWhitespace.ts +1 -1
  43. package/src/utils/formatters/strings/pluralize.ts +3 -7
  44. package/src/utils/formatters/strings/slugify.ts +3 -3
  45. package/src/utils/formatters/strings/splitWords.ts +5 -5
  46. package/src/utils/formatters/strings/stripDiacritics.ts +1 -1
  47. package/src/utils/formatters/strings/toCamelCase.ts +7 -7
  48. package/src/utils/formatters/strings/toKebabCase.ts +2 -2
  49. package/src/utils/formatters/strings/toPascalCase.ts +4 -4
  50. package/src/utils/formatters/strings/toSnakeCase.ts +2 -2
  51. package/src/utils/formatters/strings/toStartCase.test.ts +40 -45
  52. package/src/utils/formatters/strings/toStartCase.ts +4 -4
  53. package/src/utils/formatters/strings/truncate.ts +11 -15
  54. package/src/utils/index.ts +2 -1
@@ -1,4 +1,4 @@
1
- import { formatNumberBase } from "./formatNumberBase.js";
1
+ import { formatNumberBase } from './formatNumberBase.js'
2
2
 
3
3
  /**
4
4
  * Formats a number in compact notation (e.g. `1200` → `1.2K`).
@@ -8,12 +8,12 @@ import { formatNumberBase } from "./formatNumberBase.js";
8
8
  */
9
9
  export const formatNumberCompact = (
10
10
  value: number | string,
11
- display: "short" | "long" = "short",
11
+ display: 'short' | 'long' = 'short',
12
12
  options?: Intl.NumberFormatOptions,
13
13
  ) => {
14
14
  return formatNumberBase(value, {
15
- notation: "compact",
15
+ notation: 'compact',
16
16
  compactDisplay: display,
17
17
  ...options,
18
- });
19
- };
18
+ })
19
+ }
@@ -1,5 +1,5 @@
1
- import { DEFAULT_DECIMAL_PLACES } from "../constants.js";
2
- import { formatNumberBase } from "./formatNumberBase.js";
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.
@@ -18,8 +18,8 @@ export const formatNumberCurrency = (
18
18
  return formatNumberBase(value, {
19
19
  maximumFractionDigits: decimalCount,
20
20
  ...options,
21
- style: "currency",
22
- currencyDisplay: "narrowSymbol",
21
+ style: 'currency',
22
+ currencyDisplay: 'narrowSymbol',
23
23
  currency,
24
- });
25
- };
24
+ })
25
+ }
@@ -1,5 +1,5 @@
1
- import { DEFAULT_DECIMAL_PLACES } from "../constants.js";
2
- import { formatNumberBase } from "./formatNumberBase.js";
1
+ import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
2
+ import { formatNumberBase } from './formatNumberBase.js'
3
3
 
4
4
  /**
5
5
  * Formats a number with decimal places.
@@ -14,8 +14,8 @@ export const formatNumberDecimal = (
14
14
  options?: Intl.NumberFormatOptions,
15
15
  ) => {
16
16
  return formatNumberBase(value, {
17
- style: "decimal",
17
+ style: 'decimal',
18
18
  maximumFractionDigits: decimalCount,
19
19
  ...options,
20
- });
21
- };
20
+ })
21
+ }
@@ -1,5 +1,5 @@
1
- import { DEFAULT_DECIMAL_PLACES } from "../constants.js";
2
- import { formatNumberDecimal } from "./formatNumberDecimal.js";
1
+ import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
2
+ import { formatNumberDecimal } from './formatNumberDecimal.js'
3
3
 
4
4
  /**
5
5
  * Formats a number with decimal places.
@@ -19,5 +19,5 @@ export const formatNumberDecimalForceDecimalPlaces = (
19
19
  // decimal places if possible so I have ensured that the number of decimal
20
20
  // places will be kept no matter what here.
21
21
  minimumFractionDigits: decimalCount,
22
- });
23
- };
22
+ })
23
+ }
@@ -1,25 +1,22 @@
1
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
- };
2
+ zero: 'th',
3
+ one: 'st',
4
+ two: 'nd',
5
+ few: 'rd',
6
+ many: 'th',
7
+ other: 'th',
8
+ }
9
9
 
10
10
  /**
11
11
  * Formats an integer with its English ordinal suffix (e.g. `1` → `1st`).
12
12
  * @param value - The integer to format.
13
13
  * @param locale - Optional. Defaults to `"en-GB"`.
14
14
  */
15
- export const formatNumberOrdinal = (
16
- value: number,
17
- locale: string = "en-GB",
18
- ) => {
15
+ export const formatNumberOrdinal = (value: number, locale: string = 'en-GB') => {
19
16
  if (!Number.isInteger(value)) {
20
- throw new Error("formatNumberOrdinal: Value must be an integer");
17
+ throw new Error('formatNumberOrdinal: Value must be an integer')
21
18
  }
22
- const rules = new Intl.PluralRules(locale, { type: "ordinal" });
23
- const suffix = ORDINAL_SUFFIXES[rules.select(value)];
24
- return `${value}${suffix}`;
25
- };
19
+ const rules = new Intl.PluralRules(locale, { type: 'ordinal' })
20
+ const suffix = ORDINAL_SUFFIXES[rules.select(value)]
21
+ return `${value}${suffix}`
22
+ }
@@ -1,5 +1,5 @@
1
- import { DEFAULT_DECIMAL_PLACES } from "../constants.js";
2
- import { formatNumberBase } from "./formatNumberBase.js";
1
+ import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
2
+ import { formatNumberBase } from './formatNumberBase.js'
3
3
 
4
4
  /**
5
5
  * Formats a number as a percentage. The value is expected to already be in
@@ -14,8 +14,8 @@ export const formatNumberPercent = (
14
14
  options?: Intl.NumberFormatOptions,
15
15
  ) => {
16
16
  return formatNumberBase(value, {
17
- style: "percent",
17
+ style: 'percent',
18
18
  maximumFractionDigits: decimalCount,
19
19
  ...options,
20
- });
21
- };
20
+ })
21
+ }
@@ -10,13 +10,13 @@ export const formatNumberRange = (
10
10
  min: number,
11
11
  max: number,
12
12
  options?: Intl.NumberFormatOptions,
13
- locale: string = "en-GB",
13
+ locale: string = 'en-GB',
14
14
  ) => {
15
15
  if (!Number.isFinite(min) || !Number.isFinite(max)) {
16
- throw new Error("formatNumberRange: Invalid range");
16
+ throw new Error('formatNumberRange: Invalid range')
17
17
  }
18
18
  if (min > max) {
19
- throw new RangeError("formatNumberRange: min must be <= max");
19
+ throw new RangeError('formatNumberRange: min must be <= max')
20
20
  }
21
- return new Intl.NumberFormat(locale, options).formatRange(min, max);
22
- };
21
+ return new Intl.NumberFormat(locale, options).formatRange(min, max)
22
+ }
@@ -1,5 +1,5 @@
1
- import { DEFAULT_DECIMAL_PLACES } from "../constants.js";
2
- import { formatNumberBase } from "./formatNumberBase.js";
1
+ import { DEFAULT_DECIMAL_PLACES } from '../constants.js'
2
+ import { formatNumberBase } from './formatNumberBase.js'
3
3
 
4
4
  /**
5
5
  * Formats a number with an explicit sign for non-zero values (e.g. `5` → `+5`,
@@ -12,13 +12,13 @@ import { formatNumberBase } from "./formatNumberBase.js";
12
12
  export const formatNumberSignDisplay = (
13
13
  value: number | string,
14
14
  decimalCount: number = DEFAULT_DECIMAL_PLACES,
15
- signDisplay: Intl.NumberFormatOptions["signDisplay"] = "exceptZero",
15
+ signDisplay: Intl.NumberFormatOptions['signDisplay'] = 'exceptZero',
16
16
  options?: Intl.NumberFormatOptions,
17
17
  ) => {
18
18
  return formatNumberBase(value, {
19
- style: "decimal",
19
+ style: 'decimal',
20
20
  maximumFractionDigits: decimalCount,
21
21
  signDisplay,
22
22
  ...options,
23
- });
24
- };
23
+ })
24
+ }
@@ -6,12 +6,12 @@
6
6
  * @example formatInitials("Mary Jane Watson", 3) // "MJW"
7
7
  */
8
8
  export function formatInitials(name: string, maxLength: number = 2): string {
9
- if (!name) return "";
9
+ if (!name) return ''
10
10
  return name
11
11
  .trim()
12
12
  .split(/\s+/)
13
13
  .map((part) => part.charAt(0).toUpperCase())
14
14
  .filter(Boolean)
15
15
  .slice(0, maxLength)
16
- .join("");
16
+ .join('')
17
17
  }
@@ -1,51 +1,48 @@
1
- import { describe, expect, it } from "bun:test";
2
-
3
- import { formatList } from "./formatList";
4
-
5
- describe("formatList", () => {
6
- it("formats list with default options (long conjunction)", () => {
7
- const input = ["apple", "banana", "orange"];
8
- expect(formatList(input)).toBe("apple, banana, and orange");
9
- });
10
-
11
- it("formats list with disjunction type", () => {
12
- const input = ["apple", "banana", "orange"];
13
- expect(formatList(input, { type: "disjunction" })).toBe(
14
- "apple, banana, or orange",
15
- );
16
- });
17
-
18
- it("formats list with unit type", () => {
19
- const input = ["apple", "banana", "orange"];
20
- expect(formatList(input, { type: "unit" })).toBe("apple, banana, orange");
21
- });
22
-
23
- it("formats list with short style", () => {
24
- const input = ["apple", "banana", "orange"];
25
- expect(formatList(input, { style: "short", type: "conjunction" })).toBe(
26
- "apple, banana, & orange",
27
- );
28
- });
29
-
30
- it("formats list with narrow style", () => {
31
- const input = ["apple", "banana", "orange"];
32
- expect(formatList(input, { style: "narrow", type: "conjunction" })).toBe(
33
- "apple, banana, orange",
34
- );
35
- });
36
-
37
- it("handles single item list", () => {
38
- const input = ["apple"];
39
- expect(formatList(input)).toBe("apple");
40
- });
41
-
42
- it("handles two item list", () => {
43
- const input = ["apple", "banana"];
44
- expect(formatList(input)).toBe("apple and banana");
45
- });
46
-
47
- it("handles empty list", () => {
48
- const input: string[] = [];
49
- expect(formatList(input)).toBe("");
50
- });
51
- });
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
+ })
@@ -1,10 +1,10 @@
1
1
  export function formatList(
2
2
  array: Array<string>,
3
3
  options: Intl.ListFormatOptions = {
4
- style: "long",
5
- type: "conjunction",
4
+ style: 'long',
5
+ type: 'conjunction',
6
6
  },
7
7
  ) {
8
- const formatter = new Intl.ListFormat("en", options);
9
- return formatter.format(array);
8
+ const formatter = new Intl.ListFormat('en', options)
9
+ return formatter.format(array)
10
10
  }
@@ -1,10 +1,10 @@
1
1
  export interface MaskStringOptions {
2
2
  /** Number of characters to keep visible at the start. Default 0. */
3
- visibleStart?: number;
3
+ visibleStart?: number
4
4
  /** Number of characters to keep visible at the end. Default 4. */
5
- visibleEnd?: number;
5
+ visibleEnd?: number
6
6
  /** Character used to mask the hidden portion. Default `"•"`. */
7
- maskChar?: string;
7
+ maskChar?: string
8
8
  }
9
9
 
10
10
  /**
@@ -13,18 +13,15 @@ export interface MaskStringOptions {
13
13
  * (tokens, card numbers, emails).
14
14
  * @example maskString("4242424242424242", { visibleEnd: 4 }) // "••••••••••••4242"
15
15
  */
16
- export function maskString(
17
- str: string,
18
- options: MaskStringOptions = {},
19
- ): string {
20
- const { visibleStart = 0, visibleEnd = 4, maskChar = "•" } = options;
21
- if (!str) return "";
16
+ export function maskString(str: string, options: MaskStringOptions = {}): string {
17
+ const { visibleStart = 0, visibleEnd = 4, maskChar = '•' } = options
18
+ if (!str) return ''
22
19
 
23
- const total = str.length;
24
- if (visibleStart + visibleEnd >= total) return str;
20
+ const total = str.length
21
+ if (visibleStart + visibleEnd >= total) return str
25
22
 
26
- const start = str.slice(0, visibleStart);
27
- const end = visibleEnd === 0 ? "" : str.slice(-visibleEnd);
28
- const masked = maskChar.repeat(total - visibleStart - visibleEnd);
29
- return `${start}${masked}${end}`;
23
+ const start = str.slice(0, visibleStart)
24
+ const end = visibleEnd === 0 ? '' : str.slice(-visibleEnd)
25
+ const masked = maskChar.repeat(total - visibleStart - visibleEnd)
26
+ return `${start}${masked}${end}`
30
27
  }
@@ -4,5 +4,5 @@
4
4
  * @example normalizeWhitespace(" hello\n\tworld ") // "hello world"
5
5
  */
6
6
  export function normalizeWhitespace(str: string): string {
7
- return str.replace(/\s+/g, " ").trim();
7
+ return str.replace(/\s+/g, ' ').trim()
8
8
  }
@@ -5,11 +5,7 @@
5
5
  * @example pluralize(2, "apple") // "apples"
6
6
  * @example pluralize(2, "child", "children") // "children"
7
7
  */
8
- export function pluralize(
9
- count: number,
10
- singular: string,
11
- plural?: string,
12
- ): string {
13
- if (count === 1) return singular;
14
- return plural ?? `${singular}s`;
8
+ export function pluralize(count: number, singular: string, plural?: string): string {
9
+ if (count === 1) return singular
10
+ return plural ?? `${singular}s`
15
11
  }
@@ -1,4 +1,4 @@
1
- import { stripDiacritics } from "./stripDiacritics.js";
1
+ import { stripDiacritics } from './stripDiacritics.js'
2
2
 
3
3
  /**
4
4
  * Converts a string to a URL-friendly slug.
@@ -7,6 +7,6 @@ import { stripDiacritics } from "./stripDiacritics.js";
7
7
  export function slugify(str: string): string {
8
8
  return stripDiacritics(str)
9
9
  .toLowerCase()
10
- .replace(/[^a-z0-9]+/g, "-")
11
- .replace(/^-+|-+$/g, "");
10
+ .replace(/[^a-z0-9]+/g, '-')
11
+ .replace(/^-+|-+$/g, '')
12
12
  }
@@ -3,10 +3,10 @@
3
3
  * PascalCase, ACRONYMTransitions, snake_case, kebab-case, and whitespace.
4
4
  */
5
5
  export function splitWords(str: string): string[] {
6
- if (!str) return [];
6
+ if (!str) return []
7
7
  return str
8
- .replace(/([a-z\d])([A-Z])/g, "$1 $2")
9
- .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
10
- .split(/[\s_\-]+/)
11
- .filter(Boolean);
8
+ .replace(/([a-z\d])([A-Z])/g, '$1 $2')
9
+ .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
10
+ .split(/[\s_-]+/)
11
+ .filter(Boolean)
12
12
  }
@@ -3,5 +3,5 @@
3
3
  * @example stripDiacritics("café") // "cafe"
4
4
  */
5
5
  export function stripDiacritics(str: string): string {
6
- return str.normalize("NFD").replace(/\p{Diacritic}/gu, "");
6
+ return str.normalize('NFD').replace(/\p{Diacritic}/gu, '')
7
7
  }
@@ -1,17 +1,17 @@
1
- import { splitWords } from "./splitWords.js";
1
+ import { splitWords } from './splitWords.js'
2
2
 
3
3
  /**
4
4
  * Converts a string to `camelCase`.
5
5
  * @example toCamelCase("hello_world") // "helloWorld"
6
6
  */
7
7
  export function toCamelCase(str: string): string {
8
- const words = splitWords(str);
9
- if (words.length === 0) return "";
8
+ const words = splitWords(str)
9
+ if (words.length === 0) return ''
10
10
  return words
11
11
  .map((word, index) => {
12
- const lower = word.toLowerCase();
13
- if (index === 0) return lower;
14
- return lower.charAt(0).toUpperCase() + lower.slice(1);
12
+ const lower = word.toLowerCase()
13
+ if (index === 0) return lower
14
+ return lower.charAt(0).toUpperCase() + lower.slice(1)
15
15
  })
16
- .join("");
16
+ .join('')
17
17
  }
@@ -1,4 +1,4 @@
1
- import { splitWords } from "./splitWords.js";
1
+ import { splitWords } from './splitWords.js'
2
2
 
3
3
  /**
4
4
  * Converts a string to `kebab-case`.
@@ -7,5 +7,5 @@ import { splitWords } from "./splitWords.js";
7
7
  export function toKebabCase(str: string): string {
8
8
  return splitWords(str)
9
9
  .map((word) => word.toLowerCase())
10
- .join("-");
10
+ .join('-')
11
11
  }
@@ -1,4 +1,4 @@
1
- import { splitWords } from "./splitWords.js";
1
+ import { splitWords } from './splitWords.js'
2
2
 
3
3
  /**
4
4
  * Converts a string to `PascalCase`.
@@ -7,8 +7,8 @@ import { splitWords } from "./splitWords.js";
7
7
  export function toPascalCase(str: string): string {
8
8
  return splitWords(str)
9
9
  .map((word) => {
10
- const lower = word.toLowerCase();
11
- return lower.charAt(0).toUpperCase() + lower.slice(1);
10
+ const lower = word.toLowerCase()
11
+ return lower.charAt(0).toUpperCase() + lower.slice(1)
12
12
  })
13
- .join("");
13
+ .join('')
14
14
  }
@@ -1,4 +1,4 @@
1
- import { splitWords } from "./splitWords.js";
1
+ import { splitWords } from './splitWords.js'
2
2
 
3
3
  /**
4
4
  * Converts a string to `snake_case`.
@@ -7,5 +7,5 @@ import { splitWords } from "./splitWords.js";
7
7
  export function toSnakeCase(str: string): string {
8
8
  return splitWords(str)
9
9
  .map((word) => word.toLowerCase())
10
- .join("_");
10
+ .join('_')
11
11
  }
@@ -1,45 +1,40 @@
1
- import { describe, expect, it } from "bun:test";
2
-
3
- import { toStartCase } from "./toStartCase";
4
-
5
- describe("toStartCase", () => {
6
- it("converts underscore_case to Start Case", () => {
7
- expect(toStartCase("hello_world")).toBe("Hello World");
8
- expect(toStartCase("multiple_word_string")).toBe("Multiple Word String");
9
- expect(toStartCase("with_numbers_123")).toBe("With Numbers 123");
10
- });
11
-
12
- it("converts camelCase to Start Case", () => {
13
- expect(toStartCase("helloWorld")).toBe("Hello World");
14
- expect(toStartCase("multipleWordString")).toBe("Multiple Word String");
15
- expect(toStartCase("withNumbers123")).toBe("With Numbers123");
16
- });
17
-
18
- it("handles already capitalized words", () => {
19
- expect(toStartCase("Hello World")).toBe("Hello World");
20
- expect(toStartCase("HELLO WORLD")).toBe("HELLO WORLD");
21
- });
22
-
23
- it("capitalizes first letter of each word", () => {
24
- expect(toStartCase("hello world")).toBe("Hello World");
25
- expect(toStartCase("a small sentence")).toBe("A Small Sentence");
26
- });
27
-
28
- it("handles empty string", () => {
29
- expect(toStartCase("")).toBe("");
30
- });
31
-
32
- it("handles single word", () => {
33
- expect(toStartCase("hello")).toBe("Hello");
34
- expect(toStartCase("a")).toBe("A");
35
- });
36
-
37
- it("handles mixed formats", () => {
38
- expect(toStartCase("hello_camelCased-string")).toBe(
39
- "Hello Camel Cased-string",
40
- );
41
- expect(toStartCase("mixed_case-with SPACES")).toBe(
42
- "Mixed Case-with SPACES",
43
- );
44
- });
45
- });
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { toStartCase } from './toStartCase'
3
+
4
+ describe('toStartCase', () => {
5
+ it('converts underscore_case to Start Case', () => {
6
+ expect(toStartCase('hello_world')).toBe('Hello World')
7
+ expect(toStartCase('multiple_word_string')).toBe('Multiple Word String')
8
+ expect(toStartCase('with_numbers_123')).toBe('With Numbers 123')
9
+ })
10
+
11
+ it('converts camelCase to Start Case', () => {
12
+ expect(toStartCase('helloWorld')).toBe('Hello World')
13
+ expect(toStartCase('multipleWordString')).toBe('Multiple Word String')
14
+ expect(toStartCase('withNumbers123')).toBe('With Numbers123')
15
+ })
16
+
17
+ it('handles already capitalized words', () => {
18
+ expect(toStartCase('Hello World')).toBe('Hello World')
19
+ expect(toStartCase('HELLO WORLD')).toBe('HELLO WORLD')
20
+ })
21
+
22
+ it('capitalizes first letter of each word', () => {
23
+ expect(toStartCase('hello world')).toBe('Hello World')
24
+ expect(toStartCase('a small sentence')).toBe('A Small Sentence')
25
+ })
26
+
27
+ it('handles empty string', () => {
28
+ expect(toStartCase('')).toBe('')
29
+ })
30
+
31
+ it('handles single word', () => {
32
+ expect(toStartCase('hello')).toBe('Hello')
33
+ expect(toStartCase('a')).toBe('A')
34
+ })
35
+
36
+ it('handles mixed formats', () => {
37
+ expect(toStartCase('hello_camelCased-string')).toBe('Hello Camel Cased-string')
38
+ expect(toStartCase('mixed_case-with SPACES')).toBe('Mixed Case-with SPACES')
39
+ })
40
+ })
@@ -12,10 +12,10 @@
12
12
  * FYI hyphens will be kept so this will not convert kebab-case to Start Case
13
13
  */
14
14
  export function toStartCase(str: string): string {
15
- if (!str) return "";
15
+ if (!str) return ''
16
16
 
17
17
  return str
18
- .replace(/_/g, " ")
19
- .replace(/([a-z])([A-Z])/g, (_str, $1, $2) => $1 + " " + $2)
20
- .replace(/(\s|^)(\w)/g, (_str, $1, $2) => $1 + $2.toUpperCase());
18
+ .replace(/_/g, ' ')
19
+ .replace(/([a-z])([A-Z])/g, (_str, $1, $2) => `${$1} ${$2}`)
20
+ .replace(/(\s|^)(\w)/g, (_str, $1, $2) => $1 + $2.toUpperCase())
21
21
  }