diff --git a/README.md b/README.md index 64974bf..08059a5 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,12 @@ import { messageByString, messageByStringSpread, messageByFn, + randomOpts, randomStyleFn, randomStyleString, + random, styles as s, -} from 'https://deno.land/x/trailmix@1.0.1/mod.ts'; +} from 'https://deno.land/x/trailmix@1.0.2/mod.ts'; // random StyleFn Message Functions console.log(messageByFn('hello', [s.cyan, s.bgRed])); // cyan text, red BG @@ -31,7 +33,10 @@ console.log(randomStyleString('color')); // get a random color string typeof Sty // random StyleString Message Functions console.log(messageByString('hello', [randomStyleString('color')])); // random text color console.log(messageByStringSpread('hello', randomStyleString('bgColor'))); // random background color -console.log(messageByFnSpread('hello', s[randomStyleString('emphasis')])); +console.log(messageByFnSpread('hello', s[randomStyleString('emphasis')])); // call style list with random style fn +console.log(random('hello')); // get random style on this string (50% chance of color/bg/emphasis) +console.log(random('hello', { color: true })); // get random color on this string +console.log(random('hello', randomOpts({ color: true }))); // get random color 100%, (50% chance for others) ``` ### Complex diff --git a/color/Color.d.ts b/color/Color.d.ts index 18befa8..380038d 100644 --- a/color/Color.d.ts +++ b/color/Color.d.ts @@ -1,4 +1,4 @@ -import type { styleEnum, EnumColor, EnumBgColor, EnumEmphasis } from './enum.ts'; +import type { styleEnum, EnumColor, EnumBgColor, EnumEmphasis } from 'trailmix/color/enum.ts'; export type StyleTypes = Exclude; // list of all style strings diff --git a/color/Color.ts b/color/Color.ts index 174104c..ce9c1c5 100644 --- a/color/Color.ts +++ b/color/Color.ts @@ -1,6 +1,6 @@ import * as colors from 'fmt/colors.ts'; -import type { StylesMap, StyleMap, StyleFn, Styles, StyleTypes, RandomStyleOptions } from './Color.d.ts'; -import { EnumColor, EnumBgColor, EnumEmphasis } from './enum.ts'; +import type { StylesMap, StyleMap, StyleFn, Styles, StyleTypes, RandomStyleOptions } from 'trailmix/color/Color.d.ts'; +import { EnumColor, EnumBgColor, EnumEmphasis } from 'trailmix/color/enum.ts'; export default class Color { public static stylesMap: StylesMap = { @@ -63,13 +63,21 @@ export default class Color { return msg; } - public static random(str: string, { color, bgColor, emphasis }: RandomStyleOptions): string { + public static random(str: string, { color, bgColor, emphasis }: RandomStyleOptions = Color.randomOpts()): string { const c = [undefined, false].includes(color) ? undefined : Color.randomStyleFn(); const bgC = [undefined, false].includes(bgColor) ? undefined : Color.randomStyleFn('bgColor'); const e = [undefined, false].includes(emphasis) ? undefined : Color.randomStyleFn('emphasis'); const r: Array = new Array(c ?? undefined, bgC ?? undefined, e ?? undefined); return Color.messageByFn(str, r); } + public static randomOpts({ color, bgColor, emphasis }: RandomStyleOptions = {}): RandomStyleOptions { + const ret: RandomStyleOptions = { + color: color ?? Math.random() >= 0.5 ? true : false, + bgColor: bgColor ?? Math.random() >= 0.5 ? true : false, + emphasis: emphasis ?? Math.random() >= 0.5 ? true : false, + }; + return ret; + } /** * * @param type pass in a string type of style @@ -109,6 +117,7 @@ export const messageByFnSpread = Color.messageByFnSpread; export const messageByString = Color.messageByString; export const messageByStringSpread = Color.messageByStringSpread; export const random = Color.random; +export const randomOpts = Color.randomOpts; export const randomStyleFn = Color.randomStyleFn; export const randomStyleString = Color.randomStyleString; export const stylesMap: StylesMap = Color.stylesMap; diff --git a/color/Color_test.ts b/color/Color_test.ts index fd17622..007297b 100644 --- a/color/Color_test.ts +++ b/color/Color_test.ts @@ -5,12 +5,13 @@ import { messageByString, messageByStringSpread, random, + randomOpts, randomStyleFn, randomStyleString, stylesMap, -} from './Color.ts'; -import { styleEnum } from './enum.ts'; -import type { Styles, StyleFn } from './Color.d.ts'; +} from 'trailmix/color/Color.ts'; +import { styleEnum } from 'trailmix/color/enum.ts'; +import type { Styles, StyleFn } from 'trailmix/color/Color.d.ts'; import { assertStrictEquals, assertNotEquals } from 'testing/asserts.ts'; import { Table, Row, Cell } from 'cliffy/table'; @@ -38,22 +39,25 @@ function consoleMock(...data: string[]) { expected, `console.log() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`, ); - assertStrictEquals( - JSON.parse(actual), - JSON.parse(expected), - `console.log() JSON.parse() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`, - ); + if (typeof JSON.parse(actual) !== 'object') + assertStrictEquals( + JSON.parse(actual), + JSON.parse(expected), + `console.log() JSON.parse() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`, + ); table.push( Row.from([ Cell.from('๐Ÿงช๐Ÿงช๐Ÿงช๐Ÿงช\t\x1b[1m\x1b[92m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m\n').colSpan(3), ]).border(false), [actual, '===', expected], - [JSON.parse(actual), '===', JSON.parse(expected)], ); + if (typeof JSON.parse(actual) !== 'object') table.push([JSON.parse(actual), '===', JSON.parse(expected)]); } catch (e) { table.push( Row.from([ - Cell.from('๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ\t\x1b[1m\x1b[91m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m\n').colSpan(3), + Cell.from('๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ\t\x1b[1m\x1b[91m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m' + e + '\n').colSpan( + 3, + ), ]).border(false), [actual, '!==', expected], [JSON.parse(actual ?? []), '!==', JSON.parse(expected ?? [])], @@ -74,6 +78,7 @@ const tests = { emphasis: false, }, }, + randomOpts: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') }, randomStyleFn: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') }, randomStyleString: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') }, }, @@ -88,6 +93,9 @@ const tests = { fixed: [], }, }, + undefined: { + random: undefined, + }, stringUndefined: { random: { color: { @@ -257,18 +265,20 @@ for (const test of Object.keys(tests)) { for (const fn of Object.keys(tests[test])) { for (const obj of [true, false]) { // @ts-ignore - for (const stylefn of Object.keys(tests[test][fn])) { + for (const stylefn of Object.keys(tests[test][fn] ?? { undefined: undefined })) { for (const spread of [true, false]) { Deno.test({ name: `Color.ts`, fn: () => { testName = `${test}, fn:${fn}, styleFn:${stylefn}, spread:${spread}, fromObj:${obj}\n`; + ogConsole(testName); // @ts-ignore - const args = tests[test][fn][stylefn]; + const args = stylefn === 'undefined' ? tests[test][fn] : tests[test][fn][stylefn]; let TmessageByStringSpread = messageByStringSpread; let TmessageByString = messageByString; let TmessageByFnSpread = messageByFnSpread; let TmessageByFn = messageByFn; + let TrandomOpts = randomOpts; let TrandomStyleFn = randomStyleFn; let TrandomStyleString = randomStyleString; let Trandom = random; @@ -277,6 +287,7 @@ for (const test of Object.keys(tests)) { TmessageByString = Color.messageByString; TmessageByFnSpread = Color.messageByFnSpread; TmessageByFn = Color.messageByFn; + TrandomOpts = Color.randomOpts; TrandomStyleFn = Color.randomStyleFn; TrandomStyleString = Color.randomStyleString; Trandom = Color.random; @@ -290,6 +301,7 @@ for (const test of Object.keys(tests)) { if (spread) msg = JSON.stringify(TmessageByFnSpread(test, ...(args as StyleFn[]))); else msg = JSON.stringify(TmessageByFn(test, args as StyleFn[])); } + if (fn === 'randomOpts') msg = JSON.stringify(TrandomOpts(args)); if (fn === 'randomStyleFn') msg = JSON.stringify(TrandomStyleFn(args)(test)); if (fn === 'randomStyleString') msg = JSON.stringify(TrandomStyleString(args)); if (fn === 'random') msg = JSON.stringify(Trandom(test, args)); diff --git a/color/mod.ts b/color/mod.ts index 2c751c1..82d3133 100644 --- a/color/mod.ts +++ b/color/mod.ts @@ -5,10 +5,19 @@ export { messageByString, messageByStringSpread, random, + randomOpts, randomStyleFn, randomStyleString, styles, stylesMap, -} from './Color.ts'; -export { styleEnum, EnumColor, EnumBgColor, EnumEmphasis, EnumSuffix } from './enum.ts'; -export type { StyleTypes, Styles, StyleFn, StylesMap, StyleMap, StyleOptions, RandomStyleOptions } from './Color.d.ts'; +} from 'trailmix/color/Color.ts'; +export { styleEnum, EnumColor, EnumBgColor, EnumEmphasis, EnumSuffix } from 'trailmix/color/enum.ts'; +export type { + StyleTypes, + Styles, + StyleFn, + StylesMap, + StyleMap, + StyleOptions, + RandomStyleOptions, +} from 'trailmix/color/Color.d.ts'; diff --git a/mod.ts b/mod.ts index 199932c..766c58f 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -export type { Styles } from 'trailmix/color'; +export type { Styles } from 'trailmix/color/mod.ts'; export { Color, messageByFn, @@ -6,9 +6,10 @@ export { messageByString, messageByStringSpread, random, + randomOpts, randomStyleFn, randomStyleString, styleEnum, styles, stylesMap, -} from 'trailmix/color'; +} from 'trailmix/color/mod.ts';