Skip to content

Commit 2a35d4a

Browse files
Optimize loop. (#384)
* Optimize loop. * Update test.yml
1 parent 70eb30a commit 2a35d4a

33 files changed

+291
-195
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- uses: actions/setup-node@v4
1313
with:
1414
node-version: 20
15-
- run: npm install && npm run bootstrap
15+
- run: npm install && cd packages/typeit && npm install && cd ../typeit-react && npm install && cd ../../ && npm run build
1616
- run: npm test
1717
- uses: preactjs/compressed-size-action@v2
1818
with:

lerna.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"packages/*"
44
],
55
"version": "independent",
6+
"useNx": false,
67
"command": {
78
"publish": {
89
"ignoreChanges": [

packages/typeit-react/src/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe("rendering as specific element", () => {
1616
}}
1717
>
1818
A span tag.
19-
</TypeIt>
19+
</TypeIt>,
2020
);
2121
});
2222

@@ -39,7 +39,7 @@ describe("rendering as specific element", () => {
3939
}}
4040
>
4141
An H1 tag.
42-
</TypeIt>
42+
</TypeIt>,
4343
);
4444
});
4545

packages/typeit-react/src/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@testing-library/jest-dom/vitest';
1+
import "@testing-library/jest-dom/vitest";
22
import { beforeEach } from "vitest";
33
import "vitest-dom/extend-expect";
44

packages/typeit/__tests__/TypeIt.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe("go()", () => {
149149
let element = document.getElementById("element");
150150

151151
expect(element.dataset.typeitId).toEqual(
152-
cursorNode.dataset.tiAnimationId
152+
cursorNode.dataset.tiAnimationId,
153153
);
154154
expect(cursorNode.style.visibility).toEqual("");
155155
expect(cursorNode).not.toBeNull();
@@ -220,7 +220,7 @@ describe("type()", () => {
220220
let functionalInstance = new TypeIt("#element").type(() => "a");
221221

222222
expect(instance.getQueue().getItems()).toHaveLength(
223-
functionalInstance.getQueue().getItems().length
223+
functionalInstance.getQueue().getItems().length,
224224
);
225225
});
226226
});
@@ -262,7 +262,7 @@ describe("move()", () => {
262262
let functionalInstance = new TypeIt("#element").move(() => 9);
263263

264264
expect(instance.getQueue().getItems()).toHaveLength(
265-
functionalInstance.getQueue().getItems().length
265+
functionalInstance.getQueue().getItems().length,
266266
);
267267
});
268268
});
@@ -418,8 +418,8 @@ describe("reset()", () => {
418418
afterComplete: () => {
419419
expect(element.innerHTML).toEqual(
420420
expect.stringMatching(
421-
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
422-
)
421+
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
422+
),
423423
);
424424
instance = instance.reset();
425425
expect(element.innerHTML).toEqual("");

packages/typeit/__tests__/chainable-methods/delete.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe("deleting fires correctly", () => {
1616
afterComplete: () => {
1717
expect(el.innerHTML).toEqual(
1818
expect.stringMatching(
19-
/abc<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
20-
)
19+
/abc<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
20+
),
2121
);
2222
done();
2323
},
@@ -36,8 +36,8 @@ describe("deleting fires correctly", () => {
3636
afterComplete: () => {
3737
expect(el.innerHTML).toEqual(
3838
expect.stringMatching(
39-
/abc<strong>def<\/strong><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
40-
)
39+
/abc<strong>def<\/strong><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
40+
),
4141
);
4242
done();
4343
},
@@ -112,8 +112,8 @@ describe("cursor has been moved", () => {
112112
afterComplete: () => {
113113
expect(document.body.querySelector("#element").innerHTML).toEqual(
114114
expect.stringMatching(
115-
/abc <strong>def<\/strong> g<em><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>jkl<\/em> mno/
116-
)
115+
/abc <strong>def<\/strong> g<em><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>jkl<\/em> mno/,
116+
),
117117
);
118118
done();
119119
},
@@ -130,8 +130,8 @@ describe("cursor has been moved", () => {
130130
afterComplete: () => {
131131
expect(document.body.querySelector("#element").innerHTML).toEqual(
132132
expect.stringMatching(
133-
/^abc <span class=\"ti-cursor\" data-ti-animation-id=\".+\">|<\/span> mno/
134-
)
133+
/^abc <span class=\"ti-cursor\" data-ti-animation-id=\".+\">|<\/span> mno/,
134+
),
135135
);
136136
done();
137137
},

packages/typeit/__tests__/flush.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("deleting queue items", () => {
2828
instance.type("goodbye").flush(() => {
2929
expect(el.textContent).toEqual("hellogoodbye|");
3030
expect(instance.getQueue().getItems(true).length).toBeGreaterThan(
31-
0
31+
0,
3232
);
3333
resolve();
3434
});

packages/typeit/__tests__/helpers/chunkStrings.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,39 @@ test("Parses normal string correctly.", () => {
1717

1818
test("Parses single HTML tag.", () => {
1919
let result = chunkStringAsHtml(
20-
"Hello, this is some <strong>bold</strong> text."
20+
"Hello, this is some <strong>bold</strong> text.",
2121
);
2222

2323
expect(result).toMatchSnapshot();
2424
});
2525

2626
test("Parses multiple HTML tags.", () => {
2727
let result = chunkStringAsHtml(
28-
"Hello, this is some <strong>bold</strong> text, and some <i>italicized</i> text."
28+
"Hello, this is some <strong>bold</strong> text, and some <i>italicized</i> text.",
2929
);
3030

3131
expect(result).toMatchSnapshot();
3232
});
3333

3434
test("Parses HTML tag at beginning of string.", () => {
3535
let result = chunkStringAsHtml(
36-
"<strong>Hello!</strong> This is some text with HTML at the beginning."
36+
"<strong>Hello!</strong> This is some text with HTML at the beginning.",
3737
);
3838

3939
expect(result).toMatchSnapshot();
4040
});
4141

4242
test("Parses HTML tag at end of string.", () => {
4343
let result = chunkStringAsHtml(
44-
"This is some text with HTML at the <em>end.</em>"
44+
"This is some text with HTML at the <em>end.</em>",
4545
);
4646

4747
expect(result).toMatchSnapshot();
4848
});
4949

5050
test("Parses HTML tag with attributes.", () => {
5151
let result = chunkStringAsHtml(
52-
'This string has an <strong class="strong-class" id="strong-id" data-whatever="data-att">element</strong> with attributes.'
52+
'This string has an <strong class="strong-class" id="strong-id" data-whatever="data-att">element</strong> with attributes.',
5353
);
5454

5555
expect(result).toMatchSnapshot();
@@ -58,13 +58,13 @@ test("Parses HTML tag with attributes.", () => {
5858
describe("maybeChunkStringAsHtml()", () => {
5959
test("Should return noderized string when setting is enabled.", () => {
6060
expect(
61-
maybeChunkStringAsHtml("A <em>fancy</em> string.")
61+
maybeChunkStringAsHtml("A <em>fancy</em> string."),
6262
).toMatchSnapshot();
6363
});
6464

6565
test("Should correctly transform non-HTML string as character objects.", () => {
6666
expect(
67-
maybeChunkStringAsHtml("A <em>fancy</em> string.", false)
67+
maybeChunkStringAsHtml("A <em>fancy</em> string.", false),
6868
).toMatchSnapshot();
6969
});
7070
});
@@ -79,7 +79,7 @@ describe("walkElementNodes()", () => {
7979

8080
// "B" should have an original parent of the <span>.
8181
expect(thirdNode.originalParent).toMatchObject(
82-
document.querySelector("span")
82+
document.querySelector("span"),
8383
);
8484

8585
// Includes each text node, and separate nodes for the parent elements themselves.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { expect, it, vi } from "vitest";
2+
import cleanUpSkipped from "../../src/helpers/cleanUpSkipped";
3+
import { QueueMapPair } from "../../src/types";
4+
5+
it("should clean up skipped items", () => {
6+
const cleanUp = vi.fn();
7+
const queueItems = [
8+
[Symbol("1"), vi.fn()],
9+
[Symbol("2"), vi.fn()],
10+
[Symbol("3"), vi.fn()],
11+
[Symbol("4"), vi.fn()],
12+
[Symbol("5"), vi.fn()],
13+
] as QueueMapPair[];
14+
15+
cleanUpSkipped({
16+
index: 1,
17+
newIndex: 3,
18+
queueItems,
19+
cleanUp,
20+
});
21+
22+
expect(cleanUp).toHaveBeenCalledTimes(2);
23+
expect(cleanUp).toHaveBeenCalledWith(queueItems[2][0]);
24+
expect(cleanUp).toHaveBeenCalledWith(queueItems[3][0]);
25+
});
26+
27+
it("should clean up skipped items... again", () => {
28+
const cleanUp = vi.fn();
29+
const queueItems = [
30+
[Symbol("1"), vi.fn()],
31+
[Symbol("2"), vi.fn()],
32+
[Symbol("3"), vi.fn()],
33+
[Symbol("4"), vi.fn()],
34+
[Symbol("5"), vi.fn()],
35+
] as QueueMapPair[];
36+
37+
cleanUpSkipped({
38+
index: 0,
39+
newIndex: 4,
40+
queueItems,
41+
cleanUp,
42+
});
43+
44+
expect(cleanUp).toHaveBeenCalledTimes(4);
45+
expect(cleanUp).toHaveBeenCalledWith(queueItems[1][0]);
46+
expect(cleanUp).toHaveBeenCalledWith(queueItems[2][0]);
47+
expect(cleanUp).toHaveBeenCalledWith(queueItems[3][0]);
48+
expect(cleanUp).toHaveBeenCalledWith(queueItems[4][0]);
49+
});
50+
51+
it("should clean up skipped items... again", () => {
52+
const cleanUp = vi.fn();
53+
const queueItems = [
54+
[Symbol("1"), vi.fn()],
55+
[Symbol("2"), vi.fn()],
56+
[Symbol("3"), vi.fn()],
57+
[Symbol("4"), vi.fn()],
58+
[Symbol("5"), vi.fn()],
59+
] as QueueMapPair[];
60+
61+
cleanUpSkipped({
62+
index: 0,
63+
newIndex: 0,
64+
queueItems,
65+
cleanUp,
66+
});
67+
68+
expect(cleanUp).toHaveBeenCalledTimes(0);
69+
});
70+
71+
it("should clean up skipped items... again", () => {
72+
const cleanUp = vi.fn();
73+
const queueItems = [
74+
[Symbol("1"), vi.fn()],
75+
[Symbol("2"), vi.fn()],
76+
[Symbol("3"), vi.fn()],
77+
[Symbol("4"), vi.fn()],
78+
[Symbol("5"), vi.fn()],
79+
] as QueueMapPair[];
80+
81+
cleanUpSkipped({
82+
index: 4,
83+
newIndex: 4,
84+
queueItems,
85+
cleanUp,
86+
});
87+
88+
expect(cleanUp).toHaveBeenCalledTimes(0);
89+
});

packages/typeit/__tests__/helpers/countStepsToSelector.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("moving toward the end", () => {
7070

7171
it("calculates correctly with nested HTML", () => {
7272
let queue = buildQueueFromString(
73-
`23<strong>goodbye<em>ok, then!</em>haha.</strong>whatever.`
73+
`23<strong>goodbye<em>ok, then!</em>haha.</strong>whatever.`,
7474
);
7575

7676
const result = countStepsToSelector({

0 commit comments

Comments
 (0)