Skip to content

Commit d2f80a5

Browse files
committed
feat!: spacer functions
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 97e7dc7 commit d2f80a5

File tree

21 files changed

+457
-141
lines changed

21 files changed

+457
-141
lines changed

README.md

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ Wrap a string
2626
- [`wrap(thing, config[, options])`](#wrapthing-config-options)
2727
- [Types](#types)
2828
- [`Config`](#config)
29-
- [`LinePadding`](#linepadding)
3029
- [`LinesInfo`](#linesinfo)
3130
- [`Options`](#options)
31+
- [`SpacerFunction<[T]>`](#spacerfunctiont)
32+
- [`Spacer`](#spacer)
3233
- [`StripAnsi`](#stripansi)
3334
- [`ToString<[T]>`](#tostringt)
3435
- [Contribute](#contribute)
@@ -187,14 +188,6 @@ String wrapping configuration (`interface`).
187188
- `columns` (`number` | `string`)
188189
— the number of columns to wrap the string to
189190

190-
### `LinePadding`
191-
192-
The strings used to pad either side of each line (`type`).
193-
194-
```ts
195-
type LinePadding = [left: string, right: string]
196-
```
197-
198191
### `LinesInfo`
199192

200193
Info about the lines of a wrapped string (`interface`).
@@ -203,12 +196,14 @@ Info about the lines of a wrapped string (`interface`).
203196

204197
- `eol` (`string`)
205198
— the character, or characters, used to mark the end of a line
206-
- `indent` (`string`)
207-
— the string used to indent each line
199+
- `indent` ([`SpacerFunction<string>`](#spacerfunctiont))
200+
get the string used to indent each line
208201
- `lines` (`readonly string[]`)
209202
— the list of lines forming the wrapped string
210-
- `padding` ([`LinePadding`](#linepadding))
211-
— the strings used to pad either side of each line
203+
- `padLeft` ([`SpacerFunction<string>`](#spacerfunctiont))
204+
— get the string used to pad the left side of each line
205+
- `padRight` ([`SpacerFunction<string>`](#spacerfunctiont))
206+
— get the string used to pad the right side of each line
212207

213208
### `Options`
214209

@@ -229,11 +224,11 @@ Options for wrapping a string (`interface`).
229224
by default, long words remain unbroken and push onto the next line if they don't fit on the current line.\
230225
setting this to `true` will break long words.
231226
> 👉 **note**: setting this to `true` will break words.
232-
- `indent?` (`number` | `string` | `null` | `undefined`, optional)
227+
- `indent?` ([`SpacerFunction`](#spacerfunctiont) | `number` | `string` | `null` | `undefined`, optional)
233228
— the size of the string to use for indenting each line (as a number or numeric), or the string itself
234-
- `padLeft?` (`number` | `string` | `null` | `undefined`, optional)
229+
- `padLeft?` ([`SpacerFunction`](#spacerfunctiont) | `number` | `string` | `null` | `undefined`, optional)
235230
— the size of the string to use for padding the left side of each line (as a number or numeric), or the string itself
236-
- `padRight?` (`number` | `string` | `null` | `undefined`, optional)
231+
- `padRight?` ([`SpacerFunction`](#spacerfunctiont) | `number` | `string` | `null` | `undefined`, optional)
237232
— the size of the string to use for padding the right side of each line (as a number or numeric), or the string itself
238233
- `stringify?` ([`ToString`](#tostringt) | `null` | `undefined`, optional)
239234
— convert a value to a string
@@ -247,6 +242,54 @@ Options for wrapping a string (`interface`).
247242
> 👉 **note**: lines are trimmed before applying indents or padding.
248243
- default: `true`
249244

245+
### `SpacerFunction<[T]>`
246+
247+
Get a spacer configuration for the line at `index` (`type`).
248+
249+
Spacers can be used to indent a line, and/or pad either side of it.
250+
251+
```ts
252+
type SpacerFunction<
253+
T extends number | string | null | undefined =
254+
| number
255+
| string
256+
| null
257+
| undefined
258+
> = (
259+
this: void,
260+
index: number,
261+
lines?: readonly string[] | null | undefined
262+
) => T
263+
```
264+
265+
#### Type Parameters
266+
267+
- `T` (`number` | `string` | `null` | `undefined`, optional)
268+
— the spacer configuration
269+
270+
#### Parameters
271+
272+
- `index` (`number`)
273+
— the index of the current line
274+
- `lines` (`readonly string[]` | `null` | `undefined`, optional)
275+
— the current list of lines
276+
277+
#### Returns
278+
279+
(`T`) The spacer configuration
280+
281+
### `Spacer`
282+
283+
Union of spacer configurations (`type`).
284+
285+
Spacers can be used to indent a line, and/or pad either side of it.
286+
287+
Valid spacer configurations are functions, sizes (as a number or numeric), or the spacer itself.
288+
289+
```ts
290+
type Spacer = SpacerFunction | number | string
291+
```
292+
250293
### `StripAnsi`
251294
252295
Remove ANSI escape codes from a string (`type`).

src/interfaces/__tests__/lines-info.spec-d.mts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
*/
55

66
import type TestSubject from '#interfaces/lines-info'
7-
import type { LinePadding } from '@flex-development/string-wrap'
7+
import type { SpacerFunction } from '@flex-development/string-wrap'
88

99
describe('unit-d:interfaces/LinesInfo', () => {
1010
it('should match [eol: string]', () => {
1111
expectTypeOf<TestSubject>().toHaveProperty('eol').toEqualTypeOf<string>()
1212
})
1313

14-
it('should match [indent: string]', () => {
15-
expectTypeOf<TestSubject>().toHaveProperty('indent').toEqualTypeOf<string>()
14+
it('should match [indent: SpacerFunction<string>]', () => {
15+
expectTypeOf<TestSubject>()
16+
.toHaveProperty('indent')
17+
.toEqualTypeOf<SpacerFunction<string>>()
1618
})
1719

1820
it('should match [lines: readonly string[]]', () => {
@@ -21,9 +23,15 @@ describe('unit-d:interfaces/LinesInfo', () => {
2123
.toEqualTypeOf<readonly string[]>()
2224
})
2325

24-
it('should match [padding: LinePadding]', () => {
26+
it('should match [padLeft: SpacerFunction<string>]', () => {
27+
expectTypeOf<TestSubject>()
28+
.toHaveProperty('padLeft')
29+
.toEqualTypeOf<SpacerFunction<string>>()
30+
})
31+
32+
it('should match [padRight: SpacerFunction<string>]', () => {
2533
expectTypeOf<TestSubject>()
26-
.toHaveProperty('padding')
27-
.toEqualTypeOf<LinePadding>()
34+
.toHaveProperty('padRight')
35+
.toEqualTypeOf<SpacerFunction<string>>()
2836
})
2937
})

src/interfaces/__tests__/options.spec-d.mts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import type TestSubject from '#interfaces/options'
7-
import type { StripAnsi, ToString } from '@flex-development/string-wrap'
7+
import type { Spacer, StripAnsi, ToString } from '@flex-development/string-wrap'
88
import type { Nilable, OptionalKeys } from '@flex-development/tutils'
99

1010
describe('unit-d:interfaces/Options', () => {
@@ -30,22 +30,22 @@ describe('unit-d:interfaces/Options', () => {
3030
.toEqualTypeOf<Nilable<boolean>>()
3131
})
3232

33-
it('should match [indent?: number | string | null | undefined]', () => {
33+
it('should match [indent?: Spacer | null | undefined]', () => {
3434
expectTypeOf<TestSubject>()
3535
.toHaveProperty('indent')
36-
.toEqualTypeOf<Nilable<number | string>>()
36+
.toEqualTypeOf<Nilable<Spacer>>()
3737
})
3838

39-
it('should match [padLeft?: number | string | null | undefined]', () => {
39+
it('should match [padLeft?: Spacer | null | undefined]', () => {
4040
expectTypeOf<TestSubject>()
4141
.toHaveProperty('padLeft')
42-
.toEqualTypeOf<Nilable<number | string>>()
42+
.toEqualTypeOf<Nilable<Spacer>>()
4343
})
4444

45-
it('should match [padRight?: number | string | null | undefined]', () => {
45+
it('should match [padRight?: Spacer | null | undefined]', () => {
4646
expectTypeOf<TestSubject>()
4747
.toHaveProperty('padRight')
48-
.toEqualTypeOf<Nilable<number | string>>()
48+
.toEqualTypeOf<Nilable<Spacer>>()
4949
})
5050

5151
it('should match [stringify?: ToString | null | undefined]', () => {

src/interfaces/lines-info.mts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module string-wrap/interfaces/LinesInfo
44
*/
55

6-
import type { LinePadding } from '@flex-development/string-wrap'
6+
import type { SpacerFunction } from '@flex-development/string-wrap'
77

88
/**
99
* Info about the lines of a wrapped string.
@@ -15,21 +15,30 @@ interface LinesInfo {
1515
eol: string
1616

1717
/**
18-
* The string used to indent each line.
18+
* Get the string used to indent each line.
19+
*
20+
* @see {@linkcode SpacerFunction}
1921
*/
20-
indent: string
22+
indent: SpacerFunction<string>
2123

2224
/**
2325
* The list of lines forming the wrapped string.
2426
*/
2527
lines: readonly string[]
2628

2729
/**
28-
* The strings used to pad either side of each line.
30+
* Get the string used to pad the left side of each line.
31+
*
32+
* @see {@linkcode SpacerFunction}
33+
*/
34+
padLeft: SpacerFunction<string>
35+
36+
/**
37+
* Get the string used to pad the right side of each line.
2938
*
30-
* @see {@linkcode LinePadding}
39+
* @see {@linkcode SpacerFunction}
3140
*/
32-
padding: LinePadding
41+
padRight: SpacerFunction<string>
3342
}
3443

3544
export type { LinesInfo as default }

src/interfaces/options.mts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module string-wrap/interfaces/Options
44
*/
55

6-
import type { StripAnsi, ToString } from '@flex-development/string-wrap'
6+
import type { Spacer, StripAnsi, ToString } from '@flex-development/string-wrap'
77

88
/**
99
* Options for wrapping a string.
@@ -37,22 +37,28 @@ interface Options {
3737
hard?: boolean | null | undefined
3838

3939
/**
40-
* The size of the string to use for indenting each line (as a number or
41-
* numeric), or the string itself.
40+
* A spacer function, the size of the string to use for indenting each line
41+
* (as a number or numeric), or the string itself.
42+
*
43+
* @see {@linkcode Spacer}
4244
*/
43-
indent?: number | string | null | undefined
45+
indent?: Spacer | null | undefined
4446

4547
/**
46-
* The size of the string to use for padding the left side of each line (as a
47-
* number or numeric), or the string itself.
48+
* A spacer function, the size of the string to use for padding the left side
49+
* of each line (as a number or numeric), or the string itself.
50+
*
51+
* @see {@linkcode Spacer}
4852
*/
49-
padLeft?: number | string | null | undefined
53+
padLeft?: Spacer | null | undefined
5054

5155
/**
52-
* The size of the string to use for padding the right side of each line (as a
53-
* number or numeric), or the string itself.
56+
* A spacer function, the size of the string to use for padding the right
57+
* side of each line (as a number or numeric), or the string itself.
58+
*
59+
* @see {@linkcode Spacer}
5460
*/
55-
padRight?: number | string | null | undefined
61+
padRight?: Spacer | null | undefined
5662

5763
/**
5864
* Convert a value to a string.

src/internal/__snapshots__/happy-dom/tokenize.functional.snap

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,7 +3350,7 @@ exports[`functional:internal/tokenize > options.indent > should indent each line
33503350
"candidate": ""The quick brown fox jumped over the lazy dog"",
33513351
"cols": 18,
33523352
"columns": 20,
3353-
"indent": " ",
3353+
"indent": [Function spacer],
33543354
"lines": [
33553355
" The quick brown",
33563356
" fox jumped over",
@@ -3360,11 +3360,25 @@ exports[`functional:internal/tokenize > options.indent > should indent each line
33603360
`;
33613361
33623362
exports[`functional:internal/tokenize > options.indent > should indent each line (1) 1`] = `
3363+
{
3364+
"candidate": ""The \\u001b[1mquick\\u001b[22m brown \\u001b[31mfox jumped over \\u001b[39mthe \\u001b[3mlazy\\u001b[23m \\u001b[32mdog and then ran away with the unicorn.\\u001b[39m"",
3365+
"cols": 24,
3366+
"columns": 30,
3367+
"indent": [Function spacer],
3368+
"lines": [
3369+
"The \\u001b[1mquick\\u001b[22m brown \\u001b[31mfox jumped",
3370+
" over \\u001b[39mthe \\u001b[3mlazy\\u001b[23m \\u001b[32mdog and then",
3371+
" ran away with the unicorn.\\u001b[39m",
3372+
],
3373+
}
3374+
`;
3375+
3376+
exports[`functional:internal/tokenize > options.indent > should indent each line (2) 1`] = `
33633377
{
33643378
"candidate": ""hello\\r\\nworld 🌎"",
33653379
"cols": 6,
33663380
"columns": 7,
3367-
"indent": " ",
3381+
"indent": [Function spacer],
33683382
"lines": [
33693383
" hello",
33703384
" world",
@@ -3383,8 +3397,8 @@ exports[`functional:internal/tokenize > options.pad* > should add padding to eac
33833397
" 🦾",
33843398
" 🚀",
33853399
],
3386-
"padLeft": " ",
3387-
"padRight": "",
3400+
"padLeft": [Function spacer],
3401+
"padRight": [Function spacer],
33883402
}
33893403
`;
33903404
@@ -3406,12 +3420,34 @@ exports[`functional:internal/tokenize > options.pad* > should add padding to eac
34063420
"9 ",
34073421
" ",
34083422
],
3409-
"padLeft": "",
3410-
"padRight": " ",
3423+
"padLeft": [Function spacer],
3424+
"padRight": [Function spacer],
34113425
}
34123426
`;
34133427
34143428
exports[`functional:internal/tokenize > options.pad* > should add padding to each line (2) 1`] = `
3429+
{
3430+
"candidate": ""0 1 2 3 4 5 6 7 8 9"",
3431+
"cols": 1,
3432+
"columns": 13,
3433+
"lines": [
3434+
" |0| ",
3435+
" |1| ",
3436+
" |2| ",
3437+
" |3| ",
3438+
" |4| ",
3439+
" |5| ",
3440+
" |6| ",
3441+
" |7| ",
3442+
" |8| ",
3443+
" |9| ",
3444+
],
3445+
"padLeft": [Function spacer],
3446+
"padRight": [Function spacer],
3447+
}
3448+
`;
3449+
3450+
exports[`functional:internal/tokenize > options.pad* > should add padding to each line (3) 1`] = `
34153451
{
34163452
"candidate": ""The quick brown fox jumped over the lazy dog"",
34173453
"cols": 18,
@@ -3421,8 +3457,8 @@ exports[`functional:internal/tokenize > options.pad* > should add padding to eac
34213457
" fox jumped over ",
34223458
" the lazy dog ",
34233459
],
3424-
"padLeft": " ",
3425-
"padRight": " ",
3460+
"padLeft": [Function spacer],
3461+
"padRight": [Function spacer],
34263462
}
34273463
`;
34283464

0 commit comments

Comments
 (0)