-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_builder.ts
173 lines (151 loc) · 3.28 KB
/
string_builder.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* @poppinss/string
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { extname } from 'node:path'
import string from './index.js'
/**
* StringBuilder exposes a fluent API to transform a string value
*/
export default class StringBuilder {
#value: string
constructor(value: string | StringBuilder) {
this.#value = typeof value === 'string' ? value : value.toString()
}
/**
* Applies dash case transformation
*/
dashCase(): this {
this.#value = string.dashCase(this.#value)
return this
}
/**
* Applies dot case transformation
*/
dotCase(): this {
this.#value = string.dotCase(this.#value)
return this
}
/**
* Applies snake case transformation
*/
snakeCase(): this {
this.#value = string.snakeCase(this.#value)
return this
}
/**
* Applies pascal case transformation
*/
pascalCase(): this {
this.#value = string.pascalCase(this.#value)
return this
}
/**
* Applies camelcase case transformation
*/
camelCase(): this {
this.#value = string.camelCase(this.#value)
return this
}
/**
* Applies capital case transformation
*/
capitalCase(): this {
this.#value = string.capitalCase(this.#value)
return this
}
/**
* Applies title case transformation
*/
titleCase(): this {
this.#value = string.titleCase(this.#value)
return this
}
/**
* Applies sentence case transformation
*/
sentenceCase(): this {
this.#value = string.sentenceCase(this.#value)
return this
}
/**
* Removes all sorts of casing from the string
*/
noCase(): this {
this.#value = string.noCase(this.#value)
return this
}
/**
* Converts value to its plural form
*/
plural(): this {
this.#value = string.pluralize(this.#value)
return this
}
/**
* Converts value to its singular form
*/
singular(): this {
this.#value = string.singular(this.#value)
return this
}
/**
* Converts value to a URL friendly slug
*/
slugify(): this {
this.#value = string.slug(this.#value)
return this
}
/**
* Removes a given suffix from the string
*/
removeSuffix(suffix: string): this {
this.#value = this.#value.replace(new RegExp(`[-_]?${suffix}$`, 'i'), '')
return this
}
/**
* Adds suffix to the string
*/
suffix(suffix: string): this {
this.removeSuffix(suffix)
this.#value = `${this.#value}${suffix}`
return this
}
/**
* Removes a given prefix from the string
*/
removePrefix(prefix: string): this {
this.#value = this.#value.replace(new RegExp(`^${prefix}[-_]?`, 'i'), '')
return this
}
/**
* Adds prefix to the string
*/
prefix(prefix: string): this {
this.removePrefix(prefix)
this.#value = `${prefix}${this.#value}`
return this
}
/**
* Removes extension from the value
*/
removeExtension(): this {
this.#value = this.#value.replace(new RegExp(`${extname(this.#value)}$`), '')
return this
}
/**
* Adds extension to the value
*/
ext(extension: string): this {
this.removeExtension()
this.#value = `${this.#value}.${extension.replace(/^\./, '')}`
return this
}
toString() {
return this.#value
}
}