Skip to content

Commit 47efc44

Browse files
authored
feat: add isInteger function for integer validation (#59)
* feat: add isInteger function for integer validation - Implemented isInteger function to validate if a string represents an integer value. - Updated metadata documentation to include isInteger with examples. - Added isInteger to index exports and relevant types. - Enhanced existing validation functions (isASCII, isAlphanumeric, isEmail, isHexColor, isUUID, isUrl) to trim leading and trailing whitespace. - Created comprehensive tests for isInteger covering valid, invalid, and edge cases. - Updated assertions and builders to support IntegerString branded type. * refactor: use trimmed variable for string validation in multiple functions * chore(release): bump version to 0.24.0 and update changelog with new features and enhancements * chore: update version to 0.24.0 in package-lock.json
1 parent 9fc8229 commit 47efc44

32 files changed

+970
-287
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
## [0.24.0] - 2025-11-07
11+
12+
### Added
13+
14+
- **New Function: `isInteger()`** - Validates if a string represents an integer value (whole number without decimals)
15+
- Handles positive and negative integers (`42`, `-17`)
16+
- Rejects decimal numbers (`3.14`, `42.0`) - key difference from `isNumeric`
17+
- Automatic whitespace trimming
18+
- Rejects empty strings, `Infinity`, and scientific notation
19+
- Bundle size: 120 bytes gzipped
20+
- 100% test coverage with 80+ test scenarios
21+
- Use cases: age validation, pagination parameters, database ID validation, e-commerce quantities
22+
23+
- **Branded Type Integration for IntegerString** - Full type-safe integer string handling
24+
- New `IntegerString` branded type for compile-time safety
25+
- Type guard: `isValidInteger()` for type narrowing
26+
- Builder function: `toIntegerString()` returns `IntegerString | null` with validation
27+
- Assertion function: `assertIntegerString()` throws `BrandedTypeError` on invalid input
28+
- Unsafe cast: `unsafeIntegerString()` for trusted input (no validation)
29+
- Zero runtime overhead - type safety enforced at compile time
30+
31+
- **CLI Support** - `isInteger` available via `nano-string` command
32+
- `nano-string isInteger "42"` - returns true/false with proper exit codes
33+
- Pipe support for Unix-style command chaining
34+
35+
### Enhanced
36+
37+
- **Whitespace Handling** - Consistent trimming across validation functions
38+
- Updated 6 existing validation functions to trim leading/trailing whitespace before validation
39+
- Functions enhanced: `isASCII`, `isAlphanumeric`, `isEmail`, `isHexColor`, `isUUID`, `isUrl`
40+
- Quality-of-life improvement that handles common user input patterns (e.g., `" user@email.com "`)
41+
- All functions now document whitespace trimming behavior in JSDoc
42+
- Additional test coverage for whitespace handling in each function
43+
44+
### Improved
45+
46+
- **Code Consistency** - Standardized variable naming patterns
47+
- All validation functions now use consistent `const trimmed = str.trim()` pattern instead of reassignment
48+
- More explicit code that avoids parameter mutation
49+
- Better maintainability across the codebase
50+
- Follows best practices established in `isInteger` and `isNumeric`
51+
52+
### Documentation
53+
54+
- Updated function count from 51 to 52 utilities
55+
- Added comprehensive JSDoc documentation for `isInteger` with real-world usage examples
56+
- Updated README with function descriptions, comparison table vs `isNumeric`, examples, and bundle sizes
57+
- Added metadata and playground examples to documentation site
58+
- Regenerated bundle size and performance benchmark data
59+
1060
## [0.23.0] - 2025-10-21
1161

1262
### Added

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fuzzyMatch("usrctrl", "userController.js"); // { matched: true, score: 0.444 }
177177
fuzzyMatch("of", "openFile"); // { matched: true, score: 0.75 }
178178
```
179179

180-
> 📖 **See all 45 functions in the API Reference below**
180+
> 📖 **See all 52 functions in the API Reference below**
181181
182182
## CLI
183183

@@ -269,6 +269,7 @@ nano-string isUrl "https://example.com" # true
269269
nano-string isASCII "hello" # true
270270
nano-string isHexColor "#ff5733" # true
271271
nano-string isNumeric "42" # true
272+
nano-string isInteger "42" # true
272273
nano-string isAlphanumeric "user123" # true
273274
nano-string isUUID "550e8400-e29b-41d4-a716-446655440000" # true
274275
```
@@ -297,7 +298,7 @@ nano-string slugify --help
297298

298299
## API Reference
299300

300-
The library provides 51 string utility functions organized by category. Click on any category to explore the available functions.
301+
The library provides 52 string utility functions organized by category. Click on any category to explore the available functions.
301302

302303
<details>
303304
<summary><b>🔤 Case Conversion Functions (10 functions)</b></summary>
@@ -810,7 +811,7 @@ humanizeList([]);
810811
</details>
811812

812813
<details>
813-
<summary><b>✅ Validation Functions (8 functions)</b></summary>
814+
<summary><b>✅ Validation Functions (9 functions)</b></summary>
814815

815816
### String Validation
816817

@@ -879,6 +880,24 @@ isNumeric("Infinity"); // false
879880
isNumeric("1e5"); // false (scientific notation not supported)
880881
```
881882

883+
#### `isInteger(str: string): boolean`
884+
885+
Validates if a string represents an integer value (whole number without decimals). Useful for validating age fields, quantities, IDs, pagination parameters, and other inputs that should only accept whole numbers.
886+
887+
```javascript
888+
isInteger("42"); // true
889+
isInteger("-17"); // true
890+
isInteger("0"); // true
891+
isInteger(" 42 "); // true (whitespace trimmed)
892+
isInteger("007"); // true (leading zeros allowed)
893+
isInteger("3.14"); // false (decimal number)
894+
isInteger("42.0"); // false (contains decimal point)
895+
isInteger("abc"); // false
896+
isInteger(""); // false
897+
isInteger("Infinity"); // false
898+
isInteger("1e5"); // false (scientific notation not supported)
899+
```
900+
882901
#### `isAlphanumeric(str: string): boolean`
883902

884903
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.
@@ -1567,6 +1586,7 @@ Each utility is optimized to be as small as possible:
15671586
| isASCII | 128 bytes |
15681587
| isHexColor | 103 bytes |
15691588
| isNumeric | 122 bytes |
1589+
| isInteger | 120 bytes |
15701590
| isAlphanumeric | 88 bytes |
15711591
| isUUID | 89 bytes |
15721592
| toASCII | 1.3 KB |

benchmarks/bundle-sizes.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"generated": "2025-10-21T22:00:10.658Z",
2+
"generated": "2025-10-23T20:56:17.411Z",
33
"totalFunctions": 52,
44
"functions": [
55
{
@@ -204,11 +204,11 @@
204204
"nano": {
205205
"bundled": {
206206
"raw": 1030,
207-
"gzip": 573
207+
"gzip": 572
208208
},
209209
"treeShaken": {
210210
"raw": 1030,
211-
"gzip": 573
211+
"gzip": 572
212212
}
213213
},
214214
"lodash": null,
@@ -451,11 +451,11 @@
451451
"nano": {
452452
"bundled": {
453453
"raw": 919,
454-
"gzip": 473
454+
"gzip": 472
455455
},
456456
"treeShaken": {
457457
"raw": 919,
458-
"gzip": 473
458+
"gzip": 472
459459
}
460460
},
461461
"lodash": null,
@@ -467,11 +467,11 @@
467467
"nano": {
468468
"bundled": {
469469
"raw": 555,
470-
"gzip": 335
470+
"gzip": 334
471471
},
472472
"treeShaken": {
473473
"raw": 555,
474-
"gzip": 335
474+
"gzip": 334
475475
}
476476
},
477477
"lodash": null,
@@ -817,11 +817,11 @@
817817
"nano": {
818818
"bundled": {
819819
"raw": 818,
820-
"gzip": 502
820+
"gzip": 501
821821
},
822822
"treeShaken": {
823823
"raw": 818,
824-
"gzip": 502
824+
"gzip": 501
825825
}
826826
},
827827
"lodash": null,

0 commit comments

Comments
 (0)