-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin.ts
More file actions
85 lines (82 loc) · 2.32 KB
/
join.ts
File metadata and controls
85 lines (82 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @fileoverview Grammatical list joiners via `Intl.ListFormat` —
* Oxford-comma aware and locale-correct. `joinAnd` ("a, b, and c"),
* `joinOr` ("a, b, or c").
*/
import { getConjunctionFormatter, getDisjunctionFormatter } from './_internal'
/**
* Join array elements with proper "and" conjunction formatting.
*
* Formats an array of strings into a grammatically correct list using
* "and" as the conjunction. Uses `Intl.ListFormat` for proper English
* formatting with Oxford comma support.
*
* @param arr - Array of strings to join (can be readonly)
* @returns Formatted string with proper "and" conjunction
*
* @example
* ```ts
* // Two items
* joinAnd(['apples', 'oranges'])
* // Returns: "apples and oranges"
*
* // Three or more items (Oxford comma)
* joinAnd(['apples', 'oranges', 'bananas'])
* // Returns: "apples, oranges, and bananas"
*
* // Single item
* joinAnd(['apples'])
* // Returns: "apples"
*
* // Empty array
* joinAnd([])
* // Returns: ""
*
* // Usage in messages
* const items = ['React', 'Vue', 'Angular']
* console.log(`You can choose ${joinAnd(items)}`)
* // Outputs: "You can choose React, Vue, and Angular"
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function joinAnd(arr: string[] | readonly string[]): string {
return getConjunctionFormatter().format(arr)
}
/**
* Join array elements with proper "or" disjunction formatting.
*
* Formats an array of strings into a grammatically correct list using
* "or" as the disjunction. Uses `Intl.ListFormat` for proper English
* formatting with Oxford comma support.
*
* @param arr - Array of strings to join (can be readonly)
* @returns Formatted string with proper "or" disjunction
*
* @example
* ```ts
* // Two items
* joinOr(['yes', 'no'])
* // Returns: "yes or no"
*
* // Three or more items (Oxford comma)
* joinOr(['red', 'green', 'blue'])
* // Returns: "red, green, or blue"
*
* // Single item
* joinOr(['maybe'])
* // Returns: "maybe"
*
* // Empty array
* joinOr([])
* // Returns: ""
*
* // Usage in prompts
* const options = ['npm', 'yarn', 'pnpm']
* console.log(`Choose a package manager: ${joinOr(options)}`)
* // Outputs: "Choose a package manager: npm, yarn, or pnpm"
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function joinOr(arr: string[] | readonly string[]): string {
return getDisjunctionFormatter().format(arr)
}