@bodar/totallylazy
Advanced tools
Comparing version 0.401.243 to 0.402.244
@@ -35,3 +35,3 @@ "use strict"; | ||
{ year: 'numeric', month: "long", day: 'numeric' }, | ||
{ year: 'numeric', month: 'short', day: 'numeric' } | ||
{ year: 'numeric', month: 'short', day: 'numeric' }, | ||
]; | ||
@@ -38,0 +38,0 @@ Months.cache = {}; |
@@ -41,7 +41,7 @@ "use strict"; | ||
case "year": | ||
return "(?<year>\\d{" + _this.lengthOf(part.value) + "})"; | ||
return "(?<year>[" + parsing_1.digits(_this.locale) + "]{" + _this.lengthOf(part.value) + "})"; | ||
case "month": | ||
return "(?<month>" + _this.monthsPattern() + ")"; | ||
case "day": | ||
return '(?<day>\\d{1,2})'; | ||
return "(?<day>[" + parsing_1.digits(_this.locale) + "]{1,2})"; | ||
case "weekday": | ||
@@ -75,3 +75,3 @@ return "(?<weekday>" + index_1.Weekdays.get(_this.locale).pattern.toLocaleLowerCase(_this.locale) + ")"; | ||
RegexBuilder.prototype.monthsPattern = function () { | ||
var numericPattern = "\\d{1,2}"; | ||
var numericPattern = "[" + parsing_1.digits(this.locale) + "]{1,2}"; | ||
var textPattern = index_1.Months.get(this.locale).pattern.toLocaleLowerCase(this.locale); | ||
@@ -135,6 +135,7 @@ if (this.options.month === "2-digit" || this.options.month === "numeric") | ||
function dateFrom(parts, locale, options) { | ||
var parser = parsing_1.numberParser('.', locale); | ||
var _a = tslib_1.__read(parts.filter(function (p) { return p.type === 'day'; }), 1), day = _a[0]; | ||
if (!day) | ||
throw new Error("No day found"); | ||
var dd = Number(day.value); | ||
var dd = parser.parse(day.value); | ||
var _b = tslib_1.__read(parts.filter(function (p) { return p.type === 'month'; }), 1), month = _b[0]; | ||
@@ -145,3 +146,3 @@ if (!month) | ||
var _c = tslib_1.__read(parts.filter(function (p) { return p.type === 'year'; }), 1), year = _c[0]; | ||
var yyyy = year ? Number(year.value) : undefined; | ||
var yyyy = year ? parser.parse(year.value) : undefined; | ||
// @ts-ignore | ||
@@ -148,0 +149,0 @@ var factory = functions_1.get(function () { return options.factory; }, new DefaultDateFactory()); |
import { Money } from "./money"; | ||
import { MatchStrategy, Parser } from "../parsing"; | ||
import { NamedRegExp } from "../characters"; | ||
import { Mapper } from "../collections"; | ||
import { AllowedDecimalSeparators, MatchStrategy, Parser } from "../parsing"; | ||
export interface Options { | ||
@@ -10,3 +8,2 @@ strategy?: MatchStrategy<string>; | ||
export declare function flexibleParse(value: string, locale?: string, options?: Options): Money; | ||
export declare function mapIgnoreError<A, B>(mapper: Mapper<A, B>): import("../transducers").FlatMapTransducer<A, B>; | ||
export declare class FlexibleMoneyParser implements Parser<Money> { | ||
@@ -24,14 +21,1 @@ private locale; | ||
export declare function findDecimalSeparator(isoCurrency: string, amount: string): AllowedDecimalSeparators; | ||
export declare type AllowedDecimalSeparators = '.' | ',' | '٫'; | ||
export declare class NumberParser implements Parser<number> { | ||
private decimalSeparator; | ||
private locale; | ||
readonly strictNumberPattern: RegExp; | ||
readonly globalNumberPattern: NamedRegExp; | ||
constructor(decimalSeparator: AllowedDecimalSeparators, locale: string); | ||
parse(value: string): number; | ||
parseAll(value: string): number[]; | ||
private parseSingle; | ||
private numberOf; | ||
} | ||
export declare function numberParser(decimalSeparator: AllowedDecimalSeparators, locale?: string): NumberParser; |
@@ -12,3 +12,2 @@ "use strict"; | ||
var lazy_1 = require("../lazy"); | ||
var functions_1 = require("../functions"); | ||
function flexibleParse(value, locale, options) { | ||
@@ -19,18 +18,2 @@ if (locale === void 0) { locale = 'en'; } | ||
exports.flexibleParse = flexibleParse; | ||
var allowedSeparators = "\u066C\u066B,.'\u2019" + parsing_1.Spaces.spaces; | ||
var numberPattern = cache_1.caching(function (locale) { | ||
var d = parsing_1.digits(locale); | ||
return "(?:[" + d + "]+[" + allowedSeparators + "])*[" + d + "]+"; | ||
}); | ||
function mapIgnoreError(mapper) { | ||
return transducers_1.flatMap(function (value) { | ||
try { | ||
return [mapper(value)]; | ||
} | ||
catch (e) { | ||
return []; | ||
} | ||
}); | ||
} | ||
exports.mapIgnoreError = mapIgnoreError; | ||
var FlexibleMoneyParser = /** @class */ (function () { | ||
@@ -49,3 +32,3 @@ function FlexibleMoneyParser(locale, options) { | ||
FlexibleMoneyParser.patternFor = function (locale) { | ||
return characters_1.NamedRegExp.create(parsing_1.atBoundaryOnly("(?<currency>" + money_1.CurrencySymbols.get(locale).pattern + ")?(?<literal>[" + parsing_1.Spaces.spaces + "])?(?<number>" + numberPattern(locale) + ")(?<literal>[" + parsing_1.Spaces.spaces + "])?(?<currency>" + money_1.CurrencySymbols.get(locale).pattern + ")?")); | ||
return characters_1.NamedRegExp.create(parsing_1.atBoundaryOnly("(?<currency>" + money_1.CurrencySymbols.get(locale).pattern + ")?(?<literal>[" + parsing_1.Spaces.spaces + "])?(?<number>" + parsing_1.numberPattern(locale) + ")(?<literal>[" + parsing_1.Spaces.spaces + "])?(?<currency>" + money_1.CurrencySymbols.get(locale).pattern + ")?")); | ||
}; | ||
@@ -62,3 +45,3 @@ FlexibleMoneyParser.prototype.parse = function (value) { | ||
var _this = this; | ||
return collections_1.array(this.pattern.exec(value), mapIgnoreError(function (match) { return _this.parseSingle(match); })); | ||
return collections_1.array(this.pattern.exec(value), parsing_1.mapIgnoreError(function (match) { return _this.parseSingle(match); })); | ||
}; | ||
@@ -77,3 +60,3 @@ FlexibleMoneyParser.prototype.parseSingle = function (result) { | ||
var amount = collections_1.single(result, transducers_1.find(function (m) { return m.name === 'number' && m.value !== undefined; })).value; | ||
var instance = numberParser((this.options && this.options.decimalSeparator) || findDecimalSeparator(currency, amount), this.locale); | ||
var instance = parsing_1.numberParser((this.options && this.options.decimalSeparator) || findDecimalSeparator(currency, amount), this.locale); | ||
return money_1.money(currency, instance.parse(amount)); | ||
@@ -95,9 +78,2 @@ }; | ||
exports.flexibleMoneyParser = flexibleMoneyParser; | ||
var separatorsPattern = characters_1.NamedRegExp.create("(?<separator>[" + allowedSeparators + "])"); | ||
function separatorsOf(amount) { | ||
return collections_1.array(separatorsPattern.exec(amount), transducers_1.map(function (_a) { | ||
var _b = tslib_1.__read(_a, 1), match = _b[0]; | ||
return match.value; | ||
})); | ||
} | ||
function isDecimalSeparator(value) { | ||
@@ -115,3 +91,3 @@ return value === '.' || value === ',' || value === '٫'; | ||
function findDecimalSeparator(isoCurrency, amount) { | ||
var separators = separatorsOf(amount); | ||
var separators = parsing_1.separatorsOf(amount); | ||
if (separators.length === 0) | ||
@@ -136,57 +112,1 @@ return '.'; | ||
exports.findDecimalSeparator = findDecimalSeparator; | ||
var NumberParser = /** @class */ (function () { | ||
function NumberParser(decimalSeparator, locale) { | ||
this.decimalSeparator = decimalSeparator; | ||
this.locale = locale; | ||
this.strictNumberPattern = new RegExp("^" + numberPattern(locale) + "$"); | ||
this.globalNumberPattern = characters_1.NamedRegExp.create("(?<number>" + numberPattern(locale) + ")", 'g'); | ||
} | ||
NumberParser.prototype.parse = function (value) { | ||
if (!this.strictNumberPattern.test(value)) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
return this.parseSingle(value); | ||
}; | ||
NumberParser.prototype.parseAll = function (value) { | ||
var _this = this; | ||
return collections_1.array(this.globalNumberPattern.exec(value), mapIgnoreError(function (_a) { | ||
var _b = tslib_1.__read(_a, 1), match = _b[0]; | ||
return _this.parseSingle(match.value.trim()); | ||
})); | ||
}; | ||
NumberParser.prototype.parseSingle = function (value) { | ||
var _this = this; | ||
var separators = separatorsOf(value); | ||
if (separators.length === 0) | ||
return this.numberOf(value); | ||
var lastSeparator = separators[separators.length - 1]; | ||
var groupSeparators = lastSeparator === this.decimalSeparator ? separators.slice(0, separators.length - 1) : separators; | ||
if (groupSeparators.indexOf(this.decimalSeparator) !== -1) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
if (arrays_1.unique(groupSeparators).length > 1) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
var numerals = parsing_1.Numerals.get(this.locale); | ||
var result = characters_1.characters(value).map(function (c) { | ||
if (c === _this.decimalSeparator) | ||
return '.'; | ||
var number = functions_1.get(function () { return numerals.parse(c); }); | ||
if (isNaN(number)) | ||
return ''; | ||
return number.toString(); | ||
}).join(''); | ||
return this.numberOf(result, value); | ||
}; | ||
NumberParser.prototype.numberOf = function (cleaned, original) { | ||
if (original === void 0) { original = cleaned; } | ||
var result = Number(cleaned); | ||
if (isNaN(result)) | ||
throw new Error("Unable to parse '" + original + "'"); | ||
return result; | ||
}; | ||
return NumberParser; | ||
}()); | ||
exports.NumberParser = NumberParser; | ||
function numberParser(decimalSeparator, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return new NumberParser(decimalSeparator, locale); | ||
} | ||
exports.numberParser = numberParser; |
@@ -13,3 +13,2 @@ "use strict"; | ||
var cache_1 = require("../cache"); | ||
var flexible_parsing_1 = require("./flexible-parsing"); | ||
var functions_1 = require("../functions"); | ||
@@ -33,3 +32,3 @@ function money(currency, amount) { | ||
var text = filtered.map(function (p) { return p.value; }).join(''); | ||
var value = flexible_parsing_1.numberParser(decimal, locale).parse(text); | ||
var value = parsing_1.numberParser(decimal, locale).parse(text); | ||
return money(currency, value); | ||
@@ -36,0 +35,0 @@ } |
{ | ||
"name": "@bodar/totallylazy", | ||
"version": "0.401.243", | ||
"version": "0.402.244", | ||
"description": "Totallylazy", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:bodar/totallylazy.js.git", |
@@ -90,1 +90,18 @@ import { NamedMatch, NamedRegExp } from "./characters"; | ||
} | ||
export declare const numberPattern: (locale: string) => string; | ||
export declare function mapIgnoreError<A, B>(mapper: Mapper<A, B>): import("./transducers").FlatMapTransducer<A, B>; | ||
export declare function separatorsOf(amount: string): string[]; | ||
export declare type AllowedDecimalSeparators = '.' | ',' | '٫'; | ||
export declare class NumberParser implements Parser<number> { | ||
private decimalSeparator; | ||
private locale; | ||
readonly strictNumberPattern: RegExp; | ||
readonly globalNumberPattern: NamedRegExp; | ||
constructor(decimalSeparator: AllowedDecimalSeparators, locale: string); | ||
parse(value: string): number; | ||
parseAll(value: string): number[]; | ||
private parseSingle; | ||
private convert; | ||
private numberOf; | ||
} | ||
export declare function numberParser(decimalSeparator: AllowedDecimalSeparators, locale?: string): NumberParser; |
@@ -11,2 +11,3 @@ "use strict"; | ||
var preferred_currencies_1 = require("./money/preferred-currencies"); | ||
var functions_1 = require("./functions"); | ||
var NamedRegexParser = /** @class */ (function () { | ||
@@ -312,1 +313,84 @@ function NamedRegexParser(regex) { | ||
exports.Spaces = Spaces; | ||
var allowedSeparators = "\u066C\u066B,.'\u2019" + Spaces.spaces; | ||
exports.numberPattern = cache_1.caching(function (locale) { | ||
var d = digits(locale); | ||
return "(?:[" + d + "]+[" + allowedSeparators + "])*[" + d + "]+"; | ||
}); | ||
function mapIgnoreError(mapper) { | ||
return transducers_1.flatMap(function (value) { | ||
try { | ||
return [mapper(value)]; | ||
} | ||
catch (e) { | ||
return []; | ||
} | ||
}); | ||
} | ||
exports.mapIgnoreError = mapIgnoreError; | ||
var separatorsPattern = characters_1.NamedRegExp.create("(?<separator>[" + allowedSeparators + "])"); | ||
function separatorsOf(amount) { | ||
return collections_1.array(separatorsPattern.exec(amount), transducers_1.map(function (_a) { | ||
var _b = tslib_1.__read(_a, 1), match = _b[0]; | ||
return match.value; | ||
})); | ||
} | ||
exports.separatorsOf = separatorsOf; | ||
var NumberParser = /** @class */ (function () { | ||
function NumberParser(decimalSeparator, locale) { | ||
this.decimalSeparator = decimalSeparator; | ||
this.locale = locale; | ||
this.strictNumberPattern = new RegExp("^" + exports.numberPattern(locale) + "$"); | ||
this.globalNumberPattern = characters_1.NamedRegExp.create("(?<number>" + exports.numberPattern(locale) + ")", 'g'); | ||
} | ||
NumberParser.prototype.parse = function (value) { | ||
if (!this.strictNumberPattern.test(value)) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
return this.parseSingle(value); | ||
}; | ||
NumberParser.prototype.parseAll = function (value) { | ||
var _this = this; | ||
return collections_1.array(this.globalNumberPattern.exec(value), mapIgnoreError(function (_a) { | ||
var _b = tslib_1.__read(_a, 1), match = _b[0]; | ||
return _this.parseSingle(match.value.trim()); | ||
})); | ||
}; | ||
NumberParser.prototype.parseSingle = function (value) { | ||
var separators = separatorsOf(value); | ||
if (separators.length === 0) | ||
return this.numberOf(value); | ||
var lastSeparator = separators[separators.length - 1]; | ||
var groupSeparators = lastSeparator === this.decimalSeparator ? separators.slice(0, separators.length - 1) : separators; | ||
if (groupSeparators.indexOf(this.decimalSeparator) !== -1) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
if (arrays_1.unique(groupSeparators).length > 1) | ||
throw new Error("Unable to parse '" + value + "'"); | ||
return this.numberOf(value); | ||
}; | ||
NumberParser.prototype.convert = function (value) { | ||
var _this = this; | ||
var numerals = Numerals.get(this.locale); | ||
return characters_1.characters(value).map(function (c) { | ||
if (c === _this.decimalSeparator) | ||
return '.'; | ||
var number = functions_1.get(function () { return numerals.parse(c); }); | ||
if (isNaN(number)) | ||
return ''; | ||
return number.toString(); | ||
}).join(''); | ||
}; | ||
NumberParser.prototype.numberOf = function (value) { | ||
var text = this.convert(value); | ||
var result = Number(text); | ||
if (isNaN(result)) { | ||
throw new Error("Unable to parse '" + value + "'"); | ||
} | ||
return result; | ||
}; | ||
return NumberParser; | ||
}()); | ||
exports.NumberParser = NumberParser; | ||
function numberParser(decimalSeparator, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return new NumberParser(decimalSeparator, locale); | ||
} | ||
exports.numberParser = numberParser; |
309514
7878