You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* chore: bump version to 0.20.0 and update package metadata
feat: enhance documentation with examples for string utility functions
- Added detailed examples for `capitalize`, `codePoints`, `deburr`, `detectScript`, `escapeHtml`, `fuzzyMatch`, `graphemes`, `hashString`, `pad`, `padEnd`, `padStart`, `pluralize`, `randomString`, `reverse`, `singularize`, `smartSplit`, `stripHtml`, `toASCII`, and `wordCount`.
feat: introduce SafeHTML type and sanitization functions
- Added `SafeHTML` branded type for sanitized HTML.
- Implemented `toSafeHTML` for sanitizing strings into SafeHTML.
- Added `unsafeSafeHTML` for casting strings to SafeHTML without sanitization.
fix: update size limits in package.json for better performance
- Adjusted size limits for `dist/index.js` and `dist/index.cjs`.
test: add unit tests for new SafeHTML functionality
- Created tests for `toSafeHTML` and `unsafeSafeHTML` to ensure proper sanitization and type safety.
* update package json
Copy file name to clipboardExpand all lines: README.md
+61-8Lines changed: 61 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1310,6 +1310,62 @@ if (validated) {
1310
1310
}
1311
1311
```
1312
1312
1313
+
#### Extending with Custom Branded Types
1314
+
1315
+
The `Brand<T, K>` utility allows you to create your own custom branded types beyond the built-in ones. This is perfect for domain-specific validation without bloating the library.
1316
+
1317
+
```typescript
1318
+
importtype { Brand } from"nano-string-utils";
1319
+
import { sanitize } from"nano-string-utils";
1320
+
1321
+
// Create custom branded types for your domain
1322
+
typePhoneNumber=Brand<string, "PhoneNumber">;
1323
+
typePostalCode=Brand<string, "PostalCode">;
1324
+
typeCreditCard=Brand<string, "CreditCard">;
1325
+
1326
+
// Build type-safe constructors
1327
+
function toPhoneNumber(str:string):PhoneNumber|null {
1328
+
const cleaned =str.replace(/\D/g, "");
1329
+
if (cleaned.length===10||cleaned.length===11) {
1330
+
returncleanedasPhoneNumber;
1331
+
}
1332
+
returnnull;
1333
+
}
1334
+
1335
+
function toPostalCode(str:string):PostalCode|null {
1336
+
// US ZIP code validation
1337
+
if (/^\d{5}(-\d{4})?$/.test(str)) {
1338
+
returnstrasPostalCode;
1339
+
}
1340
+
returnnull;
1341
+
}
1342
+
1343
+
// Type guards for runtime checking
1344
+
function isPhoneNumber(str:string):strisPhoneNumber {
1345
+
returntoPhoneNumber(str) !==null;
1346
+
}
1347
+
1348
+
// Use in your application
1349
+
function sendSMS(phone:PhoneNumber, message:string) {
1350
+
// Can only be called with validated phone numbers
1351
+
console.log(`Sending to ${phone}: ${message}`);
1352
+
}
1353
+
1354
+
const userInput ="(555) 123-4567";
1355
+
const phone =toPhoneNumber(userInput);
1356
+
if (phone) {
1357
+
sendSMS(phone); // ✅ Type safe!
1358
+
}
1359
+
// sendSMS(userInput); // ❌ Type error - string is not PhoneNumber
1360
+
```
1361
+
1362
+
This pattern gives you:
1363
+
1364
+
-**Compile-time safety** - Prevent using unvalidated data
1365
+
-**Zero bundle cost** - Only import what you use from the library
1366
+
-**Domain modeling** - Express business rules in the type system
1367
+
-**Composability** - Mix custom types with built-in branded types
1368
+
1313
1369
### Template Literal Types (TypeScript)
1314
1370
1315
1371
Case conversion functions now provide precise type inference for literal strings at compile time. This feature enhances IDE support with exact type transformations while maintaining full backward compatibility.
@@ -1534,14 +1590,11 @@ We continuously benchmark nano-string-utils against popular alternatives (lodash
1534
1590
### Running Benchmarks
1535
1591
1536
1592
```bash
1537
-
# Run all benchmarks
1538
-
npm run bench:all
1539
-
1540
-
# Run performance benchmarks only
1541
-
npm run bench:perf
1593
+
# Run vitest benchmark tests
1594
+
npm run bench
1542
1595
1543
-
#Run bundle size analysis only
1544
-
npm run bench:size
1596
+
#Generate benchmark data for docs website
1597
+
npm run bench:data
1545
1598
```
1546
1599
1547
1600
### Latest Results
@@ -1578,7 +1631,7 @@ npm run bench:size
1578
1631
1579
1632
- 🏆 **Smallest bundle sizes**: nano-string-utils wins 47 out of 48 tested functions (98% win rate)
1580
1633
- ⚡ **Competitive performance**: Wins 10 out of 14 benchmarked functions against es-toolkit
1581
-
- 📊 **Detailed benchmarks**: See [benchmark-results.md](./benchmarks/benchmark-results.md) for full comparison
1634
+
- 📊 **[View full interactive benchmarks](https://zheruel.github.io/nano-string-utils/#bundle-size)** with detailed comparison
1582
1635
- ⚡ **Optimized performance**:
1583
1636
-**Case conversions**: 3.4M-4.3M ops/s, competitive with es-toolkit
1584
1637
-**Truncate**: 23.4M ops/s for fast string truncation
0 commit comments