-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces the function `span()` to create text spans more conveniently. Using this function provides better tool support since the tooling can suggest text properties and values. Since the function returns objects with a specific type, less type annotations are required. For example, the following line of code ```ts let block = { text: ['Hello ', { text: 'World!', fontWeight: 'bold' }] }; ``` can now be replaced with: ```ts let block = { text: ['Hello ', span('World!', { fontWeight: 'bold' })]); ```
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import crypto from 'node:crypto'; | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { span } from './text.ts'; | ||
|
||
global.crypto ??= (crypto as any).webcrypto; | ||
|
||
describe('test', () => { | ||
describe('span', () => { | ||
it('creates text span with given string', () => { | ||
const sp = span('foo'); | ||
|
||
expect(sp).toEqual({ text: 'foo' }); | ||
}); | ||
|
||
it('creates text span with given string and props', () => { | ||
const sp = span('foo', { fontStyle: 'italic' }); | ||
|
||
expect(sp).toEqual({ text: 'foo', fontStyle: 'italic' }); | ||
}); | ||
|
||
it('creates text span with given array and props', () => { | ||
const sp = span(['foo', span('bar', { fontStyle: 'italic' })], { fontSize: 8 }); | ||
|
||
expect(sp).toEqual({ text: ['foo', { text: 'bar', fontStyle: 'italic' }], fontSize: 8 }); | ||
}); | ||
|
||
it('extracts a single array element in text', () => { | ||
const sp = span(['foo'], { fontStyle: 'italic' }); | ||
|
||
expect(sp).toEqual({ text: 'foo', fontStyle: 'italic' }); | ||
}); | ||
|
||
it('merges a single text span in text, inner span takes precedence', () => { | ||
const sp = span(span('foo', { fontWeight: 'bold' }), { | ||
fontStyle: 'italic', | ||
fontWeight: 'normal', | ||
}); | ||
|
||
expect(sp).toEqual({ text: 'foo', fontWeight: 'bold', fontStyle: 'italic' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters