@ptolemy2002/regex-utils 1.0.0 → 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/README.md +172 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Regex Utils
|
|
2
|
+
This library contains utilities that are useful when working with JavaScript regular expressions.
|
|
3
|
+
|
|
4
|
+
The functions are not exported as default, so you can import them in one of the following ways:
|
|
5
|
+
```
|
|
6
|
+
// ES6
|
|
7
|
+
import { functionName } from '@ptolemy2002/regex-utils';
|
|
8
|
+
// CommonJS
|
|
9
|
+
const { functionName } = require('@ptolemy2002/regex-utils');
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Functions
|
|
13
|
+
The following functions are available in the library:
|
|
14
|
+
|
|
15
|
+
### combineFlags
|
|
16
|
+
#### Description
|
|
17
|
+
Combines two sets of flags into one string. If a flag is present in both sets, it will only appear once in the result.
|
|
18
|
+
|
|
19
|
+
#### Parameters
|
|
20
|
+
- `...flags` (`String[]`): The sets of flags to be combined. Falsey values will be ignored.
|
|
21
|
+
|
|
22
|
+
#### Returns
|
|
23
|
+
`String` - The combined flags.
|
|
24
|
+
|
|
25
|
+
### escapeRegex
|
|
26
|
+
#### Description
|
|
27
|
+
Escapes a string so that it can be used as a literal in a regular expression. If spefifying a `RegExp` object, the flags are added after transforming the source to a literal.
|
|
28
|
+
|
|
29
|
+
#### Parameters
|
|
30
|
+
- `value` (`String | RegExp`): The value to be escaped.
|
|
31
|
+
- `flags` (`String`): The flags to be used in the regular expression. If a `RegExp` object is passed, these flags will be added to the existing flags.
|
|
32
|
+
|
|
33
|
+
#### Returns
|
|
34
|
+
`RegExp` - The escaped regular expression.
|
|
35
|
+
|
|
36
|
+
### regexAccentInsensitive
|
|
37
|
+
#### Description
|
|
38
|
+
Attempts to transform a `RegExp` object to be accent insensitive. Note that this is done using a static list of common characters and their accented versions, so it is not perfect.
|
|
39
|
+
|
|
40
|
+
#### Parameters
|
|
41
|
+
- `value` (`String | RegExp`): The value to be transformed to be accent insensitive.
|
|
42
|
+
- `flags` (`String`): The flags to be used in the regular expression. If a `RegExp` object is passed, these flags will be added to the existing flags.
|
|
43
|
+
|
|
44
|
+
#### Returns
|
|
45
|
+
`RegExp` - The accent insensitive regular expression.
|
|
46
|
+
|
|
47
|
+
### removeAccents
|
|
48
|
+
#### Description
|
|
49
|
+
Attempts to remove accents from a string. Note that this is done using a static list of common characters and their accented versions, so it is not perfect.
|
|
50
|
+
|
|
51
|
+
#### Parameters
|
|
52
|
+
- `value` (`String`): The value to be transformed to remove accents.
|
|
53
|
+
|
|
54
|
+
#### Returns
|
|
55
|
+
`String` - The string with accents removed.
|
|
56
|
+
|
|
57
|
+
### regexCaseInsensitive
|
|
58
|
+
#### Description
|
|
59
|
+
Transforms a `RegExp` object to be case insensitive by adding the `i` flag.
|
|
60
|
+
|
|
61
|
+
#### Parameters
|
|
62
|
+
- `value` (`String | RegExp`): The value to be transformed to be case insensitive.
|
|
63
|
+
- `flags` (`String`): The flags to be used in the regular expression. If a `RegExp` object is passed, these flags will be added to the existing flags.
|
|
64
|
+
|
|
65
|
+
#### Returns
|
|
66
|
+
`RegExp` - The case insensitive regular expression.
|
|
67
|
+
|
|
68
|
+
### regexMatchWhole
|
|
69
|
+
#### Description
|
|
70
|
+
Transforms a `RegExp` object to match the whole string by adding `^` and `$` to the beginning and end of the source, respectively.
|
|
71
|
+
|
|
72
|
+
#### Parameters
|
|
73
|
+
- `value` (`String | RegExp`): The value to be transformed to match the whole string.
|
|
74
|
+
- `flags` (`String`): The flags to be used in the regular expression. If a `RegExp` object is passed, these flags will be added to the existing flags.
|
|
75
|
+
|
|
76
|
+
### transformRegex
|
|
77
|
+
#### Description
|
|
78
|
+
This function is a quick way to apply multiple supported transformations to a regular expression.
|
|
79
|
+
|
|
80
|
+
#### Parameters
|
|
81
|
+
- `value` (`String | RegExp`): The value to be transformed.
|
|
82
|
+
- `args` (`Object`): Used to specify optional arguments.
|
|
83
|
+
- `accentInsensitive` (`Boolean`): Whether to make the regular expression accent insensitive. Default is `false`.
|
|
84
|
+
- `caseInsensitive` (`Boolean`): Whether to make the regular expression case insensitive. Default is `false`.
|
|
85
|
+
- `matchWhole` (`Boolean`): Whether to make the regular expression match the whole string. Default is `false`.
|
|
86
|
+
- `flags` (`String`): The flags to be used in the regular expression. If a `RegExp` object is passed, these flags will be added to the existing flags.
|
|
87
|
+
|
|
88
|
+
#### Returns
|
|
89
|
+
`RegExp` - The transformed regular expression.
|
|
90
|
+
|
|
91
|
+
### isValidRegex
|
|
92
|
+
#### Description
|
|
93
|
+
Checks if a string is a valid regular expression by attempting to create a `RegExp` object with it.
|
|
94
|
+
|
|
95
|
+
#### Parameters
|
|
96
|
+
- `value` (`String`): The value to be checked.
|
|
97
|
+
- `flags` (`String`): The flags to be used in the regular expression.
|
|
98
|
+
|
|
99
|
+
#### Returns
|
|
100
|
+
`Boolean` - `true` if the string is a valid regular expression, `false` otherwise.
|
|
101
|
+
|
|
102
|
+
### isValidRegexFlags
|
|
103
|
+
#### Description
|
|
104
|
+
Checks if a string is a valid set of regular expression flags.
|
|
105
|
+
|
|
106
|
+
#### Parameters
|
|
107
|
+
- `value` (`String`): The value to be checked.
|
|
108
|
+
|
|
109
|
+
#### Returns
|
|
110
|
+
`Boolean` - `true` if the string is a valid set of regular expression flags, `false` otherwise.
|
|
111
|
+
|
|
112
|
+
### isAlphanumeric
|
|
113
|
+
#### 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.
|
|
115
|
+
|
|
116
|
+
#### Parameters
|
|
117
|
+
- `str` (`String`): The value to be checked.
|
|
118
|
+
|
|
119
|
+
#### Returns
|
|
120
|
+
`Boolean` - `true` if the string is alphanumeric, `false` otherwise.
|
|
121
|
+
|
|
122
|
+
### toAlphanumeric
|
|
123
|
+
#### Description
|
|
124
|
+
Transforms a string to be alphanumeric by removing accents, separating it by non-alphanumeric segments, and then joining the words with the specified `separator`.
|
|
125
|
+
|
|
126
|
+
#### Parameters
|
|
127
|
+
- `str` (`String`): The value to be transformed.
|
|
128
|
+
- `separator` (`String`): The separator to be used between words. Default is `'-'`.
|
|
129
|
+
|
|
130
|
+
#### Returns
|
|
131
|
+
`String` - The transformed string.
|
|
132
|
+
|
|
133
|
+
### isValidEmail
|
|
134
|
+
#### 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.
|
|
136
|
+
|
|
137
|
+
#### Parameters
|
|
138
|
+
- `value` (`String`): The value to be checked.
|
|
139
|
+
|
|
140
|
+
#### Returns
|
|
141
|
+
`Boolean` - `true` if the string is a valid email address, `false` otherwise.
|
|
142
|
+
|
|
143
|
+
### isValidPhoneNumber
|
|
144
|
+
#### 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.
|
|
146
|
+
|
|
147
|
+
#### Parameters
|
|
148
|
+
- `value` (`String`): The value to be checked.
|
|
149
|
+
|
|
150
|
+
#### Returns
|
|
151
|
+
`Boolean` - `true` if the string is a valid phone number, `false` otherwise.
|
|
152
|
+
|
|
153
|
+
### isValidURL
|
|
154
|
+
#### 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.
|
|
156
|
+
|
|
157
|
+
#### Parameters
|
|
158
|
+
- `value` (`String`): The value to be checked.
|
|
159
|
+
|
|
160
|
+
#### Returns
|
|
161
|
+
`Boolean` - `true` if the string is a valid URL, `false` otherwise.
|
|
162
|
+
|
|
163
|
+
### isValidSSN
|
|
164
|
+
#### 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.
|
|
166
|
+
|
|
167
|
+
#### Parameters
|
|
168
|
+
- `value` (`String`): The value to be checked.
|
|
169
|
+
|
|
170
|
+
#### Returns
|
|
171
|
+
`Boolean` - `true` if the string is a valid SSN, `false` otherwise.
|
|
172
|
+
|
|
2
173
|
## Meta
|
|
3
174
|
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
175
|
For now, the library makes use of React 18 and does not use TypeScript.
|