Skip to content

Commit c254bd5

Browse files
authored
Add Three New Validation Functions (#51)
* add isNumeric, isAlphaNumeric and isUUID * remove stray "s" * package lock fix * bump version
1 parent fee6a83 commit c254bd5

25 files changed

+2205
-289
lines changed

CHANGELOG.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,61 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.23.0] - 2025-10-21
11+
12+
### Added
13+
14+
- **New Function: `isNumeric()`** - Validates if a string represents a numeric value (integer or decimal)
15+
16+
- Handles positive and negative integers (`42`, `-17`)
17+
- Supports decimal numbers (`3.14`, `-0.5`)
18+
- Automatic whitespace trimming
19+
- Rejects empty strings, `Infinity`, and scientific notation
20+
- Bundle size: 122 bytes gzipped
21+
- 100% test coverage
22+
- Use cases: form validation, CSV parsing, API input validation
23+
24+
- **New Function: `isAlphanumeric()`** - Validates if a string contains only alphanumeric characters (a-z, A-Z, 0-9)
25+
26+
- Strict ASCII validation (no Unicode, special characters, or whitespace)
27+
- Perfect for username validation, identifiers, and tokens
28+
- Bundle size: 88 bytes gzipped
29+
- 100% test coverage
30+
- Use cases: username validation, identifier validation, input sanitization
31+
32+
- **New Function: `isUUID()`** - Validates if a string is a valid UUID in standard 8-4-4-4-12 format
33+
34+
- Supports all UUID versions (v1-v5)
35+
- Accepts NIL UUID (`00000000-0000-0000-0000-000000000000`)
36+
- Case-insensitive validation
37+
- Bundle size: 89 bytes gzipped
38+
- 100% test coverage
39+
- Use cases: API identifier validation, session token validation, database ID validation
40+
41+
- **Branded Type Integration** - Full type-safe handling for all three validation functions
42+
43+
- New branded types: `NumericString`, `AlphanumericString`, `UUID`
44+
- Type guards: `isValidNumeric()`, `isValidAlphanumeric()`, `isValidUUID()`
45+
- Builder functions: `toNumericString()`, `toAlphanumericString()`, `toUUID()`
46+
- Assertion functions: `assertNumericString()`, `assertAlphanumericString()`, `assertUUID()`
47+
- Unsafe casts: `unsafeNumericString()`, `unsafeAlphanumericString()`, `unsafeUUID()`
48+
- Zero runtime overhead - type safety enforced at compile time
49+
50+
- **CLI Support** - All three functions available via `nano-string` command
51+
- `nano-string isNumeric "42"` - returns true/false with proper exit codes
52+
- `nano-string isAlphanumeric "user123"` - validates alphanumeric strings
53+
- `nano-string isUUID "550e8400-e29b-41d4-a716-446655440000"` - validates UUIDs
54+
- Pipe support for Unix-style command chaining
55+
56+
### Documentation
57+
58+
- Updated function count from 48 to 51 utilities
59+
- Added comprehensive JSDoc documentation with real-world usage examples
60+
- Updated README with function descriptions, examples, and bundle sizes
61+
- Added metadata and playground examples to documentation site
62+
- Regenerated bundle size and performance benchmark data
63+
- Updated CLI help with new validation functions
64+
1065
## [0.22.0] - 2025-10-16
1166

1267
### Improved

README.md

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,13 @@ nano-string humanizeList "apple,banana,orange" --conjunction "or" # apple, bana
264264
#### Validation functions
265265

266266
```bash
267-
nano-string isEmail "[email protected]" # true
268-
nano-string isUrl "https://example.com" # true
269-
nano-string isASCII "hello" # true
267+
nano-string isEmail "[email protected]" # true
268+
nano-string isUrl "https://example.com" # true
269+
nano-string isASCII "hello" # true
270+
nano-string isHexColor "#ff5733" # true
271+
nano-string isNumeric "42" # true
272+
nano-string isAlphanumeric "user123" # true
273+
nano-string isUUID "550e8400-e29b-41d4-a716-446655440000" # true
270274
```
271275

272276
#### Analysis functions
@@ -293,7 +297,7 @@ nano-string slugify --help
293297

294298
## API Reference
295299

296-
The library provides 48 string utility functions organized by category. Click on any category to explore the available functions.
300+
The library provides 51 string utility functions organized by category. Click on any category to explore the available functions.
297301

298302
<details>
299303
<summary><b>🔤 Case Conversion Functions (10 functions)</b></summary>
@@ -806,7 +810,7 @@ humanizeList([]);
806810
</details>
807811

808812
<details>
809-
<summary><b>✅ Validation Functions (4 functions)</b></summary>
813+
<summary><b>✅ Validation Functions (8 functions)</b></summary>
810814

811815
### String Validation
812816

@@ -845,6 +849,68 @@ isASCII("abc123!@#"); // true
845849
isASCII(""); // true
846850
```
847851

852+
#### `isHexColor(str: string): boolean`
853+
854+
Validates if a string is a valid hexadecimal color code.
855+
856+
```javascript
857+
isHexColor("#fff"); // true (3-digit)
858+
isHexColor("#ffffff"); // true (6-digit)
859+
isHexColor("#fff8"); // true (4-digit with alpha)
860+
isHexColor("#ffffff80"); // true (8-digit with alpha)
861+
isHexColor("#FFF"); // true (case-insensitive)
862+
isHexColor("fff"); // false (missing #)
863+
isHexColor("#gggggg"); // false (invalid characters)
864+
```
865+
866+
#### `isNumeric(str: string): boolean`
867+
868+
Validates if a string represents a numeric value (integer or decimal).
869+
870+
```javascript
871+
isNumeric("42"); // true
872+
isNumeric("-17"); // true
873+
isNumeric("3.14"); // true
874+
isNumeric("-0.5"); // true
875+
isNumeric(" 42 "); // true (whitespace trimmed)
876+
isNumeric("abc"); // false
877+
isNumeric(""); // false
878+
isNumeric("Infinity"); // false
879+
isNumeric("1e5"); // false (scientific notation not supported)
880+
```
881+
882+
#### `isAlphanumeric(str: string): boolean`
883+
884+
Validates if a string contains only alphanumeric characters (a-z, A-Z, 0-9). Useful for validating usernames, identifiers, and other inputs that should not contain special characters or whitespace.
885+
886+
```javascript
887+
isAlphanumeric("user123"); // true
888+
isAlphanumeric("HelloWorld"); // true
889+
isAlphanumeric("ABC123XYZ"); // true
890+
isAlphanumeric("test"); // true
891+
isAlphanumeric("123"); // true
892+
isAlphanumeric("hello_world"); // false (underscore not allowed)
893+
isAlphanumeric("hello world"); // false (whitespace not allowed)
894+
isAlphanumeric("test-123"); // false (hyphen not allowed)
895+
isAlphanumeric("café"); // false (Unicode not allowed)
896+
isAlphanumeric(""); // false (empty string)
897+
```
898+
899+
#### `isUUID(str: string): boolean`
900+
901+
Validates if a string is a valid UUID (Universally Unique Identifier) in the standard 8-4-4-4-12 format. Accepts all UUID versions (v1-v5), the NIL UUID, and is case-insensitive. Perfect for validating API identifiers, session tokens, and database IDs.
902+
903+
```javascript
904+
isUUID("550e8400-e29b-41d4-a716-446655440000"); // true (v4)
905+
isUUID("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); // true (v1)
906+
isUUID("00000000-0000-0000-0000-000000000000"); // true (NIL UUID)
907+
isUUID("550E8400-E29B-41D4-A716-446655440000"); // true (uppercase)
908+
isUUID("550e8400e29b41d4a716446655440000"); // false (no hyphens)
909+
isUUID("550e8400-e29b-41d4-a716"); // false (too short)
910+
isUUID("not-a-uuid"); // false (invalid format)
911+
isUUID(""); // false (empty string)
912+
```
913+
848914
#### `detectScript(str: string): 'latin' | 'cjk' | 'arabic' | 'cyrillic' | 'hebrew' | 'devanagari' | 'greek' | 'thai' | 'unknown'`
849915

850916
Detects the dominant writing system (script) in a text string.
@@ -1499,6 +1565,10 @@ Each utility is optimized to be as small as possible:
14991565
| isEmail | 148 bytes |
15001566
| isUrl | 155 bytes |
15011567
| isASCII | 128 bytes |
1568+
| isHexColor | 103 bytes |
1569+
| isNumeric | 122 bytes |
1570+
| isAlphanumeric | 88 bytes |
1571+
| isUUID | 89 bytes |
15021572
| toASCII | 1.3 KB |
15031573
| wordCount | 123 bytes |
15041574
| normalizeWhitespace | 268 bytes |

benchmarks/all-functions.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[
2+
"assertAlphanumericString",
3+
"assertNumericString",
4+
"assertUUID",
25
"camelCase",
36
"capitalize",
47
"classifyText",
@@ -17,9 +20,15 @@
1720
"highlight",
1821
"humanizeList",
1922
"isASCII",
23+
"isAlphanumeric",
2024
"isEmail",
2125
"isHexColor",
26+
"isNumeric",
27+
"isUUID",
2228
"isUrl",
29+
"isValidAlphanumeric",
30+
"isValidNumeric",
31+
"isValidUUID",
2332
"kebabCase",
2433
"levenshtein",
2534
"levenshteinNormalized",
@@ -46,6 +55,12 @@
4655
"templateSafe",
4756
"titleCase",
4857
"toASCII",
58+
"toAlphanumericString",
59+
"toNumericString",
60+
"toUUID",
4961
"truncate",
62+
"unsafeAlphanumericString",
63+
"unsafeNumericString",
64+
"unsafeUUID",
5065
"wordCount"
5166
]

0 commit comments

Comments
 (0)