string-tuner 1.0.3 → 1.0.5

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/dist/index.d.ts CHANGED
@@ -1,14 +1,193 @@
1
- declare const st: {
2
- capitalize: (str: string, fullSentence?: boolean) => string | undefined;
1
+ /**
2
+ * Capitalizes a string or sentence
3
+ * @param str - The string to capitalize
4
+ * @param fullSentence - If true, capitalizes each word in the sentence. If false, only capitalizes the first character
5
+ * @returns The capitalized string, or empty string if input is falsy
6
+ * @example
7
+ * transform.capitalize('hello world') // 'Hello World'
8
+ * transform.capitalize('hello world', false) // 'Hello world'
9
+ */
10
+ declare const capitalize: (str: string, fullSentence?: boolean) => string;
11
+ /**
12
+ * String transformation utilities for converting strings to different formats and cases
13
+ * @property capitalize - Capitalizes a string or sentence
14
+ * @property camel - Converts to camelCase
15
+ * @property kebab - Converts to kebab-case
16
+ * @property pascal - Converts to PascalCase
17
+ * @property snake - Converts to snake_case
18
+ * @property snakeScream - Converts to SCREAMING_SNAKE_CASE
19
+ * @property reverse - Reverses a string
20
+ * @property slugify - Creates URL-friendly slugs
21
+ * @example
22
+ * transform.capitalize('hello world') // 'Hello World'
23
+ * transform.camel('hello world') // 'helloWorld'
24
+ * transform.kebab('helloWorld') // 'hello-world'
25
+ * transform.pascal('hello-world') // 'HelloWorld'
26
+ * transform.snake('helloWorld') // 'hello_world'
27
+ * transform.snakeScream('hello world') // 'HELLO_WORLD'
28
+ * transform.reverse('hello') // 'olleh'
29
+ * transform.slugify('Hello World!') // 'hello-world'
30
+ */
31
+ declare const transform: {
32
+ capitalize: (str: string, fullSentence?: boolean) => string;
33
+ camel: (str: string) => string;
34
+ kebab: (str: string) => string;
35
+ pascal: (str: string) => string;
36
+ snake: (str: string) => string;
37
+ snakeScream: (str: string) => string;
38
+ reverse: (str: string) => string;
39
+ slugify: (str: string) => string;
40
+ };
41
+ declare const validate: {
42
+ email: (str: string, customRegex?: RegExp) => boolean;
43
+ url: (str: string, customRegex?: RegExp) => boolean;
44
+ phone: (str: string, customRegex?: RegExp) => boolean;
45
+ };
46
+ declare const edit: {
3
47
  trim: (str: string, maxLength?: number, trimChar?: string) => string;
48
+ repeat: (str: string, count: number) => string;
49
+ pad: (str: string, length: number, char?: string, position?: "start" | "end" | "both") => string;
50
+ wrap: (str: string, wrapper: string | {
51
+ start: string;
52
+ end: string;
53
+ }) => string;
54
+ unwrap: (str: string, wrapper: string | {
55
+ start: string;
56
+ end: string;
57
+ }) => string;
4
58
  prefix: (pf: string) => {
59
+ /**
60
+ * Adds the prefix to a string if it doesn't already start with it
61
+ * @param str - The string to add prefix to
62
+ * @returns The string with prefix, or empty string if input is falsy
63
+ */
5
64
  add: (str: string | undefined) => string;
65
+ /**
66
+ * Removes the prefix from a string if it starts with it
67
+ * @param str - The string to remove prefix from
68
+ * @returns The string without prefix, or empty string if input is falsy
69
+ */
6
70
  remove: (str: string | undefined) => string;
7
71
  };
8
72
  suffix: (sf: string) => {
73
+ /**
74
+ * Adds the suffix to a string if it doesn't already end with it
75
+ * @param str - The string to add suffix to
76
+ * @returns The string with suffix, or empty string if input is falsy
77
+ */
9
78
  add: (str: string | undefined) => string;
79
+ /**
80
+ * Removes the suffix from a string if it ends with it
81
+ * @param str - The string to remove suffix from
82
+ * @returns The string without suffix, or empty string if input is falsy
83
+ */
10
84
  remove: (str: string | undefined) => string;
11
85
  };
86
+ initials: (str: string, maxInitials?: number) => string;
87
+ };
88
+ declare const clean: {
89
+ whitespace: (str: string) => string;
90
+ normalizeWhitespace: (str: string) => string;
91
+ html: (str: string) => string;
92
+ escapeHtml: (str: string) => string;
93
+ unescapeHtml: (str: string) => string;
94
+ };
95
+ declare const format: {
96
+ mask: (str: string, visibleChars?: number, maskChar?: string) => string;
97
+ highlight: (str: string, search: string, wrapper?: string | {
98
+ start: string;
99
+ end: string;
100
+ }, caseSensitive?: boolean) => string;
101
+ quote: (type?: "single" | "double" | "backtick") => {
102
+ /**
103
+ * Adds quotes to a string
104
+ * @param str - The string to quote
105
+ * @returns The quoted string
106
+ */
107
+ add: (str: string) => string;
108
+ /**
109
+ * Removes quotes from a string
110
+ * @param str - The string to unquote
111
+ * @returns The unquoted string
112
+ */
113
+ remove: (str: string) => string;
114
+ };
115
+ bracket: (type?: "round" | "square" | "curly" | "angle") => {
116
+ /**
117
+ * Adds brackets to a string
118
+ * @param str - The string to wrap
119
+ * @returns The bracketed string
120
+ */
121
+ add: (str: string) => string;
122
+ /**
123
+ * Removes brackets from a string
124
+ * @param str - The string to unwrap
125
+ * @returns The unbracketted string
126
+ */
127
+ remove: (str: string) => string;
128
+ };
129
+ };
130
+
131
+ declare const st: {
132
+ transform: {
133
+ capitalize: (str: string, fullSentence?: boolean) => string;
134
+ camel: (str: string) => string;
135
+ kebab: (str: string) => string;
136
+ pascal: (str: string) => string;
137
+ snake: (str: string) => string;
138
+ snakeScream: (str: string) => string;
139
+ reverse: (str: string) => string;
140
+ slugify: (str: string) => string;
141
+ };
142
+ validate: {
143
+ email: (str: string, customRegex?: RegExp) => boolean;
144
+ url: (str: string, customRegex?: RegExp) => boolean;
145
+ phone: (str: string, customRegex?: RegExp) => boolean;
146
+ };
147
+ edit: {
148
+ trim: (str: string, maxLength?: number, trimChar?: string) => string;
149
+ repeat: (str: string, count: number) => string;
150
+ pad: (str: string, length: number, char?: string, position?: "start" | "end" | "both") => string;
151
+ wrap: (str: string, wrapper: string | {
152
+ start: string;
153
+ end: string;
154
+ }) => string;
155
+ unwrap: (str: string, wrapper: string | {
156
+ start: string;
157
+ end: string;
158
+ }) => string;
159
+ prefix: (pf: string) => {
160
+ add: (str: string | undefined) => string;
161
+ remove: (str: string | undefined) => string;
162
+ };
163
+ suffix: (sf: string) => {
164
+ add: (str: string | undefined) => string;
165
+ remove: (str: string | undefined) => string;
166
+ };
167
+ initials: (str: string, maxInitials?: number) => string;
168
+ };
169
+ clean: {
170
+ whitespace: (str: string) => string;
171
+ normalizeWhitespace: (str: string) => string;
172
+ html: (str: string) => string;
173
+ escapeHtml: (str: string) => string;
174
+ unescapeHtml: (str: string) => string;
175
+ };
176
+ format: {
177
+ mask: (str: string, visibleChars?: number, maskChar?: string) => string;
178
+ highlight: (str: string, search: string, wrapper?: string | {
179
+ start: string;
180
+ end: string;
181
+ }, caseSensitive?: boolean) => string;
182
+ quote: (type?: "single" | "double" | "backtick") => {
183
+ add: (str: string) => string;
184
+ remove: (str: string) => string;
185
+ };
186
+ bracket: (type?: "round" | "square" | "curly" | "angle") => {
187
+ add: (str: string) => string;
188
+ remove: (str: string) => string;
189
+ };
190
+ };
12
191
  };
13
192
 
14
- export { st as default };
193
+ export { capitalize, clean, st as default, edit, format, transform, validate };
package/dist/index.js CHANGED
@@ -20,37 +20,322 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- default: () => index_default
23
+ capitalize: () => capitalize,
24
+ clean: () => clean,
25
+ default: () => index_default,
26
+ edit: () => edit,
27
+ format: () => format,
28
+ transform: () => transform,
29
+ validate: () => validate
24
30
  });
25
31
  module.exports = __toCommonJS(index_exports);
26
32
 
33
+ // src/regex.ts
34
+ var REGEX_CAMEL_CASE_SEPARATOR = /[^a-zA-Z0-9]+(.)/g;
35
+ var REGEX_CAMEL_CASE_FIRST_CHAR = /^[A-Z]/;
36
+ var REGEX_SNAKE_CASE_CAPITAL = /([A-Z])/g;
37
+ var REGEX_SNAKE_CASE_NON_ALPHANUMERIC = /[^a-zA-Z0-9]+/g;
38
+ var REGEX_SNAKE_CASE_LEADING_UNDERSCORE = /^_/;
39
+ var REGEX_KEBAB_CASE_CAPITAL = /([A-Z])/g;
40
+ var REGEX_KEBAB_CASE_NON_ALPHANUMERIC = /[^a-zA-Z0-9]+/g;
41
+ var REGEX_KEBAB_CASE_LEADING_DASH = /^-/;
42
+ var REGEX_PASCAL_CASE_SEPARATOR = /[^a-zA-Z0-9]+(.)/g;
43
+ var REGEX_PASCAL_CASE_FIRST_CHAR = /^[a-z]/;
44
+ var REGEX_SLUGIFY_NON_WORD = /[^\w\s-]/g;
45
+ var REGEX_SLUGIFY_WHITESPACE = /[\s_-]+/g;
46
+ var REGEX_SLUGIFY_TRIM_DASHES = /^-+|-+$/g;
47
+ var REGEX_EMAIL_DEFAULT = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
48
+ var REGEX_URL_DEFAULT = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
49
+ var REGEX_PHONE_DEFAULT = /^[\+]?[(]?[0-9]{1,4}[)]?[-\s\.]?[(]?[0-9]{1,4}[)]?[-\s\.]?[0-9]{1,9}$/;
50
+ var REGEX_INITIALS_WHITESPACE = /\s+/;
51
+ var REGEX_INITIALS_ALPHA = /[A-Z]/;
52
+ var REGEX_WHITESPACE_ALL = /\s+/g;
53
+ var REGEX_HTML_TAGS = /<[^>]*>/g;
54
+ var REGEX_HTML_ESCAPE_CHARS = /[&<>"']/g;
55
+ var REGEX_HTML_UNESCAPE_ENTITIES = /&(?:amp|lt|gt|quot|#39);/g;
56
+ var REGEX_HIGHLIGHT_ESCAPE = /[.*+?^${}()|[\]\\]/g;
57
+
27
58
  // src/functions.ts
28
59
  var capitalize = (str, fullSentence = true) => {
29
60
  if (!str) return "";
30
61
  if (fullSentence) {
31
- str.toLowerCase().split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ") || str;
62
+ return str.toLowerCase().split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ") || str;
32
63
  } else {
33
64
  return str.charAt(0).toUpperCase() + str.slice(1);
34
65
  }
35
66
  };
67
+ var camelCase = (str) => {
68
+ if (!str) return "";
69
+ return str.replace(REGEX_CAMEL_CASE_SEPARATOR, (_, chr) => chr.toUpperCase()).replace(REGEX_CAMEL_CASE_FIRST_CHAR, (chr) => chr.toLowerCase());
70
+ };
71
+ var kebabCase = (str) => {
72
+ if (!str) return "";
73
+ return str.replace(REGEX_KEBAB_CASE_CAPITAL, "-$1").replace(REGEX_KEBAB_CASE_NON_ALPHANUMERIC, "-").replace(REGEX_KEBAB_CASE_LEADING_DASH, "").toLowerCase();
74
+ };
75
+ var pascalCase = (str) => {
76
+ if (!str) return "";
77
+ return str.replace(REGEX_PASCAL_CASE_SEPARATOR, (_, chr) => chr.toUpperCase()).replace(REGEX_PASCAL_CASE_FIRST_CHAR, (chr) => chr.toUpperCase());
78
+ };
79
+ var snakeCase = (str) => {
80
+ if (!str) return "";
81
+ return str.replace(REGEX_SNAKE_CASE_CAPITAL, "_$1").replace(REGEX_SNAKE_CASE_NON_ALPHANUMERIC, "_").replace(REGEX_SNAKE_CASE_LEADING_UNDERSCORE, "").toLowerCase();
82
+ };
83
+ var snakeScreamCase = (str) => {
84
+ if (!str) return "";
85
+ return str.replace(REGEX_SNAKE_CASE_CAPITAL, "_$1").replace(REGEX_SNAKE_CASE_NON_ALPHANUMERIC, "_").replace(REGEX_SNAKE_CASE_LEADING_UNDERSCORE, "").toUpperCase();
86
+ };
87
+ var reverse = (str) => {
88
+ if (!str) return "";
89
+ return str.split("").reverse().join("");
90
+ };
91
+ var slugify = (str) => {
92
+ if (!str) return "";
93
+ return str.toLowerCase().trim().replace(REGEX_SLUGIFY_NON_WORD, "").replace(REGEX_SLUGIFY_WHITESPACE, "-").replace(REGEX_SLUGIFY_TRIM_DASHES, "");
94
+ };
95
+ var transform = {
96
+ capitalize,
97
+ camel: camelCase,
98
+ kebab: kebabCase,
99
+ pascal: pascalCase,
100
+ snake: snakeCase,
101
+ snakeScream: snakeScreamCase,
102
+ reverse,
103
+ slugify
104
+ };
105
+ var isEmail = (str, customRegex) => {
106
+ if (!str) return false;
107
+ const regex = customRegex || REGEX_EMAIL_DEFAULT;
108
+ return regex.test(str);
109
+ };
110
+ var isUrl = (str, customRegex) => {
111
+ if (!str) return false;
112
+ const regex = customRegex || REGEX_URL_DEFAULT;
113
+ return regex.test(str);
114
+ };
115
+ var isPhone = (str, customRegex) => {
116
+ if (!str) return false;
117
+ const regex = customRegex || REGEX_PHONE_DEFAULT;
118
+ return regex.test(str);
119
+ };
120
+ var validate = {
121
+ email: isEmail,
122
+ url: isUrl,
123
+ phone: isPhone
124
+ };
36
125
  var trim = (str, maxLength, trimChar = "...") => {
37
126
  if (!maxLength) return str;
38
127
  return str?.length > maxLength ? `${str?.slice(0, maxLength)}${trimChar}` : str;
39
128
  };
129
+ var repeat = (str, count) => {
130
+ if (!str || count < 1) return "";
131
+ return str.repeat(count);
132
+ };
133
+ var pad = (str, length, char = " ", position = "both") => {
134
+ if (!str || str.length >= length) return str;
135
+ const padLength = length - str.length;
136
+ if (position === "start") {
137
+ return char.repeat(padLength) + str;
138
+ } else if (position === "end") {
139
+ return str + char.repeat(padLength);
140
+ } else {
141
+ const leftPad = Math.floor(padLength / 2);
142
+ const rightPad = padLength - leftPad;
143
+ return char.repeat(leftPad) + str + char.repeat(rightPad);
144
+ }
145
+ };
146
+ var wrap = (str, wrapper) => {
147
+ if (!str) return "";
148
+ if (typeof wrapper === "string") {
149
+ return `${wrapper}${str}${wrapper}`;
150
+ }
151
+ return `${wrapper.start}${str}${wrapper.end}`;
152
+ };
153
+ var unwrap = (str, wrapper) => {
154
+ if (!str) return "";
155
+ if (typeof wrapper === "string") {
156
+ if (str.startsWith(wrapper) && str.endsWith(wrapper)) {
157
+ return str.slice(wrapper.length, -wrapper.length);
158
+ }
159
+ return str;
160
+ }
161
+ if (str.startsWith(wrapper.start) && str.endsWith(wrapper.end)) {
162
+ return str.slice(wrapper.start.length, -wrapper.end.length);
163
+ }
164
+ return str;
165
+ };
40
166
  var prefix = (pf) => ({
167
+ /**
168
+ * Adds the prefix to a string if it doesn't already start with it
169
+ * @param str - The string to add prefix to
170
+ * @returns The string with prefix, or empty string if input is falsy
171
+ */
41
172
  add: (str) => !str ? "" : str?.startsWith(pf) ? str : `${pf}${str}`,
42
- remove: (str) => !str ? "" : str?.startsWith(pf) ? str?.slice(1) : str
173
+ /**
174
+ * Removes the prefix from a string if it starts with it
175
+ * @param str - The string to remove prefix from
176
+ * @returns The string without prefix, or empty string if input is falsy
177
+ */
178
+ remove: (str) => !str ? "" : str?.startsWith(pf) ? str?.slice(pf.length) : str
43
179
  });
44
180
  var suffix = (sf) => ({
181
+ /**
182
+ * Adds the suffix to a string if it doesn't already end with it
183
+ * @param str - The string to add suffix to
184
+ * @returns The string with suffix, or empty string if input is falsy
185
+ */
45
186
  add: (str) => !str ? "" : str?.endsWith(sf) ? str : `${str}${sf}`,
46
- remove: (str) => !str ? "" : str?.endsWith(sf) ? str?.slice(0, -1) : str
187
+ /**
188
+ * Removes the suffix from a string if it ends with it
189
+ * @param str - The string to remove suffix from
190
+ * @returns The string without suffix, or empty string if input is falsy
191
+ */
192
+ remove: (str) => !str ? "" : str?.endsWith(sf) ? str?.slice(0, -sf.length) : str
47
193
  });
194
+ var initials = (str, maxInitials) => {
195
+ if (!str) return "";
196
+ const words = str.trim().split(REGEX_INITIALS_WHITESPACE);
197
+ const initialsArray = words.map((word) => word.charAt(0).toUpperCase()).filter((char) => REGEX_INITIALS_ALPHA.test(char));
198
+ if (maxInitials) {
199
+ return initialsArray.slice(0, maxInitials).join("");
200
+ }
201
+ return initialsArray.join("");
202
+ };
203
+ var edit = {
204
+ trim,
205
+ repeat,
206
+ pad,
207
+ wrap,
208
+ unwrap,
209
+ prefix,
210
+ suffix,
211
+ initials
212
+ };
213
+ var removeWhitespace = (str) => {
214
+ if (!str) return "";
215
+ return str.replace(REGEX_WHITESPACE_ALL, "");
216
+ };
217
+ var normalizeWhitespace = (str) => {
218
+ if (!str) return "";
219
+ return str.replace(REGEX_WHITESPACE_ALL, " ").trim();
220
+ };
221
+ var stripHtml = (str) => {
222
+ if (!str) return "";
223
+ return str.replace(REGEX_HTML_TAGS, "");
224
+ };
225
+ var escapeHtml = (str) => {
226
+ if (!str) return "";
227
+ const htmlEscapes = {
228
+ "&": "&amp;",
229
+ "<": "&lt;",
230
+ ">": "&gt;",
231
+ '"': "&quot;",
232
+ "'": "&#39;"
233
+ };
234
+ return str.replace(REGEX_HTML_ESCAPE_CHARS, (char) => htmlEscapes[char]);
235
+ };
236
+ var unescapeHtml = (str) => {
237
+ if (!str) return "";
238
+ const htmlUnescapes = {
239
+ "&amp;": "&",
240
+ "&lt;": "<",
241
+ "&gt;": ">",
242
+ "&quot;": '"',
243
+ "&#39;": "'"
244
+ };
245
+ return str.replace(
246
+ REGEX_HTML_UNESCAPE_ENTITIES,
247
+ (entity) => htmlUnescapes[entity]
248
+ );
249
+ };
250
+ var clean = {
251
+ whitespace: removeWhitespace,
252
+ normalizeWhitespace,
253
+ html: stripHtml,
254
+ escapeHtml,
255
+ unescapeHtml
256
+ };
257
+ var mask = (str, visibleChars = 4, maskChar = "*") => {
258
+ if (!str) return "";
259
+ if (str.length <= visibleChars) return str;
260
+ const maskLength = str.length - visibleChars;
261
+ return maskChar.repeat(maskLength) + str.slice(-visibleChars);
262
+ };
263
+ var highlight = (str, search, wrapper = {
264
+ start: "<mark>",
265
+ end: "</mark>"
266
+ }, caseSensitive = false) => {
267
+ if (!str || !search) return str;
268
+ const wrapStart = typeof wrapper === "string" ? wrapper : wrapper.start;
269
+ const wrapEnd = typeof wrapper === "string" ? wrapper : wrapper.end;
270
+ const flags = caseSensitive ? "g" : "gi";
271
+ const regex = new RegExp(
272
+ search.replace(REGEX_HIGHLIGHT_ESCAPE, "\\$&"),
273
+ flags
274
+ );
275
+ return str.replace(regex, (match) => `${wrapStart}${match}${wrapEnd}`);
276
+ };
277
+ var quote = (type = "double") => {
278
+ const quoteChar = type === "single" ? "'" : type === "backtick" ? "`" : '"';
279
+ return {
280
+ /**
281
+ * Adds quotes to a string
282
+ * @param str - The string to quote
283
+ * @returns The quoted string
284
+ */
285
+ add: (str) => wrap(str, quoteChar),
286
+ /**
287
+ * Removes quotes from a string
288
+ * @param str - The string to unquote
289
+ * @returns The unquoted string
290
+ */
291
+ remove: (str) => unwrap(str, quoteChar)
292
+ };
293
+ };
294
+ var bracket = (type = "round") => {
295
+ const brackets = {
296
+ round: { start: "(", end: ")" },
297
+ square: { start: "[", end: "]" },
298
+ curly: { start: "{", end: "}" },
299
+ angle: { start: "<", end: ">" }
300
+ };
301
+ const bracketPair = brackets[type];
302
+ return {
303
+ /**
304
+ * Adds brackets to a string
305
+ * @param str - The string to wrap
306
+ * @returns The bracketed string
307
+ */
308
+ add: (str) => wrap(str, bracketPair),
309
+ /**
310
+ * Removes brackets from a string
311
+ * @param str - The string to unwrap
312
+ * @returns The unbracketted string
313
+ */
314
+ remove: (str) => unwrap(str, bracketPair)
315
+ };
316
+ };
317
+ var format = {
318
+ mask,
319
+ highlight,
320
+ quote,
321
+ bracket
322
+ };
48
323
 
49
324
  // src/index.ts
50
325
  var st = {
51
- capitalize,
52
- trim,
53
- prefix,
54
- suffix
326
+ transform,
327
+ validate,
328
+ edit,
329
+ clean,
330
+ format
55
331
  };
56
332
  var index_default = st;
333
+ // Annotate the CommonJS export names for ESM import in node:
334
+ 0 && (module.exports = {
335
+ capitalize,
336
+ clean,
337
+ edit,
338
+ format,
339
+ transform,
340
+ validate
341
+ });