elliptical-datetime
Version:
Elliptical phrases to handle natural language dates and times
159 lines (140 loc) • 4.56 kB
JavaScript
/** @jsx createElement */
import _ from 'lodash';
import { createElement } from 'elliptical';
import { Integer } from 'elliptical-number';
function isUnique(array) {
return _.uniq(array).length === array.length;
}
function filterBase(option) {
return isUnique(_.map(option.result, 'id'));
}
var BaseDuration = {
mapResult: function mapResult(result) {
var newResult = {};
_.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 createElement(
'placeholder',
{
label: props.label,
arguments: props.phraseArguments || (props.phraseArguments ? [props.phraseArgument] : [props.label]) },
createElement(
'filter',
{ outbound: filterBase, skipIncomplete: true },
createElement(
'repeat',
{ separator: 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 = _.map(singularDurations, function (_ref4) {
var text = _ref4.text,
value = _ref4.value;
return {
text: text + 's',
value: value
};
});
return createElement(
'choice',
{ limit: 1 },
createElement(
'sequence',
null,
createElement(Integer, { allowWordForm: true, allowIndefiniteArticles: true, max: 1, min: 1, id: 'num', limit: 1 }),
createElement('literal', { text: ' ' }),
createElement(
'placeholder',
{ label: 'time period', merge: true },
createElement('list', { items: singularDurations })
)
),
createElement(
'sequence',
null,
createElement(Integer, { allowWordForm: true, allowIndefiniteArticles: true, id: 'num', min: 2, limit: 1 }),
createElement('literal', { text: ' ' }),
createElement(
'placeholder',
{ label: 'time period', merge: true },
createElement('list', { items: pluralDurations })
)
)
);
}
};
export var DateDuration = {
id: 'elliptical-datetime:DateDuration',
defaultProps: {
label: 'date duration'
},
describe: function describe(_ref5) {
var props = _ref5.props;
return createElement(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
createElement(InternalDuration, { type: 'date', seconds: props.seconds })
);
}
};
export var TimeDuration = {
id: 'elliptical-datetime:TimeDuration',
defaultProps: {
seconds: true,
label: 'time duration'
},
describe: function describe(_ref6) {
var props = _ref6.props;
return createElement(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
createElement(InternalDuration, { type: 'time', seconds: props.seconds })
);
}
};
export var Duration = {
id: 'elliptical-datetime:Duration',
defaultProps: {
seconds: false,
label: 'duration'
},
describe: function describe(_ref7) {
var props = _ref7.props;
return createElement(
BaseDuration,
{
label: props.label,
phraseArguments: props.phraseArguments,
phraseArgument: props.phraseArgument },
createElement(InternalDuration, { seconds: props.seconds })
);
}
};