UNPKG

elliptical-datetime

Version:

Elliptical phrases to handle natural language dates and times

156 lines (134 loc) 4.97 kB
'use strict'; 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')); } const BaseDuration = { mapResult(result) { const newResult = {}; _lodash2.default.forEach(result, ({ num, type }) => { newResult[type] = (newResult[type] || 0) + num; }); return newResult; }, describe({ props, 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 ) ) ); } }; const InternalDuration = { mapResult(result) { return { id: result.id, type: result.type, num: result.num * (result.multiplier || 1) }; }, describe({ props }) { const 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' } }] : []); const pluralDurations = _lodash2.default.map(singularDurations, ({ text, value }) => ({ text: `${ text }s`, 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 }) ) ) ); } }; const DateDuration = exports.DateDuration = { id: 'elliptical-datetime:DateDuration', defaultProps: { label: 'date duration' }, describe({ props }) { return (0, _elliptical.createElement)( BaseDuration, { label: props.label, phraseArguments: props.phraseArguments, phraseArgument: props.phraseArgument }, (0, _elliptical.createElement)(InternalDuration, { type: 'date', seconds: props.seconds }) ); } }; const TimeDuration = exports.TimeDuration = { id: 'elliptical-datetime:TimeDuration', defaultProps: { seconds: true, label: 'time duration' }, describe({ props }) { return (0, _elliptical.createElement)( BaseDuration, { label: props.label, phraseArguments: props.phraseArguments, phraseArgument: props.phraseArgument }, (0, _elliptical.createElement)(InternalDuration, { type: 'time', seconds: props.seconds }) ); } }; const Duration = exports.Duration = { id: 'elliptical-datetime:Duration', defaultProps: { seconds: false, label: 'duration' }, describe({ props }) { return (0, _elliptical.createElement)( BaseDuration, { label: props.label, phraseArguments: props.phraseArguments, phraseArgument: props.phraseArgument }, (0, _elliptical.createElement)(InternalDuration, { seconds: props.seconds }) ); } };