Skip to content

Commit

Permalink
randomOpts for random
Browse files Browse the repository at this point in the history
  • Loading branch information
trilom committed Mar 22, 2021
1 parent 0161c06 commit e4b8378
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion color/Color.d.ts
Original file line number Diff line number Diff line change
@@ -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<keyof typeof styleEnum, 'suffix'>;
// list of all style strings
Expand Down
15 changes: 12 additions & 3 deletions color/Color.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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<StyleFn | undefined> = 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
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 24 additions & 12 deletions color/Color_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 ?? [])],
Expand All @@ -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') },
},
Expand All @@ -88,6 +93,9 @@ const tests = {
fixed: [],
},
},
undefined: {
random: undefined,
},
stringUndefined: {
random: {
color: {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand Down
15 changes: 12 additions & 3 deletions color/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export type { Styles } from 'trailmix/color';
export type { Styles } from 'trailmix/color/mod.ts';
export {
Color,
messageByFn,
messageByFnSpread,
messageByString,
messageByStringSpread,
random,
randomOpts,
randomStyleFn,
randomStyleString,
styleEnum,
styles,
stylesMap,
} from 'trailmix/color';
} from 'trailmix/color/mod.ts';

0 comments on commit e4b8378

Please sign in to comment.