Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit a383ce8

Browse files
authored
feat(packages/test): ✨ Add new groups: emoji and sample (#31)
1 parent e9803e8 commit a383ce8

File tree

9 files changed

+395
-123
lines changed

9 files changed

+395
-123
lines changed

packages/test/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
<!-- prettier-ignore-start -->
2020
<!-- MODULES LINKS -->
21+
[`@terminal-nerds/snippets-test/emoji`]: https://github.com/terminal-nerds/snippets/blob/main/packages/test/source/emoji/emoji.ts
22+
[emoji size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/emoji/emoji.js?label=gzip
23+
[emoji size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/emoji/emoji.js?label=brotli
24+
25+
[`@terminal-nerds/snippets-test/sample`]: https://github.com/terminal-nerds/snippets/blob/main/packages/test/source/sample/sample.ts
26+
[sample size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/sample/sample.js?label=gzip
27+
[sample size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/sample/sample.js?label=brotli
28+
2129
[`@terminal-nerds/snippets-test/unit`]: https://github.com/terminal-nerds/snippets/blob/main/packages/test/source/unit/unit.ts
2230
[unit size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/unit/unit.js?label=gzip
2331
[unit size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-test/dist/unit/unit.js?label=brotli

packages/test/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,11 @@
5959
"lint:pkg": "syncpack list-mismatches",
6060
"lint:types": "tsc --noEmit",
6161
"test:unit": "vitest run --dir \"./source\""
62+
},
63+
"dependencies": {
64+
"@terminal-nerds/snippets-type": "workspace:*"
65+
},
66+
"devDependencies": {
67+
"zod": "3.20.6"
6268
}
6369
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
import { ZodError } from "zod";
3+
4+
import { getValueTypeEmoji, VALUE_TYPE_EMOJIS } from "./emoji.js";
5+
6+
describe("getValueTypeEmoji(value)", () => {
7+
const expected = VALUE_TYPE_EMOJIS.ZodError;
8+
9+
it(`returns '${expected}' on custom object: ZodError`, () => {
10+
expect(getValueTypeEmoji(ZodError)).toBe(expected);
11+
});
12+
13+
const expectedUnknown = VALUE_TYPE_EMOJIS.UNRECOGNIZED_TYPE_EMOJI;
14+
15+
class Unknown {}
16+
17+
it(`returns '${expectedUnknown}' on unrecognized type`, () => {
18+
expect(getValueTypeEmoji(Unknown)).toBe(expectedUnknown);
19+
});
20+
});

packages/test/source/emoji/emoji.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { type BuiltInObjectName, isBuiltInObjectName } from "@terminal-nerds/snippets-type/built-in";
2+
import { getConstructorName } from "@terminal-nerds/snippets-type/constructor";
3+
import { getPrimitiveName, isPrimitiveName, type PrimitiveName } from "@terminal-nerds/snippets-type/primitive";
4+
5+
export const PRIMITIVES_EMOJIS = {
6+
bigint: `🇧`,
7+
boolean: {
8+
false: `🔴`,
9+
true: `🟢`,
10+
},
11+
null: `❎`,
12+
number: `🇳`,
13+
symbol: `💠`,
14+
string: `🇸`,
15+
undefined: `🫥`,
16+
} as const;
17+
export type PrimitiveEmoji =
18+
| (typeof PRIMITIVES_EMOJIS)[Exclude<PrimitiveName, "boolean">]
19+
| (typeof PRIMITIVES_EMOJIS.boolean)["false" | "true"];
20+
21+
export const BUILT_IN_OBJECTS_EMOJIS = {
22+
// Fundamental
23+
Function: `🇫`,
24+
Object: `🇴`,
25+
// Error
26+
Error: `📛`,
27+
AggregateError: `📛`,
28+
EvalError: `📛`,
29+
RangeError: `📛`,
30+
ReferenceError: `📛`,
31+
SyntaxError: `📛`,
32+
TypeError: `📛`,
33+
URIError: `📛`,
34+
// Math and date
35+
Date: `🗓️`,
36+
Math: `🧮`,
37+
// Text processing
38+
RegExp: `🔎🇸"`,
39+
// Indexed collection
40+
Array: `🇦`,
41+
Int8Array: `❔`,
42+
Uint8Array: `❔`,
43+
Uint8ClampedArray: `❔`,
44+
Int16Array: `❔`,
45+
Uint16Array: `❔`,
46+
Int32Array: `❔`,
47+
Uint32Array: `❔`,
48+
BigInt64Array: `❔`,
49+
BigUint64Array: `❔`,
50+
Float32Array: `❔`,
51+
Float64Array: `❔`,
52+
// Keyed collection
53+
Map: `🇲`,
54+
Set: `🦄`,
55+
WeakMap: `❔`,
56+
WeakSet: `❔`,
57+
// Structured data
58+
ArrayBuffer: `❔`,
59+
SharedArrayBuffer: `❔`,
60+
DataView: `❔`,
61+
Atomics: `❔`,
62+
JSON: `❔`,
63+
// Managing memory
64+
WeakRef: `❔`,
65+
FinalizationRegistry: `❔`,
66+
// Control abstraction
67+
Promise: `❔`,
68+
// GeneratorFunction: `❔`,
69+
// AsyncGeneratorFunction: `❔`,
70+
// Generator: `❔`,
71+
// AsyncGenerator: `❔`,
72+
// AsyncFunction: `❔`,
73+
// Reflection
74+
Reflect: `❔`,
75+
Proxy: `❔`,
76+
// Internationalization
77+
Intl: `🌐`,
78+
} as const;
79+
80+
type BuiltInNameWithoutPrimitives = Exclude<BuiltInObjectName, "BigInt" | "Boolean" | "Number" | "String" | "Symbol">;
81+
export type BuiltInEmoji = (typeof BUILT_IN_OBJECTS_EMOJIS)[BuiltInNameWithoutPrimitives];
82+
83+
export const CUSTOM_TYPE_EMOJIS = {
84+
ZodError: `📛`,
85+
} as const;
86+
87+
export type CustomTypeName = keyof typeof CUSTOM_TYPE_EMOJIS;
88+
export type CustomTypeEmoji = (typeof CUSTOM_TYPE_EMOJIS)[CustomTypeName];
89+
90+
export const UNRECOGNIZED_TYPE_EMOJI = `❓`;
91+
export type UnrecognizedTypeEmoji = typeof UNRECOGNIZED_TYPE_EMOJI;
92+
93+
export const VALUE_TYPE_EMOJIS = {
94+
...PRIMITIVES_EMOJIS,
95+
...BUILT_IN_OBJECTS_EMOJIS,
96+
...CUSTOM_TYPE_EMOJIS,
97+
UNRECOGNIZED_TYPE_EMOJI,
98+
} as const;
99+
100+
export type ValueTypeName = keyof typeof PRIMITIVES_EMOJIS | keyof typeof BUILT_IN_OBJECTS_EMOJIS;
101+
export type ValueTypeEmoji =
102+
| (typeof VALUE_TYPE_EMOJIS)[Exclude<ValueTypeName, "boolean">]
103+
| (typeof PRIMITIVES_EMOJIS)["boolean"]["true" | "false"]
104+
| UnrecognizedTypeEmoji;
105+
106+
function getPrimitiveEmoji(value: unknown): PrimitiveEmoji {
107+
const primitiveName = getPrimitiveName(value);
108+
109+
if (primitiveName) {
110+
return primitiveName === "boolean"
111+
? PRIMITIVES_EMOJIS["boolean"][String(value) as "true" | "false"]
112+
: PRIMITIVES_EMOJIS[primitiveName];
113+
} else {
114+
throw new TypeError(`Unrecognized primitive, cannot get emoji.`);
115+
}
116+
}
117+
118+
export function getValueTypeEmoji(value: unknown): ValueTypeEmoji {
119+
const name = getConstructorName(value);
120+
121+
if (isPrimitiveName(name.toLowerCase())) {
122+
return getPrimitiveEmoji(value);
123+
} else if (isBuiltInObjectName(name)) {
124+
return BUILT_IN_OBJECTS_EMOJIS[name as BuiltInNameWithoutPrimitives];
125+
} else {
126+
// @ts-ignore TODO: Add type safety for custom objects?
127+
const emoji = CUSTOM_TYPE_EMOJIS[name];
128+
129+
return emoji ?? UNRECOGNIZED_TYPE_EMOJI;
130+
}
131+
}

packages/test/source/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/* MODULES */
2+
export * from "./emoji/emoji.js";
3+
export * from "./sample/sample.js";
24
export * from "./unit/unit.js";

packages/test/source/sample/sample.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/* eslint-disable unicorn/no-useless-undefined, unicorn/prefer-number-properties, unicorn/no-null */
2+
3+
export const FALSY_BIG_INTS = [0n, -0n, BigInt(0), BigInt(-0)] as const;
4+
export const TRUTHY_BIG_INTS = [
5+
BigInt(Number.MIN_SAFE_INTEGER),
6+
BigInt(Number.MAX_SAFE_INTEGER),
7+
BigInt(Number.MAX_VALUE),
8+
] as const;
9+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt} */
10+
export const SAMPLE_BIG_INTS = [...FALSY_BIG_INTS, ...TRUTHY_BIG_INTS] as const;
11+
12+
export const FALSY_BOOLEANS = [
13+
false,
14+
Boolean(false),
15+
Boolean(),
16+
Boolean(0),
17+
Boolean(""),
18+
Boolean(undefined),
19+
Boolean(null),
20+
] as const;
21+
export const TRUTHY_BOOLEANS = [true, Boolean(true), Boolean(1), Boolean("a")] as const;
22+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean} */
23+
export const SAMPLE_BOOLEANS = [...FALSY_BOOLEANS, ...TRUTHY_BOOLEANS] as const;
24+
25+
export const SAMPLE_INFINITIES = [-Infinity, Number.NEGATIVE_INFINITY, Infinity, Number.POSITIVE_INFINITY] as const;
26+
export const SAMPLE_NANS = [NaN, Number.NaN, Number(NaN), Number(Number.NaN)] as const;
27+
export const FALSY_NUMBERS = [-0, 0, Number(), ...SAMPLE_NANS] as const;
28+
export const TRUTHY_NUMBERS = [
29+
Number.MIN_SAFE_INTEGER,
30+
Number.MIN_VALUE,
31+
Number.EPSILON,
32+
Number.MAX_SAFE_INTEGER,
33+
Number.MAX_VALUE,
34+
] as const;
35+
36+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number} */
37+
export const SAMPLE_NUMBERS = [...SAMPLE_INFINITIES, ...FALSY_NUMBERS, ...TRUTHY_NUMBERS] as const;
38+
39+
/**
40+
* This is a bug, because its type is an object.
41+
*
42+
* @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Null}
43+
*/
44+
export const SAMPLE_NULLS = [null] as const;
45+
46+
/* prettier-ignore */
47+
export const FALSY_STRINGS = ["", '', ``, String(), String(""), String(''), String(``)] as const;
48+
export const NUMERIC_STRINGS = [
49+
"123456789",
50+
"-123456789",
51+
"123.456789",
52+
"-123.456789",
53+
" 123.456789 ",
54+
" -123.456789 ",
55+
"0b11111111", // 255
56+
"0o377", // 255
57+
"0xFF", // 255
58+
"10e1000",
59+
"Infinity",
60+
"-Infinity",
61+
] as const;
62+
export const TRUTHY_STRINGS = [
63+
'string from double quotes (")',
64+
"string from single quotes (')",
65+
`string from backtick - ${"template literals"} - (\`)`,
66+
...NUMERIC_STRINGS,
67+
] as const;
68+
/* prettier-ignore */
69+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String} */
70+
export const SAMPLE_STRINGS = [
71+
...FALSY_STRINGS,
72+
...TRUTHY_STRINGS,
73+
] as const;
74+
75+
/* prettier-ignore */
76+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol} */
77+
export const SAMPLE_SYMBOLS = [
78+
Symbol(),
79+
Symbol(1),
80+
Symbol(0),
81+
Symbol(-0),
82+
Symbol(NaN),
83+
// eslint-disable-next-line unicorn/no-useless-undefined
84+
Symbol(undefined),
85+
Symbol("string"),
86+
Symbol(""),
87+
] as const;
88+
89+
/* prettier-ignore */
90+
/** @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Undefined} */
91+
export const SAMPLE_UNDEFINEDES = [
92+
undefined,
93+
void 0,
94+
];
95+
96+
/** @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy} */
97+
export const FALSY_PRIMITIVES = [
98+
...FALSY_BIG_INTS,
99+
...FALSY_BOOLEANS,
100+
...FALSY_NUMBERS,
101+
...SAMPLE_NULLS,
102+
...FALSY_STRINGS,
103+
...SAMPLE_UNDEFINEDES,
104+
] as const;
105+
106+
/** @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Truthy} */
107+
export const TRUTHY_PRIMITIVES = [
108+
...TRUTHY_BIG_INTS,
109+
...TRUTHY_BOOLEANS,
110+
...TRUTHY_NUMBERS,
111+
...TRUTHY_STRINGS,
112+
] as const;
113+
114+
export const SAMPLE_PRIMITIVES = [
115+
...SAMPLE_BIG_INTS,
116+
...SAMPLE_BOOLEANS,
117+
...SAMPLE_NULLS,
118+
...SAMPLE_NUMBERS,
119+
...SAMPLE_STRINGS,
120+
...SAMPLE_SYMBOLS,
121+
...SAMPLE_UNDEFINEDES,
122+
] as const;
123+
124+
export const SAMPLE_BIG_INT = BigInt(987);
125+
export const SAMPLE_NUMBER = 1337;
126+
export const SAMPLE_STRING = "terminal-nerds.DEV@2023";
127+
export const SAMPLE_SYMBOL = Symbol("terminal-nerds");
128+
129+
export const SAMPLE_REGEXES = [
130+
// eslint-disable-next-line prefer-regex-literals
131+
new RegExp(""),
132+
/nerds/i,
133+
] as const;
134+
135+
export const SAMPLE_ARRAYS = [[], [1, 2]] as const;
136+
export const SAMPLE_DATES = [new Date(), new Date("2023-01-01T00:00:00.000Z")];
137+
export const SAMPLE_MAPS = [
138+
new Map(),
139+
new Map([
140+
[1, "one"],
141+
[2, "two"],
142+
[3, "three"],
143+
]),
144+
] as const;
145+
export const SAMPLE_OBJECTS = [{}, { key: "value" }] as const;
146+
export const SAMPLE_SETS = [new Set(), new Set([1, 2, 2])] as const;
147+
148+
export const NON_PRIMITIVES = [
149+
...SAMPLE_ARRAYS,
150+
...SAMPLE_DATES,
151+
...SAMPLE_MAPS,
152+
...SAMPLE_OBJECTS,
153+
...SAMPLE_SETS,
154+
...SAMPLE_REGEXES,
155+
] as const;
156+
157+
export const ALL_SAMPLES = [...SAMPLE_PRIMITIVES, ...NON_PRIMITIVES] as const;

packages/test/source/unit/unit.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { returns, throws, VALUE_TYPE_EMOJIS } from "./unit.js";
3+
import { VALUE_TYPE_EMOJIS } from "../emoji/emoji.js";
4+
import { returns, throws } from "./unit.js";
45

56
describe("throws(value)", () => {
67
const expectedBase = `💥 throws 📛 'Error'`;
@@ -20,13 +21,13 @@ describe("throws(value)", () => {
2021
});
2122

2223
describe("returns(value)", () => {
23-
const expectedNumberBase = `🔙 returns ${VALUE_TYPE_EMOJIS.Number} 'Number' (0)`;
24+
const expectedNumberBase = `🔙 returns ${VALUE_TYPE_EMOJIS.number} 'Number' (0)`;
2425

2526
it(`🔙 returns a string: "${expectedNumberBase}"`, () => {
2627
expect(`${returns(0)}`).toStrictEqual(expectedNumberBase);
2728
});
2829

29-
const expectedStringBase = `🔙 returns ${VALUE_TYPE_EMOJIS.String} 'String' ("")`;
30+
const expectedStringBase = `🔙 returns ${VALUE_TYPE_EMOJIS.string} 'String' ("")`;
3031

3132
it(`🔙 returns a string: "${expectedStringBase}"`, () => {
3233
expect(`${returns("")}`).toStrictEqual(expectedStringBase);

0 commit comments

Comments
 (0)