UNPKG

angular-l10n

Version:

An Angular library to translate messages, dates and numbers

92 lines 3.27 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 = Number.MIN_VALUE, MAX_VALUE = Number.MAX_VALUE) { /** @type {?} */ const localeValidation = InjectorRef.get(LocaleValidation); return (c) => { if (c.value == "" || c.value == null) return null; /** @type {?} */ const 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 }; } }; } export class L10nNumberValidatorDirective { constructor() { this.MIN_VALUE = Number.MIN_VALUE; this.MAX_VALUE = Number.MAX_VALUE; } /** * @param {?} digits * @return {?} */ set l10nValidateNumber(digits) { this.digits = digits; } /** * @return {?} */ ngOnInit() { this.validator = l10nValidateNumber(this.digits, this.minValue || this.MIN_VALUE, this.maxValue || this.MAX_VALUE); } /** * @param {?} c * @return {?} */ validate(c) { return this.validator(c); } } L10nNumberValidatorDirective.decorators = [ { type: Directive, args: [{ selector: '[l10nValidateNumber][ngModel],[l10nValidateNumber][formControl],[l10nValidateNumber][formControlName]', providers: [ { provide: NG_VALIDATORS, useExisting: forwardRef(() => L10nNumberValidatorDirective), multi: true } ] },] } ]; L10nNumberValidatorDirective.propDecorators = { l10nValidateNumber: [{ type: Input }], digits: [{ type: Input }], minValue: [{ type: Input }], maxValue: [{ type: Input }] }; 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