Skip to content

Commit

Permalink
feat: refactored and adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
simonecorsi committed Oct 6, 2021
1 parent 2ee562d commit 08b2399
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 142 deletions.
5 changes: 5 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
branches: 95
functions: 95
lines: 95
statements: 95
ts: true
66 changes: 0 additions & 66 deletions index.ts

This file was deleted.

27 changes: 6 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "A simple and configurable Next.js API handler wrapper that avoids pointless boilerplate",
"main": "index.mjs",
"scripts": {
"build": "tsc index.ts",
"build": "tsc",
"test": "tap --ts",
"coverage": "tap --coverage-report=html",
"prepare": "husky install"
},
"keywords": [
Expand Down Expand Up @@ -42,8 +43,5 @@
"tap": "^15.0.10",
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
},
"dependencies": {
"@types/tap": "^15.0.5"
}
}
118 changes: 118 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { table, LB, CR } from './utils/common';
export * from './utils/common';

export type ListItems<T> = Array<T>;
export type ListItem = { text: string; depth?: number };
export type TaskItem = { text: string; checked?: boolean };

export default class Markdown {
LINES: string[];

constructor(title: string) {
this.LINES = [];
if (!title) {
throw new TypeError('Must Provide a title for this markdown');
}
this.header(title, 1);
}

tableOfContent(onTop = true) {
let list = [];
if (!this.LINES.length) return;

this.header('Table of Contents', 2);

for (const line of this.LINES) {
if (line.startsWith('#')) {
const depth = line.split('#').filter((c) => c === '').length - 1;
const text = line.match(/^\#+\s?(.*)$/)?.[1];
if (!text && !depth) continue;
list.push({ text, depth });
}
}

this.list(list);

if (onTop) {
const lng = this.LINES.length;
const toc = this.LINES.splice(lng - 2, 2);
this.LINES.splice(1, 0, ...toc);
}

return this;
}

render() {
return this.LINES.join(CR);
}

addLine(data = LB): this {
this.LINES.push(`${data}`);
return this;
}

paragraph(data: string | number): this {
this.addLine(`${data}`);
return this;
}

header(title: string, n = 1): this {
this.addLine(`${'#'.repeat(n)} ${title}`);
return this;
}

image(filepath: string, altText?: string): this {
const str = `![${altText || filepath}](${filepath})`;
this.addLine(str);
return this;
}

table(columns = [], rows = [], fmtFnc?): this {
this.addLine(table(columns, rows, fmtFnc));
return this;
}

list(items: ListItems<ListItem> = [], numbered = false): this {
if (!Array.isArray(items)) {
throw new TypeError(
'List items should be an array of { text: string, depth: number}'
);
}

let list = [];

for (let i = 0; i < items.length; i++) {
let parsed;
const { text, depth } = items[i];
if (numbered) parsed = `${i + 1}. ${text}`;
else {
parsed = `${!depth ? '' : ' '.repeat(depth)}- ${text}`;
}
list.push(parsed);
}

this.addLine(list.join('\n'));

return this;
}

tasks(items: ListItems<TaskItem> = []): this {
if (!Array.isArray(items)) {
throw new TypeError(
'List items should be an array of { text: string, checked: boolean }'
);
}

let list = [];

for (let i = 0; i < items.length; i++) {
const { text, checked } = items[i];
const parsed = `- [${checked ? 'X' : ' '}] ${text}`;
list.push(parsed);
}

this.addLine(list.join('\n'));

return this;
}
}
23 changes: 15 additions & 8 deletions utils/common.ts → src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export const CR = '\n\n';
export const LB = '\n';

export const table = (
headers: string[] = [],
rows: string[] = [],
fmtFnc = (rowValue: any) => rowValue
) => {
let t = '';
t += `| ${headers.join(' | ')} |\n|${' --- |'.repeat(headers.length)}\n`;
t += `| ${rows.map(fmtFnc).join(' | ')} |\n`;
t += `| ${headers.join(' | ')} |${LB}|${' --- |'.repeat(headers.length)}`;
t += `${LB}| ${rows.map(fmtFnc).join(' | ')} |`;
return t;
};

Expand All @@ -24,17 +27,21 @@ export const bold = (text = '') => {
};

export const link = (text, url) => {
if (!text && !url) return '';
return `[${text}](${url})`;
};

export const quote = (text) => {
return `> ${text}`;
export const inlineCode = (text) => {
if (!text) return '';
return '`' + text + '`';
};

export const code = (text, language = '') => {
return '```' + language + '\n' + text + '\n```';
export const quote = (text) => {
if (!text) return '';
return `${LB}> ${text}`;
};

export const inlineCode = (text) => {
return '`' + text + '`';
export const code = (text, language = '') => {
if (!text) return '';
return CR + '```' + language + LB + text + LB + '```';
};
59 changes: 59 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import tap from 'tap';
import {
table,
link,
code,
inlineCode,
quote,
italic,
bold,
} from '../src/utils/common';

tap.test('italic', (t) => {
t.equal(italic('ok'), '*ok*');
t.equal(italic(null), '');
t.end();
});

tap.test('bold', (t) => {
t.equal(bold('ok'), '**ok**');
t.equal(bold(null), '');
t.end();
});

tap.test('quote', (t) => {
t.equal(quote('ok'), '\n> ok');
t.equal(quote(null), '');
t.end();
});

tap.test('link', (t) => {
const l = 'http://google.com';
const txt = 'link';
t.equal(link(txt, l), `[${txt}](${l})`);
t.equal(link(null, null), '');
t.end();
});

tap.test('code', (t) => {
const codeBlock = "alert('x')";
t.equal(inlineCode(codeBlock), '`' + codeBlock + '`');
t.equal(inlineCode(null), '');
t.equal(code(codeBlock), '\n\n```\n' + codeBlock + '\n```');
t.equal(
code(codeBlock, 'javascript'),
'\n\n```javascript\n' + codeBlock + '\n```'
);
t.equal(code(null), '');
t.end();
});

tap.test('table', (t) => {
const headers = ['id', 'name'];
const rows = ['1', 'Ajeje'];
let out = '| id | name |\n';
out += '| --- | --- |\n';
out += '| 1 | Ajeje |';
t.equal(table(headers, rows), out);
t.end();
});
Loading

0 comments on commit 08b2399

Please sign in to comment.