styled-json-console
Advanced tools
| import type { StyleOptions, StyleTextFormatArray } from './types.js'; | ||
| export declare function createSimpleStyleOptions(value: StyleTextFormatArray): StyleOptions; |
| import { defaultStyleOptions } from './defaults.js'; | ||
| import { isStyleKey } from './isStyleKey.js'; | ||
| export function createSimpleStyleOptions(value) { | ||
| const options = { ...defaultStyleOptions }; | ||
| for (const key in options) { | ||
| if (isStyleKey(key)) { | ||
| options[key] = value; | ||
| } | ||
| } | ||
| return options; | ||
| } |
| import type { StyleOptions } from './types.js'; | ||
| export declare const defaultStyleOptions: Readonly<StyleOptions>; |
| export const defaultStyleOptions = { | ||
| string: ['green'], | ||
| number: ['yellowBright'], | ||
| boolean: ['blueBright'], | ||
| null: ['redBright'], | ||
| bracket: ['white', 'blue', 'yellow', 'cyan', 'green', 'red'], | ||
| comma: ['white'], | ||
| colon: ['white'], | ||
| quoteKey: ['cyan'], | ||
| quoteString: ['green'], | ||
| key: ['cyan'], | ||
| }; |
| import type { StyleKey } from './types.js'; | ||
| export declare const isStyleKey: (value: unknown) => value is StyleKey; |
| export const isStyleKey = (value) => { | ||
| return (value === 'string' || | ||
| value === 'number' || | ||
| value === 'boolean' || | ||
| value === 'bracket' || | ||
| value === 'comma' || | ||
| value === 'colon' || | ||
| value === 'quoteKey' || | ||
| value === 'quoteString' || | ||
| value === 'key' || | ||
| value === 'null'); | ||
| }; |
| import type { StyleOptions, StyleTextFormatArray } from './types.js'; | ||
| export declare function mergeStyleOptions(base?: Partial<StyleOptions> | StyleTextFormatArray, override?: Partial<StyleOptions> | StyleTextFormatArray): StyleOptions; |
| import { createSimpleStyleOptions } from './createSimpleStyleOptions.js'; | ||
| import { defaultStyleOptions } from './defaults.js'; | ||
| export function mergeStyleOptions(base, override) { | ||
| if (Array.isArray(override)) { | ||
| return createSimpleStyleOptions(override); | ||
| } | ||
| if (Array.isArray(base)) { | ||
| return { | ||
| ...createSimpleStyleOptions(base), | ||
| ...override, | ||
| }; | ||
| } | ||
| return { ...defaultStyleOptions, ...base, ...override }; | ||
| } |
| import { Writable, type WritableOptions } from 'node:stream'; | ||
| import { Style, type StyleOptions } from './Style.js'; | ||
| import type { JsonReplacerFn } from './types.js'; | ||
| import { Style } from './Style.js'; | ||
| import type { JsonReplacerFn, StyleOptions, StyleTextFormatArray } from './types.js'; | ||
| export type ModifyOutputFn = (output: string, style: Style) => string; | ||
@@ -10,3 +10,3 @@ export type AnsiJsonWritableOptions = WritableOptions & { | ||
| replacer?: JsonReplacerFn; | ||
| styleOptions?: Partial<StyleOptions>; | ||
| styleOptions?: Partial<StyleOptions> | StyleTextFormatArray; | ||
| modifyOutput?: ModifyOutputFn; | ||
@@ -13,0 +13,0 @@ }; |
| import { type ConsoleConstructorOptions } from 'node:console'; | ||
| import { type ModifyOutputFn } from './AnsiJsonWritable.js'; | ||
| import type { StyleOptions } from './Style.js'; | ||
| import type { JsonReplacerFn } from './types.js'; | ||
| import type { JsonReplacerFn, StyleOptions, StyleTextFormatArray } from './types.js'; | ||
| export type JsonConsoleOptions = { | ||
| style?: Partial<StyleOptions>; | ||
| style?: Partial<StyleOptions> | StyleTextFormatArray; | ||
| space?: number | string; | ||
@@ -8,0 +7,0 @@ replacer?: JsonReplacerFn; |
| import { Console } from 'node:console'; | ||
| import { AnsiJsonWritable } from './AnsiJsonWritable.js'; | ||
| import { mergeStyleOptions } from './mergeStyleOptions.js'; | ||
| export function createConsole(options = {}) { | ||
@@ -10,6 +11,3 @@ const { stdout = process.stdout, stderr = process.stderr, eol, modifyOutput, json, ...consoleOptions } = options; | ||
| ...json[1], | ||
| style: { | ||
| ...stdOutJson?.style, | ||
| ...json[1]?.style, | ||
| }, | ||
| style: mergeStyleOptions(stdOutJson?.style, json[1]?.style), | ||
| } | ||
@@ -16,0 +14,0 @@ : json; |
+3
-9
@@ -1,15 +0,9 @@ | ||
| import type { StyleTextFormat } from './types.js'; | ||
| export type StyleKeys = 'string' | 'number' | 'boolean' | 'bracket' | 'comma' | 'colon' | 'quoteKey' | 'quoteString' | 'key' | 'null'; | ||
| import type { StyleKey, StyleOptions, StyleTextFormatArray } from './types.js'; | ||
| export type StyleFn = (value: string, depth: number) => string; | ||
| export type BaseStyle = { | ||
| [K in StyleKeys]: StyleFn; | ||
| [K in StyleKey]: StyleFn; | ||
| }; | ||
| export type StyleOptions = Record<StyleKeys, [ | ||
| item: StyleTextFormat, | ||
| ...rest: StyleTextFormat[] | ||
| ]>; | ||
| export declare const defaultStyleOptions: StyleOptions; | ||
| export declare class Style implements BaseStyle { | ||
| #private; | ||
| constructor(options?: Partial<StyleOptions>); | ||
| constructor(options?: Partial<StyleOptions> | StyleTextFormatArray); | ||
| forceBright(input: string): string; | ||
@@ -16,0 +10,0 @@ bracket(char: string, depth: number): string; |
+3
-13
| import { styleText } from 'node:util'; | ||
| export const defaultStyleOptions = { | ||
| string: ['green'], | ||
| number: ['yellowBright'], | ||
| boolean: ['blueBright'], | ||
| null: ['redBright'], | ||
| bracket: ['white', 'blue', 'yellow', 'cyan', 'green', 'red'], | ||
| comma: ['white'], | ||
| colon: ['white'], | ||
| quoteKey: ['cyan'], | ||
| quoteString: ['green'], | ||
| key: ['cyan'], | ||
| }; | ||
| import { defaultStyleOptions } from './defaults.js'; | ||
| import { mergeStyleOptions } from './mergeStyleOptions.js'; | ||
| export class Style { | ||
@@ -18,3 +8,3 @@ #options; | ||
| constructor(options) { | ||
| this.#options = { ...defaultStyleOptions, ...options }; | ||
| this.#options = mergeStyleOptions(defaultStyleOptions, options); | ||
| } | ||
@@ -21,0 +11,0 @@ #getStyleTextFormat(type, depth) { |
+3
-0
| import type { styleText } from 'node:util'; | ||
| export type JsonReplacerFn = (this: unknown, key: string, value: unknown) => unknown; | ||
| export type StyleTextFormat = Parameters<typeof styleText>[0]; | ||
| export type StyleTextFormatArray = [item: StyleTextFormat, ...rest: StyleTextFormat[]]; | ||
| type TextFormatStrings = Exclude<StyleTextFormat, string[]>; | ||
@@ -8,2 +9,4 @@ export type StyleTextBgColor = Extract<TextFormatStrings, `bg${string}`>; | ||
| export type StyleTextModifier = Exclude<TextFormatStrings, StyleTextBgColor | StyleTextColor>; | ||
| export type StyleKey = 'string' | 'number' | 'boolean' | 'bracket' | 'comma' | 'colon' | 'quoteKey' | 'quoteString' | 'key' | 'null'; | ||
| export type StyleOptions = Record<StyleKey, StyleTextFormatArray>; | ||
| export {}; |
+4
-4
| { | ||
| "name": "styled-json-console", | ||
| "description": "Custom Node.js Console that styles your JSON output", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "publishConfig": { | ||
@@ -51,7 +51,7 @@ "access": "public" | ||
| "scripts": { | ||
| "clean": "rimraf ./dist/", | ||
| "clean": "rimraf ./dist/ ./cache/", | ||
| "prebuild": "pnpm clean", | ||
| "build": "tsc --build tsconfig.src.json --force", | ||
| "typecheck": "tsc --build tsconfig.src.json tsconfig.test.json tsconfig.configs.json --verbose", | ||
| "lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ --ext .ts", | ||
| "typecheck": "tsc --build tsconfig.json --verbose", | ||
| "lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./example/ --ext .ts", | ||
| "test": "vitest --typecheck", | ||
@@ -58,0 +58,0 @@ "coverage": "vitest run --coverage --typecheck", |
19716
11.25%29
38.1%390
11.75%