@ptolemy2002/regex-utils 1.0.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 +22 -0
  2. package/index.js +159 -0
  3. package/package.json +33 -0
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # React Library CRA Project
2
+ ## Meta
3
+ 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). It contains methods of building and publishing your library to npm.
4
+ For now, the library makes use of React 18 and does not use TypeScript.
5
+
6
+ ## Peer Dependencies
7
+ This project does not have any peer dependencies, so it should work out of the box.
8
+
9
+ ## Commands
10
+ The following commands exist in the project:
11
+
12
+ - `npm run uninstall` - Uninstalls all dependencies for the library
13
+ - `npm run reinstall` - Uninstalls and then Reinstalls all dependencies for the library
14
+ - `npm run example-uninstall` - Uninstalls all dependencies for the example app
15
+ - `npm run example-install` - Installs all dependencies for the example app
16
+ - `npm run example-reinstall` - Uninstalls and then Reinstalls all dependencies for the example app
17
+ - `npm run example-start` - Starts the example app after building the library
18
+ - `npm run build` - Builds the library
19
+ - `npm run release` - Publishes the library to npm without changing the version
20
+ - `npm run release-patch` - Publishes the library to npm with a patch version bump
21
+ - `npm run release-minor` - Publishes the library to npm with a minor version bump
22
+ - `npm run release-major` - Publishes the library to npm with a major version bump
package/index.js ADDED
@@ -0,0 +1,159 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.js
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ alphanumericPattern: () => alphanumericPattern,
23
+ combineFlags: () => combineFlags,
24
+ escapeRegex: () => escapeRegex,
25
+ isAlphanumeric: () => isAlphanumeric,
26
+ isValidEmail: () => isValidEmail,
27
+ isValidPhoneNumber: () => isValidPhoneNumber,
28
+ isValidRegex: () => isValidRegex,
29
+ isValidRegexFlags: () => isValidRegexFlags,
30
+ isValidSSN: () => isValidSSN,
31
+ isValidURL: () => isValidURL,
32
+ nonAlphanumericPattern: () => nonAlphanumericPattern,
33
+ regexAccentInsensitive: () => regexAccentInsensitive,
34
+ regexCaseInsensitive: () => regexCaseInsensitive,
35
+ regexMatchWhole: () => regexMatchWhole,
36
+ removeAccents: () => removeAccents,
37
+ toAlphanumeric: () => toAlphanumeric,
38
+ transformRegex: () => transformRegex
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+ var accentPatterns = [
42
+ "(a|\xE1|\xE0|\xE4|\xE2|\xE3)",
43
+ "(A|\xC1|\xC0|\xC4|\xC2|\xC3)",
44
+ "(e|\xE9|\xE8|\xEB|\xEA)",
45
+ "(E|\xC9|\xC8|\xCB|\xCA)",
46
+ "(i|\xED|\xEC|\xEF|\xEE)",
47
+ "(I|\xCD|\xCC|\xCF|\xCE)",
48
+ "(o|\xF3|\xF2|\xF6|\xF4|\xF5)",
49
+ "(O|\xD3|\xD2|\xD6|\xD4|\xD5)",
50
+ "(u|\xFA|\xF9|\xFC|\xFB)",
51
+ "(U|\xDA|\xD9|\xDC|\xDB)"
52
+ ];
53
+ function combineFlags(...flags) {
54
+ const result = /* @__PURE__ */ new Set();
55
+ flags.forEach((flag) => {
56
+ if (flag) {
57
+ flag.split("").forEach((c) => {
58
+ if (!result.has(c)) result.add(c);
59
+ });
60
+ }
61
+ });
62
+ return [...result].join("");
63
+ }
64
+ function escapeRegex(value, flags = "") {
65
+ if (typeof value === "string") {
66
+ return new RegExp(value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, "\\$&"), flags);
67
+ } else if (value instanceof RegExp) {
68
+ return escapeRegex(value.source, combineFlags(value.flags, flags));
69
+ } else {
70
+ throw new TypeError("Expected string or RegExp, got " + typeof value);
71
+ }
72
+ }
73
+ function regexAccentInsensitive(value, flags = "") {
74
+ if (typeof value === "string") {
75
+ accentPatterns.forEach((pattern) => {
76
+ value = value.replaceAll(new RegExp(pattern, "g"), pattern);
77
+ });
78
+ return new RegExp(value, flags);
79
+ } else if (value instanceof RegExp) {
80
+ return regexAccentInsensitive(value.source, combineFlags(value.flags, flags));
81
+ } else {
82
+ throw new TypeError("Expected string or RegExp, got " + typeof value);
83
+ }
84
+ }
85
+ function removeAccents(value) {
86
+ if (typeof value === "string") {
87
+ accentPatterns.forEach((pattern) => {
88
+ value = value.replaceAll(new RegExp(pattern, "g"), pattern[1]);
89
+ });
90
+ return value;
91
+ } else {
92
+ throw new TypeError("Expected string, got " + typeof value);
93
+ }
94
+ }
95
+ function regexCaseInsensitive(value, flags = "") {
96
+ if (typeof value === "string") {
97
+ if (!flags.includes("i")) {
98
+ flags += "i";
99
+ }
100
+ return new RegExp(value, flags);
101
+ } else if (value instanceof RegExp) {
102
+ return regexCaseInsensitive(value.source, combineFlags(value.flags, flags));
103
+ } else {
104
+ throw new TypeError("Expected string or RegExp, got " + typeof value);
105
+ }
106
+ }
107
+ function regexMatchWhole(value, flags = "") {
108
+ if (typeof value === "string") {
109
+ return new RegExp(`^${value}$`, flags);
110
+ } else if (value instanceof RegExp) {
111
+ return regexMatchWhole(value.source, combineFlags(value.flags, flags));
112
+ } else {
113
+ throw new TypeError("Expected string or RegExp, got " + typeof value);
114
+ }
115
+ }
116
+ function transformRegex(value, { flags = "", accentInsensitive = false, caseInsensitive = false, matchWhole = false }) {
117
+ if (accentInsensitive) value = regexAccentInsensitive(value, flags);
118
+ if (caseInsensitive) value = regexCaseInsensitive(value, flags);
119
+ if (matchWhole) value = regexMatchWhole(value, flags);
120
+ value = new RegExp(value, combineFlags(value.flags, flags));
121
+ console.log(value);
122
+ return value;
123
+ }
124
+ function isValidRegex(value, flags = "") {
125
+ try {
126
+ new RegExp(value, flags);
127
+ return true;
128
+ } catch {
129
+ return false;
130
+ }
131
+ }
132
+ function isValidRegexFlags(value) {
133
+ return /^[gimsuy]*$/.test(value);
134
+ }
135
+ var alphanumericPattern = /[\w_-]+/i;
136
+ var nonAlphanumericPattern = /[^\w_-]+/i;
137
+ function isAlphanumeric(str) {
138
+ return transformRegex(alphanumericPattern, { matchWhole: true }).test(str);
139
+ }
140
+ function toAlphanumeric(str, separator = "-") {
141
+ const globalNonAlphanumericPattern = transformRegex(nonAlphanumericPattern, { flags: "g" });
142
+ str = removeAccents(str);
143
+ const words = str.split(globalNonAlphanumericPattern);
144
+ return words.map((word) => {
145
+ return word.replaceAll(globalNonAlphanumericPattern, "");
146
+ }).filter((word) => word).join(separator);
147
+ }
148
+ function isValidEmail(v) {
149
+ return /^\S+@\S+\.\S+$/.test(v);
150
+ }
151
+ function isValidPhoneNumber(v) {
152
+ return /^\+?\d?\s*(\(\d{3}\)|\d{3})(\s*|-)\d{3}(\s*|-)\d{4}$/.test(v);
153
+ }
154
+ function isValidURL(v) {
155
+ return /^https?:\/\/\S+\.\S+$/.test(v);
156
+ }
157
+ function isValidSSN(v) {
158
+ return /^\d{3}-?\d{2}-?\d{4}$/.test(v);
159
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@ptolemy2002/regex-utils",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "files": [
6
+ "index.js"
7
+ ],
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",
10
+ "postinstall": "npx typesync",
11
+ "uninstall": "bash ./scripts/uninstall.sh",
12
+ "reinstall": "bash ./scripts/reinstall.sh",
13
+ "example-uninstall": "bash ./scripts/example-uninstall.sh",
14
+ "example-install": "bash ./scripts/example-install.sh",
15
+ "example-reinstall": "bash ./scripts/example-reinstall.sh",
16
+ "example-start": "bash ./scripts/example-start.sh",
17
+ "release": "bash ./scripts/release.sh",
18
+ "release-patch": "bash ./scripts/release.sh patch",
19
+ "release-minor": "bash ./scripts/release.sh minor",
20
+ "release-major": "bash ./scripts/release.sh major"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/Ptolemy2002/regex-utils",
25
+ "directory": "lib"
26
+ },
27
+ "description": "Utilities for working with regular expressions",
28
+ "license": "ISC",
29
+ "peerDependencies": {},
30
+ "devDependencies": {
31
+ "esbuild": "^0.23.0"
32
+ }
33
+ }