Skip to content

Commit 922c59e

Browse files
committed
Finish tests and lint
1 parent abf9456 commit 922c59e

File tree

7 files changed

+331
-146
lines changed

7 files changed

+331
-146
lines changed

.eslintrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"extends": ["pythoncoderas-combo"]
2+
"extends": ["pythoncoderas-combo"],
3+
"rules": {
4+
"max-classes-per-file": "off",
5+
"@typescript-eslint/no-use-before-define": "off",
6+
"complexity": "off",
7+
"no-lonely-if": "off",
8+
"max-depth": "off"
9+
}
310
}

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# typescript-project-template
2+
23
Template repo for new TypeScript projects.
34

45
This repo assumes the following:
56

6-
* Source code lives in the `src` directory.
7-
* The main export is located in `src/index.ts`.
8-
* Tests live in the `test` directory.
9-
* Each test is name `<name>.test.ts`.
10-
* The `npm_token`, `app_id`, and `app_private_key` secrets are present in the repo.
11-
* The code will be a CommonJS module.
7+
- Source code lives in the `src` directory.
8+
- The main export is located in `src/index.ts`.
9+
- Tests live in the `test` directory.
10+
- Each test is name `<name>.test.ts`.
11+
- The `npm_token`, `app_id`, and `app_private_key` secrets are present in the repo.
12+
- The code will be a CommonJS module.

src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ import Parser from "./parser";
33
export * from "./node";
44
export { default as Parser } from "./parser";
55

6-
const defaultTags = ["b", "u", "i", "s", "center", "right", "color", "size", "yt", "list", "*", "url", "img", "spoiler", "code"]
6+
const defaultTags = [
7+
"b",
8+
"u",
9+
"i",
10+
"s",
11+
"center",
12+
"right",
13+
"color",
14+
"size",
15+
"yt",
16+
"list",
17+
"*",
18+
"url",
19+
"img",
20+
"spoiler",
21+
"code",
22+
"quote",
23+
];
724
const defaultParser = new Parser(defaultTags, false);
825
export default defaultParser;

src/node.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ export interface AttributeHolder {
2525

2626
export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
2727
name: string;
28+
2829
attributes: { [key: string]: string };
30+
2931
children: BaseNode[] = [];
32+
3033
// For simple parameterized values, like [x=y]...[/x]
3134
value?: string;
3235

@@ -38,20 +41,25 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
3841
}
3942

4043
clone(): Node {
41-
const node = new Node({name: this.name, attributes: this.attributes, value: this.value});
42-
node.children = this.children.map(child => child.clone());
44+
const node = new Node({
45+
name: this.name,
46+
attributes: this.attributes,
47+
value: this.value,
48+
});
49+
node.children = this.children.map((child) => child.clone());
4350
return node;
4451
}
4552

4653
addChild(child: BaseNode): void {
47-
if (child instanceof TextNode){
54+
if (child instanceof TextNode) {
4855
const previousChild = this.children[this.children.length - 1];
49-
if (previousChild instanceof TextNode){
56+
if (previousChild instanceof TextNode) {
5057
// We flatten the text nodes.
5158
previousChild.text += child.text;
5259
return;
5360
}
5461
}
62+
5563
this.children.push(child.clone());
5664
}
5765

@@ -63,17 +71,17 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
6371
this.attributes[key] = value;
6472
}
6573

66-
6774
toString(): string {
6875
let nodeString = `[${this.name}`;
69-
if (this.value){
76+
if (this.value) {
7077
nodeString += `=${this.value}`;
7178
}
79+
7280
Object.entries(this.attributes).forEach(([key, value]) => {
7381
nodeString += ` ${key}=${value}`;
7482
});
75-
nodeString += ']';
76-
this.children.forEach(child => {
83+
nodeString += "]";
84+
this.children.forEach((child) => {
7785
nodeString += child.toString();
7886
});
7987
nodeString += `[/${this.name}]`;
@@ -83,7 +91,8 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
8391

8492
export class TextNode extends BaseNode {
8593
text: string;
86-
name: string = 'TextNode';
94+
95+
name = "TextNode";
8796

8897
constructor(text: string) {
8998
super();
@@ -101,6 +110,7 @@ export class TextNode extends BaseNode {
101110

102111
export class RootNode extends BaseNode implements ChildrenHolder {
103112
name = "RootNode";
113+
104114
children: BaseNode[];
105115

106116
constructor(children: BaseNode[] = []) {
@@ -109,34 +119,35 @@ export class RootNode extends BaseNode implements ChildrenHolder {
109119
}
110120

111121
addChild(child: BaseNode): void {
112-
if (child instanceof TextNode){
122+
if (child instanceof TextNode) {
113123
const previousChild = this.children[this.children.length - 1];
114-
if (previousChild instanceof TextNode){
124+
if (previousChild instanceof TextNode) {
115125
// We flatten the text nodes.
116126
previousChild.text += child.text;
117127
return;
118128
}
119129
}
130+
120131
this.children.push(child.clone());
121132
}
122133

123134
clone(): RootNode {
124-
return new RootNode(this.children.map(child => child.clone()));
135+
return new RootNode(this.children.map((child) => child.clone()));
125136
}
126137

127138
toString(): string {
128-
return this.children.map(child => child.toString()).join('');
139+
return this.children.map((child) => child.toString()).join("");
129140
}
130141
}
131142

132143
export class ListItemNode extends RootNode {
133144
name = "*";
134145

135146
toString(): string {
136-
return "[*]" + super.toString();
147+
return `[*]${super.toString()}`;
137148
}
138149

139150
clone(): ListItemNode {
140-
return new ListItemNode(this.children.map(child => child.clone()));
151+
return new ListItemNode(this.children.map((child) => child.clone()));
141152
}
142153
}

0 commit comments

Comments
 (0)