phani-validations 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +174 -2
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/utils/helpers.js +47 -0
- package/dist/cjs/validators/characters.js +260 -0
- package/dist/cjs/validators/common.js +261 -0
- package/dist/cjs/validators/email.js +103 -0
- package/dist/cjs/validators/password.js +161 -0
- package/dist/cjs/validators/schema.js +78 -0
- package/dist/esm/index.js +122 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/utils/helpers.js +39 -0
- package/dist/esm/validators/characters.js +245 -0
- package/dist/esm/validators/common.js +249 -0
- package/dist/esm/validators/email.js +98 -0
- package/dist/esm/validators/password.js +160 -0
- package/dist/esm/validators/schema.js +74 -0
- package/dist/index.d.ts +238 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Phani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# phani-validations
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight, zero-dependency validation library for JavaScript and TypeScript projects. Works with **ESM (`import`)**, **CommonJS (`require`)**, and includes full **TypeScript definitions (`.d.ts`)**.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Email validation**: `isEmail`, `validateEmail` (domain allowlist/blocklist, strict RFC checks)
|
|
8
|
+
- **Password validation**: `isPassword`, `isStrongPassword`, `validatePassword`, `getPasswordStrength`
|
|
9
|
+
- **Numbers & Digits**: `isNumber`, `isNumeric`, `hasNumbers`, `isDigits`, `hasDigits`
|
|
10
|
+
- **Alphabets & Alphanumeric**: `isAlphabets`, `isAlpha`, `hasAlphabets`, `hasAlpha`, `isAlphanumeric`
|
|
11
|
+
- **Special Characters & Case**: `isSpecialChars`, `hasSpecialChars`, `hasNoSpecialChars`, `hasUppercase`, `hasLowercase`, `isUppercase`, `isLowercase`, `hasSpaces`
|
|
12
|
+
- **Common Validators**: `isEmpty`, `isRequired`, `isLength`, `isRange`, `isPhone`, `isURL`, `isUsername`, `isUUID`, `matches`
|
|
13
|
+
- **Form / Object Schema Validation**: `validate(data, schema)`
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Installation (in another project)
|
|
18
|
+
|
|
19
|
+
After publishing to npm:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install phani-validations
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### ES Modules (`import`)
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import {
|
|
33
|
+
isEmail,
|
|
34
|
+
validatePassword,
|
|
35
|
+
isNumber,
|
|
36
|
+
isDigits,
|
|
37
|
+
isAlphabets,
|
|
38
|
+
isAlphanumeric,
|
|
39
|
+
hasSpecialChars,
|
|
40
|
+
validate,
|
|
41
|
+
} from 'phani-validations';
|
|
42
|
+
|
|
43
|
+
isEmail('user@example.com'); // true
|
|
44
|
+
isNumber('-42.5'); // true
|
|
45
|
+
isDigits('123456'); // true
|
|
46
|
+
isAlphabets('Hello'); // true
|
|
47
|
+
isAlphanumeric('User123'); // true
|
|
48
|
+
hasSpecialChars('Pass@1'); // true
|
|
49
|
+
|
|
50
|
+
const passResult = validatePassword('Pass@123');
|
|
51
|
+
// {
|
|
52
|
+
// valid: true,
|
|
53
|
+
// errors: [],
|
|
54
|
+
// strength: 'strong',
|
|
55
|
+
// checks: { minLength: true, maxLength: true, uppercase: true, lowercase: true, digits: true, specialChars: true, noSpaces: true }
|
|
56
|
+
// }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Default Object Import
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import validation from 'phani-validations';
|
|
63
|
+
|
|
64
|
+
validation.isEmail('user@example.com'); // true
|
|
65
|
+
validation.isDigits('90210'); // true
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### CommonJS (`require`)
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const { isEmail, isPassword, isAlphanumeric } = require('phani-validations');
|
|
72
|
+
|
|
73
|
+
console.log(isEmail('hello@example.com')); // true
|
|
74
|
+
console.log(isPassword('Strong@123')); // true
|
|
75
|
+
console.log(isAlphanumeric('abc123')); // true
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Form / Object Validation
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { validate, isEmail, validatePassword, isDigits, isLength } from 'phani-validations';
|
|
82
|
+
|
|
83
|
+
const result = validate(
|
|
84
|
+
{
|
|
85
|
+
email: 'dev@example.com',
|
|
86
|
+
password: 'weak',
|
|
87
|
+
otp: '123456',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
email: [{ validator: isEmail, message: 'Enter a valid email address.' }],
|
|
91
|
+
password: [{ validator: validatePassword }],
|
|
92
|
+
otp: [
|
|
93
|
+
{ validator: isDigits, message: 'OTP must contain only digits.' },
|
|
94
|
+
{ validator: isLength, options: { min: 6, max: 6 }, message: 'OTP must be 6 digits.' },
|
|
95
|
+
],
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
console.log(result.valid); // false
|
|
100
|
+
console.log(result.errors); // { password: [...] }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## API Reference
|
|
106
|
+
|
|
107
|
+
| Category | Function | Description |
|
|
108
|
+
| --- | --- | --- |
|
|
109
|
+
| **Email** | `isEmail(value, options?)` | Returns `true` if valid email (`{ trim, allowedDomains, blockedDomains }`) |
|
|
110
|
+
| | `validateEmail(value, options?)` | Returns `{ valid, errors }` |
|
|
111
|
+
| **Password** | `isPassword(value, options?)` | Returns `true` if password passes all rules |
|
|
112
|
+
| | `isStrongPassword(value, options?)` | Alias for `isPassword` |
|
|
113
|
+
| | `validatePassword(value, options?)` | Returns `{ valid, errors, strength, checks }` |
|
|
114
|
+
| | `getPasswordStrength(value)` | Returns `'weak' \| 'medium' \| 'strong'` |
|
|
115
|
+
| **Numbers** | `isNumber(value, options?)` | Validates integer/decimal/signed numbers (`{ allowNegative, allowDecimal, allowPlus }`) |
|
|
116
|
+
| | `isNumeric(value, options?)` | Alias for `isNumber` |
|
|
117
|
+
| | `hasNumbers(value)` | Contains at least one digit (`0-9`) |
|
|
118
|
+
| **Digits** | `isDigits(value)` | Strictly digits `0-9` only (no signs or decimals) |
|
|
119
|
+
| | `hasDigits(value)` | Contains at least one digit (`0-9`) |
|
|
120
|
+
| **Alphabets** | `isAlphabets(value, options?)` | Only `A-Za-z` (`{ allowSpaces }`) |
|
|
121
|
+
| | `isAlpha(value, options?)` | Alias for `isAlphabets` |
|
|
122
|
+
| | `hasAlphabets(value)` / `hasAlpha(value)` | Contains at least one letter |
|
|
123
|
+
| **Alphanumeric** | `isAlphanumeric(value, options?)` | Only `A-Za-z0-9` (`{ allowSpaces, allowUnderscore, allowHyphen }`) |
|
|
124
|
+
| **Special Chars** | `isSpecialChars(value, customChars?)` | Exclusively special characters |
|
|
125
|
+
| | `hasSpecialChars(value, customChars?)` | Contains at least one special character |
|
|
126
|
+
| | `hasNoSpecialChars(value, customChars?)` | Contains no special characters |
|
|
127
|
+
| **Case & Space** | `hasUppercase(value)` / `hasLowercase(value)` | Contains uppercase / lowercase letter |
|
|
128
|
+
| | `isUppercase(value)` / `isLowercase(value)` | All letters uppercase / lowercase |
|
|
129
|
+
| | `hasSpaces(value)` | Contains whitespace |
|
|
130
|
+
| **Common** | `isEmpty(value, options?)` / `isRequired(value, options?)` | Empty / non-empty check |
|
|
131
|
+
| | `isLength(value, { min, max })` | String length range |
|
|
132
|
+
| | `isRange(value, { min, max })` | Numeric value range |
|
|
133
|
+
| | `isPhone(value, options?)` | Phone number check |
|
|
134
|
+
| | `isURL(value, options?)` | URL validation |
|
|
135
|
+
| | `isUsername(value, options?)` | Username format validation |
|
|
136
|
+
| | `isUUID(value, version?)` | UUID v1–v5 validation |
|
|
137
|
+
| | `matches(value, pattern, flags?)` | Custom regex check |
|
|
138
|
+
| **Schema** | `validate(data, schema)` | Validates an object against field rules |
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## Publishing to npm
|
|
143
|
+
|
|
144
|
+
1. Update `"name"` in `package.json` to a unique package name (or scoped name like `"@your-npm-username/input-validator"`).
|
|
145
|
+
2. Run tests and build:
|
|
146
|
+
```bash
|
|
147
|
+
npm test
|
|
148
|
+
```
|
|
149
|
+
3. Log in to npm:
|
|
150
|
+
```bash
|
|
151
|
+
npm login
|
|
152
|
+
```
|
|
153
|
+
4. Publish:
|
|
154
|
+
```bash
|
|
155
|
+
npm publish
|
|
156
|
+
# If using a scoped package name (@username/pkg-name):
|
|
157
|
+
npm publish --access public
|
|
158
|
+
```
|
|
159
|
+
5. Install and use in any other project:
|
|
160
|
+
```bash
|
|
161
|
+
npm install <your-package-name>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Local Testing Before Publishing
|
|
165
|
+
|
|
166
|
+
You can test installing this library in another local project before publishing to npm:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
# Inside this library folder:
|
|
170
|
+
npm pack
|
|
171
|
+
# This creates phani-validations-1.0.0.tgz
|
|
172
|
+
|
|
173
|
+
# Inside your other project:
|
|
174
|
+
npm install /path/to/npm_library/phani-validations-1.0.0.tgz
|
|
175
|
+
```
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isEmail, validateEmail } = require('./validators/email.js');
|
|
4
|
+
const { isPassword, isStrongPassword, validatePassword, getPasswordStrength } = require('./validators/password.js');
|
|
5
|
+
const { isNumber, isNumeric, hasNumbers, isDigits, hasDigits, isAlphabets, isAlpha, hasAlphabets, hasAlpha, isAlphanumeric, isSpecialChars, hasSpecialChars, hasNoSpecialChars, hasUppercase, hasLowercase, isUppercase, isLowercase, hasSpaces } = require('./validators/characters.js');
|
|
6
|
+
const { isEmpty, isRequired, isLength, isRange, isPhone, isURL, isUsername, isUUID, matches } = require('./validators/common.js');
|
|
7
|
+
const { validate } = require('./validators/schema.js');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const validation = {
|
|
12
|
+
isEmail,
|
|
13
|
+
validateEmail,
|
|
14
|
+
isPassword,
|
|
15
|
+
isStrongPassword,
|
|
16
|
+
validatePassword,
|
|
17
|
+
getPasswordStrength,
|
|
18
|
+
isNumber,
|
|
19
|
+
isNumeric,
|
|
20
|
+
hasNumbers,
|
|
21
|
+
isDigits,
|
|
22
|
+
hasDigits,
|
|
23
|
+
isAlphabets,
|
|
24
|
+
isAlpha,
|
|
25
|
+
hasAlphabets,
|
|
26
|
+
hasAlpha,
|
|
27
|
+
isAlphanumeric,
|
|
28
|
+
isSpecialChars,
|
|
29
|
+
hasSpecialChars,
|
|
30
|
+
hasNoSpecialChars,
|
|
31
|
+
hasUppercase,
|
|
32
|
+
hasLowercase,
|
|
33
|
+
isUppercase,
|
|
34
|
+
isLowercase,
|
|
35
|
+
hasSpaces,
|
|
36
|
+
isEmpty,
|
|
37
|
+
isRequired,
|
|
38
|
+
isLength,
|
|
39
|
+
isRange,
|
|
40
|
+
isPhone,
|
|
41
|
+
isURL,
|
|
42
|
+
isUsername,
|
|
43
|
+
isUUID,
|
|
44
|
+
matches,
|
|
45
|
+
validate,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.validation = validation;
|
|
49
|
+
exports.isEmail = isEmail;
|
|
50
|
+
exports.validateEmail = validateEmail;
|
|
51
|
+
exports.isPassword = isPassword;
|
|
52
|
+
exports.isStrongPassword = isStrongPassword;
|
|
53
|
+
exports.validatePassword = validatePassword;
|
|
54
|
+
exports.getPasswordStrength = getPasswordStrength;
|
|
55
|
+
exports.isNumber = isNumber;
|
|
56
|
+
exports.isNumeric = isNumeric;
|
|
57
|
+
exports.hasNumbers = hasNumbers;
|
|
58
|
+
exports.isDigits = isDigits;
|
|
59
|
+
exports.hasDigits = hasDigits;
|
|
60
|
+
exports.isAlphabets = isAlphabets;
|
|
61
|
+
exports.isAlpha = isAlpha;
|
|
62
|
+
exports.hasAlphabets = hasAlphabets;
|
|
63
|
+
exports.hasAlpha = hasAlpha;
|
|
64
|
+
exports.isAlphanumeric = isAlphanumeric;
|
|
65
|
+
exports.isSpecialChars = isSpecialChars;
|
|
66
|
+
exports.hasSpecialChars = hasSpecialChars;
|
|
67
|
+
exports.hasNoSpecialChars = hasNoSpecialChars;
|
|
68
|
+
exports.hasUppercase = hasUppercase;
|
|
69
|
+
exports.hasLowercase = hasLowercase;
|
|
70
|
+
exports.isUppercase = isUppercase;
|
|
71
|
+
exports.isLowercase = isLowercase;
|
|
72
|
+
exports.hasSpaces = hasSpaces;
|
|
73
|
+
exports.isEmpty = isEmpty;
|
|
74
|
+
exports.isRequired = isRequired;
|
|
75
|
+
exports.isLength = isLength;
|
|
76
|
+
exports.isRange = isRange;
|
|
77
|
+
exports.isPhone = isPhone;
|
|
78
|
+
exports.isURL = isURL;
|
|
79
|
+
exports.isUsername = isUsername;
|
|
80
|
+
exports.isUUID = isUUID;
|
|
81
|
+
exports.matches = matches;
|
|
82
|
+
exports.validate = validate;
|
|
83
|
+
exports.default = validation;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a value is a non-empty string.
|
|
5
|
+
* @param {unknown} value
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
function isNonEmptyString(value) {
|
|
9
|
+
return typeof value === 'string' && value.length > 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Escapes special regular expression characters in a string.
|
|
14
|
+
* @param {string} str
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
function escapeRegExp(str) {
|
|
18
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Normalizes a string or finite number into a string representation.
|
|
23
|
+
* Returns null for all other types (booleans, objects, null, undefined, NaN, Infinity).
|
|
24
|
+
* @param {unknown} value
|
|
25
|
+
* @returns {string | null}
|
|
26
|
+
*/
|
|
27
|
+
function toNumericString(value) {
|
|
28
|
+
if (typeof value === 'number') {
|
|
29
|
+
return Number.isFinite(value) ? String(value) : null;
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === 'string') {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Default printable ASCII special characters (non-alphanumeric, non-whitespace).
|
|
39
|
+
*/
|
|
40
|
+
const SPECIAL_CHAR_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
|
|
41
|
+
const ONLY_SPECIAL_CHARS_REGEX = /^[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]+$/;
|
|
42
|
+
|
|
43
|
+
exports.isNonEmptyString = isNonEmptyString;
|
|
44
|
+
exports.escapeRegExp = escapeRegExp;
|
|
45
|
+
exports.toNumericString = toNumericString;
|
|
46
|
+
exports.SPECIAL_CHAR_REGEX = SPECIAL_CHAR_REGEX;
|
|
47
|
+
exports.ONLY_SPECIAL_CHARS_REGEX = ONLY_SPECIAL_CHARS_REGEX;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isNonEmptyString, escapeRegExp, toNumericString, SPECIAL_CHAR_REGEX, ONLY_SPECIAL_CHARS_REGEX } = require('../utils/helpers.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a value is a valid number (string or finite JS number).
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {boolean} [options.allowNegative=true] - Allow leading "-" sign.
|
|
11
|
+
* @param {boolean} [options.allowDecimal=true] - Allow decimal point.
|
|
12
|
+
* @param {boolean} [options.allowPlus=true] - Allow leading "+" sign.
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
function isNumber(value, options = {}) {
|
|
16
|
+
const { allowNegative = true, allowDecimal = true, allowPlus = true } = options;
|
|
17
|
+
const str = toNumericString(value);
|
|
18
|
+
if (str === null || str.length === 0) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let signPattern = '';
|
|
23
|
+
if (allowNegative && allowPlus) {
|
|
24
|
+
signPattern = '[+-]?';
|
|
25
|
+
} else if (allowNegative) {
|
|
26
|
+
signPattern = '-?';
|
|
27
|
+
} else if (allowPlus) {
|
|
28
|
+
signPattern = '\\+?';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const bodyPattern = allowDecimal ? '(?:\\d+(?:\\.\\d+)?|\\.\\d+)' : '\\d+';
|
|
32
|
+
const regex = new RegExp(`^${signPattern}${bodyPattern}$`);
|
|
33
|
+
return regex.test(str);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Alias for {@link isNumber}.
|
|
38
|
+
*/
|
|
39
|
+
function isNumeric(value, options) {
|
|
40
|
+
return isNumber(value, options);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether a value contains at least one digit (0-9).
|
|
45
|
+
*
|
|
46
|
+
* @param {unknown} value
|
|
47
|
+
* @returns {boolean}
|
|
48
|
+
*/
|
|
49
|
+
function hasDigits(value) {
|
|
50
|
+
const str = toNumericString(value);
|
|
51
|
+
return str !== null && /\d/.test(str);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Alias for {@link hasDigits}. Checks whether a value contains at least one numeric digit.
|
|
56
|
+
*
|
|
57
|
+
* @param {unknown} value
|
|
58
|
+
* @returns {boolean}
|
|
59
|
+
*/
|
|
60
|
+
function hasNumbers(value) {
|
|
61
|
+
return hasDigits(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Checks whether a value consists strictly of ASCII digits (0-9).
|
|
66
|
+
* Rejects signs ("+", "-"), decimals ("."), and whitespace.
|
|
67
|
+
*
|
|
68
|
+
* @param {unknown} value
|
|
69
|
+
* @returns {boolean}
|
|
70
|
+
*/
|
|
71
|
+
function isDigits(value) {
|
|
72
|
+
const str = toNumericString(value);
|
|
73
|
+
return str !== null && /^\d+$/.test(str);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Checks whether a string consists only of English alphabetic characters (A-Z, a-z).
|
|
78
|
+
*
|
|
79
|
+
* @param {unknown} value
|
|
80
|
+
* @param {object} [options]
|
|
81
|
+
* @param {boolean} [options.allowSpaces=false] - Allow spaces between letters.
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
function isAlphabets(value, options = {}) {
|
|
85
|
+
if (!isNonEmptyString(value)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const { allowSpaces = false } = options;
|
|
89
|
+
if (allowSpaces) {
|
|
90
|
+
return /^[A-Za-z ]+$/.test(value) && /[A-Za-z]/.test(value);
|
|
91
|
+
}
|
|
92
|
+
return /^[A-Za-z]+$/.test(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Alias for {@link isAlphabets}.
|
|
97
|
+
*/
|
|
98
|
+
function isAlpha(value, options) {
|
|
99
|
+
return isAlphabets(value, options);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Checks whether a string contains at least one alphabetic character (A-Z, a-z).
|
|
104
|
+
*
|
|
105
|
+
* @param {unknown} value
|
|
106
|
+
* @returns {boolean}
|
|
107
|
+
*/
|
|
108
|
+
function hasAlphabets(value) {
|
|
109
|
+
return isNonEmptyString(value) && /[A-Za-z]/.test(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Alias for {@link hasAlphabets}.
|
|
114
|
+
*/
|
|
115
|
+
function hasAlpha(value) {
|
|
116
|
+
return hasAlphabets(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Checks whether a string consists only of alphanumeric characters (A-Z, a-z, 0-9).
|
|
121
|
+
*
|
|
122
|
+
* @param {unknown} value
|
|
123
|
+
* @param {object} [options]
|
|
124
|
+
* @param {boolean} [options.allowSpaces=false] - Allow spaces.
|
|
125
|
+
* @param {boolean} [options.allowUnderscore=false] - Allow underscores ("_").
|
|
126
|
+
* @param {boolean} [options.allowHyphen=false] - Allow hyphens ("-").
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*/
|
|
129
|
+
function isAlphanumeric(value, options = {}) {
|
|
130
|
+
if (!isNonEmptyString(value)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const { allowSpaces = false, allowUnderscore = false, allowHyphen = false } = options;
|
|
134
|
+
let allowed = 'A-Za-z0-9';
|
|
135
|
+
if (allowSpaces) allowed += ' ';
|
|
136
|
+
if (allowUnderscore) allowed += '_';
|
|
137
|
+
if (allowHyphen) allowed += '\\-';
|
|
138
|
+
|
|
139
|
+
const regex = new RegExp(`^[${allowed}]+$`);
|
|
140
|
+
return regex.test(value) && /[A-Za-z0-9]/.test(value);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Checks whether a string contains at least one special character.
|
|
145
|
+
*
|
|
146
|
+
* @param {unknown} value
|
|
147
|
+
* @param {string} [customChars] - Optional string of characters to treat as special characters.
|
|
148
|
+
* @returns {boolean}
|
|
149
|
+
*/
|
|
150
|
+
function hasSpecialChars(value, customChars) {
|
|
151
|
+
if (!isNonEmptyString(value)) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (typeof customChars === 'string' && customChars.length > 0) {
|
|
155
|
+
const escaped = escapeRegExp(customChars);
|
|
156
|
+
return new RegExp(`[${escaped}]`).test(value);
|
|
157
|
+
}
|
|
158
|
+
return SPECIAL_CHAR_REGEX.test(value);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Checks whether a string consists exclusively of special characters.
|
|
163
|
+
*
|
|
164
|
+
* @param {unknown} value
|
|
165
|
+
* @param {string} [customChars] - Optional string of characters to treat as special characters.
|
|
166
|
+
* @returns {boolean}
|
|
167
|
+
*/
|
|
168
|
+
function isSpecialChars(value, customChars) {
|
|
169
|
+
if (!isNonEmptyString(value)) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
if (typeof customChars === 'string' && customChars.length > 0) {
|
|
173
|
+
const escaped = escapeRegExp(customChars);
|
|
174
|
+
return new RegExp(`^[${escaped}]+$`).test(value);
|
|
175
|
+
}
|
|
176
|
+
return ONLY_SPECIAL_CHARS_REGEX.test(value);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Checks whether a string contains no special characters.
|
|
181
|
+
*
|
|
182
|
+
* @param {unknown} value
|
|
183
|
+
* @param {string} [customChars] - Optional string of special characters to disallow.
|
|
184
|
+
* @returns {boolean}
|
|
185
|
+
*/
|
|
186
|
+
function hasNoSpecialChars(value, customChars) {
|
|
187
|
+
if (typeof value !== 'string') {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return !hasSpecialChars(value, customChars);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Checks whether a string contains at least one uppercase letter (A-Z).
|
|
195
|
+
*
|
|
196
|
+
* @param {unknown} value
|
|
197
|
+
* @returns {boolean}
|
|
198
|
+
*/
|
|
199
|
+
function hasUppercase(value) {
|
|
200
|
+
return isNonEmptyString(value) && /[A-Z]/.test(value);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Checks whether a string contains at least one lowercase letter (a-z).
|
|
205
|
+
*
|
|
206
|
+
* @param {unknown} value
|
|
207
|
+
* @returns {boolean}
|
|
208
|
+
*/
|
|
209
|
+
function hasLowercase(value) {
|
|
210
|
+
return isNonEmptyString(value) && /[a-z]/.test(value);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Checks whether all alphabetic characters in a non-empty string are uppercase.
|
|
215
|
+
*
|
|
216
|
+
* @param {unknown} value
|
|
217
|
+
* @returns {boolean}
|
|
218
|
+
*/
|
|
219
|
+
function isUppercase(value) {
|
|
220
|
+
return isNonEmptyString(value) && /[A-Z]/.test(value) && value === value.toUpperCase();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Checks whether all alphabetic characters in a non-empty string are lowercase.
|
|
225
|
+
*
|
|
226
|
+
* @param {unknown} value
|
|
227
|
+
* @returns {boolean}
|
|
228
|
+
*/
|
|
229
|
+
function isLowercase(value) {
|
|
230
|
+
return isNonEmptyString(value) && /[a-z]/.test(value) && value === value.toLowerCase();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Checks whether a string contains any whitespace character.
|
|
235
|
+
*
|
|
236
|
+
* @param {unknown} value
|
|
237
|
+
* @returns {boolean}
|
|
238
|
+
*/
|
|
239
|
+
function hasSpaces(value) {
|
|
240
|
+
return typeof value === 'string' && /\s/.test(value);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
exports.isNumber = isNumber;
|
|
244
|
+
exports.isNumeric = isNumeric;
|
|
245
|
+
exports.hasDigits = hasDigits;
|
|
246
|
+
exports.hasNumbers = hasNumbers;
|
|
247
|
+
exports.isDigits = isDigits;
|
|
248
|
+
exports.isAlphabets = isAlphabets;
|
|
249
|
+
exports.isAlpha = isAlpha;
|
|
250
|
+
exports.hasAlphabets = hasAlphabets;
|
|
251
|
+
exports.hasAlpha = hasAlpha;
|
|
252
|
+
exports.isAlphanumeric = isAlphanumeric;
|
|
253
|
+
exports.hasSpecialChars = hasSpecialChars;
|
|
254
|
+
exports.isSpecialChars = isSpecialChars;
|
|
255
|
+
exports.hasNoSpecialChars = hasNoSpecialChars;
|
|
256
|
+
exports.hasUppercase = hasUppercase;
|
|
257
|
+
exports.hasLowercase = hasLowercase;
|
|
258
|
+
exports.isUppercase = isUppercase;
|
|
259
|
+
exports.isLowercase = isLowercase;
|
|
260
|
+
exports.hasSpaces = hasSpaces;
|