Commit d5720c0
Add string utility function: truncate (#70)
This PR adds the essential `truncate` string utility function from the
VS Code codebase to enhance the javascript-utils library.
## New Function
### `truncate(value, maxLength, suffix?)`
Truncates a string to a maximum length, adding an optional suffix
(default empty string) if truncated.
```typescript
truncate("hello world", 8) // "hello wo"
truncate("hello world", 15) // "hello world"
truncate("hello", 3, "...") // "hel..."
```
## Features
- **Robust null/undefined handling**: Function gracefully handles null
and undefined inputs
- **Comprehensive test coverage**: Extensive test cases covering edge
cases and various input scenarios
- **TypeScript type safety**: Proper type definitions with union types
for null/undefined safety
- **Code style compliance**: Follows project linting rules and
formatting standards
- **Consistent API**: Follows the same patterns as existing utility
functions in the library
## Implementation Notes
The function is based on the VS Code source implementation but adapted
to match this library's patterns for null/undefined handling and error
recovery. The function returns sensible defaults rather than throwing
errors for invalid inputs.
- [x] Implement truncate function with proper null/undefined handling
- [x] Add comprehensive test coverage including edge cases
- [x] Update changelog with new function
- [x] Ensure code style compliance and linting passes
- [x] Add test group for truncate without suffix parameter
Fixes #29.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: drebrez <1611547+drebrez@users.noreply.github.com>
Co-authored-by: manni497 <65862280+manni497@users.noreply.github.com>1 parent 06e711c commit d5720c0
3 files changed
Lines changed: 60 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
87 | 123 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
0 commit comments