UNPKG

angular-l10n

Version:

An Angular library to translate messages, dates and numbers

107 lines 3.95 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ import { Directive, forwardRef, Input } from '@angular/core'; import { NG_VALIDATORS } from '@angular/forms'; import { LocaleValidation } from '../services/locale-validation'; import { InjectorRef } from '../models/injector-ref'; /** * Function that takes a control and returns either null when it’s valid, or an error object if it’s not. * @param {?} digits An alias of the format * @param {?=} MIN_VALUE The minimum value for the number * @param {?=} MAX_VALUE The maximum value for the number * @return {?} An error object: 'format', 'minValue' or 'maxValue'; null in case the value is valid */ export function l10nValidateNumber(digits, MIN_VALUE, MAX_VALUE) { if (MIN_VALUE === void 0) { MIN_VALUE = Number.MIN_VALUE; } if (MAX_VALUE === void 0) { MAX_VALUE = Number.MAX_VALUE; } /** @type {?} */ var localeValidation = InjectorRef.get(LocaleValidation); return function (c) { if (c.value == "" || c.value == null) return null; /** @type {?} */ var parsedValue = localeValidation.parseNumber(c.value, digits); if (parsedValue != null && !isNaN(parsedValue)) { if (parsedValue < MIN_VALUE) { return { minValue: true }; } else if (parsedValue > MAX_VALUE) { return { maxValue: true }; } return null; // The number is valid. } else { return { format: true }; } }; } var L10nNumberValidatorDirective = /** @class */ (function () { function L10nNumberValidatorDirective() { this.MIN_VALUE = Number.MIN_VALUE; this.MAX_VALUE = Number.MAX_VALUE; } Object.defineProperty(L10nNumberValidatorDirective.prototype, "l10nValidateNumber", { set: /** * @param {?} digits * @return {?} */ function (digits) { this.digits = digits; }, enumerable: true, configurable: true }); /** * @return {?} */ L10nNumberValidatorDirective.prototype.ngOnInit = /** * @return {?} */ function () { this.validator = l10nValidateNumber(this.digits, this.minValue || this.MIN_VALUE, this.maxValue || this.MAX_VALUE); }; /** * @param {?} c * @return {?} */ L10nNumberValidatorDirective.prototype.validate = /** * @param {?} c * @return {?} */ function (c) { return this.validator(c); }; L10nNumberValidatorDirective.decorators = [ { type: Directive, args: [{ selector: '[l10nValidateNumber][ngModel],[l10nValidateNumber][formControl],[l10nValidateNumber][formControlName]', providers: [ { provide: NG_VALIDATORS, useExisting: forwardRef(function () { return L10nNumberValidatorDirective; }), multi: true } ] },] } ]; L10nNumberValidatorDirective.propDecorators = { l10nValidateNumber: [{ type: Input }], digits: [{ type: Input }], minValue: [{ type: Input }], maxValue: [{ type: Input }] }; return L10nNumberValidatorDirective; }()); export { L10nNumberValidatorDirective }; if (false) { /** @type {?} */ L10nNumberValidatorDirective.prototype.digits; /** @type {?} */ L10nNumberValidatorDirective.prototype.minValue; /** @type {?} */ L10nNumberValidatorDirective.prototype.maxValue; /** @type {?} */ L10nNumberValidatorDirective.prototype.MIN_VALUE; /** @type {?} */ L10nNumberValidatorDirective.prototype.MAX_VALUE; /** @type {?} */ L10nNumberValidatorDirective.prototype.validator; } //# sourceMappingURL=l10n-number-validator.directive.js.map