@sprucelabs/calendar-utils 31.0.113 → 31.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/parsing/AbstractParser.d.ts +13 -0
- package/build/esm/parsing/AbstractParser.js +5 -0
- package/build/esm/parsing/DateParser.d.ts +7 -0
- package/build/esm/parsing/DateParser.js +30 -0
- package/build/esm/parsing/ParserFactory.d.ts +10 -0
- package/build/esm/parsing/ParserFactory.js +32 -0
- package/build/esm/parsing/strategies/DowParser.d.ts +4 -0
- package/build/esm/parsing/strategies/DowParser.js +54 -0
- package/build/esm/parsing/strategies/MonthParser.d.ts +5 -0
- package/build/esm/parsing/strategies/MonthParser.js +86 -0
- package/build/esm/parsing/strategies/TimeParser.d.ts +4 -0
- package/build/esm/parsing/strategies/TimeParser.js +28 -0
- package/build/esm/parsing/strategies/WeekParser.d.ts +4 -0
- package/build/esm/parsing/strategies/WeekParser.js +16 -0
- package/build/esm/parsing/strategies/YearParser.d.ts +4 -0
- package/build/esm/parsing/strategies/YearParser.js +12 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/parsing/AbstractParser.d.ts +13 -0
- package/build/parsing/AbstractParser.js +9 -0
- package/build/parsing/DateParser.d.ts +7 -0
- package/build/parsing/DateParser.js +36 -0
- package/build/parsing/ParserFactory.d.ts +10 -0
- package/build/parsing/ParserFactory.js +38 -0
- package/build/parsing/strategies/DowParser.d.ts +4 -0
- package/build/parsing/strategies/DowParser.js +60 -0
- package/build/parsing/strategies/MonthParser.d.ts +5 -0
- package/build/parsing/strategies/MonthParser.js +89 -0
- package/build/parsing/strategies/TimeParser.d.ts +4 -0
- package/build/parsing/strategies/TimeParser.js +34 -0
- package/build/parsing/strategies/WeekParser.d.ts +4 -0
- package/build/parsing/strategies/WeekParser.js +19 -0
- package/build/parsing/strategies/YearParser.d.ts +4 -0
- package/build/parsing/strategies/YearParser.js +15 -0
- package/package.json +1 -1
package/build/esm/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { default as calendarEventTargetSchema } from './.spruce/schemas/calendar
|
|
|
15
15
|
export { default as eventExclusionDateSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventExclusionDate.schema';
|
|
16
16
|
export { default as eventTimeBlockSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventTimeBlock.schema';
|
|
17
17
|
export { default as calendarEventLineIconSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/calendarEventLineIcon.schema';
|
|
18
|
+
export { default as DateParser } from './parsing/DateParser';
|
|
18
19
|
export * from './dates';
|
|
19
20
|
export { SpruceSchemas } from "./.spruce/schemas/schemas.types";
|
|
20
21
|
export * from './constants';
|
package/build/esm/index.js
CHANGED
|
@@ -15,5 +15,6 @@ export { default as calendarEventTargetSchema } from './.spruce/schemas/calendar
|
|
|
15
15
|
export { default as eventExclusionDateSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventExclusionDate.schema.js';
|
|
16
16
|
export { default as eventTimeBlockSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventTimeBlock.schema.js';
|
|
17
17
|
export { default as calendarEventLineIconSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/calendarEventLineIcon.schema.js';
|
|
18
|
+
export { default as DateParser } from './parsing/DateParser.js';
|
|
18
19
|
export * from './dates.js';
|
|
19
20
|
export * from './constants.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IDate } from '../utilities/date.utility';
|
|
2
|
+
export declare abstract class AbstractParser implements Parser {
|
|
3
|
+
protected now: () => number;
|
|
4
|
+
constructor(now: () => number);
|
|
5
|
+
abstract parse(str: string, date: Required<IDate>, context: ParserContext): string;
|
|
6
|
+
}
|
|
7
|
+
export interface Parser {
|
|
8
|
+
parse(str: string, date: SplitDate, context: ParserContext): string;
|
|
9
|
+
}
|
|
10
|
+
export interface ParserContext {
|
|
11
|
+
hasYear: boolean;
|
|
12
|
+
}
|
|
13
|
+
export type SplitDate = Required<IDate>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { assertOptions } from '@sprucelabs/schema';
|
|
2
|
+
import dateUtil from '../utilities/date.utility.js';
|
|
3
|
+
import ParserFactory from './ParserFactory.js';
|
|
4
|
+
export default class DateParser {
|
|
5
|
+
constructor(now) {
|
|
6
|
+
this.parsers = [];
|
|
7
|
+
this.now = now;
|
|
8
|
+
const parsers = ParserFactory.Factory(now);
|
|
9
|
+
this.parsers = [...parsers.all()];
|
|
10
|
+
}
|
|
11
|
+
static Parser(now) {
|
|
12
|
+
assertOptions({ now }, ['now']);
|
|
13
|
+
return new this(now);
|
|
14
|
+
}
|
|
15
|
+
parse(str) {
|
|
16
|
+
const now = this.now();
|
|
17
|
+
const date = dateUtil.splitDate(now);
|
|
18
|
+
let parsed = str.toLocaleLowerCase().replace(/[^a-z0-9: ]/g, '');
|
|
19
|
+
const context = {
|
|
20
|
+
hasYear: false,
|
|
21
|
+
};
|
|
22
|
+
if (str !== 'now') {
|
|
23
|
+
for (const parser of this.parsers) {
|
|
24
|
+
parsed = parser.parse(parsed, date, context);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
let normalized = dateUtil.date(Object.assign(Object.assign({}, date), { milliseconds: 0, second: 0 }));
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Parser } from './AbstractParser';
|
|
2
|
+
export default class ParserFactory {
|
|
3
|
+
all(): Parser[];
|
|
4
|
+
private now;
|
|
5
|
+
private strategies;
|
|
6
|
+
private constructor();
|
|
7
|
+
static Factory(now: () => number): ParserFactory;
|
|
8
|
+
Parser(parserType: ParserStrategy): Parser;
|
|
9
|
+
}
|
|
10
|
+
export type ParserStrategy = 'Year' | 'Month' | 'Dow' | 'Time' | 'Week';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import DowParser from './strategies/DowParser.js';
|
|
2
|
+
import MonthParser from './strategies/MonthParser.js';
|
|
3
|
+
import TimeParser from './strategies/TimeParser.js';
|
|
4
|
+
import WeekParser from './strategies/WeekParser.js';
|
|
5
|
+
import YearParser from './strategies/YearParser.js';
|
|
6
|
+
export default class ParserFactory {
|
|
7
|
+
all() {
|
|
8
|
+
return [
|
|
9
|
+
this.Parser('Year'),
|
|
10
|
+
this.Parser('Month'),
|
|
11
|
+
this.Parser('Dow'),
|
|
12
|
+
this.Parser('Week'),
|
|
13
|
+
this.Parser('Time'),
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
constructor(now) {
|
|
17
|
+
this.strategies = {
|
|
18
|
+
Year: () => new YearParser(this.now),
|
|
19
|
+
Month: () => new MonthParser(this.now),
|
|
20
|
+
Dow: () => new DowParser(this.now),
|
|
21
|
+
Time: () => new TimeParser(this.now),
|
|
22
|
+
Week: () => new WeekParser(this.now),
|
|
23
|
+
};
|
|
24
|
+
this.now = now;
|
|
25
|
+
}
|
|
26
|
+
static Factory(now) {
|
|
27
|
+
return new this(now);
|
|
28
|
+
}
|
|
29
|
+
Parser(parserType) {
|
|
30
|
+
return this.strategies[parserType]();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import dateUtil from '../../utilities/date.utility.js';
|
|
2
|
+
import { AbstractParser } from '../AbstractParser.js';
|
|
3
|
+
export default class DowParser extends AbstractParser {
|
|
4
|
+
parse(str, date) {
|
|
5
|
+
var _a;
|
|
6
|
+
if (str.includes('tomorrow')) {
|
|
7
|
+
date.day += 1;
|
|
8
|
+
str = str.replace('tomorrow', '');
|
|
9
|
+
}
|
|
10
|
+
const dows = [
|
|
11
|
+
{
|
|
12
|
+
abbr: 'sun',
|
|
13
|
+
full: 'sunday',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
abbr: 'mon',
|
|
17
|
+
full: 'monday',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
abbr: 'tue',
|
|
21
|
+
full: 'tuesday',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
abbr: 'wed',
|
|
25
|
+
full: 'wednesday',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
abbr: 'thu',
|
|
29
|
+
full: 'thursday',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
abbr: 'fri',
|
|
33
|
+
full: 'friday',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
abbr: 'sat',
|
|
37
|
+
full: 'saturday',
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
for (const dow of dows) {
|
|
41
|
+
const re = new RegExp(`(^|\\W)${dow.abbr}(\\W|$)`, 'gi');
|
|
42
|
+
str = str.replace(re, `$1${dow.full}$2`);
|
|
43
|
+
}
|
|
44
|
+
const match = (_a = str.match(/(monday|tuesday|wednesday|thursday|friday|saturday|sunday)/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
45
|
+
if (match) {
|
|
46
|
+
while (dateUtil.format(dateUtil.date(date), 'EEEE').toLocaleLowerCase() !==
|
|
47
|
+
match) {
|
|
48
|
+
date.day += 1;
|
|
49
|
+
str = str.replace(match, '');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return str;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { AbstractParser } from '../AbstractParser.js';
|
|
2
|
+
export default class MonthParser extends AbstractParser {
|
|
3
|
+
parse(str, date) {
|
|
4
|
+
let normalized = str;
|
|
5
|
+
normalized = this.normalizeMonths(normalized);
|
|
6
|
+
const matches = normalized.match(/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) ?(\d+)(?:nd|rd|th|st)?/i);
|
|
7
|
+
if (matches) {
|
|
8
|
+
date.month = [
|
|
9
|
+
'jan',
|
|
10
|
+
'feb',
|
|
11
|
+
'mar',
|
|
12
|
+
'apr',
|
|
13
|
+
'may',
|
|
14
|
+
'jun',
|
|
15
|
+
'jul',
|
|
16
|
+
'aug',
|
|
17
|
+
'sep',
|
|
18
|
+
'oct',
|
|
19
|
+
'nov',
|
|
20
|
+
'dec',
|
|
21
|
+
].indexOf(matches[1]);
|
|
22
|
+
date.day = parseInt(matches[2], 10);
|
|
23
|
+
normalized = normalized.replace(matches[0], '');
|
|
24
|
+
return normalized;
|
|
25
|
+
}
|
|
26
|
+
return str;
|
|
27
|
+
}
|
|
28
|
+
normalizeMonths(normalized) {
|
|
29
|
+
const months = [
|
|
30
|
+
{
|
|
31
|
+
desired: 'jan',
|
|
32
|
+
possible: ['january'],
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
desired: 'feb',
|
|
36
|
+
possible: ['february'],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
desired: 'mar',
|
|
40
|
+
possible: ['march'],
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
desired: 'apr',
|
|
44
|
+
possible: ['april'],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
desired: 'may',
|
|
48
|
+
possible: ['may'],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
desired: 'jun',
|
|
52
|
+
possible: ['june'],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
desired: 'jul',
|
|
56
|
+
possible: ['july'],
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
desired: 'aug',
|
|
60
|
+
possible: ['august'],
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
desired: 'sep',
|
|
64
|
+
possible: ['september'],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
desired: 'oct',
|
|
68
|
+
possible: ['october'],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
desired: 'nov',
|
|
72
|
+
possible: ['november'],
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
desired: 'dec',
|
|
76
|
+
possible: ['december'],
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
for (const month of months) {
|
|
80
|
+
for (const possible of month.possible) {
|
|
81
|
+
normalized = normalized.replace(possible, month.desired);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return normalized;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import dateUtil from '../../utilities/date.utility.js';
|
|
2
|
+
import { AbstractParser } from '../AbstractParser.js';
|
|
3
|
+
export default class TimeParser extends AbstractParser {
|
|
4
|
+
parse(str, date, context) {
|
|
5
|
+
var _a;
|
|
6
|
+
const match = (_a = str.match(/(\d{1,2})(:(\d{2}))? ?(am|pm)?/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
7
|
+
if (!match) {
|
|
8
|
+
return str;
|
|
9
|
+
}
|
|
10
|
+
if (match) {
|
|
11
|
+
date.hour = parseInt(match, 10);
|
|
12
|
+
date.minute = 0;
|
|
13
|
+
}
|
|
14
|
+
if (str.includes(':')) {
|
|
15
|
+
date.minute = parseInt(str.split(':')[1], 10);
|
|
16
|
+
}
|
|
17
|
+
if (str.includes('pm')) {
|
|
18
|
+
date.hour += 12;
|
|
19
|
+
}
|
|
20
|
+
else if (date.hour === 12 && str.includes('am')) {
|
|
21
|
+
date.hour = 0;
|
|
22
|
+
}
|
|
23
|
+
if (!context.hasYear && dateUtil.date(date) < this.now() && str !== 'now') {
|
|
24
|
+
date.hour += 12;
|
|
25
|
+
}
|
|
26
|
+
return str;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AbstractParser } from '../AbstractParser.js';
|
|
2
|
+
export default class WeekParser extends AbstractParser {
|
|
3
|
+
parse(str, date) {
|
|
4
|
+
var _a;
|
|
5
|
+
if (str.includes('next week')) {
|
|
6
|
+
date.day += 7;
|
|
7
|
+
str = str.replace('next week', '');
|
|
8
|
+
}
|
|
9
|
+
const match = (_a = str.match(/(\d{1,2}) ?(weeks?)/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
10
|
+
if (match) {
|
|
11
|
+
date.day += parseInt(match, 10) * 7;
|
|
12
|
+
str = str.replace(match, '');
|
|
13
|
+
}
|
|
14
|
+
return str;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractParser } from '../AbstractParser.js';
|
|
2
|
+
export default class YearParser extends AbstractParser {
|
|
3
|
+
parse(str, date, context) {
|
|
4
|
+
const matches = str.match(/\d{4}/);
|
|
5
|
+
if (matches) {
|
|
6
|
+
date.year = parseInt(matches[0], 10);
|
|
7
|
+
str = str.replace(matches[0], '');
|
|
8
|
+
context.hasYear = true;
|
|
9
|
+
}
|
|
10
|
+
return str;
|
|
11
|
+
}
|
|
12
|
+
}
|
package/build/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { default as calendarEventTargetSchema } from './.spruce/schemas/calendar
|
|
|
15
15
|
export { default as eventExclusionDateSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventExclusionDate.schema';
|
|
16
16
|
export { default as eventTimeBlockSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/eventTimeBlock.schema';
|
|
17
17
|
export { default as calendarEventLineIconSchema } from './.spruce/schemas/calendarUtils/v2021_05_19/calendarEventLineIcon.schema';
|
|
18
|
+
export { default as DateParser } from './parsing/DateParser';
|
|
18
19
|
export * from './dates';
|
|
19
20
|
export { SpruceSchemas } from "./.spruce/schemas/schemas.types";
|
|
20
21
|
export * from './constants';
|
package/build/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.calendarEventLineIconSchema = exports.eventTimeBlockSchema = exports.eventExclusionDateSchema = exports.calendarEventTargetSchema = exports.calendarEventSchema = exports.calculateEventDurationMinutes = exports.calculateEventDurationMillis = exports.TimezoneChoiceSorter = exports.DateUtilDecorator = exports.LocaleImpl = exports.PeopleSorter = exports.durationUtil = exports.dateUtil = exports.calendarUtil = void 0;
|
|
20
|
+
exports.DateParser = exports.calendarEventLineIconSchema = exports.eventTimeBlockSchema = exports.eventExclusionDateSchema = exports.calendarEventTargetSchema = exports.calendarEventSchema = exports.calculateEventDurationMinutes = exports.calculateEventDurationMillis = exports.TimezoneChoiceSorter = exports.DateUtilDecorator = exports.LocaleImpl = exports.PeopleSorter = exports.durationUtil = exports.dateUtil = exports.calendarUtil = void 0;
|
|
21
21
|
var calendar_utility_1 = require("./utilities/calendar.utility");
|
|
22
22
|
Object.defineProperty(exports, "calendarUtil", { enumerable: true, get: function () { return __importDefault(calendar_utility_1).default; } });
|
|
23
23
|
var date_utility_1 = require("./utilities/date.utility");
|
|
@@ -49,5 +49,7 @@ var eventTimeBlock_schema_1 = require("./.spruce/schemas/calendarUtils/v2021_05_
|
|
|
49
49
|
Object.defineProperty(exports, "eventTimeBlockSchema", { enumerable: true, get: function () { return __importDefault(eventTimeBlock_schema_1).default; } });
|
|
50
50
|
var calendarEventLineIcon_schema_1 = require("./.spruce/schemas/calendarUtils/v2021_05_19/calendarEventLineIcon.schema");
|
|
51
51
|
Object.defineProperty(exports, "calendarEventLineIconSchema", { enumerable: true, get: function () { return __importDefault(calendarEventLineIcon_schema_1).default; } });
|
|
52
|
+
var DateParser_1 = require("./parsing/DateParser");
|
|
53
|
+
Object.defineProperty(exports, "DateParser", { enumerable: true, get: function () { return __importDefault(DateParser_1).default; } });
|
|
52
54
|
__exportStar(require("./dates"), exports);
|
|
53
55
|
__exportStar(require("./constants"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IDate } from '../utilities/date.utility';
|
|
2
|
+
export declare abstract class AbstractParser implements Parser {
|
|
3
|
+
protected now: () => number;
|
|
4
|
+
constructor(now: () => number);
|
|
5
|
+
abstract parse(str: string, date: Required<IDate>, context: ParserContext): string;
|
|
6
|
+
}
|
|
7
|
+
export interface Parser {
|
|
8
|
+
parse(str: string, date: SplitDate, context: ParserContext): string;
|
|
9
|
+
}
|
|
10
|
+
export interface ParserContext {
|
|
11
|
+
hasYear: boolean;
|
|
12
|
+
}
|
|
13
|
+
export type SplitDate = Required<IDate>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
7
|
+
const date_utility_1 = __importDefault(require("../utilities/date.utility"));
|
|
8
|
+
const ParserFactory_1 = __importDefault(require("./ParserFactory"));
|
|
9
|
+
class DateParser {
|
|
10
|
+
constructor(now) {
|
|
11
|
+
this.parsers = [];
|
|
12
|
+
this.now = now;
|
|
13
|
+
const parsers = ParserFactory_1.default.Factory(now);
|
|
14
|
+
this.parsers = [...parsers.all()];
|
|
15
|
+
}
|
|
16
|
+
static Parser(now) {
|
|
17
|
+
(0, schema_1.assertOptions)({ now }, ['now']);
|
|
18
|
+
return new this(now);
|
|
19
|
+
}
|
|
20
|
+
parse(str) {
|
|
21
|
+
const now = this.now();
|
|
22
|
+
const date = date_utility_1.default.splitDate(now);
|
|
23
|
+
let parsed = str.toLocaleLowerCase().replace(/[^a-z0-9: ]/g, '');
|
|
24
|
+
const context = {
|
|
25
|
+
hasYear: false,
|
|
26
|
+
};
|
|
27
|
+
if (str !== 'now') {
|
|
28
|
+
for (const parser of this.parsers) {
|
|
29
|
+
parsed = parser.parse(parsed, date, context);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let normalized = date_utility_1.default.date(Object.assign(Object.assign({}, date), { milliseconds: 0, second: 0 }));
|
|
33
|
+
return normalized;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.default = DateParser;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Parser } from './AbstractParser';
|
|
2
|
+
export default class ParserFactory {
|
|
3
|
+
all(): Parser[];
|
|
4
|
+
private now;
|
|
5
|
+
private strategies;
|
|
6
|
+
private constructor();
|
|
7
|
+
static Factory(now: () => number): ParserFactory;
|
|
8
|
+
Parser(parserType: ParserStrategy): Parser;
|
|
9
|
+
}
|
|
10
|
+
export type ParserStrategy = 'Year' | 'Month' | 'Dow' | 'Time' | 'Week';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const DowParser_1 = __importDefault(require("./strategies/DowParser"));
|
|
7
|
+
const MonthParser_1 = __importDefault(require("./strategies/MonthParser"));
|
|
8
|
+
const TimeParser_1 = __importDefault(require("./strategies/TimeParser"));
|
|
9
|
+
const WeekParser_1 = __importDefault(require("./strategies/WeekParser"));
|
|
10
|
+
const YearParser_1 = __importDefault(require("./strategies/YearParser"));
|
|
11
|
+
class ParserFactory {
|
|
12
|
+
all() {
|
|
13
|
+
return [
|
|
14
|
+
this.Parser('Year'),
|
|
15
|
+
this.Parser('Month'),
|
|
16
|
+
this.Parser('Dow'),
|
|
17
|
+
this.Parser('Week'),
|
|
18
|
+
this.Parser('Time'),
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
constructor(now) {
|
|
22
|
+
this.strategies = {
|
|
23
|
+
Year: () => new YearParser_1.default(this.now),
|
|
24
|
+
Month: () => new MonthParser_1.default(this.now),
|
|
25
|
+
Dow: () => new DowParser_1.default(this.now),
|
|
26
|
+
Time: () => new TimeParser_1.default(this.now),
|
|
27
|
+
Week: () => new WeekParser_1.default(this.now),
|
|
28
|
+
};
|
|
29
|
+
this.now = now;
|
|
30
|
+
}
|
|
31
|
+
static Factory(now) {
|
|
32
|
+
return new this(now);
|
|
33
|
+
}
|
|
34
|
+
Parser(parserType) {
|
|
35
|
+
return this.strategies[parserType]();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.default = ParserFactory;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const date_utility_1 = __importDefault(require("../../utilities/date.utility"));
|
|
7
|
+
const AbstractParser_1 = require("../AbstractParser");
|
|
8
|
+
class DowParser extends AbstractParser_1.AbstractParser {
|
|
9
|
+
parse(str, date) {
|
|
10
|
+
var _a;
|
|
11
|
+
if (str.includes('tomorrow')) {
|
|
12
|
+
date.day += 1;
|
|
13
|
+
str = str.replace('tomorrow', '');
|
|
14
|
+
}
|
|
15
|
+
const dows = [
|
|
16
|
+
{
|
|
17
|
+
abbr: 'sun',
|
|
18
|
+
full: 'sunday',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
abbr: 'mon',
|
|
22
|
+
full: 'monday',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
abbr: 'tue',
|
|
26
|
+
full: 'tuesday',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
abbr: 'wed',
|
|
30
|
+
full: 'wednesday',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
abbr: 'thu',
|
|
34
|
+
full: 'thursday',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
abbr: 'fri',
|
|
38
|
+
full: 'friday',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
abbr: 'sat',
|
|
42
|
+
full: 'saturday',
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
for (const dow of dows) {
|
|
46
|
+
const re = new RegExp(`(^|\\W)${dow.abbr}(\\W|$)`, 'gi');
|
|
47
|
+
str = str.replace(re, `$1${dow.full}$2`);
|
|
48
|
+
}
|
|
49
|
+
const match = (_a = str.match(/(monday|tuesday|wednesday|thursday|friday|saturday|sunday)/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
50
|
+
if (match) {
|
|
51
|
+
while (date_utility_1.default.format(date_utility_1.default.date(date), 'EEEE').toLocaleLowerCase() !==
|
|
52
|
+
match) {
|
|
53
|
+
date.day += 1;
|
|
54
|
+
str = str.replace(match, '');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return str;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.default = DowParser;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AbstractParser_1 = require("../AbstractParser");
|
|
4
|
+
class MonthParser extends AbstractParser_1.AbstractParser {
|
|
5
|
+
parse(str, date) {
|
|
6
|
+
let normalized = str;
|
|
7
|
+
normalized = this.normalizeMonths(normalized);
|
|
8
|
+
const matches = normalized.match(/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) ?(\d+)(?:nd|rd|th|st)?/i);
|
|
9
|
+
if (matches) {
|
|
10
|
+
date.month = [
|
|
11
|
+
'jan',
|
|
12
|
+
'feb',
|
|
13
|
+
'mar',
|
|
14
|
+
'apr',
|
|
15
|
+
'may',
|
|
16
|
+
'jun',
|
|
17
|
+
'jul',
|
|
18
|
+
'aug',
|
|
19
|
+
'sep',
|
|
20
|
+
'oct',
|
|
21
|
+
'nov',
|
|
22
|
+
'dec',
|
|
23
|
+
].indexOf(matches[1]);
|
|
24
|
+
date.day = parseInt(matches[2], 10);
|
|
25
|
+
normalized = normalized.replace(matches[0], '');
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
|
28
|
+
return str;
|
|
29
|
+
}
|
|
30
|
+
normalizeMonths(normalized) {
|
|
31
|
+
const months = [
|
|
32
|
+
{
|
|
33
|
+
desired: 'jan',
|
|
34
|
+
possible: ['january'],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
desired: 'feb',
|
|
38
|
+
possible: ['february'],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
desired: 'mar',
|
|
42
|
+
possible: ['march'],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
desired: 'apr',
|
|
46
|
+
possible: ['april'],
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
desired: 'may',
|
|
50
|
+
possible: ['may'],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
desired: 'jun',
|
|
54
|
+
possible: ['june'],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
desired: 'jul',
|
|
58
|
+
possible: ['july'],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
desired: 'aug',
|
|
62
|
+
possible: ['august'],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
desired: 'sep',
|
|
66
|
+
possible: ['september'],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
desired: 'oct',
|
|
70
|
+
possible: ['october'],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
desired: 'nov',
|
|
74
|
+
possible: ['november'],
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
desired: 'dec',
|
|
78
|
+
possible: ['december'],
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
for (const month of months) {
|
|
82
|
+
for (const possible of month.possible) {
|
|
83
|
+
normalized = normalized.replace(possible, month.desired);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return normalized;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.default = MonthParser;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const date_utility_1 = __importDefault(require("../../utilities/date.utility"));
|
|
7
|
+
const AbstractParser_1 = require("../AbstractParser");
|
|
8
|
+
class TimeParser extends AbstractParser_1.AbstractParser {
|
|
9
|
+
parse(str, date, context) {
|
|
10
|
+
var _a;
|
|
11
|
+
const match = (_a = str.match(/(\d{1,2})(:(\d{2}))? ?(am|pm)?/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
12
|
+
if (!match) {
|
|
13
|
+
return str;
|
|
14
|
+
}
|
|
15
|
+
if (match) {
|
|
16
|
+
date.hour = parseInt(match, 10);
|
|
17
|
+
date.minute = 0;
|
|
18
|
+
}
|
|
19
|
+
if (str.includes(':')) {
|
|
20
|
+
date.minute = parseInt(str.split(':')[1], 10);
|
|
21
|
+
}
|
|
22
|
+
if (str.includes('pm')) {
|
|
23
|
+
date.hour += 12;
|
|
24
|
+
}
|
|
25
|
+
else if (date.hour === 12 && str.includes('am')) {
|
|
26
|
+
date.hour = 0;
|
|
27
|
+
}
|
|
28
|
+
if (!context.hasYear && date_utility_1.default.date(date) < this.now() && str !== 'now') {
|
|
29
|
+
date.hour += 12;
|
|
30
|
+
}
|
|
31
|
+
return str;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.default = TimeParser;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AbstractParser_1 = require("../AbstractParser");
|
|
4
|
+
class WeekParser extends AbstractParser_1.AbstractParser {
|
|
5
|
+
parse(str, date) {
|
|
6
|
+
var _a;
|
|
7
|
+
if (str.includes('next week')) {
|
|
8
|
+
date.day += 7;
|
|
9
|
+
str = str.replace('next week', '');
|
|
10
|
+
}
|
|
11
|
+
const match = (_a = str.match(/(\d{1,2}) ?(weeks?)/)) === null || _a === void 0 ? void 0 : _a[0];
|
|
12
|
+
if (match) {
|
|
13
|
+
date.day += parseInt(match, 10) * 7;
|
|
14
|
+
str = str.replace(match, '');
|
|
15
|
+
}
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.default = WeekParser;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AbstractParser_1 = require("../AbstractParser");
|
|
4
|
+
class YearParser extends AbstractParser_1.AbstractParser {
|
|
5
|
+
parse(str, date, context) {
|
|
6
|
+
const matches = str.match(/\d{4}/);
|
|
7
|
+
if (matches) {
|
|
8
|
+
date.year = parseInt(matches[0], 10);
|
|
9
|
+
str = str.replace(matches[0], '');
|
|
10
|
+
context.hasYear = true;
|
|
11
|
+
}
|
|
12
|
+
return str;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.default = YearParser;
|