Skip to content

Commit a87308f

Browse files
committed
Refactor typescript interfaces and publishing files
1 parent 2e5e9ca commit a87308f

49 files changed

Lines changed: 184 additions & 3529 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Node Modules
2+
dist/
23
node_modules/*
34
package-lock.json
45

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Chrono’s extraction pipeline configuration consists of `parsers: Parser[]` and
172172
interface Parser {
173173
pattern: (context: ParsingContext) => RegExp,
174174
extract: (context: ParsingContext, match: RegExpMatchArray) =>
175-
(ParsingComponents | ParsingResult | {[c: Component]: string|number} | null)
175+
(ParsingComponents | ParsingResult | {[c in Component]?: number} | null)
176176
}
177177
```
178178

dist/chrono.js

Lines changed: 0 additions & 3299 deletions
This file was deleted.

dist/chrono.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/chrono.min.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

dist/chrono.min.js.LICENSE.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

dist/chrono.min.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,28 @@
88
},
99
"jest": {
1010
"verbose": true,
11-
"testURL": "http://localhost/"
11+
"testURL": "http://localhost/",
12+
"preset": "ts-jest"
1213
},
1314
"license": "MIT",
1415
"version": "2.0.3",
1516
"directories": {
1617
"source": "./src",
1718
"test": "./test"
1819
},
19-
"main": "./dist/chrono.js",
20-
"babel": {
21-
"presets": [
22-
"@babel/preset-env",
23-
"@babel/preset-typescript"
24-
],
25-
"plugins": [
26-
"@babel/plugin-proposal-export-namespace-from"
27-
]
28-
},
29-
"eslintConfig": {
30-
"ignorePatterns": [
31-
"webpack.config.js",
32-
"dist/*.js"
33-
],
34-
"parser": "@typescript-eslint/parser",
35-
"plugins": [
36-
"@typescript-eslint",
37-
"jest"
38-
],
39-
"extends": [
40-
"eslint:recommended",
41-
"plugin:@typescript-eslint/recommended",
42-
"plugin:jest/recommended"
43-
],
44-
"rules": {
45-
"@typescript-eslint/explicit-module-boundary-types": "off",
46-
"jest/expect-expect": "off"
47-
}
48-
},
20+
"main": "dist/index.js",
21+
"types": "dist/index.d.ts",
4922
"devDependencies": {
50-
"@babel/core": "^7.10.4",
51-
"@babel/plugin-proposal-export-namespace-from": "^7.10.4",
52-
"@babel/preset-env": "^7.10.4",
53-
"@babel/preset-typescript": "^7.10.4",
54-
"@typescript-eslint/eslint-plugin": "^3.5.0",
55-
"@typescript-eslint/parser": "^3.5.0",
56-
"babel-jest": "^25.5.1",
57-
"babel-loader": "^8.0.6",
58-
"coveralls": "^3.1.0",
59-
"eslint": "^7.4.0",
60-
"eslint-plugin-jest": "^23.18.0",
61-
"jest": "^25.5.4",
62-
"terser-webpack-plugin": "^2.3.7",
63-
"typescript": "^3.9.6",
64-
"webpack": "^4.43.0",
65-
"webpack-cli": "^3.3.12"
23+
"@types/jest": "^26.0.4",
24+
"jest": "^26.1.0",
25+
"ts-jest": "^26.1.1",
26+
"typescript": "^3.9.6"
6627
},
6728
"scripts": {
68-
"build": "webpack",
29+
"build": "tsc -p tsconfig.build.json",
6930
"watch": "jest --watch",
70-
"pretest": "eslint .",
7131
"test": "jest --coverage",
72-
"coveralls": "npm run test && cat coverage/lcov.info | coveralls",
73-
"version": "npm run build && git add -A dist"
32+
"coveralls": "npm run test && cat coverage/lcov.info | coveralls"
7433
},
7534
"dependencies": {
7635
"dayjs": "^1.8.29"

src/chrono.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export interface Configuration {
99
}
1010

1111
export interface Parser {
12-
pattern: (context: ParsingContext) => RegExp,
13-
extract: (context: ParsingContext, match: RegExpMatchArray) =>
14-
(ParsingComponents | ParsingResult | {[c: Component]: string|number} | null)
12+
pattern(context: ParsingContext): RegExp,
13+
extract(context: ParsingContext, match: RegExpMatchArray):
14+
(ParsingComponents | ParsingResult | {[c in Component]?: number} | null)
1515
}
1616

1717
export interface Refiner {
@@ -28,7 +28,7 @@ export class Chrono {
2828
this.refiners = [...configuration.refiners];
2929
}
3030

31-
parseDate(text, refDate, opt): Date {
31+
parseDate(text, refDate?, opt?): Date {
3232
const results = this.parse(text, refDate, opt);
3333
return (results.length > 0) ? results[0].start.date() : null;
3434
}
@@ -113,14 +113,14 @@ export class ParsingContext implements DebugHandler {
113113
readonly option: ParsingOption
114114
) {}
115115

116-
createParsingComponents(components?: {[c: Component]: string|number}) : ParsingComponents {
116+
createParsingComponents(components?: {[c in Component]?: number}) : ParsingComponents {
117117
return new ParsingComponents(this.refDate, components)
118118
}
119119

120120
createParsingResult(
121121
index: number, textOrEndIndex: number | string,
122-
startComponents?: {[c: Component]: string|number},
123-
endComponents?: {[c: Component]: string|number}
122+
startComponents?: {[c in Component]?: number},
123+
endComponents?: {[c in Component]?: number}
124124
) : ParsingResult {
125125

126126
const text = (typeof textOrEndIndex === 'string') ? textOrEndIndex :
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Parser, ParsingContext} from "../../chrono";
2+
import {ParsingComponents, ParsingResult} from "../../results";
3+
import {Component} from "../../index";
4+
5+
export abstract class AbstractParserWithWordBoundaryChecking implements Parser {
6+
7+
abstract innerPattern(context: ParsingContext): RegExp;
8+
abstract innerExtract(context: ParsingContext, match: RegExpMatchArray):
9+
(ParsingComponents | ParsingResult | {[c in Component]?: number} | null)
10+
11+
pattern(context: ParsingContext): RegExp {
12+
const innerPattern = this.innerPattern(context);
13+
return new RegExp(`(\\W|^)${innerPattern.source}`, innerPattern.flags);
14+
}
15+
16+
extract(context: ParsingContext, match: RegExpMatchArray) {
17+
const header = match[1]
18+
match.index = match.index + header.length
19+
match[0] = match[0].substring(header.length)
20+
for (let i=2; i<match.length; i++) {
21+
match[i-1] = match[i]
22+
}
23+
24+
return this.innerExtract(context, match)
25+
}
26+
}
27+

0 commit comments

Comments
 (0)