UNPKG

chalk-string

Version:
62 lines (41 loc) 1.24 kB
import colorsOption from"colors-option"; import{ARGS_METHODS}from"./args.js"; const chalkString=(styles,opts)=>{ if(typeof styles!=="string"){ throw new TypeError(`Styles must be a string, not: ${styles}`) } const chalk=colorsOption(opts); const chalkMethod=styles. trim(). split(STYLE_SEPARATOR). reduce(useChalkMethod,chalk); return addStyles.bind(undefined,chalkMethod) }; export default chalkString; const STYLE_SEPARATOR=/\s+/u; const useChalkMethod=(chalk,style)=>{ const[method,...args]=style.split(ARGS_SEPARATOR); if(typeof chalk[method]!=="function"){ throw new TypeError(`Style "${style}" is unknown.`) } return method in ARGS_METHODS? getArgsChalkMethod(chalk,method,args): getNoArgsChalkMethod(chalk,method,args) }; const ARGS_SEPARATOR="-"; const getArgsChalkMethod=(chalk,method,args)=>{ const argsA=ARGS_METHODS[method](args,method); return chalk[method](...argsA) }; const getNoArgsChalkMethod=(chalk,method,args)=>{ if(args.length!==0){ throw new TypeError(`No arguments "${args[0]}" allowed with "${method}"`) } return chalk[method] }; const addStyles=(chalkMethod,string)=>{ if(typeof string!=="string"){ throw new TypeError(`Argument must be a string, not: ${string}`) } return chalkMethod(string) };