nsole
Version:
A drop-in isomorphic console.* replacement that's pretty, small, fast, and flexible
130 lines • 5.83 kB
TypeScript
import type { ColorSpace, EnvKeyValMap, RgbaTuple } from '../types.ts';
/**
* checks for presence of key/id in argv
* @param {string} key - argv key/id (--key=value)
* @param {string[]} [argv=ARGV]
* @return {boolean}
* @tested _utils_.test.ts
*/
export declare const hasArgv: (key: string, argv?: string[]) => boolean;
/**
* gets any* reasonable argv value by key/id (returns null for flags, must have value)
* @NOTE -> unreadable values: non-escaped multi-lines; malformed keys; mismatching quotes
* @matches --key=value --key="value" --key="val ue" --key "val ue" (-- | - | "" | '')
* @param {string} key - argv key/id (--key=value)
* @param {string[]} [argv=ARGV]
* @return {null | string}
* @tested _utils_.test.ts
*/
export declare const getArgvValue: (key: string, argv?: string[]) => string | null;
/**
* snags an argv by key/id including flags (a flag value is '', null-check-it)
* @example
* --ptag-lvl=0 --ptag_lvl=0 -ptag-lvl=0 -ptag_lvl=0
* --ptag-lvl 0 --ptag_lvl 0 -ptag-lvl 0 -ptag_lvl 0
* --PTAG-LVL=0 --PTAG_LVL=0 -PTAG-LVL=0 -PTAG_LVL=0
* --PTAG-LVL 0 --PTAG_LVL 0 -PTAG-LVL 0 -PTAG_LVL 0
* @param {string} key - argv key/id (--key=value)
* @param {string[]} [argv=ARGV]
* @return {null | string | ''}
* @tested _utils_.test.ts
* @memoized big micro-buck initialization saver; argv doesn't change and this
* runs on every init; @perf ~150/µs -> ~6/µs
*/
export declare const getArgv: (key: string, argv?: string[]) => null | string | "";
/**
* gets env var key/value pairs; priority: argv > env > globalThis > null
* @param {string} key - key (argv not case sensitive, procEnv/globalThis is)
* @param {string[]} [argv=ARGV] - process['argv']
* @param {NodeJS.ProcessEnv} [procEnv=PROC_ENV] - process['env']
* @param {globalThis} [gthis=globalThis] - globalThis
* @humbug one could argue that globalThis should trump env
* @tested _utils_.test.ts
*/
export declare const getEnvVar: <E = string>(key: string, argv?: string[], procEnv?: any, gthis?: any) => E | null;
/**
* ptag env wraper to get raw value
* @param {string} key - env key to get (without PTAG_)
* @return {V | null}
*/
export declare const getEnvRaw: <V>(key: string) => null | V;
/**
* env/lookup/default value priority: global-force > arg > global > default
* @note -> differs from browser version, as it needs to handle: globalThis, process, and argv
* but uses same map-er format
*/
export declare const getEnvDefVal: <M extends string, K extends keyof EnvKeyValMap<M>>([key, param, def]: [K, undefined | null | EnvKeyValMap[K], null | EnvKeyValMap[K]], idx: number, _ignore?: unknown[], dval?: EnvKeyValMap[K]) => [EnvKeyValMap[K], EnvKeyValMap[K] | null | undefined];
/**
* COLOR_SPACE - covers all reasonable and most un-reasonable cases
* @implements nodejs.org/api/cli.html#force_color1-2-3
* 0=no-color, 1=16, 2=256, 3=true-color
*/
export declare const COLOR_SPACE: ColorSpace;
/**
* ansi escaper
* @param {string} str - actual value of string to esc
* @param {string | number} open - ansi open
* @param {string | number} close - ansi close
* @param {string | number} replace - ansi replace (for font)
* @param {0 | 1 | 2 | 3} [colorSpace=0] - 3=true-color, 2=256, 1=16, 0=no-color
* @return {string}
*/
export declare const escAnsi: (str: string, open: string | number, close: string | number, replace?: string | number, colorSpace?: number) => string;
/**
* parse css hex color (e.g. "#abc" or "aabbcc" or "aabbccff" with alpha) to RGBA
* @param {string} color - color to parse
* @return {RgbaTuple} rgba
*/
export declare const parseHexColor: (color: string) => RgbaTuple;
/**
* parse rgb/rgba colors (supports numeric and percentage values) to RGBA
* @param {string} color - color to parse
* @return {RgbaTuple} rgb
*/
export declare const parseRgbColor: (color: string) => RgbaTuple;
/**
* parse hsl/hsla colors to RGBA
* @param {string} color - hsl color to parse
* @return {RgbaTuple} rgb
*/
export declare const parseHslColor: (color: string) => RgbaTuple;
/**
* converts css color to RGBA tuple
* @param {string} color - css string color
* @return {RgbaTuple}
*/
export declare const cssColorToRgb: (color: string) => RgbaTuple;
/**
* cssColorToAnsi (memoized iife wrapper)
* @param {string} color - css color string, hex, rgba?, hsla?
* @param {Boolean} [isBg=false] - if bg, used for set/escape
* @param {0 | 1 | 2 | 3} [colorSpace=ENV] - 3=true-color, 2=256, 1=16, 0=no-color
* @return {string}
* @memoized pretty minor in the big scheme; but it drops cached lookups: 3µs -> 141ns
*/
export declare const cssColorToAnsi: (color: string, isBg?: 0 | 1, colorSpace?: 0 | 1 | 3 | 2) => string;
/**
* convert string to ansi foreground color
* @param {string} str - string to convert
* @param {string} hex - hex color
* @param {number} colorSpace - use colors
* @return {string}
*/
export declare const toAnsiFg: (str: string, hex: string, colorSpace?: 0 | 1 | 3 | 2) => string;
/**
* convert string to ansi background color
* @param {string} str - string to convert
* @param {string} hex - hex color
* @param {number} colorSpace - use colors
* @return {string}
*/
export declare const toAnsiBg: (str: string, hex: string, colorSpace?: 0 | 1 | 3 | 2) => string;
/**
* convert css/css-ansish to ansi - only fg, bg, normal/bold (fw/font-weight)
* @param {string} str - string to convert
* @param {string} hex - hex color
* @param {number} colorSpace - use colors
* @return {string}
*/
export declare const fromCssToAnsi: (str: string, css: string, colorSpace?: 0 | 1 | 3 | 2) => string;
//# sourceMappingURL=_utils_.d.ts.map