@stacksjs/ts-validation
Version:
A simple TypeScript starter kit using Bun.
25 lines (22 loc) • 893 B
TypeScript
import type { DecimalValidatorOptions } from '../types';
declare function decimalRegExp(options: DecimalValidatorOptions): RegExp;
declare const default_decimal_options: {
force_decimal: false;
decimal_digits: '1,';
locale: 'en-US'
};
declare const blacklist: Array<'' | '-' | '+']
* Check if the string is Decimal
*
* str - The string to check
* options - Options object
* True if the string matches the validation, false otherwise
*>;
export default function isDecimal(str: string, options: Partial<DecimalValidatorOptions>): boolean {
assertString(str)
const mergedOptions = merge(options, default_decimal_options) as DecimalValidatorOptions
if (mergedOptions.locale in decimal) {
return !includes(blacklist, str.replace(/ /g, '')) && decimalRegExp(mergedOptions).test(str)
}
throw new Error(`Invalid locale '${mergedOptions.locale}'`)
};