@jdsalasc/solvejs-regex 1.0.1 → 1.0.2
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/cjs/index.cjs +25 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -1
- package/package.json +6 -6
package/dist/cjs/index.cjs
CHANGED
|
@@ -11,16 +11,41 @@ exports.REGEX_PATTERNS = {
|
|
|
11
11
|
hexColor: /^#(?:[0-9a-fA-F]{3}){1,2}$/,
|
|
12
12
|
username: /^[a-zA-Z0-9_]{3,30}$/
|
|
13
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Tests a string against a regular expression safely.
|
|
16
|
+
*
|
|
17
|
+
* @param input - Input text.
|
|
18
|
+
* @param pattern - Regular expression pattern.
|
|
19
|
+
* @returns `true` when the pattern matches the input.
|
|
20
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
21
|
+
*/
|
|
14
22
|
function testPattern(input, pattern) {
|
|
15
23
|
if (!(pattern instanceof RegExp)) {
|
|
16
24
|
throw new TypeError("Expected pattern to be a RegExp instance.");
|
|
17
25
|
}
|
|
26
|
+
pattern.lastIndex = 0;
|
|
18
27
|
return pattern.test(input);
|
|
19
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Validates text using a custom regex pattern.
|
|
31
|
+
*
|
|
32
|
+
* @param input - Input text.
|
|
33
|
+
* @param pattern - Regular expression pattern.
|
|
34
|
+
* @param options - Validation options.
|
|
35
|
+
* @returns `true` when the pattern matches.
|
|
36
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
37
|
+
*/
|
|
20
38
|
function validateWithPattern(input, pattern, options) {
|
|
21
39
|
const normalized = options?.trim ? input.trim() : input;
|
|
22
40
|
return testPattern(normalized, pattern);
|
|
23
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Validates text using one of the built-in common patterns.
|
|
44
|
+
*
|
|
45
|
+
* @param input - Input text.
|
|
46
|
+
* @param patternName - Built-in pattern name.
|
|
47
|
+
* @returns `true` when the selected pattern matches.
|
|
48
|
+
*/
|
|
24
49
|
function validateByName(input, patternName) {
|
|
25
50
|
return testPattern(input, exports.REGEX_PATTERNS[patternName]);
|
|
26
51
|
}
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAkBA,kCAMC;AAWD,kDAGC;AASD,wCAEC;AAjDY,QAAA,cAAc,GAAG;IAC5B,KAAK,EAAE,4BAA4B;IACnC,SAAS,EAAE,mBAAmB;IAC9B,OAAO,EAAE,iBAAiB;IAC1B,QAAQ,EAAE,4BAA4B;IACtC,QAAQ,EAAE,sBAAsB;CACxB,CAAC;AAIX;;;;;;;GAOG;AACH,SAAgB,WAAW,CAAC,KAAa,EAAE,OAAe;IACxD,IAAI,CAAC,CAAC,OAAO,YAAY,MAAM,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,SAAS,CAAC,2CAA2C,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,CAAC,SAAS,GAAG,CAAC,CAAC;IACtB,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC7B,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAAC,KAAa,EAAE,OAAe,EAAE,OAA4B;IAC9F,MAAM,UAAU,GAAG,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACxD,OAAO,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,WAAwB;IACpE,OAAO,WAAW,CAAC,KAAK,EAAE,sBAAc,CAAC,WAAW,CAAC,CAAC,CAAC;AACzD,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,8 +6,32 @@ export declare const REGEX_PATTERNS: {
|
|
|
6
6
|
readonly username: RegExp;
|
|
7
7
|
};
|
|
8
8
|
export type PatternName = keyof typeof REGEX_PATTERNS;
|
|
9
|
+
/**
|
|
10
|
+
* Tests a string against a regular expression safely.
|
|
11
|
+
*
|
|
12
|
+
* @param input - Input text.
|
|
13
|
+
* @param pattern - Regular expression pattern.
|
|
14
|
+
* @returns `true` when the pattern matches the input.
|
|
15
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
16
|
+
*/
|
|
9
17
|
export declare function testPattern(input: string, pattern: RegExp): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Validates text using a custom regex pattern.
|
|
20
|
+
*
|
|
21
|
+
* @param input - Input text.
|
|
22
|
+
* @param pattern - Regular expression pattern.
|
|
23
|
+
* @param options - Validation options.
|
|
24
|
+
* @returns `true` when the pattern matches.
|
|
25
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
26
|
+
*/
|
|
10
27
|
export declare function validateWithPattern(input: string, pattern: RegExp, options?: {
|
|
11
28
|
trim?: boolean;
|
|
12
29
|
}): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Validates text using one of the built-in common patterns.
|
|
32
|
+
*
|
|
33
|
+
* @param input - Input text.
|
|
34
|
+
* @param patternName - Built-in pattern name.
|
|
35
|
+
* @returns `true` when the selected pattern matches.
|
|
36
|
+
*/
|
|
13
37
|
export declare function validateByName(input: string, patternName: PatternName): boolean;
|
package/dist/esm/index.js
CHANGED
|
@@ -5,16 +5,41 @@ export const REGEX_PATTERNS = {
|
|
|
5
5
|
hexColor: /^#(?:[0-9a-fA-F]{3}){1,2}$/,
|
|
6
6
|
username: /^[a-zA-Z0-9_]{3,30}$/
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Tests a string against a regular expression safely.
|
|
10
|
+
*
|
|
11
|
+
* @param input - Input text.
|
|
12
|
+
* @param pattern - Regular expression pattern.
|
|
13
|
+
* @returns `true` when the pattern matches the input.
|
|
14
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
15
|
+
*/
|
|
8
16
|
export function testPattern(input, pattern) {
|
|
9
17
|
if (!(pattern instanceof RegExp)) {
|
|
10
18
|
throw new TypeError("Expected pattern to be a RegExp instance.");
|
|
11
19
|
}
|
|
20
|
+
pattern.lastIndex = 0;
|
|
12
21
|
return pattern.test(input);
|
|
13
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Validates text using a custom regex pattern.
|
|
25
|
+
*
|
|
26
|
+
* @param input - Input text.
|
|
27
|
+
* @param pattern - Regular expression pattern.
|
|
28
|
+
* @param options - Validation options.
|
|
29
|
+
* @returns `true` when the pattern matches.
|
|
30
|
+
* @throws {TypeError} If `pattern` is not a RegExp instance.
|
|
31
|
+
*/
|
|
14
32
|
export function validateWithPattern(input, pattern, options) {
|
|
15
33
|
const normalized = options?.trim ? input.trim() : input;
|
|
16
34
|
return testPattern(normalized, pattern);
|
|
17
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Validates text using one of the built-in common patterns.
|
|
38
|
+
*
|
|
39
|
+
* @param input - Input text.
|
|
40
|
+
* @param patternName - Built-in pattern name.
|
|
41
|
+
* @returns `true` when the selected pattern matches.
|
|
42
|
+
*/
|
|
18
43
|
export function validateByName(input, patternName) {
|
|
19
44
|
return testPattern(input, REGEX_PATTERNS[patternName]);
|
|
20
45
|
}
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,KAAK,EAAE,4BAA4B;IACnC,SAAS,EAAE,mBAAmB;IAC9B,OAAO,EAAE,iBAAiB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B,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa,EAAE,OAAe,EAAE,OAA4B;IAC9F,MAAM,UAAU,GAAG,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACxD,OAAO,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,WAAwB;IACpE,OAAO,WAAW,CAAC,KAAK,EAAE,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC;AACzD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdsalasc/solvejs-regex",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Common regex patterns and validators for email, username, URL, hex color, and phone-like strings.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -43,10 +43,10 @@
|
|
|
43
43
|
"node": ">=18"
|
|
44
44
|
},
|
|
45
45
|
"keywords": [
|
|
46
|
-
"regex",
|
|
47
|
-
"regex
|
|
48
|
-
"
|
|
49
|
-
"
|
|
46
|
+
"regex utilities",
|
|
47
|
+
"email regex",
|
|
48
|
+
"validation",
|
|
49
|
+
"typescript regex",
|
|
50
50
|
"solvejs"
|
|
51
51
|
]
|
|
52
52
|
}
|