Skip to content

Commit dc21863

Browse files
committed
removed dependency to Babel
1 parent b1403cd commit dc21863

File tree

6 files changed

+85
-292
lines changed

6 files changed

+85
-292
lines changed

.babelrc

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

lib/parse-date-intervals.js

Lines changed: 81 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,100 @@
1-
"use strict";
1+
'use strict';
22

3-
var _typeof2 = require("babel-runtime/helpers/typeof");
3+
const ParseDates = require("@datagica/parse-dates").ParseDates;
4+
const XRegExp = require("xregexp").XRegExp;
45

5-
var _typeof3 = _interopRequireDefault(_typeof2);
6+
class ParseDateIntervals extends ParseDates {
7+
constructor(opts) {
68

7-
var _promise = require("babel-runtime/core-js/promise");
8-
9-
var _promise2 = _interopRequireDefault(_promise);
10-
11-
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
12-
13-
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
14-
15-
var _createClass2 = require("babel-runtime/helpers/createClass");
16-
17-
var _createClass3 = _interopRequireDefault(_createClass2);
18-
19-
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
20-
21-
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
22-
23-
var _inherits2 = require("babel-runtime/helpers/inherits");
24-
25-
var _inherits3 = _interopRequireDefault(_inherits2);
26-
27-
var _parseDates = require("@datagica/parse-dates");
28-
29-
var _xregexp = require("xregexp");
30-
31-
var _xregexp2 = _interopRequireDefault(_xregexp);
32-
33-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34-
35-
var ParseDateIntervals = function (_ParseDates) {
36-
(0, _inherits3.default)(ParseDateIntervals, _ParseDates);
37-
38-
function ParseDateIntervals() {
39-
var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
40-
(0, _classCallCheck3.default)(this, ParseDateIntervals);
41-
42-
var _this = (0, _possibleConstructorReturn3.default)(this, Object.getPrototypeOf(ParseDateIntervals).call(this, opts));
9+
if (typeof opts === 'undefined') {
10+
opts = {};
11+
}
4312

44-
_this.intervalPrefix = "(?:(?:de|from)?\\s+)?";
45-
_this.intervalSeparator = "\\s*(?:-|,|until|au|to|à)\\s*";
46-
_this.dateIntervalStrPatterns = [];
47-
for (var i = 1; i < 10; i++) {
48-
if (typeof _this["datePattern" + i] === "function") {
49-
_this.dateIntervalStrPatterns.push("" + _this.intervalPrefix + _this['datePattern' + i]('from') + ("" + _this.intervalSeparator + _this['datePattern' + i]('to')));
13+
super(opts);
14+
15+
this.intervalPrefix = `(?:(?:de|from)?\\s+)?`;
16+
this.intervalSeparator = `\\s*(?:-|,|until|au|to|à)\\s*`;
17+
this.dateIntervalStrPatterns = [];
18+
for (let i = 1; i < 10; i++) {
19+
if (typeof this[`datePattern${i}`] === "function") {
20+
this.dateIntervalStrPatterns.push(
21+
`${this.intervalPrefix}${this['datePattern'+i]('from')}` +
22+
`${this.intervalSeparator}${this['datePattern'+i]('to')}`
23+
)
5024
}
5125
}
5226

53-
_this.dateIntervalsPatterns = new _xregexp2.default(_this.dateIntervalStrPatterns.join('|'), 'i');
54-
return _this;
27+
this.dateIntervalsPatterns = new XRegExp(this.dateIntervalStrPatterns.join('|'), 'i')
5528
}
5629

57-
(0, _createClass3.default)(ParseDateIntervals, [{
58-
key: "matchToDateInterval",
59-
value: function matchToDateInterval(match) {
60-
61-
for (var i = 1; i < 10; i++) {
62-
if (match["year" + i + "from"] && match["month" + i + "from"] && match["year" + i + "from"] && match["month" + i + "to"]) {
63-
var dateFrom = new Date(this.matchToYears(match, i, 'from'), this.matchToMonths(match, i, 'from') - 1, this.matchToDays(match, i, 'from'));
64-
var dateTo = new Date(this.matchToYears(match, i, 'to'), this.matchToMonths(match, i, 'to') - 1, this.matchToDays(match, i, 'to'));
65-
return {
66-
from: {
67-
str: dateFrom.toString(),
68-
timestamp: +dateFrom,
69-
month: dateFrom.getMonth() + 1,
70-
date: dateFrom.getDate(),
71-
year: dateFrom.getFullYear()
72-
},
73-
to: {
74-
str: dateTo.toString(),
75-
timestamp: +dateTo,
76-
month: dateTo.getMonth() + 1,
77-
date: dateTo.getDate(),
78-
year: dateTo.getFullYear()
79-
}
80-
};
30+
matchToDateInterval(match) {
31+
32+
for (let i = 1; i < 10; i++) {
33+
if (match[`year${i}from`] && match[`month${i}from`] &&
34+
match[`year${i}from`] && match[`month${i}to`]) {
35+
const dateFrom = new Date(
36+
this.matchToYears(match, i, 'from'),
37+
this.matchToMonths(match, i, 'from') - 1,
38+
this.matchToDays(match, i, 'from')
39+
);
40+
const dateTo = new Date(
41+
this.matchToYears(match, i, 'to'),
42+
this.matchToMonths(match, i, 'to') - 1,
43+
this.matchToDays(match, i, 'to')
44+
);
45+
return {
46+
from: {
47+
str: dateFrom.toString(),
48+
timestamp: +dateFrom,
49+
month: dateFrom.getMonth() + 1,
50+
date: dateFrom.getDate(),
51+
year: dateFrom.getFullYear()
52+
},
53+
to: {
54+
str: dateTo.toString(),
55+
timestamp: +dateTo,
56+
month: dateTo.getMonth() + 1,
57+
date: dateTo.getDate(),
58+
year: dateTo.getFullYear()
59+
}
8160
}
8261
}
83-
throw new Error("couldn't find patterns");
8462
}
85-
}, {
86-
key: "parseDateIntervals",
87-
value: function parseDateIntervals(input) {
88-
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
63+
throw new Error("couldn't find patterns");
64+
}
65+
parseDateIntervals(input, opts) {
66+
67+
if (typeof opts === 'undefined') {
68+
opts = {};
69+
}
8970

90-
var text = "";
91-
if (typeof input === 'string') {
92-
text = input;
93-
} else if (typeof input.text === 'string') {
94-
text = input.text;
95-
} else {
96-
return _promise2.default.reject(new Error("input is not text but " + (typeof input === "undefined" ? "undefined" : (0, _typeof3.default)(input))));
97-
}
98-
var match = _xregexp2.default.exec(text, this.dateIntervalsPatterns);
71+
let text = "";
72+
if (typeof input === 'string') {
73+
text = input
74+
} else if (typeof input.text === 'string') {
75+
text = input.text
76+
} else {
77+
return Promise.reject(new Error(`input is not text but ${typeof input}`))
78+
}
79+
const match = XRegExp.exec(text, this.dateIntervalsPatterns);
9980

100-
//console.log(JSON.stringify(match));
81+
//console.log(JSON.stringify(match));
10182

102-
try {
103-
return _promise2.default.resolve(this.matchToDateInterval(match));
104-
} catch (err) {
105-
console.log("error: " + err);
106-
return _promise2.default.resolve(null);
107-
}
83+
try {
84+
return Promise.resolve(this.matchToDateInterval(match));
85+
} catch (err) {
86+
console.log("error: " + err);
87+
return Promise.resolve(null);
10888
}
109-
}]);
110-
return ParseDateIntervals;
111-
}(_parseDates.ParseDates);
89+
}
90+
}
11291

113-
var singletonInstance = new ParseDateIntervals();
114-
var singletonMethod = function singletonMethod() {
92+
const singletonInstance = new ParseDateIntervals()
93+
const singletonMethod = function() {
11594
return singletonInstance.parseDateIntervals.apply(singletonInstance, arguments);
116-
};
95+
}
11796

118-
module.exports = singletonMethod;
119-
module.exports.default = singletonMethod;
120-
module.exports.parseDateIntervals = singletonInstance;
121-
module.exports.ParseDateIntervals = ParseDateIntervals;
97+
module.exports = singletonMethod
98+
module.exports.default = singletonMethod
99+
module.exports.parseDateIntervals = singletonInstance
100+
module.exports.ParseDateIntervals = ParseDateIntervals

package.json

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"name": "@datagica/parse-date-intervals",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"description": "Date interval parser. Can detect date intervals in a document. well, most of the time.",
55
"main": "./lib/parse-date-intervals.js",
66
"scripts": {
7-
"build": "node_modules/.bin/babel src/ --out-dir lib/ && node_modules/.bin/babel tests/src/ --out-dir tests/dist/",
8-
"test": "node_modules/.bin/mocha --bail --require chai tests/dist/*.js"
7+
"test": "node_modules/.bin/mocha --harmony --bail --require chai tests/*.js"
98
},
109
"author": "Julian Bilcke <[email protected]>",
1110
"license": "GPL-3.0",
@@ -17,20 +16,10 @@
1716
"access": "public"
1817
},
1918
"dependencies": {
20-
"babel-runtime": "6.0.14",
21-
"@datagica/parse-dates": "0.0.0",
19+
"@datagica/parse-dates": "0.0.1",
2220
"xregexp": "3.0.0"
2321
},
2422
"devDependencies": {
25-
"babel": "6.0.15",
26-
"babel-cli": "6.1.1",
27-
"babel-core": "6.0.20",
28-
"babel-plugin-transform-regenerator": "6.0.18",
29-
"babel-plugin-transform-runtime": "6.0.16",
30-
"babel-polyfill": "6.0.16",
31-
"babel-preset-es2015": "6.0.15",
32-
"babel-preset-stage-0": "6.0.15",
33-
"babel-preset-stage-1": "6.0.15",
3423
"chai": "3.2.0",
3524
"chai-fuzzy": "1.6.0",
3625
"deep-eql": "0.1.3",

src/parse-date-intervals.js

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

tests/src/all.js renamed to tests/all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const chai = require('chai');
22
chai.use(require('chai-fuzzy'));
33
const expect = chai.expect;
44

5-
import parseDateIntervals from '../../lib/parse-date-intervals';
5+
const parseDateIntervals = require("../lib/parse-date-intervals");
66

77
describe('@datagica/parse-date-intervals', () => {
88

0 commit comments

Comments
 (0)