Skip to content

Commit cc82079

Browse files
authored
Merge pull request #2 from yet3/test/consts
Small lib refactor and tests
2 parents d76bf34 + cbd5d63 commit cc82079

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

src/consts.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ export const LINE_WIDTHS = [
66
export const GRID_CELL_SIZE: number[] = [...Array(128)].map((_, i) => i + 1);
77
export const OFFSETS: number[] = [...Array(128 + 16)].map((_, i) => i + 1);
88

9-
export const arrToObj = (arr: number[], unit = "") => {
10-
const obj: Record<string, string> = {};
11-
12-
for (const val of arr) {
13-
obj[val] = val + unit;
14-
}
15-
16-
return obj;
17-
};
18-
199
export const DEFAULT_OPTS: IResolvedOpts = {
2010
prefix: "bg-",
2111
};

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import plugin from "tailwindcss/plugin";
2-
import { GRID_CELL_SIZE, LINE_WIDTHS, OFFSETS, arrToObj } from "./consts";
2+
import { GRID_CELL_SIZE, LINE_WIDTHS, OFFSETS } from "./consts";
33
import { generateCellSizes, matchCellSize } from "./lib/cellSizes";
44
import {
55
generateLineColors,
@@ -10,6 +10,7 @@ import { generateOffsets, matchOffsets } from "./lib/offsets";
1010
import { resolveOptions } from "./lib/resolveOptions";
1111
import { generateGridClass } from "./patterns/grid";
1212
import type { IOptions } from "./types";
13+
import { arrToTwConfig } from "./lib/arrToTwConfig";
1314

1415
export default plugin.withOptions<IOptions | undefined>(
1516
(options) => (api) => {
@@ -32,9 +33,9 @@ export default plugin.withOptions<IOptions | undefined>(
3233
},
3334
() => ({
3435
theme: {
35-
bgPatternLineWidth: arrToObj(LINE_WIDTHS),
36-
bgPatternCellSize: arrToObj(GRID_CELL_SIZE),
37-
bgPatternOffsets: arrToObj(OFFSETS, "px"),
36+
bgPatternLineWidth: arrToTwConfig(LINE_WIDTHS),
37+
bgPatternCellSize: arrToTwConfig(GRID_CELL_SIZE),
38+
bgPatternOffsets: arrToTwConfig(OFFSETS, "px"),
3839
},
3940
}),
4041
);

src/lib/arrToTwConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const arrToTwConfig = (arr: number[], unit = "") => {
2+
const obj: Record<string, string> = {};
3+
4+
for (const val of arr) {
5+
obj[val] = val + unit;
6+
}
7+
8+
return obj;
9+
};

src/patterns/grid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CSSRuleObject, PluginAPI } from "tailwindcss/types/config";
1+
import type { CSSRuleObject } from "tailwindcss/types/config";
22

33
interface IOpts {
44
lineSize: number;

tests/consts.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import { describe, expect, test } from "vitest";
2-
import { DEFAULT_OPTS } from "../src/consts";
2+
import {
3+
DEFAULT_OPTS,
4+
GRID_CELL_SIZE,
5+
LINE_WIDTHS,
6+
OFFSETS,
7+
} from "../src/consts";
38

49
describe("default production values", () => {
510
test("prefix", () => {
611
expect(DEFAULT_OPTS).toHaveProperty("prefix");
712
expect(DEFAULT_OPTS.prefix).toBe("bg-");
813
});
14+
15+
test("line widths", () => {
16+
expect(LINE_WIDTHS).toEqual([
17+
0.5, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16,
18+
]);
19+
});
20+
21+
test("grid cell sizes", () => {
22+
expect(GRID_CELL_SIZE).toEqual([...Array(128)].map((_, i) => i + 1));
23+
});
24+
25+
test("offsets", () => {
26+
expect(OFFSETS).toEqual([...Array(128 + 16)].map((_, i) => i + 1));
27+
});
928
});

tests/lib.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from "vitest";
2+
import { arrToTwConfig } from "../src/lib/arrToTwConfig";
3+
4+
describe("arrToTwConfig", () => {
5+
const array: number[] = [0.5, 1, 2, 3, 4];
6+
7+
test("without unit", () => {
8+
expect(arrToTwConfig(array)).toEqual({
9+
0.5: "0.5",
10+
1: "1",
11+
2: "2",
12+
3: "3",
13+
4: "4",
14+
});
15+
});
16+
17+
test("with unit", () => {
18+
expect(arrToTwConfig(array, "px")).toEqual({
19+
0.5: "0.5px",
20+
1: "1px",
21+
2: "2px",
22+
3: "3px",
23+
4: "4px",
24+
});
25+
});
26+
});

0 commit comments

Comments
 (0)