Skip to content

Commit 11aecd2

Browse files
committed
update README.md
1 parent d88151f commit 11aecd2

14 files changed

+1302
-42
lines changed

README.md

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
1-
# ts-dev-template
2-
ts 开发基础配置环境
3-
## Notice
4-
这是一个模板仓库,可用于初始化Ts开发时的一些推荐配置
5-
## Usage
6-
1.调用模板创建仓库
1+
# text-parsing
2+
可扩展的nodejs 文本解析库
3+
## 安装
74
```shell
8-
点击仓库首页右边绿色按钮(Use This Template)或访问https://github.com/lc-cn/ts-dev-template/generate,根据github提示创建仓库
5+
npm install text-parsing
6+
# or
7+
yarn add text-parsing
98
```
10-
2.拉取你刚刚创建的仓库(`<>`表示括号内描述的内容为必填)
11-
```shell
12-
git clone <你刚刚创建的仓库地址>
13-
```
14-
2.安装依赖
15-
```shell
16-
npm install
17-
# 或 npm i
9+
## 使用样例
10+
### 1. 直接使用解析器
11+
```javascript
12+
//假设今日为2022-01-01
13+
const {TimeParser} = require('text-parsing')
14+
const tp=new TimeParser()
15+
console.log(tp.parse('明天晚上8点半开会',{format:'YYYY-MM-DD hh:mm:ss'}))
16+
//打印结果:2022-01-02 08:30:00
1817
```
19-
3.运行开发环境(ts-node-dev运行ts代码)
20-
```shell
21-
npm run dev
18+
### 2. 使用ParserManger同时使用多个解析器
19+
```javascript
20+
const {ParserManger}=require('text-parsing')
21+
const pm=new ParserManger()
22+
pm.use('timeParser',{format:'YYYY-MM-DD',timeBase:new Date('2022-01-01')})
23+
console.log(pm.parse('下周二一起聚餐'))
24+
//打印结果:{ timeParser:'2022-01-04' },如果有其他解析器,会将结果一同返回
2225
```
23-
4.将ts编译成js(编译代码成普通node可执行的js代码)
24-
```shell
25-
npm run build
26-
```
27-
5.执行编译后的js(通常生产环境调用)
28-
```shell
29-
npm start
30-
# 或 npm run start
26+
### 3.定义自己的解析器
27+
你可以通过继承BaseParser实现任意你想实现的解析器
28+
29+
```typescript
30+
// cardParser.ts
31+
import {BaseParser} from 'text-parsing'
32+
33+
class CardParser extends BaseParser<string> {
34+
constructor() {
35+
super('cardParser')
36+
}
37+
// 定义文本接收函数
38+
parse(expression: string,options?:{location:string}) {
39+
// do sth
40+
return expression.replace(expression,/!([\d]{17}[0-9|x|X]{1})/)//此处的返回值类型需在BaseParser的泛型中声明,否则在ts中将无法获得正确代码提示
41+
}
42+
}
43+
44+
// index.ts
45+
import {ParserManager} from "text-parsing";
46+
import {CardParser} from './cardParser'
47+
const pm=new ParserManager()
48+
pm.use(new ParserManager(),{location:'china'})//use的第二个参数将传递给parser的parse函数的option
49+
console.log(pm.parse('515323194803141144'))
3150
```

package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
2-
"name": "@lc-cn/ts-dev-template",
2+
"name": "text-parsing",
33
"version": "1.0.0",
4-
"description": "ts开发环境配置",
4+
"description": "可扩展的nodejs 文本解析库",
55
"main": "lib/index.js",
66
"scripts": {
7-
"start": "node .",
7+
"start": "node ./lib/test.js",
88
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
9-
"dev": "ts-node-dev -r tsconfig-paths/register ./src/index.ts",
9+
"dev": "ts-node-dev -r tsconfig-paths/register ./src/test.ts",
1010
"pub": "npm publish --access public",
1111
"test": "echo \"Error: no test specified\" && exit 1"
1212
},
1313
"repository": {
1414
"type": "git",
15-
"url": "git+https://github.com/lc-cn/ts-dev-template.git"
15+
"url": "git+https://github.com/liucl-cn/text-parsing.git"
1616
},
1717
"keywords": [
1818
"ts",
19-
"dev",
20-
"template"
19+
"text",
20+
"parsing"
2121
],
2222
"author": "凉菜",
2323
"license": "MIT",
2424
"bugs": {
25-
"url": "https://github.com/lc-cn/ts-dev-template/issues"
25+
"url": "https://github.com/liucl-cn/text-parsing/issues"
2626
},
27-
"homepage": "https://github.com/lc-cn/ts-dev-template#readme",
27+
"homepage": "https://github.com/liucl-cn/text-parsing#readme",
2828
"devDependencies": {
2929
"@types/node": "latest",
3030
"ts-node-dev": "latest",

src/hello.ts

-3
This file was deleted.

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
import {sayHello} from "@/hello";
2-
console.log(sayHello('world'))
1+
import {ParserManager} from "./parser";
2+
export * from './parser'
3+
export * from './module'
4+
export default ParserManager

src/module/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as TimeParser} from './timeParser'

src/module/timeParser/index.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {preHander} from './strPreHanding';
2+
import {TimeUnit} from './timeUnit';
3+
import {BaseParser} from "@/parser";
4+
export interface TimeOptions{
5+
timeBase?:Date;
6+
format?:string;
7+
}
8+
export default class TimeParser extends BaseParser<string|Date>{
9+
public timeBase:Date
10+
public expression:string
11+
public isPreferFuture:boolean
12+
constructor() {
13+
super('timeParser');
14+
this.timeBase = null;
15+
this.expression = '';
16+
this.isPreferFuture = true;
17+
}
18+
turnOffPreferFuture(){
19+
this.isPreferFuture=false
20+
}
21+
getTimeBase(){
22+
return this.timeBase
23+
}
24+
setTimeBase(s:Date){
25+
this.timeBase=s
26+
}
27+
parse(expression:string,options?:TimeOptions):Date|string{
28+
let format:string,timeBase:Date;
29+
if(options){
30+
timeBase = options.timeBase;
31+
format = options.format;
32+
}
33+
this.expression = expression;
34+
const exp = this._preHandling(expression);
35+
if (timeBase) {
36+
if (typeof timeBase === 'string') {
37+
this.timeBase = new Date(timeBase);
38+
} else {
39+
this.timeBase = timeBase;
40+
}
41+
} else {
42+
this.timeBase = new Date();
43+
}
44+
let tu = new TimeUnit(exp, this.isPreferFuture, this.timeBase);
45+
return tu.timeNormalization(format);
46+
}
47+
private _preHandling(expression:string){
48+
expression = preHander.delKeyword(expression, '\\s+'); // 清理空白符
49+
expression = preHander.delKeyword(expression, '[的]+'); // 清理语气助词
50+
expression = preHander.DBC2CDB(expression); // 全角转半角
51+
expression = preHander.numberTranslator(expression); // 大写数字转化
52+
return expression;
53+
}
54+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as util from '@/utils'
2+
const zhNumChar={
3+
: 0,
4+
: 1,
5+
: 2,
6+
: 2,
7+
: 3,
8+
: 4,
9+
: 5,
10+
: 6,
11+
: 7,
12+
: 8,
13+
: 9
14+
}
15+
const zhNumValue={
16+
: { value: 10, secUnit: false },
17+
: { value: 100, secUnit: false },
18+
: { value: 1000, secUnit: false },
19+
: { value: 10000, secUnit: true },
20+
亿: { value: 100000000, secUnit: true }
21+
}
22+
export const preHander={
23+
delKeyword(target, rules) {
24+
var r = new RegExp(rules, 'g');
25+
return target.replace(r, '');
26+
},
27+
28+
numberTranslator(target) {
29+
var tmp = util.reverseStr(target);
30+
var rule = new RegExp('[末天日](?=(周|期星))', 'g');
31+
tmp = tmp.replace(rule, '7');
32+
target = util.reverseStr(tmp);
33+
34+
var section = 0;
35+
var number = 0;
36+
var rtn = 0;
37+
var secUnit = false;
38+
var str = target.split('');
39+
var result = '';
40+
var flag = false;
41+
for (var i = 0; i < str.length; i++) {
42+
if (zhNumChar.hasOwnProperty(str[i]) || zhNumValue.hasOwnProperty(str[i])) {
43+
flag = true;
44+
if (zhNumChar.hasOwnProperty(str[i])) {
45+
number = zhNumChar[str[i]];
46+
} else {
47+
var unit = zhNumValue[str[i]].value;
48+
secUnit = zhNumValue[str[i]].secUnit;
49+
if (secUnit) {
50+
section = (section + number) * unit;
51+
rtn += section;
52+
section = 0;
53+
} else {
54+
section += number * unit;
55+
}
56+
number = 0;
57+
}
58+
} else {
59+
if (flag) {
60+
result += (rtn + section + number).toString();
61+
flag = false;
62+
number = 0;
63+
section = 0;
64+
rtn = 0;
65+
secUnit = false;
66+
}
67+
result += str[i];
68+
}
69+
}
70+
if (flag) {
71+
result += (rtn + section + number).toString();
72+
}
73+
return result;
74+
},
75+
76+
DBC2CDB(target) {
77+
var tmp = '';
78+
for (var i = 0; i < target.length; i++) {
79+
if (target.charCodeAt(i) > 65248 && target.charCodeAt(i) < 65375) {
80+
tmp += String.fromCharCode(target.charCodeAt(i) - 65248);
81+
} else {
82+
tmp += String.fromCharCode(target.charCodeAt(i));
83+
}
84+
}
85+
return tmp;
86+
}
87+
}

src/module/timeParser/timePoint.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class TimePoint{
2+
public tunit:number[]
3+
constructor(date?:string|number|Date) {
4+
if (date) {
5+
var d = new Date(date);
6+
this.tunit = [d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()];
7+
} else {
8+
this.tunit = [-1, -1, -1, -1, -1, -1];
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)