@ptolemy2002/regex-utils 1.0.4 → 1.2.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.
Files changed (3) hide show
  1. package/README.md +21 -6
  2. package/index.js +19 -10
  3. package/package.json +5 -3
package/README.md CHANGED
@@ -109,9 +109,23 @@ Checks if a string is a valid set of regular expression flags.
109
109
  #### Returns
110
110
  `Boolean` - `true` if the string is a valid set of regular expression flags, `false` otherwise.
111
111
 
112
+ ### zodValidate
113
+ #### Description
114
+ This is a simple function that takes a zod schema, returning a function that takes a value. If the value matches, the function returns `true`. Otherwise, it returns whatever error message the zod schema provides by default, or `false` if the `returnError` flag is disabled.
115
+
116
+ #### Parameters
117
+ - `p` (`ZodSchema`): The zod schema to be used for validation.
118
+ - `returnError` (`Boolean`): Whether to return the error message if the value does not match the schema. Default is `true`. If `false`, the function will return `false` on failure instead.
119
+
120
+ #### Returns
121
+ `Function` - A function that takes a value and returns `true` if the value matches the schema, an error message if the value does not match the schema and `returnError` is `true`, or `false` if the value does not match the schema and `returnError` is `false`.
122
+
123
+ #### Returns
124
+ `Function` - A function that takes a value and returns `true` if the value matches the schema, or an error message otherwise.
125
+
112
126
  ### isAlphanumeric
113
127
  #### Description
114
- Checks if a string is alphanumeric, that is, if it only contains letters, numbers, dashes, and underscores. Empty strings are not considered alphanumeric.
128
+ Uses `zodValidate` to check if a string is alphanumeric, that is, if it only contains letters, numbers, dashes, and underscores. Empty strings are not considered alphanumeric.
115
129
 
116
130
  #### Parameters
117
131
  - `str` (`String`): The value to be checked.
@@ -132,7 +146,7 @@ Transforms a string to be alphanumeric by removing accents, separating it by non
132
146
 
133
147
  ### isValidEmail
134
148
  #### Description
135
- Attempts to check if a string is a valid email address. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
149
+ Uses `zodValidate` to check if a string is a valid email address. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
136
150
 
137
151
  #### Parameters
138
152
  - `value` (`String`): The value to be checked.
@@ -142,7 +156,7 @@ Attempts to check if a string is a valid email address. Note that this is a simp
142
156
 
143
157
  ### isValidPhoneNumber
144
158
  #### Description
145
- Attempts to check if a string is a valid phone number. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
159
+ Uses `zodValidate` to check if a string is a valid phone number. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
146
160
 
147
161
  #### Parameters
148
162
  - `value` (`String`): The value to be checked.
@@ -152,7 +166,7 @@ Attempts to check if a string is a valid phone number. Note that this is a simpl
152
166
 
153
167
  ### isValidURL
154
168
  #### Description
155
- Attempts to check if a string is a valid URL. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
169
+ Uses `zodValidate` to check if a string is a valid URL. Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
156
170
 
157
171
  #### Parameters
158
172
  - `value` (`String`): The value to be checked.
@@ -162,7 +176,7 @@ Attempts to check if a string is a valid URL. Note that this is a simple check a
162
176
 
163
177
  ### isValidSSN
164
178
  #### Description
165
- Attempts to check if a string is a valid Social Security Number (SSN). Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
179
+ Uses `zodValidate` to check if a string is a valid Social Security Number (SSN). Note that this is a simple check and may not cover all cases, but it should be good enough for most purposes.
166
180
 
167
181
  #### Parameters
168
182
  - `value` (`String`): The value to be checked.
@@ -174,7 +188,8 @@ Attempts to check if a string is a valid Social Security Number (SSN). Note that
174
188
  This is a React Library Created by Ptolemy2002's [cra-template-react-library](https://www.npmjs.com/package/@ptolemy2002/cra-template-react-library) template in combination with [create-react-app](https://www.npmjs.com/package/create-react-app). However, it does not actually depend on React - it has been modified to work out of the box. It contains methods of building and publishing your library to npm.
175
189
 
176
190
  ## Peer Dependencies
177
- This project does not have any peer dependencies, so it should work out of the box.
191
+ These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
192
+ - zod: ^3.23.8
178
193
 
179
194
  ## Commands
180
195
  The following commands exist in the project:
package/index.js CHANGED
@@ -19,7 +19,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.js
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
- alphanumericPattern: () => alphanumericPattern,
23
22
  combineFlags: () => combineFlags,
24
23
  escapeRegex: () => escapeRegex,
25
24
  isAlphanumeric: () => isAlphanumeric,
@@ -29,15 +28,16 @@ __export(src_exports, {
29
28
  isValidRegexFlags: () => isValidRegexFlags,
30
29
  isValidSSN: () => isValidSSN,
31
30
  isValidURL: () => isValidURL,
32
- nonAlphanumericPattern: () => nonAlphanumericPattern,
33
31
  regexAccentInsensitive: () => regexAccentInsensitive,
34
32
  regexCaseInsensitive: () => regexCaseInsensitive,
35
33
  regexMatchWhole: () => regexMatchWhole,
36
34
  removeAccents: () => removeAccents,
37
35
  toAlphanumeric: () => toAlphanumeric,
38
- transformRegex: () => transformRegex
36
+ transformRegex: () => transformRegex,
37
+ zodValidate: () => zodValidate
39
38
  });
40
39
  module.exports = __toCommonJS(src_exports);
40
+ var import_zod = require("zod");
41
41
  var accentPatterns = [
42
42
  "(a|\xE1|\xE0|\xE4|\xE2|\xE3)",
43
43
  "(A|\xC1|\xC0|\xC4|\xC2|\xC3)",
@@ -135,10 +135,19 @@ function isValidRegex(value, flags = "") {
135
135
  function isValidRegexFlags(value) {
136
136
  return /^[gimsuy]*$/.test(value);
137
137
  }
138
+ function zodValidate(p, returnError = true) {
139
+ return (v) => {
140
+ const result = p.safeParse(v);
141
+ if (result.success) return true;
142
+ if (!returnError) return false;
143
+ if (result.error.errors.length === 1) return result.error.errors[0].message;
144
+ return result.error.errors.map((e) => e.message);
145
+ };
146
+ }
138
147
  var alphanumericPattern = /[\w_-]+/i;
139
148
  var nonAlphanumericPattern = /[^\w_-]+/i;
140
149
  function isAlphanumeric(str) {
141
- return transformRegex(alphanumericPattern, { matchWhole: true }).test(str);
150
+ return zodValidate(import_zod.z.string().regex(transformRegex(alphanumericPattern, { matchWhole: true })), false)(str);
142
151
  }
143
152
  function toAlphanumeric(str, separator = "-") {
144
153
  const globalNonAlphanumericPattern = transformRegex(nonAlphanumericPattern, { flags: "g" });
@@ -149,14 +158,14 @@ function toAlphanumeric(str, separator = "-") {
149
158
  }).filter((word) => word).join(separator);
150
159
  }
151
160
  function isValidEmail(v) {
152
- return /^\S+@\S+\.\S+$/.test(v);
153
- }
154
- function isValidPhoneNumber(v) {
155
- return /^\+?\d?\s*(\(\d{3}\)|\d{3})(\s*|-)\d{3}(\s*|-)\d{4}$/.test(v);
161
+ return zodValidate(import_zod.z.string().email(), false)(v);
156
162
  }
157
163
  function isValidURL(v) {
158
- return /^https?:\/\/\S+\.\S+$/.test(v);
164
+ return zodValidate(import_zod.z.string().url(), false)(v);
165
+ }
166
+ function isValidPhoneNumber(v) {
167
+ return zodValidate(import_zod.z.coerce.string().regex(/^\+?\d?\s*(\(\d{3}\)|\d{3})(\s*|-)\d{3}(\s*|-)\d{4}$/), false)(v);
159
168
  }
160
169
  function isValidSSN(v) {
161
- return /^\d{3}-?\d{2}-?\d{4}$/.test(v);
170
+ return zodValidate(import_zod.z.coerce.string().regex(/^\d{3}-?\d{2}-?\d{4}$/), false)(v);
162
171
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "1.0.4",
3
+ "version": "1.2.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js"
7
7
  ],
8
8
  "scripts": {
9
- "build": "esbuild src/index.js --bundle --format=cjs --outfile=index.js --loader:.js=jsx --external:react --external:react-dom --external:@types/react --external:@types/react-dom",
9
+ "build": "esbuild src/index.js --bundle --format=cjs --outfile=index.js --loader:.js=jsx --external:zod",
10
10
  "postinstall": "npx typesync",
11
11
  "uninstall": "bash ./scripts/uninstall.sh",
12
12
  "reinstall": "bash ./scripts/reinstall.sh",
@@ -26,8 +26,10 @@
26
26
  },
27
27
  "description": "Utilities for working with regular expressions",
28
28
  "license": "ISC",
29
- "peerDependencies": {},
30
29
  "devDependencies": {
31
30
  "esbuild": "^0.23.0"
31
+ },
32
+ "peerDependencies": {
33
+ "zod": "^3.23.8"
32
34
  }
33
35
  }