Skip to content

Commit

Permalink
Migrate to typescript (#6)
Browse files Browse the repository at this point in the history
* Add typescript-related packages
  * "@babel/preset-typescript" and "typescript" to build src
  * "ts-node" to run unit test
* Add types for function parameters
* Add class `Article`, `Board`
* prevent sending 0 length message
* Move .babelrc to babel.config.js
  • Loading branch information
kevinptt0323 authored Sep 20, 2019
1 parent 103ada6 commit 79a85ce
Show file tree
Hide file tree
Showing 29 changed files with 419 additions and 143 deletions.
10 changes: 0 additions & 10 deletions .babelrc

This file was deleted.

18 changes: 18 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

module.exports = function(api) {
api.cache(true);
const config = {
presets: [
"@babel/env",
"@babel/preset-typescript"
],
plugins: [
"@babel/plugin-proposal-export-default-from",
["@babel/plugin-proposal-class-properties", { loose: false }],
"@babel/plugin-proposal-object-rest-spread",
["@babel/plugin-transform-runtime", { regenerator: true }],
]
}
return config;
}
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"version": "0.8.1",
"description": "A Node client for fetching data from ptt.cc.",
"main": "./dist/index.js",
"types": "./dist/index.d.js",
"scripts": {
"test": "mocha ./test/index.js",
"build": "$npm_execpath run build:babel",
"build:babel": "cross-env NODE_ENV=production babel ./src --out-dir ./dist"
"type:check": "tsc",
"type:declaration": "tsc --noEmit false --emitDeclarationOnly --declaration --declarationDir ./dist",
"test": "cross-env TS_NODE_PROJECT=tsconfig.test.json mocha ./test/index.ts",
"build": "$npm_execpath run build:babel && $npm_execpath run type:declaration",
"build:babel": "cross-env NODE_ENV=production babel ./src --out-dir ./dist --extensions '.ts'"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,10 +41,16 @@
"@babel/node": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/plugin-transform-runtime": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"@babel/preset-typescript": "^7.3.3",
"@babel/register": "^7.4.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.1",
"cross-env": "^5.2.0",
"mocha": "^6.1.4"
"mocha": "^6.1.4",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
}
}
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface Config {
[key: string]: any;
};
2 changes: 0 additions & 2 deletions src/index.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as default } from './sites/ptt';
export { default as ptt } from './sites/ptt';
export { default as Config } from './config';
export { default as Socket } from './socket';
47 changes: 47 additions & 0 deletions src/sites/ptt/Article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {substrWidth} from '../../utils/char';

export class Article {
boardname: string;
sn: number;
push: string;
date: string;
timestamp: string;
author: string;
status: string;
title: string;
fixed: boolean;
private _content: string[] = [];

constructor() {
}

static fromLine(line: string): Article {
let article = new Article();
article.sn =+substrWidth('dbcs', line, 1, 7).trim();
article.push = substrWidth('dbcs', line, 9, 2).trim();
article.date = substrWidth('dbcs', line, 11, 5).trim();
article.author = substrWidth('dbcs', line, 17, 12).trim();
article.status = substrWidth('dbcs', line, 30, 2).trim();
article.title = substrWidth('dbcs', line, 32 ).trim();
article.fixed = substrWidth('dbcs', line, 1, 7).trim().includes('★');
return article;
}

get content(): ReadonlyArray<string> {
return this._content;
}
set content(data: ReadonlyArray<string>) {
this._content = data.slice();
}
/**
* @deprecated
*/
get lines(): ReadonlyArray<string> {
return this.content;
}
set lines(data: ReadonlyArray<string>) {
this.content = data;
}
};

export default Article;
45 changes: 45 additions & 0 deletions src/sites/ptt/Board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {substrWidth} from '../../utils/char';

export class Board {
boardname: string;
bn: number;
read: boolean;
category: string;
title: string;
users: string;
admin: string;
folder: boolean = false;
divider: boolean = false;

constructor() {
}

static fromLine(line: string): Board {
let board = new Board();
board.bn =+substrWidth('dbcs', line, 3, 4).trim();
board.read = substrWidth('dbcs', line, 8, 2).trim() === '';
board.boardname = substrWidth('dbcs', line, 10, 12).trim();
board.category = substrWidth('dbcs', line, 23, 4).trim();
switch (board.boardname) {
case 'MyFavFolder':
board.title = substrWidth('dbcs', line, 30);
board.users = '';
board.admin = '';
board.folder = true;
break;
case '------------':
board.title = substrWidth('dbcs', line, 30);
board.users = '';
board.admin = '';
board.divider = true;
break;
default:
board.title = substrWidth('dbcs', line, 30, 31);
board.users = substrWidth('dbcs', line, 62, 5).trim();
board.admin = substrWidth('dbcs', line, 67 ).trim();
break;
}
return board;
}

};
Loading

0 comments on commit 79a85ce

Please sign in to comment.