Skip to content

Commit ca7846d

Browse files
committed
first commit
1 parent 5b97b33 commit ca7846d

File tree

8 files changed

+9222
-0
lines changed

8 files changed

+9222
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
dist
64+
.cache

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"semi": false
3+
}

babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript"
5+
]
6+
};

jest.config.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "/private/var/folders/0v/d46trfxs4cd95792qwgn0_l00000gp/T/jest_dy",
16+
17+
// Automatically clear mock calls and instances between every test
18+
// clearMocks: false,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: null,
25+
26+
// The directory where Jest should output its coverage files
27+
// coverageDirectory: null,
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "/node_modules/"
32+
// ],
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: null,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: null,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: null,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: null,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// An array of directory names to be searched recursively up from the requiring module's location
64+
// moduleDirectories: [
65+
// "node_modules"
66+
// ],
67+
68+
// An array of file extensions your modules use
69+
// moduleFileExtensions: [
70+
// "js",
71+
// "json",
72+
// "jsx",
73+
// "ts",
74+
// "tsx",
75+
// "node"
76+
// ],
77+
78+
// A map from regular expressions to module names that allow to stub out resources with a single module
79+
// moduleNameMapper: {},
80+
81+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
82+
// modulePathIgnorePatterns: [],
83+
84+
// Activates notifications for test results
85+
// notify: false,
86+
87+
// An enum that specifies notification mode. Requires { notify: true }
88+
// notifyMode: "failure-change",
89+
90+
// A preset that is used as a base for Jest's configuration
91+
// preset: null,
92+
93+
// Run tests from one or more projects
94+
// projects: null,
95+
96+
// Use this configuration option to add custom reporters to Jest
97+
// reporters: undefined,
98+
99+
// Automatically reset mock state between every test
100+
// resetMocks: false,
101+
102+
// Reset the module registry before running each individual test
103+
// resetModules: false,
104+
105+
// A path to a custom resolver
106+
// resolver: null,
107+
108+
// Automatically restore mock state between every test
109+
// restoreMocks: false,
110+
111+
// The root directory that Jest should scan for tests and modules within
112+
// rootDir: null,
113+
114+
// A list of paths to directories that Jest should use to search for files in
115+
// roots: [
116+
// "<rootDir>"
117+
// ],
118+
119+
// Allows you to use a custom runner instead of Jest's default test runner
120+
// runner: "jest-runner",
121+
122+
// The paths to modules that run some code to configure or set up the testing environment before each test
123+
// setupFiles: [],
124+
125+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
126+
// setupFilesAfterEnv: [],
127+
128+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
129+
// snapshotSerializers: [],
130+
131+
// The test environment that will be used for testing
132+
// testEnvironment: "jest-environment-jsdom",
133+
134+
// Options that will be passed to the testEnvironment
135+
// testEnvironmentOptions: {},
136+
137+
// Adds a location field to test results
138+
// testLocationInResults: false,
139+
140+
// The glob patterns Jest uses to detect test files
141+
// testMatch: [
142+
// "**/__tests__/**/*.[jt]s?(x)",
143+
// "**/?(*.)+(spec|test).[tj]s?(x)"
144+
// ],
145+
146+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
147+
// testPathIgnorePatterns: [
148+
// "/node_modules/"
149+
// ],
150+
151+
// The regexp pattern or array of patterns that Jest uses to detect test files
152+
// testRegex: [],
153+
154+
// This option allows the use of a custom results processor
155+
// testResultsProcessor: null,
156+
157+
// This option allows use of a custom test runner
158+
// testRunner: "jasmine2",
159+
160+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
161+
// testURL: "http://localhost",
162+
163+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
164+
// timers: "real",
165+
166+
// A map from regular expressions to paths to transformers
167+
// transform: null,
168+
169+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
170+
// transformIgnorePatterns: [
171+
// "/node_modules/"
172+
// ],
173+
174+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
175+
// unmockedModulePathPatterns: undefined,
176+
177+
// Indicates whether each individual test should be reported during the run
178+
// verbose: null,
179+
180+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
181+
// watchPathIgnorePatterns: [],
182+
183+
// Whether to use watchman for file crawling
184+
// watchman: true,
185+
};

lib/html-table-parser.test.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { parseTable } from "./html-table-parser"
2+
3+
const cases = [
4+
{
5+
name: "can parse simple clean table",
6+
html: `
7+
<thead>
8+
<tr><th>th 1</th><th>th 2</th></tr>
9+
</thead>
10+
<tbody>
11+
<tr><td>td 1</td><td>td 2</td></tr>
12+
</tbody>
13+
`,
14+
expected: [
15+
[
16+
{ elementId: 0, tagName: "TH", textContent: "th 1", attributes: {} },
17+
{ elementId: 1, tagName: "TH", textContent: "th 2", attributes: {} }
18+
],
19+
[
20+
{ elementId: 2, tagName: "TD", textContent: "td 1", attributes: {} },
21+
{ elementId: 3, tagName: "TD", textContent: "td 2", attributes: {} }
22+
]
23+
]
24+
},
25+
{
26+
name: "can parse table with multiple tbodies",
27+
html: `
28+
<thead>
29+
<tr><th>th 1</th><th>th 2</th></tr>
30+
</thead>
31+
<tbody>
32+
<tr><td>td 1</td><td>td 2</td></tr>
33+
</tbody>
34+
<tbody>
35+
<tr><td>td 3</td><td>td 4</td></tr>
36+
</tbody>
37+
`,
38+
expected: [
39+
[
40+
{ elementId: 0, tagName: "TH", textContent: "th 1", attributes: {} },
41+
{ elementId: 1, tagName: "TH", textContent: "th 2", attributes: {} }
42+
],
43+
[
44+
{ elementId: 2, tagName: "TD", textContent: "td 1", attributes: {} },
45+
{ elementId: 3, tagName: "TD", textContent: "td 2", attributes: {} }
46+
],
47+
[
48+
{ elementId: 4, tagName: "TD", textContent: "td 3", attributes: {} },
49+
{ elementId: 5, tagName: "TD", textContent: "td 4", attributes: {} }
50+
]
51+
]
52+
},
53+
{
54+
name: "can parse nested table",
55+
html: `
56+
<thead>
57+
<tr><th>th 1</th><th>th 2</th></tr>
58+
</thead>
59+
<tbody>
60+
<tr><td>td 1</td><td>td 2<table><tbody><tr><td>foo</td></tr></tbody></table></td></tr>
61+
</tbody>
62+
`,
63+
expected: [
64+
[
65+
{ elementId: 0, tagName: "TH", textContent: "th 1", attributes: {} },
66+
{ elementId: 1, tagName: "TH", textContent: "th 2", attributes: {} }
67+
],
68+
[
69+
{ elementId: 2, tagName: "TD", textContent: "td 1", attributes: {} },
70+
{ elementId: 3, tagName: "TD", textContent: "td 2foo", attributes: {} }
71+
]
72+
]
73+
},
74+
{
75+
name: "can parse table with colspan",
76+
html: `
77+
<tr><td colspan="2">td 1</td><td>td 2</td></tr>
78+
`,
79+
expected: [
80+
[
81+
{
82+
elementId: 0,
83+
tagName: "TD",
84+
textContent: "td 1",
85+
attributes: { colspan: "2" }
86+
},
87+
{
88+
elementId: 0,
89+
tagName: "TD",
90+
textContent: "td 1",
91+
attributes: { colspan: "2" }
92+
},
93+
{ elementId: 1, tagName: "TD", textContent: "td 2", attributes: {} }
94+
]
95+
]
96+
},
97+
{
98+
name: "can parse table with rowpan",
99+
html: `
100+
<tr><td rowspan="2">td 1</td><td>td 2</td></tr>
101+
<tr><td>td 3</td></tr>
102+
`,
103+
expected: [
104+
[
105+
{
106+
elementId: 0,
107+
tagName: "TD",
108+
textContent: "td 1",
109+
attributes: { rowspan: "2" }
110+
},
111+
{ elementId: 1, tagName: "TD", textContent: "td 2", attributes: {} }
112+
],
113+
[
114+
{
115+
elementId: 0,
116+
tagName: "TD",
117+
textContent: "td 1",
118+
attributes: { rowspan: "2" }
119+
},
120+
{ elementId: 2, tagName: "TD", textContent: "td 3", attributes: {} }
121+
]
122+
]
123+
}
124+
]
125+
126+
describe("parseTable", () => {
127+
cases.forEach(testCase => {
128+
it(testCase.name, () => {
129+
const table = document.createElement("table")
130+
table.innerHTML = testCase.html
131+
132+
expect(parseTable(table)).toStrictEqual(testCase.expected)
133+
})
134+
})
135+
})

0 commit comments

Comments
 (0)