Skip to content

Commit 014e365

Browse files
author
Zaydek Michels-Gualtieri
committed
Extracted new enums module and test suites
1 parent e9f0829 commit 014e365

8 files changed

Lines changed: 48 additions & 15 deletions

File tree

src/Editor/Types/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Enum from "lib/Enum"
1+
import Enum from "lib/enums/Enum"
22
import iota from "lib/iota"
33

44
// NOTE: Imports are intentionally unsorted.
@@ -36,6 +36,7 @@ export const components = {
3636
[enumerated.a]: A,
3737
}
3838

39+
// TODO: Use NumberEnum
3940
const i = iota()
4041

4142
// Maps types to sort orders for text components; render

src/Editor/keydown/enumerated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Enum from "lib/Enum"
1+
import Enum from "lib/enums/Enum"
22

33
// Enumerates keydown types.
44
const enumerated = new Enum(

src/Editor/useEditor/applyFormat.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import * as Types from "../Types"
2-
import iota from "lib/iota"
32
import methods from "./methods"
3+
import NumberEnum from "lib/enums/NumberEnum"
44
import queryCollection from "./queryCollection"
55

6-
const i = iota(-1)
7-
8-
// Enumerates whether to apply a format.
9-
const enumerated = {
10-
plaintext: i(),
11-
shouldNotApply: i(),
12-
shouldApply: i(),
13-
}
6+
// Enumerates how to apply a format.
7+
const enumerated = new NumberEnum(
8+
"plaintext",
9+
"shouldNotApply",
10+
"shouldApply",
11+
)
1412

1513
// Tests whether to apply a format.
1614
const testShouldApply = collection => (T, P = {}) => {

src/Editor/useEditor/delete.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as Iterators from "../Iterators"
22
import * as Range from "../Range"
3-
import JSONClone from "lib/JSONClone"
43
import methods from "./methods"
54
import queryCollection from "./queryCollection"
65

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Enum from "./index"
1+
import Enum from "./Enum"
22

3-
test("Enum", () => {
3+
test("", () => {
44
const daysOfTheWeekEnum = new Enum(
55
"Monday",
66
"Tuesday",
@@ -16,7 +16,7 @@ test("Enum", () => {
1616
expect(daysOfTheWeekEnum.Thursday).toBe("Thursday")
1717
expect(daysOfTheWeekEnum.Friday).toBe("Friday")
1818
expect(daysOfTheWeekEnum.Saturday).toBe("Saturday")
19-
expect(daysOfTheWeekEnum.Saturday).toBe("Saturday")
19+
expect(daysOfTheWeekEnum.Sunday).toBe("Sunday")
2020

2121
// TypeError: Cannot assign to read only property 'Wednesday' of object '#<Enum>'
2222
expect(() => daysOfTheWeekEnum.Wednesday = "Humpday").toThrow()

src/lib/enums/NumberEnum.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Enumerated map. Note that enums cannot be mutated once
2+
// constructed.
3+
class NumberEnum {
4+
constructor(...enumKeys) {
5+
for (let x = 0; x < enumKeys.length; x++) {
6+
this[enumKeys[x]] = x
7+
}
8+
Object.freeze(this)
9+
}
10+
}
11+
12+
export default NumberEnum

src/lib/enums/NumberEnum.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import NumberEnum from "./NumberEnum"
2+
3+
test("", () => {
4+
const daysOfTheWeekEnum = new NumberEnum(
5+
"Monday",
6+
"Tuesday",
7+
"Wednesday",
8+
"Thursday",
9+
"Friday",
10+
"Saturday",
11+
"Sunday",
12+
)
13+
expect(daysOfTheWeekEnum.Monday).toBe(0)
14+
expect(daysOfTheWeekEnum.Tuesday).toBe(1)
15+
expect(daysOfTheWeekEnum.Wednesday).toBe(2)
16+
expect(daysOfTheWeekEnum.Thursday).toBe(3)
17+
expect(daysOfTheWeekEnum.Friday).toBe(4)
18+
expect(daysOfTheWeekEnum.Saturday).toBe(5)
19+
expect(daysOfTheWeekEnum.Sunday).toBe(6)
20+
21+
// TypeError: Cannot assign to read only property 'Wednesday' of object '#<NumberEnum>'
22+
expect(() => daysOfTheWeekEnum.Wednesday = "Humpday").toThrow()
23+
})

0 commit comments

Comments
 (0)