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

Commit c2cd338

Browse files
committed
refactor(packages/string): ♻️ Apply the changes
1 parent a383ce8 commit c2cd338

File tree

4 files changed

+90
-119
lines changed

4 files changed

+90
-119
lines changed

packages/string/source/char/char.test.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { ALL_SAMPLES, FALSY_STRINGS, SAMPLE_STRING } from "@terminal-nerds/snippets-test/sample";
12
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
23
import { describe, expect, it } from "vitest";
34
import { ZodError } from "zod";
45

5-
import { EMPTY_STRING_VALUES, SAMPLE_INPUT, testInvalidInput } from "../../tests/shared.js";
66
import {
77
CHAR_TYPES,
88
type CharOptions,
@@ -27,6 +27,19 @@ import {
2727
validateSingleChar,
2828
} from "./char.js";
2929

30+
const EMPTY_STRINGS = FALSY_STRINGS;
31+
const NON_STRING_VALUES = ALL_SAMPLES.filter((v) => typeof v !== "string");
32+
33+
/* eslint-disable @typescript-eslint/no-explicit-any */
34+
function testInvalidInput(method: (input: any, options?: any) => void, options?: any): void {
35+
it(throws(ZodError).on(`passed non-string input`), () => {
36+
for (const nonString of NON_STRING_VALUES) {
37+
expect(() => method(nonString, options)).toThrowError(ZodError);
38+
}
39+
});
40+
}
41+
42+
// @ts-ignore FIXME: I have absolutely no clue how to fix this one, help!
3043
const INPUT_WITH_ALL_CHARS = getJoinedChars(SINGLE_CHARS);
3144
const INPUT_WITH_UPPER_CASED_LATIN_CHARS = getJoinedChars(UPPER_CASED_LATIN_CHARS);
3245
const INPUT_WITH_LOWER_CASED_LATIN_CHARS = getJoinedChars(LOWER_CASED_LATIN_CHARS);
@@ -42,12 +55,12 @@ function testInvalidStringInput(method: Parameters<typeof testInvalidInput>[0]):
4255
function testInvalidCharInput(method: Parameters<typeof testInvalidInput>[0]): void {
4356
testInvalidStringInput(method);
4457

45-
it(throws(ZodError).on(`input longer than 1 char`).sample(SAMPLE_INPUT), () => {
46-
expect(() => method(SAMPLE_INPUT, { type: "latin" })).toThrowError(ZodError);
58+
it(throws(ZodError).on(`input longer than 1 char`).sample(SAMPLE_STRING), () => {
59+
expect(() => method(SAMPLE_STRING, { type: "latin" })).toThrowError(ZodError);
4760
});
4861

4962
it(throws(ZodError).on(`empty string input`), () => {
50-
for (const emptyString of EMPTY_STRING_VALUES) {
63+
for (const emptyString of EMPTY_STRINGS) {
5164
expect(() => method(emptyString, { type: "latin" })).toThrowError(ZodError);
5265
}
5366
});
@@ -58,7 +71,7 @@ function testInvalidCharTypeOption(method: (input: string, options: CharOptions<
5871

5972
it(throws(ZodError).on(`passed unrecognized char type option`).with(options), () => {
6073
// @ts-expect-error Testing
61-
expect(() => method(SAMPLE_INPUT, options)).toThrowError(ZodError);
74+
expect(() => method(SAMPLE_STRING, options)).toThrowError(ZodError);
6275
});
6376
}
6477

@@ -69,8 +82,8 @@ describe("isSingleChar(input)", () => {
6982
expect(isSingleChar("")).toBe(false);
7083
});
7184

72-
it(returns(false).on(`input longer than 1 char`).sample(SAMPLE_INPUT), () => {
73-
expect(isSingleChar(SAMPLE_INPUT)).toBe(false);
85+
it(returns(false).on(`input longer than 1 char`).sample(SAMPLE_STRING), () => {
86+
expect(isSingleChar(SAMPLE_STRING)).toBe(false);
7487
});
7588

7689
it(returns(true).on(`single chars input`).samples(SINGLE_CHARS), () => {
@@ -248,6 +261,16 @@ describe("hasChars(input, options?)", () => {
248261
},
249262
);
250263

264+
it(
265+
returns(true)
266+
.on(`input with lowercased latin chars`)
267+
.sample(INPUT_WITH_LOWER_CASED_LATIN_CHARS)
268+
.with(latinInsentitiveOptions),
269+
() => {
270+
expect(hasChars(INPUT_WITH_LOWER_CASED_LATIN_CHARS, latinInsentitiveOptions)).toBe(true);
271+
},
272+
);
273+
251274
it(
252275
returns(true)
253276
.on(`input with uppercased latin chars`)
@@ -291,33 +314,33 @@ describe("getChars(input, options?)", () => {
291314

292315
const latinOptions = { type: "latin" } as const;
293316
/* prettier-ignore */
294-
const expectedLatinChars = ["X", "E", "H", "O", "t", "e", "r", "m", "i", "n", "a", "l", "n", "e", "r", "d", "s"] as const;
317+
const expectedLatinChars = ["t", "e", "r", "m", "i", "n", "a", "l", "n", "e", "r", "d", "s", "D", "E", "V"] as const;
295318

296-
it(returns(expectedLatinChars).on(`sample input`).sample(SAMPLE_INPUT).with(latinOptions), () => {
297-
expect(getChars(SAMPLE_INPUT, latinOptions)).toStrictEqual(expectedLatinChars);
319+
it(returns(expectedLatinChars).on(`sample input`).sample(SAMPLE_STRING).with(latinOptions), () => {
320+
expect(getChars(SAMPLE_STRING, latinOptions)).toStrictEqual(expectedLatinChars);
298321
});
299322

300323
const latinInsentitiveOptions = { type: "latin", caseInsensitive: false } as const;
301324
const expectedLowerCasedLatinChars = ["t", "e", "r", "m", "i", "n", "a", "l", "n", "e", "r", "d", "s"] as const;
302325

303326
it(
304-
returns(expectedLowerCasedLatinChars).on(`sample input`).sample(SAMPLE_INPUT).with(latinInsentitiveOptions),
327+
returns(expectedLowerCasedLatinChars).on(`sample input`).sample(SAMPLE_STRING).with(latinInsentitiveOptions),
305328
() => {
306-
expect(getChars(SAMPLE_INPUT, latinInsentitiveOptions)).toStrictEqual(expectedLowerCasedLatinChars);
329+
expect(getChars(SAMPLE_STRING, latinInsentitiveOptions)).toStrictEqual(expectedLowerCasedLatinChars);
307330
},
308331
);
309332

310333
const numberOptions = { type: "number" } as const;
311-
const expectedNumberChars = ["9", "1"] as const;
334+
const expectedNumberChars = ["2", "0", "2", "3"] as const;
312335

313-
it(returns(expectedNumberChars).on(`sample input`).sample(SAMPLE_INPUT).with(numberOptions), () => {
314-
expect(getChars(SAMPLE_INPUT, numberOptions)).toStrictEqual(expectedNumberChars);
336+
it(returns(expectedNumberChars).on(`sample input`).sample(SAMPLE_STRING).with(numberOptions), () => {
337+
expect(getChars(SAMPLE_STRING, numberOptions)).toStrictEqual(expectedNumberChars);
315338
});
316339

317340
const specialOptions = { type: "special" } as const;
318-
const expectedSpecialChars = ["@", "-"] as const;
341+
const expectedSpecialChars = ["-", ".", "@"] as const;
319342

320-
it(returns(expectedSpecialChars).on(`sample input`).sample(SAMPLE_INPUT).with(specialOptions), () => {
321-
expect(getChars(SAMPLE_INPUT, specialOptions)).toStrictEqual(expectedSpecialChars);
343+
it(returns(expectedSpecialChars).on(`sample input`).sample(SAMPLE_STRING).with(specialOptions), () => {
344+
expect(getChars(SAMPLE_STRING, specialOptions)).toStrictEqual(expectedSpecialChars);
322345
});
323346
});

packages/string/source/schema/schema.test.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1-
import { returns } from "@terminal-nerds/snippets-test/unit";
1+
import { ALL_SAMPLES, FALSY_STRINGS, SAMPLE_STRINGS, TRUTHY_STRINGS } from "@terminal-nerds/snippets-test/sample";
2+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
23
import { describe, expect, it } from "vitest";
34
import { ZodError } from "zod";
45

5-
import {
6-
EMPTY_STRING_VALUES,
7-
NON_EMPTY_STRING_VALUES,
8-
NON_STRING_VALUES,
9-
STRING_VALUES,
10-
testInvalidInput,
11-
} from "../../tests/shared.js";
126
import { isString, isStringEmpty, validateString } from "./schema.js";
137

14-
describe("isString(input)", () => {
15-
it(returns(true).on(`passed string inputs`).samples(STRING_VALUES), () => {
16-
for (const string of STRING_VALUES) {
17-
expect(isString(string)).toBe(true);
8+
const EMPTY_STRINGS = FALSY_STRINGS;
9+
const NON_EMPTY_STRINGS = TRUTHY_STRINGS;
10+
const NON_STRING_VALUES = ALL_SAMPLES.filter((v) => typeof v !== "string");
11+
12+
/* eslint-disable @typescript-eslint/no-explicit-any */
13+
function testInvalidInput(method: (input: any, options?: any) => void, options?: any): void {
14+
it(throws(ZodError).on(`passed non-string input`), () => {
15+
for (const nonString of NON_STRING_VALUES) {
16+
expect(() => method(nonString, options)).toThrowError(ZodError);
1817
}
1918
});
19+
}
2020

21+
describe("isString(input)", () => {
2122
it(returns(false).on(`passed non-string inputs`).samples(NON_STRING_VALUES), () => {
2223
for (const nonString of NON_STRING_VALUES) {
2324
expect(isString(nonString)).toBe(false);
2425
}
2526
});
27+
28+
it(returns(true).on(`passed string inputs`).samples(SAMPLE_STRINGS), () => {
29+
for (const string of SAMPLE_STRINGS) {
30+
expect(isString(string)).toBe(true);
31+
}
32+
});
2633
});
2734

2835
describe("validateString(input)", () => {
2936
testInvalidInput(validateString);
3037

31-
it(returns(true).on(`passed string inputs`).samples(STRING_VALUES), () => {
32-
for (const value of STRING_VALUES) {
38+
it(returns(true).on(`passed string inputs`).samples(SAMPLE_STRINGS), () => {
39+
for (const value of SAMPLE_STRINGS) {
3340
expect(() => validateString(value)).not.toThrowError(ZodError);
3441
}
3542
});
@@ -38,14 +45,14 @@ describe("validateString(input)", () => {
3845
describe("isStringEmpty(input)", () => {
3946
testInvalidInput(isStringEmpty);
4047

41-
it(returns(true).on(`passed empty string inputs`).samples(EMPTY_STRING_VALUES), () => {
42-
for (const emptyString of EMPTY_STRING_VALUES) {
48+
it(returns(true).on(`passed empty string inputs`).samples(EMPTY_STRINGS), () => {
49+
for (const emptyString of EMPTY_STRINGS) {
4350
expect(isStringEmpty(emptyString)).toBe(true);
4451
}
4552
});
4653

47-
it(returns(false).on(`passed empty non-string inputs`).samples(NON_EMPTY_STRING_VALUES), () => {
48-
for (const nonEmptyString of NON_EMPTY_STRING_VALUES) {
54+
it(returns(false).on(`passed empty non-string inputs`).samples(NON_EMPTY_STRINGS), () => {
55+
for (const nonEmptyString of NON_EMPTY_STRINGS) {
4956
expect(isStringEmpty(nonEmptyString)).toBe(false);
5057
}
5158
});

packages/string/source/word/word.test.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
import { returns } from "@terminal-nerds/snippets-test/unit";
1+
import { ALL_SAMPLES, FALSY_STRINGS, NUMERIC_STRINGS, SAMPLE_STRING } from "@terminal-nerds/snippets-test/sample";
2+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
23
import { describe, expect, it } from "vitest";
4+
import { ZodError } from "zod";
35

4-
import { EMPTY_STRING_VALUES, SAMPLE_INPUT, testInvalidInput } from "../../tests/shared.js";
56
import { SINGLE_CHARS } from "../char/char.js";
67
import { isNumeric, isPalindrome, reverseString } from "./word.js";
78

9+
const EMPTY_STRING_VALUES = FALSY_STRINGS;
10+
const NON_STRING_VALUES = ALL_SAMPLES.filter((v) => typeof v !== "string");
11+
12+
/* eslint-disable @typescript-eslint/no-explicit-any */
13+
function testInvalidInput(method: (input: any, options?: any) => void, options?: any): void {
14+
it(throws(ZodError).on(`passed non-string input`), () => {
15+
for (const nonString of NON_STRING_VALUES) {
16+
expect(() => method(nonString, options)).toThrowError(ZodError);
17+
}
18+
});
19+
}
20+
821
describe("reverseString(input)", () => {
922
testInvalidInput(reverseString);
1023

11-
const reversedSampleString = `sdren-lanimret@19OHEX`;
24+
const reversedSampleString = `3202@VED.sdren-lanimret`;
1225

13-
it(returns(reversedSampleString).on(`sample input`).sample(SAMPLE_INPUT), () => {
14-
expect(reverseString(SAMPLE_INPUT)).toStrictEqual(reversedSampleString);
26+
it(returns(reversedSampleString).on(`sample input`).sample(SAMPLE_STRING), () => {
27+
expect(reverseString(SAMPLE_STRING)).toStrictEqual(reversedSampleString);
1528
});
1629
});
1730

@@ -25,8 +38,8 @@ const PALINDROME_SAMPLES = [
2538
describe("isPalindrome(input)", () => {
2639
testInvalidInput(isPalindrome);
2740

28-
it(returns(false).on(`sample input`).sample(SAMPLE_INPUT), () => {
29-
expect(isPalindrome(SAMPLE_INPUT)).toBe(false);
41+
it(returns(false).on(`sample input`).sample(SAMPLE_STRING), () => {
42+
expect(isPalindrome(SAMPLE_STRING)).toBe(false);
3043
});
3144

3245
it(returns(true).on(`empty string inputs`).samples(EMPTY_STRING_VALUES), () => {
@@ -48,27 +61,11 @@ describe("isPalindrome(input)", () => {
4861
});
4962
});
5063

51-
/* prettier-ignore */
52-
const NUMERIC_STRING_SAMPLES = [
53-
"123456789",
54-
"-123456789",
55-
"123.456789",
56-
"-123.456789",
57-
" 123.456789 ",
58-
" -123.456789 ",
59-
"0b11111111", // 255
60-
"0o377", // 255
61-
"0xFF", // 255
62-
"10e1000",
63-
"Infinity",
64-
"-Infinity",
65-
] as const;
66-
6764
describe("isNumeric(input)", () => {
6865
testInvalidInput(isNumeric);
6966

70-
it(returns(false).on(`input`).sample(SAMPLE_INPUT), () => {
71-
expect(isNumeric(SAMPLE_INPUT)).toBe(false);
67+
it(returns(false).on(`input`).sample(SAMPLE_STRING), () => {
68+
expect(isNumeric(SAMPLE_STRING)).toBe(false);
7269
});
7370

7471
it(returns(false).on(`not a number string`).sample("NaN"), () => {
@@ -86,8 +83,8 @@ describe("isNumeric(input)", () => {
8683
}
8784
});
8885

89-
it(returns(true).on(`sample numeric strings inputs`).samples(NUMERIC_STRING_SAMPLES), () => {
90-
for (const numericString of NUMERIC_STRING_SAMPLES) {
86+
it(returns(true).on(`sample numeric strings inputs`).samples(NUMERIC_STRINGS), () => {
87+
for (const numericString of NUMERIC_STRINGS) {
9188
expect(isNumeric(numericString)).toBe(true);
9289
}
9390
});

packages/string/tests/shared.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)