deno.land / x / froebel@v0.23.2 / case.ts

نووسراو ببینە
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { CamelCase, KebabCase, PascalCase, ScreamingSnakeCase, SnakeCase, StringCase, λ,} from "./types.ts";
/** Upper-case first letter of string. */export const capitalize = <T extends string>(str: T) => (str[0].toUpperCase() + str.slice(1)) as Capitalize<T>;
/** Lower-case first letter of string */export const uncapitalize = <T extends string>(str: T) => (str[0].toLowerCase() + str.slice(1)) as Uncapitalize<T>;
/** Strictly typed `String.toUpperCase()`. */export const upper = <T extends string>(str: T) => str.toUpperCase() as Uppercase<T>;
/** Strictly typed `String.toLowerCase()`. */export const lower = <T extends string>(str: T) => str.toLowerCase() as Lowercase<T>;
/** * Transforms a variable name to snake case. * * Note: The rules for transforming anything to snake case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * snake('fooBar') // 'foo_bar' * ``` */export const snake = <T extends string>(str: T): SnakeCase<T> => str .replace(/(\p{L})-(?=\p{L})/gu, "$1_") .replace(/(^|_)(\p{Lu})(?!\p{Lu})/gu, (_, a, b) => `${a}${b.toLowerCase()}`) .replace(/([^\p{Lu}])(\p{Lu})(?=\p{Lu})/gu, (_, a, b) => `${a}_${b}`) .replace( /([^\p{Lu}_0-9])(\p{Lu})/gu, (_, a, b) => `${a}_${b.toLowerCase()}`, ) .replace(/(\p{Lu}[^\p{L}_]*)(\p{Ll})/gu, (_, a, b) => `${a}_${b}`) as any;
/** * Transforms a variable name to kebab case. * * Note: The rules for transforming anything to kebab case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * kebab('fooBar') // 'foo-bar' * ``` */export const kebab = <T extends string>(str: T): KebabCase<T> => str .replace(/(\p{L})_(?=\p{L})/gu, "$1-") .replace(/(^|-)(\p{Lu})(?!\p{Lu})/gu, (_, a, b) => `${a}${b.toLowerCase()}`) .replace(/([^\p{Lu}])(\p{Lu})(?=\p{Lu})/gu, (_, a, b) => `${a}-${b}`) .replace( /([^\p{Lu}\-0-9])(\p{Lu})/gu, (_, a, b) => `${a}-${b.toLowerCase()}`, ) .replace(/(\p{Lu}[^\p{L}\-]*)(\p{Ll})/gu, (_, a, b) => `${a}-${b}`) as any;
/** * Transforms a variable name to camel case. * * Note: The rules for transforming anything to camel case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * camel('foo_bar') // 'fooBar' * ``` */export const camel = <T extends string>(str: T): CamelCase<T> => str .replace(/^\p{Lu}/u, (v) => v.toLowerCase()) .replace( /([^_-][_-]*)[_-](\p{L})/gu, (_, a, b) => a + b.toUpperCase(), ) as any;
/** * Transforms a variable name to pascal case. * * Note: The rules for transforming anything to pascal case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * pascal('foo_bar') // 'FooBar' * ``` */export const pascal = <T extends string>(str: T): PascalCase<T> => capitalize(camel(str));
/** * Transforms a variable name to screaming snake case. * * @see {@link snake} * * @example * ``` * screamingSnake('fooBar') // 'FOO_BAR' * ``` */export const screamingSnake = <T extends string>( str: T,): ScreamingSnakeCase<T> => upper(snake(str));
/** * Transform a variable name to `targetCase` * * @see {@link snake} * @see {@link kebab} * @see {@link camel} * @see {@link pascal} * @see {@link screamingSnake} */export const transformCase = <T extends string, C extends StringCase>( str: T, targetCase: C,): C extends "snake" ? SnakeCase<T> : never => { if (!(targetCase in converters)) { throw Error(`can't convert to ${targetCase} case`); } return converters[targetCase](str) as any;};
const converters: Record<StringCase, λ<[string], string>> = { camel, kebab, pascal, snake, "screaming-snake": screamingSnake,};
froebel

Version Info

Tagged at
a year ago