|
1 | 1 | import { styleText, type StyleTextOptions } from 'node:util'; |
2 | 2 |
|
3 | | -import type { StyleTextFormat } from './types.js'; |
4 | | - |
5 | | -export type StyleKeys = |
6 | | - | 'string' |
7 | | - | 'number' |
8 | | - | 'boolean' |
9 | | - | 'bracket' |
10 | | - | 'comma' |
11 | | - | 'colon' |
12 | | - | 'quoteKey' |
13 | | - | 'quoteString' |
14 | | - | 'key' |
15 | | - | 'null'; |
| 3 | +import { defaultStyleOptions } from './defaults.js'; |
| 4 | +import { mergeStyleOptions } from './mergeStyleOptions.js'; |
| 5 | + |
| 6 | +import type { StyleKey, StyleOptions, StyleTextFormat, StyleTextFormatArray } from './types.js'; |
16 | 7 |
|
17 | 8 | export type StyleFn = (value: string, depth: number) => string; |
18 | 9 |
|
19 | 10 | export type BaseStyle = { |
20 | | - [K in StyleKeys]: StyleFn; |
21 | | -}; |
22 | | - |
23 | | -export type StyleOptions = Record< |
24 | | - StyleKeys, |
25 | | - [item: StyleTextFormat, ...rest: StyleTextFormat[]] // At least one item |
26 | | ->; |
27 | | - |
28 | | -export const defaultStyleOptions: StyleOptions = { |
29 | | - // content |
30 | | - string: ['green'], |
31 | | - number: ['yellowBright'], |
32 | | - boolean: ['blueBright'], |
33 | | - null: ['redBright'], |
34 | | - |
35 | | - // structural |
36 | | - bracket: ['white', 'blue', 'yellow', 'cyan', 'green', 'red'], |
37 | | - comma: ['white'], |
38 | | - colon: ['white'], |
39 | | - |
40 | | - // quotes |
41 | | - quoteKey: ['cyan'], |
42 | | - quoteString: ['green'], |
43 | | - |
44 | | - // keys |
45 | | - key: ['cyan'], |
| 11 | + [K in StyleKey]: StyleFn; |
46 | 12 | }; |
47 | 13 |
|
48 | 14 | export class Style implements BaseStyle { |
49 | 15 | readonly #options: StyleOptions; |
50 | 16 |
|
51 | 17 | readonly #styleTextOptions: StyleTextOptions = { validateStream: false }; |
52 | 18 |
|
53 | | - constructor(options?: Partial<StyleOptions>) { |
54 | | - this.#options = { ...defaultStyleOptions, ...options }; |
| 19 | + constructor(options?: Partial<StyleOptions> | StyleTextFormatArray) { |
| 20 | + this.#options = mergeStyleOptions(defaultStyleOptions, options); |
55 | 21 | } |
56 | 22 |
|
57 | 23 | #getStyleTextFormat(type: keyof StyleOptions, depth: number): StyleTextFormat { |
|
0 commit comments