elliptical-datetime
Version:
Elliptical phrases to handle natural language dates and times
172 lines (146 loc) • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Duration = exports.TimeDuration = exports.DateDuration = undefined;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _elliptical = require('elliptical');
var _ellipticalNumber = require('elliptical-number');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function isUnique(array) {
return _lodash2.default.uniq(array).length === array.length;
} /** @jsx createElement */
function filterBase(option) {
return isUnique(_lodash2.default.map(option.result, 'id'));
}
var BaseDuration = {
mapResult: function mapResult(result) {
var newResult = {};
_lodash2.default.forEach(result, function (_ref) {
var num = _ref.num,
type = _ref.type;
newResult[type] = (newResult[type] || 0) + num;
});
return newResult;
},
describe: function describe(_ref2) {
var props = _ref2.props,
children = _ref2.children;
return (0, _elliptical.createElement)(
'placeholder',
{
label: props.label,
arguments: props.phraseArguments || (props.phraseArguments ? [props.phraseArgument] : [props.label]) },
(0, _elliptical.createElement)(
'filter',
{ outbound: filterBase, skipIncomplete: true },
(0, _elliptical.createElement)(
'repeat',
{ separator: (0, _elliptical.createElement)('list', { items: [', ', ' and ', ', and '], limit: 1 }) },
children
)
)
);
}
};
var InternalDuration = {
mapResult: function mapResult(result) {
return {
id: result.id,
type: result.type,
num: result.num * (result.multiplier || 1)
};
},
describe: function describe(_ref3) {
var props = _ref3.props;
var singularDurations = (props.type !== 'time' ? [{ text: 'day', value: { id: 'days', type: 'days' } }, { text: 'fortnight', value: { id: 'fortnights', type: 'days', multiplier: 14 } }, { text: 'week', value: { id: 'weeks', type: 'days', multiplier: 7 } }, { text: 'month', value: { id: 'months', type: 'months' } }, { text: 'year', value: { id: 'years', type: 'years' } }] : []).concat(props.type !== 'date' ? [{ text: 'hour', value: { id: 'hours', type: 'hours' } }, { text: 'minute', value: { id: 'minutes', type: 'minutes' } }] : []).concat(props.type !== 'date' && props.seconds ? [{ text: 'second', value: { id: 'seconds', type: 'seconds' } }] : []);
var pluralDurations = _lodash2.default.map(singularDurations, function (_ref4) {
var text = _ref4.text,
value = _ref4.value;
return {
text: text + 's',
value: value
};
});
return (0, _elliptical.createElement)(
'choice',
{ limit: 1 },
(0, _elliptical.createElement)(
'sequence',
null,
(0, _elliptical.createElement)(_ellipticalNumber.Integer, { allowWordForm: true, allowIndefiniteArticles: true, max: 1, min: 1, id: 'num', limit: 1 }),
(0, _elliptical.createElement)('literal', { text: ' ' }),
(0, _elliptical.createElement)(
'placeholder',
{ label: 'time period', merge: true },
(0, _elliptical.createElement)('list', { items: singularDurations })
)
),
(0, _elliptical.createElement)(
'sequence',
null,
(0, _elliptical.createElement)(_ellipticalNumber.Integer, { allowWordForm: true, allowIndefiniteArticles: true, id: 'num', min: 2, limit: 1 }),
(0, _elliptical.createElement)('literal', { text: ' ' }),
(0, _elliptical.createElement)(
'placeholder',
{ label: 'time period', merge: true },
(0, _elliptical.createElement)('list', { items: pluralDurations })
)
)
);
}
};
var DateDuration = exports.DateDuration = {
id: 'elliptical-datetime:DateDuration',
defaultProps: {
label: 'date duration'
},
describe: function describe(_ref5) {
var props = _ref5.props;
return (0, _elliptical.createElement)(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
(0, _elliptical.createElement)(InternalDuration, { type: 'date', seconds: props.seconds })
);
}
};
var TimeDuration = exports.TimeDuration = {
id: 'elliptical-datetime:TimeDuration',
defaultProps: {
seconds: true,
label: 'time duration'
},
describe: function describe(_ref6) {
var props = _ref6.props;
return (0, _elliptical.createElement)(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
(0, _elliptical.createElement)(InternalDuration, { type: 'time', seconds: props.seconds })
);
}
};
var Duration = exports.Duration = {
id: 'elliptical-datetime:Duration',
defaultProps: {
seconds: false,
label: 'duration'
},
describe: function describe(_ref7) {
var props = _ref7.props;
return (0, _elliptical.createElement)(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
(0, _elliptical.createElement)(InternalDuration, { seconds: props.seconds })
);
}
};