Skip to content

Commit dedcdcb

Browse files
committed
added tests
1 parent 9531cd7 commit dedcdcb

File tree

8 files changed

+109
-14
lines changed

8 files changed

+109
-14
lines changed

jest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Config } from "@jest/types";
2+
3+
const config: Config.InitialOptions = {
4+
preset: "ts-jest",
5+
testMatch: ["**/*.test.ts"],
6+
};
7+
8+
export default config;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"watch": "tsc -w",
8-
"build": "tsc"
8+
"build": "tsc",
9+
"test": "jest"
910
},
1011
"repository": {
1112
"type": "git",
@@ -21,6 +22,7 @@
2122
"devDependencies": {
2223
"@types/jest": "^26.0.23",
2324
"@types/node": "^15.12.4",
25+
"@types/node-fetch": "^2.5.10",
2426
"jest": "^27.0.4",
2527
"ts-jest": "^27.0.3",
2628
"ts-node": "^10.0.0",

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fetch from "node-fetch";
12
import cheerio, { Cheerio, CheerioAPI, Node } from "cheerio";
23
import { Format, output, Result } from "./utils";
34

@@ -71,7 +72,7 @@ function jsonFromTable<T extends Format>(options: Options = {}) {
7172

7273
function getHeaders($: CheerioAPI, table: Cheerio<Node>, selectors: string[]) {
7374
for (const selector of selectors) {
74-
const list = $(selector, table);
75+
const list = $(selector, table.html());
7576

7677
if (list.html() !== null) {
7778
const values = list.toArray().map((v) => $(v).text().trim());
@@ -84,14 +85,17 @@ function getHeaders($: CheerioAPI, table: Cheerio<Node>, selectors: string[]) {
8485

8586
function getBody($: CheerioAPI, table: Cheerio<Node>, selectors: string[][]) {
8687
for (const selector of selectors) {
87-
const rows = $(selector[0], table).toArray();
88+
const rows = $(selector[0], table.html()).toArray();
8889

8990
if (rows.length > 0) {
9091
let values: any[] = [];
9192

9293
for (const row of rows) {
93-
const tds = $(selector[1], row).toArray();
94-
values.push(tds.map((v) => $(v).text()));
94+
const tds = $(selector[1], $(row).html())
95+
.toArray()
96+
.map((v) => $(v).text());
97+
98+
values.push(tds);
9599
}
96100

97101
return values;

src/utils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ function camalize(str: string) {
4040

4141
function toObject(headers: string[], body: string[][]) {
4242
const obj: { [key: string]: string[] } = {};
43-
const max = Math.max(body.length, headers.length);
4443

45-
for (let i = 0; i < max; i++) {
46-
obj[camalize(`${headers[i] || i}`)] = body[i];
44+
for (let i = 0; i < headers.length; i++) {
45+
obj[camalize(`${headers[i] || i}`)] = body.map((v) => v[i]);
4746
}
4847

4948
return obj;
@@ -54,14 +53,13 @@ function toJSON(headers: string[], body: string[][]) {
5453
}
5554

5655
function toArray(headers: string[], body: string[][]) {
57-
const max = Math.max(body.length, headers.length);
5856
const arr: [string, string[]][] = [];
5957

60-
for (let i = 0; i < max; i++) {
61-
arr[i] = [`${headers[i] || i}`, body[i]];
58+
for (let i = 0; i < headers.length; i++) {
59+
arr[i] = [`${headers[i] || i}`, body.map((v) => v[i])];
6260
}
6361

6462
return arr;
6563
}
6664

67-
export { output, Format, Result };
65+
export { output, Format, Result, camalize, toJSON, toObject, toArray };

tests/index.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { jsonFromTable } from "../src/index";
2+
3+
const html = `<table><tr><th>SN</th><th>Name</th></tr><tr><td>1</td><td>Roshan</td></tr><tr><td>2</td><td>John</td></tr></table>`;
4+
5+
describe("jsonFromTable - errors", () => {
6+
it("should throw error if both html and url are not provided", () => {
7+
// @ts-ignore
8+
expect(() => jsonFromTable()).toThrowError();
9+
});
10+
11+
it("should throw error if table selector is no found in html", () => {
12+
expect(() => jsonFromTable({ html, selector: ".random" })).toThrowError();
13+
});
14+
15+
it("should not throw error if both select and html/url is valid", () => {
16+
expect(() => jsonFromTable({ html, selector: "table" })).not.toThrowError();
17+
});
18+
});
19+
20+
describe("jsonFromTable - html", () => {
21+
it("should return object from html table", () => {
22+
const obj = jsonFromTable({ html });
23+
expect(obj).toEqual({ name: ["Roshan", "John"], sn: ["1", "2"] });
24+
});
25+
26+
it("should return array from html table", () => {
27+
const arr = jsonFromTable({ html, format: "array" });
28+
expect(arr).toEqual([
29+
["SN", ["1", "2"]],
30+
["Name", ["Roshan", "John"]],
31+
]);
32+
});
33+
34+
it("should return json string from html table", () => {
35+
const json = jsonFromTable({ html, format: "json" });
36+
expect(json).toEqual(`{"sn":["1","2"],"name":["Roshan","John"]}`);
37+
});
38+
39+
it("should return raw headers and body from html table", () => {
40+
const raw = jsonFromTable({ html, format: "raw" });
41+
expect(raw).toEqual({
42+
body: [
43+
["1", "Roshan"],
44+
["2", "John"],
45+
],
46+
headers: ["SN", "Name"],
47+
});
48+
});
49+
});

tests/utils.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { camalize } from "../src/utils";
2+
3+
describe("camalize function", () => {
4+
it("should remove hypen and should capitalize a character after hyphen", () => {
5+
expect(camalize("my-name")).toBe("myName");
6+
});
7+
8+
it("should remove space and should capitalize a character after space", () => {
9+
expect(camalize("my name")).toBe("myName");
10+
});
11+
12+
it("should lowercase all characters", () => {
13+
expect(camalize("MYNAME")).toBe("myname");
14+
});
15+
16+
it("should remove underscore and should capitalize a character after underscore", () => {
17+
expect(camalize("my_name")).toBe("myName");
18+
});
19+
20+
it("should work for multiple special characters", () => {
21+
expect(camalize("my|name_is-roshan acharya")).toEqual(
22+
"myNameIsRoshanAcharya"
23+
);
24+
});
25+
});

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"declaration": true,
66
"sourceMap": true,
77
"outDir": "dist",
8-
"rootDir": "src",
98
"removeComments": true,
109
"strict": true,
1110
"moduleResolution": "node",
1211
"esModuleInterop": true,
1312
"skipLibCheck": true,
1413
"forceConsistentCasingInFileNames": true
15-
}
14+
},
15+
"include": ["src"],
16+
"exclude": ["tests", "node_modules"]
1617
}

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@
604604
jest-diff "^26.0.0"
605605
pretty-format "^26.0.0"
606606

607+
"@types/node-fetch@^2.5.10":
608+
version "2.5.10"
609+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.10.tgz#9b4d4a0425562f9fcea70b12cb3fcdd946ca8132"
610+
integrity sha512-IpkX0AasN44hgEad0gEF/V6EgR5n69VEqPEgnmoM8GsIGro3PowbWs4tR6IhxUTyPLpOn+fiGG6nrQhcmoCuIQ==
611+
dependencies:
612+
"@types/node" "*"
613+
form-data "^3.0.0"
614+
607615
"@types/node@*", "@types/node@^15.12.4":
608616
version "15.12.4"
609617
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.12.4.tgz#e1cf817d70a1e118e81922c4ff6683ce9d422e26"

0 commit comments

Comments
 (0)