@pixpilot/string 2.3.0 → 2.5.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.
package/README.md CHANGED
@@ -55,6 +55,10 @@ if (isString(value)) {
55
55
  - `isEmail(str: string): boolean` - Check if a string is a valid email address
56
56
  - `isUrl(str: string): boolean` - Check if a string is a valid URL
57
57
 
58
+ ### GUID Helpers
59
+
60
+ - `isGuidString(str: string): boolean` - Check if a string matches the canonical GUID format
61
+
58
62
  ### Manipulation Functions
59
63
 
60
64
  - `truncate(str: string, maxLength: number, ellipsis?: string): string` - Truncate a string
@@ -1 +1,25 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));exports.__toESM=s;
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+
25
+ exports.__toESM = __toESM;
package/dist/case.cjs CHANGED
@@ -1 +1,91 @@
1
- const e=require(`./_virtual/rolldown_runtime.cjs`);let t=require(`change-case`);t=e.__toESM(t);function n(e){return(0,t.camelCase)(e)}function r(e){return(0,t.capitalCase)(e)}function i(e){return(0,t.constantCase)(e)}function a(e){return(0,t.dotCase)(e)}function o(e){return(0,t.kebabCase)(e)}function s(e){return(0,t.noCase)(e)}function c(e){return(0,t.pascalCase)(e)}function l(e){return(0,t.pascalSnakeCase)(e)}function u(e){return(0,t.pathCase)(e)}function d(e){return(0,t.sentenceCase)(e)}function f(e){return(0,t.snakeCase)(e)}function p(e){return(0,t.trainCase)(e)}exports.toCamelCase=n,exports.toCapitalCase=r,exports.toConstantCase=i,exports.toDotCase=a,exports.toKebabCase=o,exports.toNoCase=s,exports.toPascalCase=c,exports.toPascalSnakeCase=l,exports.toPathCase=u,exports.toSentenceCase=d,exports.toSnakeCase=f,exports.toTrainCase=p;
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let change_case = require("change-case");
3
+ change_case = require_rolldown_runtime.__toESM(change_case);
4
+
5
+ //#region src/case.ts
6
+ /**
7
+ * Convert a string to camel case (`fooBar`).
8
+ */
9
+ function toCamelCase(str) {
10
+ return (0, change_case.camelCase)(str);
11
+ }
12
+ /**
13
+ * Convert a string to capital case (`Foo Bar`).
14
+ */
15
+ function toCapitalCase(str) {
16
+ return (0, change_case.capitalCase)(str);
17
+ }
18
+ /**
19
+ * Convert a string to constant case (`CONSTANT_CASE`).
20
+ */
21
+ function toConstantCase(str) {
22
+ return (0, change_case.constantCase)(str);
23
+ }
24
+ /**
25
+ * Convert a string to dot case (`dot.case`).
26
+ */
27
+ function toDotCase(str) {
28
+ return (0, change_case.dotCase)(str);
29
+ }
30
+ /**
31
+ * Convert a string to kebab case (`kebab-case`).
32
+ */
33
+ function toKebabCase(str) {
34
+ return (0, change_case.kebabCase)(str);
35
+ }
36
+ /**
37
+ * Convert a string to no case (`no case`).
38
+ */
39
+ function toNoCase(str) {
40
+ return (0, change_case.noCase)(str);
41
+ }
42
+ /**
43
+ * Convert a string to pascal case (`PascalCase`).
44
+ */
45
+ function toPascalCase(str) {
46
+ return (0, change_case.pascalCase)(str);
47
+ }
48
+ /**
49
+ * Convert a string to pascal snake case (`Pascal_Snake_Case`).
50
+ */
51
+ function toPascalSnakeCase(str) {
52
+ return (0, change_case.pascalSnakeCase)(str);
53
+ }
54
+ /**
55
+ * Convert a string to path case (`path/case`).
56
+ */
57
+ function toPathCase(str) {
58
+ return (0, change_case.pathCase)(str);
59
+ }
60
+ /**
61
+ * Convert a string to sentence case (`Sentence case`).
62
+ */
63
+ function toSentenceCase(str) {
64
+ return (0, change_case.sentenceCase)(str);
65
+ }
66
+ /**
67
+ * Convert a string to snake case (`snake_case`).
68
+ */
69
+ function toSnakeCase(str) {
70
+ return (0, change_case.snakeCase)(str);
71
+ }
72
+ /**
73
+ * Convert a string to train case (`Train-Case`).
74
+ */
75
+ function toTrainCase(str) {
76
+ return (0, change_case.trainCase)(str);
77
+ }
78
+
79
+ //#endregion
80
+ exports.toCamelCase = toCamelCase;
81
+ exports.toCapitalCase = toCapitalCase;
82
+ exports.toConstantCase = toConstantCase;
83
+ exports.toDotCase = toDotCase;
84
+ exports.toKebabCase = toKebabCase;
85
+ exports.toNoCase = toNoCase;
86
+ exports.toPascalCase = toPascalCase;
87
+ exports.toPascalSnakeCase = toPascalSnakeCase;
88
+ exports.toPathCase = toPathCase;
89
+ exports.toSentenceCase = toSentenceCase;
90
+ exports.toSnakeCase = toSnakeCase;
91
+ exports.toTrainCase = toTrainCase;
package/dist/case.js CHANGED
@@ -1 +1,78 @@
1
- import{camelCase as e,capitalCase as t,constantCase as n,dotCase as r,kebabCase as i,noCase as a,pascalCase as o,pascalSnakeCase as s,pathCase as c,sentenceCase as l,snakeCase as u,trainCase as d}from"change-case";function f(t){return e(t)}function p(e){return t(e)}function m(e){return n(e)}function h(e){return r(e)}function g(e){return i(e)}function _(e){return a(e)}function v(e){return o(e)}function y(e){return s(e)}function b(e){return c(e)}function x(e){return l(e)}function S(e){return u(e)}function C(e){return d(e)}export{f as toCamelCase,p as toCapitalCase,m as toConstantCase,h as toDotCase,g as toKebabCase,_ as toNoCase,v as toPascalCase,y as toPascalSnakeCase,b as toPathCase,x as toSentenceCase,S as toSnakeCase,C as toTrainCase};
1
+ import { camelCase, capitalCase, constantCase, dotCase, kebabCase, noCase, pascalCase, pascalSnakeCase, pathCase, sentenceCase, snakeCase, trainCase } from "change-case";
2
+
3
+ //#region src/case.ts
4
+ /**
5
+ * Convert a string to camel case (`fooBar`).
6
+ */
7
+ function toCamelCase(str) {
8
+ return camelCase(str);
9
+ }
10
+ /**
11
+ * Convert a string to capital case (`Foo Bar`).
12
+ */
13
+ function toCapitalCase(str) {
14
+ return capitalCase(str);
15
+ }
16
+ /**
17
+ * Convert a string to constant case (`CONSTANT_CASE`).
18
+ */
19
+ function toConstantCase(str) {
20
+ return constantCase(str);
21
+ }
22
+ /**
23
+ * Convert a string to dot case (`dot.case`).
24
+ */
25
+ function toDotCase(str) {
26
+ return dotCase(str);
27
+ }
28
+ /**
29
+ * Convert a string to kebab case (`kebab-case`).
30
+ */
31
+ function toKebabCase(str) {
32
+ return kebabCase(str);
33
+ }
34
+ /**
35
+ * Convert a string to no case (`no case`).
36
+ */
37
+ function toNoCase(str) {
38
+ return noCase(str);
39
+ }
40
+ /**
41
+ * Convert a string to pascal case (`PascalCase`).
42
+ */
43
+ function toPascalCase(str) {
44
+ return pascalCase(str);
45
+ }
46
+ /**
47
+ * Convert a string to pascal snake case (`Pascal_Snake_Case`).
48
+ */
49
+ function toPascalSnakeCase(str) {
50
+ return pascalSnakeCase(str);
51
+ }
52
+ /**
53
+ * Convert a string to path case (`path/case`).
54
+ */
55
+ function toPathCase(str) {
56
+ return pathCase(str);
57
+ }
58
+ /**
59
+ * Convert a string to sentence case (`Sentence case`).
60
+ */
61
+ function toSentenceCase(str) {
62
+ return sentenceCase(str);
63
+ }
64
+ /**
65
+ * Convert a string to snake case (`snake_case`).
66
+ */
67
+ function toSnakeCase(str) {
68
+ return snakeCase(str);
69
+ }
70
+ /**
71
+ * Convert a string to train case (`Train-Case`).
72
+ */
73
+ function toTrainCase(str) {
74
+ return trainCase(str);
75
+ }
76
+
77
+ //#endregion
78
+ export { toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase };
package/dist/index.cjs CHANGED
@@ -1 +1,33 @@
1
- const e=require(`./case.cjs`),t=require(`./manipulation.cjs`),n=require(`./type-guards.cjs`),r=require(`./validation.cjs`);exports.capitalize=t.capitalize,exports.capitalizeFirst=t.capitalizeFirst,exports.countOccurrences=t.countOccurrences,exports.isAlphanumeric=r.isAlphanumeric,exports.isEmail=r.isEmail,exports.isEmpty=r.isEmpty,exports.isString=n.isString,exports.isUrl=r.isUrl,exports.normalizeSpaces=t.normalizeSpaces,exports.padEnd=t.padEnd,exports.padStart=t.padStart,exports.removeWhitespace=t.removeWhitespace,exports.repeat=t.repeat,exports.reverse=t.reverse,exports.toCamelCase=e.toCamelCase,exports.toCapitalCase=e.toCapitalCase,exports.toConstantCase=e.toConstantCase,exports.toDotCase=e.toDotCase,exports.toKebabCase=e.toKebabCase,exports.toNoCase=e.toNoCase,exports.toPascalCase=e.toPascalCase,exports.toPascalSnakeCase=e.toPascalSnakeCase,exports.toPathCase=e.toPathCase,exports.toSentenceCase=e.toSentenceCase,exports.toSnakeCase=e.toSnakeCase,exports.toTrainCase=e.toTrainCase,exports.truncate=t.truncate,exports.words=t.words;
1
+ const require_case = require('./case.cjs');
2
+ const require_manipulation = require('./manipulation.cjs');
3
+ const require_type_guards = require('./type-guards.cjs');
4
+ const require_validation = require('./validation.cjs');
5
+
6
+ exports.capitalize = require_manipulation.capitalize;
7
+ exports.capitalizeFirst = require_manipulation.capitalizeFirst;
8
+ exports.countOccurrences = require_manipulation.countOccurrences;
9
+ exports.isAlphanumeric = require_validation.isAlphanumeric;
10
+ exports.isEmail = require_validation.isEmail;
11
+ exports.isEmpty = require_validation.isEmpty;
12
+ exports.isString = require_type_guards.isString;
13
+ exports.isUrl = require_validation.isUrl;
14
+ exports.normalizeSpaces = require_manipulation.normalizeSpaces;
15
+ exports.padEnd = require_manipulation.padEnd;
16
+ exports.padStart = require_manipulation.padStart;
17
+ exports.removeWhitespace = require_manipulation.removeWhitespace;
18
+ exports.repeat = require_manipulation.repeat;
19
+ exports.reverse = require_manipulation.reverse;
20
+ exports.toCamelCase = require_case.toCamelCase;
21
+ exports.toCapitalCase = require_case.toCapitalCase;
22
+ exports.toConstantCase = require_case.toConstantCase;
23
+ exports.toDotCase = require_case.toDotCase;
24
+ exports.toKebabCase = require_case.toKebabCase;
25
+ exports.toNoCase = require_case.toNoCase;
26
+ exports.toPascalCase = require_case.toPascalCase;
27
+ exports.toPascalSnakeCase = require_case.toPascalSnakeCase;
28
+ exports.toPathCase = require_case.toPathCase;
29
+ exports.toSentenceCase = require_case.toSentenceCase;
30
+ exports.toSnakeCase = require_case.toSnakeCase;
31
+ exports.toTrainCase = require_case.toTrainCase;
32
+ exports.truncate = require_manipulation.truncate;
33
+ exports.words = require_manipulation.words;
package/dist/index.js CHANGED
@@ -1 +1,6 @@
1
- import{toCamelCase as e,toCapitalCase as t,toConstantCase as n,toDotCase as r,toKebabCase as i,toNoCase as a,toPascalCase as o,toPascalSnakeCase as s,toPathCase as c,toSentenceCase as l,toSnakeCase as u,toTrainCase as d}from"./case.js";import{capitalize as f,capitalizeFirst as p,countOccurrences as m,normalizeSpaces as h,padEnd as g,padStart as _,removeWhitespace as v,repeat as y,reverse as b,truncate as x,words as S}from"./manipulation.js";import{isString as C}from"./type-guards.js";import{isAlphanumeric as w,isEmail as T,isEmpty as E,isUrl as D}from"./validation.js";export{f as capitalize,p as capitalizeFirst,m as countOccurrences,w as isAlphanumeric,T as isEmail,E as isEmpty,C as isString,D as isUrl,h as normalizeSpaces,g as padEnd,_ as padStart,v as removeWhitespace,y as repeat,b as reverse,e as toCamelCase,t as toCapitalCase,n as toConstantCase,r as toDotCase,i as toKebabCase,a as toNoCase,o as toPascalCase,s as toPascalSnakeCase,c as toPathCase,l as toSentenceCase,u as toSnakeCase,d as toTrainCase,x as truncate,S as words};
1
+ import { toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase } from "./case.js";
2
+ import { capitalize, capitalizeFirst, countOccurrences, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, truncate, words } from "./manipulation.js";
3
+ import { isString } from "./type-guards.js";
4
+ import { isAlphanumeric, isEmail, isEmpty, isUrl } from "./validation.js";
5
+
6
+ export { capitalize, capitalizeFirst, countOccurrences, isAlphanumeric, isEmail, isEmpty, isString, isUrl, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toNoCase, toPascalCase, toPascalSnakeCase, toPathCase, toSentenceCase, toSnakeCase, toTrainCase, truncate, words };
@@ -1 +1,206 @@
1
- function e(e,t,n=`...`){return e.length<=t?e:e.slice(0,t)+n}function t(e){return e.length===0?e:e.charAt(0).toUpperCase()+e.slice(1)}function n(e){return e.length===0?e:e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()}function r(e){return e.split(``).reverse().join(``)}function i(e){return e.replace(/\s+/gu,``)}function a(e){return e.replace(/\s+/gu,` `).trim()}function o(e,t,n=` `){return e.padStart(t,n)}function s(e,t,n=` `){return e.padEnd(t,n)}function c(e,t){return e.repeat(t)}function l(e,t,n=!0){if(t.length===0)return 0;let r=n?e:e.toLowerCase(),i=n?t:t.toLowerCase(),a=0,o=0;for(;;){let e=r.indexOf(i,o);if(e===-1)break;a++,o=e+1}return a}function u(e){return e.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu)||[]}exports.capitalize=t,exports.capitalizeFirst=n,exports.countOccurrences=l,exports.normalizeSpaces=a,exports.padEnd=s,exports.padStart=o,exports.removeWhitespace=i,exports.repeat=c,exports.reverse=r,exports.truncate=e,exports.words=u;
1
+
2
+ //#region src/manipulation.ts
3
+ /**
4
+ * Truncate a string to a specified length, adding an ellipsis if truncated.
5
+ *
6
+ * @param str - The string to truncate
7
+ * @param maxLength - The maximum length of the string
8
+ * @param ellipsis - The ellipsis to append (default: '...')
9
+ * @returns The truncated string
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * truncate('Hello World', 5); // 'Hello...'
14
+ * truncate('Hello', 10); // 'Hello'
15
+ * truncate('Hello World', 5, '…'); // 'Hello…'
16
+ * ```
17
+ */
18
+ function truncate(str, maxLength, ellipsis = "...") {
19
+ if (str.length <= maxLength) return str;
20
+ return str.slice(0, maxLength) + ellipsis;
21
+ }
22
+ /**
23
+ * Capitalize the first letter of a string.
24
+ *
25
+ * @param str - The string to capitalize
26
+ * @returns The capitalized string
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * capitalize('hello'); // 'Hello'
31
+ * capitalize('WORLD'); // 'WORLD'
32
+ * capitalize(''); // ''
33
+ * ```
34
+ */
35
+ function capitalize(str) {
36
+ if (str.length === 0) return str;
37
+ return str.charAt(0).toUpperCase() + str.slice(1);
38
+ }
39
+ /**
40
+ * Capitalize the first letter of a string and lowercase the rest.
41
+ *
42
+ * @param str - The string to capitalize
43
+ * @returns The capitalized string
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * capitalizeFirst('hello'); // 'Hello'
48
+ * capitalizeFirst('WORLD'); // 'World'
49
+ * capitalizeFirst('hELLO wORLD'); // 'Hello world'
50
+ * ```
51
+ */
52
+ function capitalizeFirst(str) {
53
+ if (str.length === 0) return str;
54
+ return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
55
+ }
56
+ /**
57
+ * Reverse a string.
58
+ *
59
+ * @param str - The string to reverse
60
+ * @returns The reversed string
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * reverse('hello'); // 'olleh'
65
+ * reverse('abc123'); // '321cba'
66
+ * ```
67
+ */
68
+ function reverse(str) {
69
+ return str.split("").reverse().join("");
70
+ }
71
+ /**
72
+ * Remove all whitespace from a string.
73
+ *
74
+ * @param str - The string to process
75
+ * @returns The string without whitespace
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * removeWhitespace('hello world'); // 'helloworld'
80
+ * removeWhitespace(' a b c '); // 'abc'
81
+ * ```
82
+ */
83
+ function removeWhitespace(str) {
84
+ return str.replace(/\s+/gu, "");
85
+ }
86
+ /**
87
+ * Replace multiple consecutive spaces with a single space.
88
+ *
89
+ * @param str - The string to process
90
+ * @returns The string with normalized spaces
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * normalizeSpaces('hello world'); // 'hello world'
95
+ * normalizeSpaces('a b c'); // 'a b c'
96
+ * ```
97
+ */
98
+ function normalizeSpaces(str) {
99
+ return str.replace(/\s+/gu, " ").trim();
100
+ }
101
+ /**
102
+ * Pad a string to a specified length from the start.
103
+ *
104
+ * @param str - The string to pad
105
+ * @param targetLength - The target length
106
+ * @param padString - The string to pad with (default: ' ')
107
+ * @returns The padded string
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * padStart('5', 3, '0'); // '005'
112
+ * padStart('hello', 10); // ' hello'
113
+ * ```
114
+ */
115
+ function padStart(str, targetLength, padString = " ") {
116
+ return str.padStart(targetLength, padString);
117
+ }
118
+ /**
119
+ * Pad a string to a specified length from the end.
120
+ *
121
+ * @param str - The string to pad
122
+ * @param targetLength - The target length
123
+ * @param padString - The string to pad with (default: ' ')
124
+ * @returns The padded string
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * padEnd('5', 3, '0'); // '500'
129
+ * padEnd('hello', 10); // 'hello '
130
+ * ```
131
+ */
132
+ function padEnd(str, targetLength, padString = " ") {
133
+ return str.padEnd(targetLength, padString);
134
+ }
135
+ /**
136
+ * Repeat a string a specified number of times.
137
+ *
138
+ * @param str - The string to repeat
139
+ * @param count - The number of times to repeat
140
+ * @returns The repeated string
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * repeat('ha', 3); // 'hahaha'
145
+ * repeat('*', 5); // '*****'
146
+ * ```
147
+ */
148
+ function repeat(str, count) {
149
+ return str.repeat(count);
150
+ }
151
+ /**
152
+ * Count the occurrences of a substring in a string.
153
+ *
154
+ * @param str - The string to search in
155
+ * @param searchValue - The substring to count
156
+ * @param caseSensitive - Whether the search is case-sensitive (default: true)
157
+ * @returns The number of occurrences
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * countOccurrences('hello world hello', 'hello'); // 2
162
+ * countOccurrences('Hello HELLO hello', 'hello', false); // 3
163
+ * ```
164
+ */
165
+ function countOccurrences(str, searchValue, caseSensitive = true) {
166
+ if (searchValue.length === 0) return 0;
167
+ const source = caseSensitive ? str : str.toLowerCase();
168
+ const search = caseSensitive ? searchValue : searchValue.toLowerCase();
169
+ let count = 0;
170
+ let position = 0;
171
+ while (true) {
172
+ const index = source.indexOf(search, position);
173
+ if (index === -1) break;
174
+ count++;
175
+ position = index + 1;
176
+ }
177
+ return count;
178
+ }
179
+ /**
180
+ * Extract words from a string.
181
+ *
182
+ * @param str - The string to extract words from
183
+ * @returns An array of words
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * words('hello world'); // ['hello', 'world']
188
+ * words('one-two_three'); // ['one', 'two', 'three']
189
+ * ```
190
+ */
191
+ function words(str) {
192
+ return str.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu) || [];
193
+ }
194
+
195
+ //#endregion
196
+ exports.capitalize = capitalize;
197
+ exports.capitalizeFirst = capitalizeFirst;
198
+ exports.countOccurrences = countOccurrences;
199
+ exports.normalizeSpaces = normalizeSpaces;
200
+ exports.padEnd = padEnd;
201
+ exports.padStart = padStart;
202
+ exports.removeWhitespace = removeWhitespace;
203
+ exports.repeat = repeat;
204
+ exports.reverse = reverse;
205
+ exports.truncate = truncate;
206
+ exports.words = words;
@@ -1 +1,195 @@
1
- function e(e,t,n=`...`){return e.length<=t?e:e.slice(0,t)+n}function t(e){return e.length===0?e:e.charAt(0).toUpperCase()+e.slice(1)}function n(e){return e.length===0?e:e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()}function r(e){return e.split(``).reverse().join(``)}function i(e){return e.replace(/\s+/gu,``)}function a(e){return e.replace(/\s+/gu,` `).trim()}function o(e,t,n=` `){return e.padStart(t,n)}function s(e,t,n=` `){return e.padEnd(t,n)}function c(e,t){return e.repeat(t)}function l(e,t,n=!0){if(t.length===0)return 0;let r=n?e:e.toLowerCase(),i=n?t:t.toLowerCase(),a=0,o=0;for(;;){let e=r.indexOf(i,o);if(e===-1)break;a++,o=e+1}return a}function u(e){return e.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu)||[]}export{t as capitalize,n as capitalizeFirst,l as countOccurrences,a as normalizeSpaces,s as padEnd,o as padStart,i as removeWhitespace,c as repeat,r as reverse,e as truncate,u as words};
1
+ //#region src/manipulation.ts
2
+ /**
3
+ * Truncate a string to a specified length, adding an ellipsis if truncated.
4
+ *
5
+ * @param str - The string to truncate
6
+ * @param maxLength - The maximum length of the string
7
+ * @param ellipsis - The ellipsis to append (default: '...')
8
+ * @returns The truncated string
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * truncate('Hello World', 5); // 'Hello...'
13
+ * truncate('Hello', 10); // 'Hello'
14
+ * truncate('Hello World', 5, '…'); // 'Hello…'
15
+ * ```
16
+ */
17
+ function truncate(str, maxLength, ellipsis = "...") {
18
+ if (str.length <= maxLength) return str;
19
+ return str.slice(0, maxLength) + ellipsis;
20
+ }
21
+ /**
22
+ * Capitalize the first letter of a string.
23
+ *
24
+ * @param str - The string to capitalize
25
+ * @returns The capitalized string
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * capitalize('hello'); // 'Hello'
30
+ * capitalize('WORLD'); // 'WORLD'
31
+ * capitalize(''); // ''
32
+ * ```
33
+ */
34
+ function capitalize(str) {
35
+ if (str.length === 0) return str;
36
+ return str.charAt(0).toUpperCase() + str.slice(1);
37
+ }
38
+ /**
39
+ * Capitalize the first letter of a string and lowercase the rest.
40
+ *
41
+ * @param str - The string to capitalize
42
+ * @returns The capitalized string
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * capitalizeFirst('hello'); // 'Hello'
47
+ * capitalizeFirst('WORLD'); // 'World'
48
+ * capitalizeFirst('hELLO wORLD'); // 'Hello world'
49
+ * ```
50
+ */
51
+ function capitalizeFirst(str) {
52
+ if (str.length === 0) return str;
53
+ return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
54
+ }
55
+ /**
56
+ * Reverse a string.
57
+ *
58
+ * @param str - The string to reverse
59
+ * @returns The reversed string
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * reverse('hello'); // 'olleh'
64
+ * reverse('abc123'); // '321cba'
65
+ * ```
66
+ */
67
+ function reverse(str) {
68
+ return str.split("").reverse().join("");
69
+ }
70
+ /**
71
+ * Remove all whitespace from a string.
72
+ *
73
+ * @param str - The string to process
74
+ * @returns The string without whitespace
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * removeWhitespace('hello world'); // 'helloworld'
79
+ * removeWhitespace(' a b c '); // 'abc'
80
+ * ```
81
+ */
82
+ function removeWhitespace(str) {
83
+ return str.replace(/\s+/gu, "");
84
+ }
85
+ /**
86
+ * Replace multiple consecutive spaces with a single space.
87
+ *
88
+ * @param str - The string to process
89
+ * @returns The string with normalized spaces
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * normalizeSpaces('hello world'); // 'hello world'
94
+ * normalizeSpaces('a b c'); // 'a b c'
95
+ * ```
96
+ */
97
+ function normalizeSpaces(str) {
98
+ return str.replace(/\s+/gu, " ").trim();
99
+ }
100
+ /**
101
+ * Pad a string to a specified length from the start.
102
+ *
103
+ * @param str - The string to pad
104
+ * @param targetLength - The target length
105
+ * @param padString - The string to pad with (default: ' ')
106
+ * @returns The padded string
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * padStart('5', 3, '0'); // '005'
111
+ * padStart('hello', 10); // ' hello'
112
+ * ```
113
+ */
114
+ function padStart(str, targetLength, padString = " ") {
115
+ return str.padStart(targetLength, padString);
116
+ }
117
+ /**
118
+ * Pad a string to a specified length from the end.
119
+ *
120
+ * @param str - The string to pad
121
+ * @param targetLength - The target length
122
+ * @param padString - The string to pad with (default: ' ')
123
+ * @returns The padded string
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * padEnd('5', 3, '0'); // '500'
128
+ * padEnd('hello', 10); // 'hello '
129
+ * ```
130
+ */
131
+ function padEnd(str, targetLength, padString = " ") {
132
+ return str.padEnd(targetLength, padString);
133
+ }
134
+ /**
135
+ * Repeat a string a specified number of times.
136
+ *
137
+ * @param str - The string to repeat
138
+ * @param count - The number of times to repeat
139
+ * @returns The repeated string
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * repeat('ha', 3); // 'hahaha'
144
+ * repeat('*', 5); // '*****'
145
+ * ```
146
+ */
147
+ function repeat(str, count) {
148
+ return str.repeat(count);
149
+ }
150
+ /**
151
+ * Count the occurrences of a substring in a string.
152
+ *
153
+ * @param str - The string to search in
154
+ * @param searchValue - The substring to count
155
+ * @param caseSensitive - Whether the search is case-sensitive (default: true)
156
+ * @returns The number of occurrences
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * countOccurrences('hello world hello', 'hello'); // 2
161
+ * countOccurrences('Hello HELLO hello', 'hello', false); // 3
162
+ * ```
163
+ */
164
+ function countOccurrences(str, searchValue, caseSensitive = true) {
165
+ if (searchValue.length === 0) return 0;
166
+ const source = caseSensitive ? str : str.toLowerCase();
167
+ const search = caseSensitive ? searchValue : searchValue.toLowerCase();
168
+ let count = 0;
169
+ let position = 0;
170
+ while (true) {
171
+ const index = source.indexOf(search, position);
172
+ if (index === -1) break;
173
+ count++;
174
+ position = index + 1;
175
+ }
176
+ return count;
177
+ }
178
+ /**
179
+ * Extract words from a string.
180
+ *
181
+ * @param str - The string to extract words from
182
+ * @returns An array of words
183
+ *
184
+ * @example
185
+ * ```typescript
186
+ * words('hello world'); // ['hello', 'world']
187
+ * words('one-two_three'); // ['one', 'two', 'three']
188
+ * ```
189
+ */
190
+ function words(str) {
191
+ return str.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/gu) || [];
192
+ }
193
+
194
+ //#endregion
195
+ export { capitalize, capitalizeFirst, countOccurrences, normalizeSpaces, padEnd, padStart, removeWhitespace, repeat, reverse, truncate, words };
@@ -1 +1,15 @@
1
- const e=36;function t(e,t=36){let n=0;for(let t=0;t<e.length;t++)n=(n<<5)-n+e.charCodeAt(t),n&=n;return`${Math.abs(n).toString(t)}`}exports.DEFAULT_RADIX=36,exports.simpleHash=t;
1
+
2
+ //#region src/simple-hash.ts
3
+ const DEFAULT_RADIX = 36;
4
+ function simpleHash(str, radix = DEFAULT_RADIX) {
5
+ let hash = 0;
6
+ for (let i = 0; i < str.length; i++) {
7
+ hash = (hash << 5) - hash + str.charCodeAt(i);
8
+ hash = hash & hash;
9
+ }
10
+ return `${Math.abs(hash).toString(radix)}`;
11
+ }
12
+
13
+ //#endregion
14
+ exports.DEFAULT_RADIX = DEFAULT_RADIX;
15
+ exports.simpleHash = simpleHash;
@@ -1 +1,13 @@
1
- const e=36;function t(e,t=36){let n=0;for(let t=0;t<e.length;t++)n=(n<<5)-n+e.charCodeAt(t),n&=n;return`${Math.abs(n).toString(t)}`}export{e as DEFAULT_RADIX,t as simpleHash};
1
+ //#region src/simple-hash.ts
2
+ const DEFAULT_RADIX = 36;
3
+ function simpleHash(str, radix = DEFAULT_RADIX) {
4
+ let hash = 0;
5
+ for (let i = 0; i < str.length; i++) {
6
+ hash = (hash << 5) - hash + str.charCodeAt(i);
7
+ hash = hash & hash;
8
+ }
9
+ return `${Math.abs(hash).toString(radix)}`;
10
+ }
11
+
12
+ //#endregion
13
+ export { DEFAULT_RADIX, simpleHash };
@@ -1 +1,21 @@
1
- function e(e){return typeof e==`string`}exports.isString=e;
1
+
2
+ //#region src/type-guards.ts
3
+ /**
4
+ * Type guard to check if a value is a string.
5
+ *
6
+ * @param value - The value to check
7
+ * @returns True if the value is a string, false otherwise
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * isString('hello'); // true
12
+ * isString(123); // false
13
+ * isString(null); // false
14
+ * ```
15
+ */
16
+ function isString(value) {
17
+ return typeof value === "string";
18
+ }
19
+
20
+ //#endregion
21
+ exports.isString = isString;
@@ -1 +1,20 @@
1
- function e(e){return typeof e==`string`}export{e as isString};
1
+ //#region src/type-guards.ts
2
+ /**
3
+ * Type guard to check if a value is a string.
4
+ *
5
+ * @param value - The value to check
6
+ * @returns True if the value is a string, false otherwise
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * isString('hello'); // true
11
+ * isString(123); // false
12
+ * isString(null); // false
13
+ * ```
14
+ */
15
+ function isString(value) {
16
+ return typeof value === "string";
17
+ }
18
+
19
+ //#endregion
20
+ export { isString };
@@ -1 +1,78 @@
1
- function e(e,t=!0){return t?e.trim().length===0:e.length===0}function t(e){return/^[\da-z]+$/iu.test(e)}function n(e){return/^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(e)}function r(e){try{return!!new URL(e)}catch{return!1}}exports.isAlphanumeric=t,exports.isEmail=n,exports.isEmpty=e,exports.isUrl=r;
1
+
2
+ //#region src/validation.ts
3
+ /**
4
+ * Check if a string is empty (zero length or only whitespace).
5
+ *
6
+ * @param str - The string to check
7
+ * @param trimWhitespace - If true, treats whitespace-only strings as empty (default: true)
8
+ * @returns True if the string is empty, false otherwise
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * isEmpty(''); // true
13
+ * isEmpty(' '); // true
14
+ * isEmpty(' ', false); // false
15
+ * isEmpty('hello'); // false
16
+ * ```
17
+ */
18
+ function isEmpty(str, trimWhitespace = true) {
19
+ if (trimWhitespace) return str.trim().length === 0;
20
+ return str.length === 0;
21
+ }
22
+ /**
23
+ * Check if a string contains only alphanumeric characters.
24
+ *
25
+ * @param str - The string to check
26
+ * @returns True if the string contains only alphanumeric characters, false otherwise
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * isAlphanumeric('abc123'); // true
31
+ * isAlphanumeric('abc-123'); // false
32
+ * isAlphanumeric(''); // false
33
+ * ```
34
+ */
35
+ function isAlphanumeric(str) {
36
+ return /^[\da-z]+$/iu.test(str);
37
+ }
38
+ /**
39
+ * Check if a string is a valid email address.
40
+ *
41
+ * @param str - The string to check
42
+ * @returns True if the string is a valid email address, false otherwise
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * isEmail('test@example.com'); // true
47
+ * isEmail('invalid-email'); // false
48
+ * ```
49
+ */
50
+ function isEmail(str) {
51
+ return /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(str);
52
+ }
53
+ /**
54
+ * Check if a string is a valid URL.
55
+ *
56
+ * @param str - The string to check
57
+ * @returns True if the string is a valid URL, false otherwise
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * isUrl('https://example.com'); // true
62
+ * isUrl('not a url'); // false
63
+ * ```
64
+ */
65
+ function isUrl(str) {
66
+ try {
67
+ const url = new URL(str);
68
+ return Boolean(url);
69
+ } catch {
70
+ return false;
71
+ }
72
+ }
73
+
74
+ //#endregion
75
+ exports.isAlphanumeric = isAlphanumeric;
76
+ exports.isEmail = isEmail;
77
+ exports.isEmpty = isEmpty;
78
+ exports.isUrl = isUrl;
@@ -1 +1,74 @@
1
- function e(e,t=!0){return t?e.trim().length===0:e.length===0}function t(e){return/^[\da-z]+$/iu.test(e)}function n(e){return/^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(e)}function r(e){try{return!!new URL(e)}catch{return!1}}export{t as isAlphanumeric,n as isEmail,e as isEmpty,r as isUrl};
1
+ //#region src/validation.ts
2
+ /**
3
+ * Check if a string is empty (zero length or only whitespace).
4
+ *
5
+ * @param str - The string to check
6
+ * @param trimWhitespace - If true, treats whitespace-only strings as empty (default: true)
7
+ * @returns True if the string is empty, false otherwise
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * isEmpty(''); // true
12
+ * isEmpty(' '); // true
13
+ * isEmpty(' ', false); // false
14
+ * isEmpty('hello'); // false
15
+ * ```
16
+ */
17
+ function isEmpty(str, trimWhitespace = true) {
18
+ if (trimWhitespace) return str.trim().length === 0;
19
+ return str.length === 0;
20
+ }
21
+ /**
22
+ * Check if a string contains only alphanumeric characters.
23
+ *
24
+ * @param str - The string to check
25
+ * @returns True if the string contains only alphanumeric characters, false otherwise
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * isAlphanumeric('abc123'); // true
30
+ * isAlphanumeric('abc-123'); // false
31
+ * isAlphanumeric(''); // false
32
+ * ```
33
+ */
34
+ function isAlphanumeric(str) {
35
+ return /^[\da-z]+$/iu.test(str);
36
+ }
37
+ /**
38
+ * Check if a string is a valid email address.
39
+ *
40
+ * @param str - The string to check
41
+ * @returns True if the string is a valid email address, false otherwise
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * isEmail('test@example.com'); // true
46
+ * isEmail('invalid-email'); // false
47
+ * ```
48
+ */
49
+ function isEmail(str) {
50
+ return /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/u.test(str);
51
+ }
52
+ /**
53
+ * Check if a string is a valid URL.
54
+ *
55
+ * @param str - The string to check
56
+ * @returns True if the string is a valid URL, false otherwise
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * isUrl('https://example.com'); // true
61
+ * isUrl('not a url'); // false
62
+ * ```
63
+ */
64
+ function isUrl(str) {
65
+ try {
66
+ const url = new URL(str);
67
+ return Boolean(url);
68
+ } catch {
69
+ return false;
70
+ }
71
+ }
72
+
73
+ //#endregion
74
+ export { isAlphanumeric, isEmail, isEmpty, isUrl };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pixpilot/string",
3
3
  "type": "module",
4
- "version": "2.3.0",
4
+ "version": "2.5.0",
5
5
  "description": "A collection of utility functions for string manipulation and validation.",
6
6
  "author": "Pixpilot <m.doaie@hotmail.com>",
7
7
  "license": "MIT",