elliptical-number
Version:
Elliptical phrases for numbers (Integer, Decimal, DigitString, Ordinal)
81 lines (59 loc) • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _elliptical = require('elliptical');
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @jsx createElement */
function isNumeric(input) {
return (/^\d+$/.test(input)
);
}
function _suppressWhen(input, props) {
if (!isNumeric(input)) return false;
if (!props.allowLeadingZeros && input !== '0' && _lodash2.default.startsWith(input, '0')) return false;
if (input.length < props.minLength) return true;
var intValue = parseInt(input, 10);
if (intValue < props.min) return true;
return false;
}
var defaultProps = {
minLength: 1,
maxLength: 9007199254740991,
min: 0,
max: 9007199254740991,
allowLeadingZeros: true,
label: 'digit string'
};
function filterResult(result, _ref) {
var props = _ref.props;
if (result.length > props.maxLength) return false;
if (result.length < props.minLength) return false;
if (!props.allowLeadingZeros && result !== '0' && _lodash2.default.startsWith(result, '0')) return false;
var intValue = parseInt(result, 10);
if (isNaN(intValue)) return false;
if (intValue > props.max) return false;
if (intValue < props.min) return false;
return true;
}
/*
you cannot actually set the "argument" property because of prepositions
USE LABEL
*/
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)('freetext', { filter: isNumeric, splitOn: /\D/, score: 1 })
);
}
exports.default = { defaultProps: defaultProps, filterResult: filterResult, describe: describe, id: 'elliptical-number:DigitString' };