UNPKG

elliptical-number

Version:

Elliptical phrases for numbers (Integer, Decimal, DigitString, Ordinal)

89 lines (70 loc) 3.57 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _elliptical = require('elliptical'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** @jsx createElement */ var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; function isSignedNumeric(input) { return (/^[\-\+]?\d+$/.test(input) ); } function getValue(option) { return _lodash2.default.assign({}, option, { result: parseInt(option.result, 10) }); } function _suppressWhen(input, props) { if (input === '-' || input === '+') return true; if (!isSignedNumeric(input)) return false; var intValue = parseInt(input, 10); if (props.min >= 0) { if (_lodash2.default.startsWith(input, '-')) return false; if (intValue < 0) return false; if (intValue > props.max) return false; if (intValue < props.min) return true; } else if (props.max <= 0) { if (!_lodash2.default.startsWith(input, '-')) return false; if (intValue > 0) return false; if (intValue < props.min) return false; if (intValue > props.max) return true; } return false; } var defaultProps = { max: MAX_SAFE_INTEGER, min: -MAX_SAFE_INTEGER, limit: 1, label: 'integer', allowWordForm: false, allowIndefiniteArticles: false }; function filterResult(result, _ref) { var props = _ref.props; return result <= props.max && result >= props.min; } function describe(_ref2) { var props = _ref2.props; return (0, _elliptical.createElement)( 'placeholder', { label: props.label, arguments: props.phraseArguments || (props.phraseArguments ? [props.phraseArgument] : [props.label]), suppressWhen: function suppressWhen(input) { return _suppressWhen(input, props); }, suppressEmpty: true }, (0, _elliptical.createElement)( 'choice', null, (0, _elliptical.createElement)( 'map', { outbound: getValue, skipIncomplete: true }, (0, _elliptical.createElement)('freetext', { filter: isSignedNumeric, limit: props.limit, splitOn: /\D/, score: 1 }) ), props.allowWordForm ? (0, _elliptical.createElement)('list', { items: [{ text: 'zero', value: 0 }, { text: 'one', value: 1 }, { text: 'two', value: 2 }, { text: 'three', value: 3 }, { text: 'four', value: 4 }, { text: 'five', value: 5 }, { text: 'six', value: 6 }, { text: 'seven', value: 7 }, { text: 'eight', value: 8 }, { text: 'nine', value: 9 }, { text: 'ten', value: 10 }, { text: 'eleven', value: 11 }, { text: 'twelve', value: 12 }, { text: 'thirteen', value: 13 }, { text: 'fourteen', value: 14 }, { text: 'fifteen', value: 15 }, { text: 'sixteen', value: 16 }, { text: 'seventeen', value: 17 }, { text: 'eighteen', value: 18 }, { text: 'nineteen', value: 19 }, { text: 'twenty', value: 20 }, { text: 'twenty-one', value: 21 }, { text: 'twenty-two', value: 22 }, { text: 'twenty-three', value: 23 }, { text: 'twenty-four', value: 24 }, { text: 'twenty-five', value: 25 }, { text: 'twenty-six', value: 26 }, { text: 'twenty-seven', value: 27 }, { text: 'twenty-eight', value: 28 }, { text: 'twenty-nine', value: 29 }, { text: 'thirty', value: 30 }, { text: 'thirty-one', value: 31 }] }) : null, props.allowIndefiniteArticles ? (0, _elliptical.createElement)('list', { items: [{ text: 'a', value: 1 }, { text: 'an', value: 1 }] }) : null ) ); } exports.default = { defaultProps: defaultProps, filterResult: filterResult, describe: describe, id: 'elliptical-number:Integer' };