Skip to content

Commit e4b8378

Browse files
committed
randomOpts for random
1 parent 0161c06 commit e4b8378

File tree

6 files changed

+59
-23
lines changed

6 files changed

+59
-23
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {
1717
messageByString,
1818
messageByStringSpread,
1919
messageByFn,
20+
randomOpts,
2021
randomStyleFn,
2122
randomStyleString,
23+
random,
2224
styles as s,
23-
} from 'https://deno.land/x/trailmix@1.0.1/mod.ts';
25+
} from 'https://deno.land/x/trailmix@1.0.2/mod.ts';
2426

2527
// random StyleFn Message Functions
2628
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
3133
// random StyleString Message Functions
3234
console.log(messageByString('hello', [randomStyleString('color')])); // random text color
3335
console.log(messageByStringSpread('hello', randomStyleString('bgColor'))); // random background color
34-
console.log(messageByFnSpread('hello', s[randomStyleString('emphasis')]));
36+
console.log(messageByFnSpread('hello', s[randomStyleString('emphasis')])); // call style list with random style fn
37+
console.log(random('hello')); // get random style on this string (50% chance of color/bg/emphasis)
38+
console.log(random('hello', { color: true })); // get random color on this string
39+
console.log(random('hello', randomOpts({ color: true }))); // get random color 100%, (50% chance for others)
3540
```
3641

3742
### Complex

color/Color.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { styleEnum, EnumColor, EnumBgColor, EnumEmphasis } from './enum.ts';
1+
import type { styleEnum, EnumColor, EnumBgColor, EnumEmphasis } from 'trailmix/color/enum.ts';
22

33
export type StyleTypes = Exclude<keyof typeof styleEnum, 'suffix'>;
44
// list of all style strings

color/Color.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as colors from 'fmt/colors.ts';
2-
import type { StylesMap, StyleMap, StyleFn, Styles, StyleTypes, RandomStyleOptions } from './Color.d.ts';
3-
import { EnumColor, EnumBgColor, EnumEmphasis } from './enum.ts';
2+
import type { StylesMap, StyleMap, StyleFn, Styles, StyleTypes, RandomStyleOptions } from 'trailmix/color/Color.d.ts';
3+
import { EnumColor, EnumBgColor, EnumEmphasis } from 'trailmix/color/enum.ts';
44

55
export default class Color {
66
public static stylesMap: StylesMap = {
@@ -63,13 +63,21 @@ export default class Color {
6363
return msg;
6464
}
6565

66-
public static random(str: string, { color, bgColor, emphasis }: RandomStyleOptions): string {
66+
public static random(str: string, { color, bgColor, emphasis }: RandomStyleOptions = Color.randomOpts()): string {
6767
const c = [undefined, false].includes(color) ? undefined : Color.randomStyleFn();
6868
const bgC = [undefined, false].includes(bgColor) ? undefined : Color.randomStyleFn('bgColor');
6969
const e = [undefined, false].includes(emphasis) ? undefined : Color.randomStyleFn('emphasis');
7070
const r: Array<StyleFn | undefined> = new Array(c ?? undefined, bgC ?? undefined, e ?? undefined);
7171
return Color.messageByFn(str, r);
7272
}
73+
public static randomOpts({ color, bgColor, emphasis }: RandomStyleOptions = {}): RandomStyleOptions {
74+
const ret: RandomStyleOptions = {
75+
color: color ?? Math.random() >= 0.5 ? true : false,
76+
bgColor: bgColor ?? Math.random() >= 0.5 ? true : false,
77+
emphasis: emphasis ?? Math.random() >= 0.5 ? true : false,
78+
};
79+
return ret;
80+
}
7381
/**
7482
*
7583
* @param type pass in a string type of style
@@ -109,6 +117,7 @@ export const messageByFnSpread = Color.messageByFnSpread;
109117
export const messageByString = Color.messageByString;
110118
export const messageByStringSpread = Color.messageByStringSpread;
111119
export const random = Color.random;
120+
export const randomOpts = Color.randomOpts;
112121
export const randomStyleFn = Color.randomStyleFn;
113122
export const randomStyleString = Color.randomStyleString;
114123
export const stylesMap: StylesMap = Color.stylesMap;

color/Color_test.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {
55
messageByString,
66
messageByStringSpread,
77
random,
8+
randomOpts,
89
randomStyleFn,
910
randomStyleString,
1011
stylesMap,
11-
} from './Color.ts';
12-
import { styleEnum } from './enum.ts';
13-
import type { Styles, StyleFn } from './Color.d.ts';
12+
} from 'trailmix/color/Color.ts';
13+
import { styleEnum } from 'trailmix/color/enum.ts';
14+
import type { Styles, StyleFn } from 'trailmix/color/Color.d.ts';
1415
import { assertStrictEquals, assertNotEquals } from 'testing/asserts.ts';
1516
import { Table, Row, Cell } from 'cliffy/table';
1617

@@ -38,22 +39,25 @@ function consoleMock(...data: string[]) {
3839
expected,
3940
`console.log() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`,
4041
);
41-
assertStrictEquals(
42-
JSON.parse(actual),
43-
JSON.parse(expected),
44-
`console.log() JSON.parse() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`,
45-
);
42+
if (typeof JSON.parse(actual) !== 'object')
43+
assertStrictEquals(
44+
JSON.parse(actual),
45+
JSON.parse(expected),
46+
`console.log() JSON.parse() messages failure: (actual !== expected)\n "${actual}" !== "${expected}"`,
47+
);
4648
table.push(
4749
Row.from([
4850
Cell.from('🧪🧪🧪🧪\t\x1b[1m\x1b[92m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m\n').colSpan(3),
4951
]).border(false),
5052
[actual, '===', expected],
51-
[JSON.parse(actual), '===', JSON.parse(expected)],
5253
);
54+
if (typeof JSON.parse(actual) !== 'object') table.push([JSON.parse(actual), '===', JSON.parse(expected)]);
5355
} catch (e) {
5456
table.push(
5557
Row.from([
56-
Cell.from('🚨🚨🚨🚨\t\x1b[1m\x1b[91m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m\n').colSpan(3),
58+
Cell.from('🚨🚨🚨🚨\t\x1b[1m\x1b[91m\x1b[4m' + testName.trim() + '\x1b[24m\x1b[39m\x1b[22m' + e + '\n').colSpan(
59+
3,
60+
),
5761
]).border(false),
5862
[actual, '!==', expected],
5963
[JSON.parse(actual ?? []), '!==', JSON.parse(expected ?? [])],
@@ -74,6 +78,7 @@ const tests = {
7478
emphasis: false,
7579
},
7680
},
81+
randomOpts: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') },
7782
randomStyleFn: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') },
7883
randomStyleString: { ...Object.keys(styleEnum).filter((key) => key !== 'suffix') },
7984
},
@@ -88,6 +93,9 @@ const tests = {
8893
fixed: [],
8994
},
9095
},
96+
undefined: {
97+
random: undefined,
98+
},
9199
stringUndefined: {
92100
random: {
93101
color: {
@@ -257,18 +265,20 @@ for (const test of Object.keys(tests)) {
257265
for (const fn of Object.keys(tests[test])) {
258266
for (const obj of [true, false]) {
259267
// @ts-ignore
260-
for (const stylefn of Object.keys(tests[test][fn])) {
268+
for (const stylefn of Object.keys(tests[test][fn] ?? { undefined: undefined })) {
261269
for (const spread of [true, false]) {
262270
Deno.test({
263271
name: `Color.ts`,
264272
fn: () => {
265273
testName = `${test}, fn:${fn}, styleFn:${stylefn}, spread:${spread}, fromObj:${obj}\n`;
274+
ogConsole(testName);
266275
// @ts-ignore
267-
const args = tests[test][fn][stylefn];
276+
const args = stylefn === 'undefined' ? tests[test][fn] : tests[test][fn][stylefn];
268277
let TmessageByStringSpread = messageByStringSpread;
269278
let TmessageByString = messageByString;
270279
let TmessageByFnSpread = messageByFnSpread;
271280
let TmessageByFn = messageByFn;
281+
let TrandomOpts = randomOpts;
272282
let TrandomStyleFn = randomStyleFn;
273283
let TrandomStyleString = randomStyleString;
274284
let Trandom = random;
@@ -277,6 +287,7 @@ for (const test of Object.keys(tests)) {
277287
TmessageByString = Color.messageByString;
278288
TmessageByFnSpread = Color.messageByFnSpread;
279289
TmessageByFn = Color.messageByFn;
290+
TrandomOpts = Color.randomOpts;
280291
TrandomStyleFn = Color.randomStyleFn;
281292
TrandomStyleString = Color.randomStyleString;
282293
Trandom = Color.random;
@@ -290,6 +301,7 @@ for (const test of Object.keys(tests)) {
290301
if (spread) msg = JSON.stringify(TmessageByFnSpread(test, ...(args as StyleFn[])));
291302
else msg = JSON.stringify(TmessageByFn(test, args as StyleFn[]));
292303
}
304+
if (fn === 'randomOpts') msg = JSON.stringify(TrandomOpts(args));
293305
if (fn === 'randomStyleFn') msg = JSON.stringify(TrandomStyleFn(args)(test));
294306
if (fn === 'randomStyleString') msg = JSON.stringify(TrandomStyleString(args));
295307
if (fn === 'random') msg = JSON.stringify(Trandom(test, args));

color/mod.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ export {
55
messageByString,
66
messageByStringSpread,
77
random,
8+
randomOpts,
89
randomStyleFn,
910
randomStyleString,
1011
styles,
1112
stylesMap,
12-
} from './Color.ts';
13-
export { styleEnum, EnumColor, EnumBgColor, EnumEmphasis, EnumSuffix } from './enum.ts';
14-
export type { StyleTypes, Styles, StyleFn, StylesMap, StyleMap, StyleOptions, RandomStyleOptions } from './Color.d.ts';
13+
} from 'trailmix/color/Color.ts';
14+
export { styleEnum, EnumColor, EnumBgColor, EnumEmphasis, EnumSuffix } from 'trailmix/color/enum.ts';
15+
export type {
16+
StyleTypes,
17+
Styles,
18+
StyleFn,
19+
StylesMap,
20+
StyleMap,
21+
StyleOptions,
22+
RandomStyleOptions,
23+
} from 'trailmix/color/Color.d.ts';

mod.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
export type { Styles } from 'trailmix/color';
1+
export type { Styles } from 'trailmix/color/mod.ts';
22
export {
33
Color,
44
messageByFn,
55
messageByFnSpread,
66
messageByString,
77
messageByStringSpread,
88
random,
9+
randomOpts,
910
randomStyleFn,
1011
randomStyleString,
1112
styleEnum,
1213
styles,
1314
stylesMap,
14-
} from 'trailmix/color';
15+
} from 'trailmix/color/mod.ts';

0 commit comments

Comments
 (0)