Comparing version 0.9.2 to 0.9.3
@@ -9,3 +9,18 @@ import { getLocale, parseLocale, addLocale, resolveLocale } from './locale'; | ||
function numfmt (pattern, l4e) { | ||
function parseCatch (pattern, l10n) { | ||
try { | ||
return parsePattern(pattern, l10n); | ||
} | ||
catch (err) { | ||
const fmt = () => '######'; | ||
fmt.color = () => 'black'; | ||
fmt.isDate = () => false; | ||
fmt.pattern = pattern; | ||
fmt.locale = l10n; | ||
fmt.error = err.message; | ||
return fmt; | ||
} | ||
} | ||
function numfmt (pattern, l4e, noThrows = false) { | ||
if (!pattern) { | ||
@@ -22,3 +37,5 @@ pattern = 'General'; | ||
// create a new formatter | ||
const formatter = parsePattern(pattern, l10n); | ||
const formatter = noThrows | ||
? parseCatch(pattern, l10n) | ||
: parsePattern(pattern, l10n); | ||
_cache[_l4e][pattern] = formatter; | ||
@@ -45,4 +62,4 @@ } | ||
// SSF interface compatibility | ||
function format (pattern, value, l4e) { | ||
return numfmt(pattern, l4e)(value); | ||
function format (pattern, value, l4e, noThrows = false) { | ||
return numfmt(pattern, l4e, noThrows)(value); | ||
} | ||
@@ -49,0 +66,0 @@ numfmt.format = format; |
@@ -15,2 +15,3 @@ import codeToLocale from './codeToLocale'; | ||
infinity: '∞', | ||
overflow: '#VALUE!', | ||
ampm: [ 'AM', 'PM' ], | ||
@@ -17,0 +18,0 @@ mmmm6: [ 'Muharram', 'Safar', 'Rabiʻ I', 'Rabiʻ II', 'Jumada I', 'Jumada II', 'Rajab', 'Shaʻban', 'Ramadan', 'Shawwal', 'Dhuʻl-Qiʻdah', 'Dhuʻl-Hijjah' ], |
import { getLocale, defaultLocale } from './locale'; | ||
import { parse_part } from './parsePart'; | ||
import { parsePart } from './parsePart'; | ||
import { runPart } from './runPart'; | ||
@@ -31,3 +31,3 @@ | ||
const default_text = parse_part('@'); | ||
const default_text = parsePart('@'); | ||
const default_color = 'black'; | ||
@@ -88,3 +88,3 @@ | ||
do { | ||
part = parse_part(p); | ||
part = parsePart(p); | ||
if (part.condition) { | ||
@@ -127,7 +127,7 @@ conditions++; | ||
if (partitions.length < 1 && text_partition) { | ||
partitions[0] = parse_part('General'); | ||
partitions[0] = parsePart('General'); | ||
} | ||
// missing negative | ||
if (partitions.length < 2) { | ||
partitions.push(parse_part('-' + partitions[0].pattern)); | ||
partitions.push(parsePart('-' + partitions[0].pattern)); | ||
} | ||
@@ -137,7 +137,7 @@ // missing zero | ||
partitions.push( | ||
parse_part(partitions[0].pattern) | ||
parsePart(partitions[0].pattern) | ||
); | ||
} | ||
if (partitions.length < 4) { // missing text | ||
partitions.push(text_partition || parse_part('@')); | ||
partitions.push(text_partition || parsePart('@')); | ||
} | ||
@@ -144,0 +144,0 @@ |
@@ -10,3 +10,3 @@ import { resolveLocale } from './locale'; | ||
function min_max_pad (str, part, prefix) { | ||
function minMaxPad (str, part, prefix) { | ||
part[prefix + '_max'] = str.length; | ||
@@ -19,3 +19,3 @@ part[prefix + '_min'] = str.replace(/#/g, '').length; | ||
const _pattcache = {}; | ||
function pattern_to_padding (ss) { | ||
function patternToPadding (ss) { | ||
if (!(ss in _pattcache)) { | ||
@@ -54,3 +54,3 @@ const nch = []; | ||
export function parse_part (pattern) { | ||
export function parsePart (pattern) { | ||
const tokens = []; | ||
@@ -437,5 +437,5 @@ const part = { | ||
min_max_pad(ipatt.replace(/[,]/g, ''), part, 'int'); | ||
min_max_pad(part.frac_pattern.join(''), part, 'frac'); | ||
min_max_pad(part.man_pattern.join(''), part, 'man'); | ||
minMaxPad(ipatt.replace(/[,]/g, ''), part, 'int'); | ||
minMaxPad(part.frac_pattern.join(''), part, 'frac'); | ||
minMaxPad(part.man_pattern.join(''), part, 'man'); | ||
@@ -452,4 +452,4 @@ let num_pat = part.num_pattern.join(''); | ||
} | ||
min_max_pad(num_pat, part, 'num'); | ||
min_max_pad(den_pat, part, 'den'); | ||
minMaxPad(num_pat, part, 'num'); | ||
minMaxPad(den_pat, part, 'den'); | ||
if (enforce_padded) { | ||
@@ -460,8 +460,8 @@ // this needs to be _after_ min/max | ||
part.int_padding = pattern_to_padding(part.int_pattern.join('')); | ||
part.frac_padding = pattern_to_padding(part.frac_pattern.join('')); | ||
part.man_padding = pattern_to_padding(part.man_pattern.join('')); | ||
part.int_padding = patternToPadding(part.int_pattern.join('')); | ||
part.frac_padding = patternToPadding(part.frac_pattern.join('')); | ||
part.man_padding = patternToPadding(part.man_pattern.join('')); | ||
part.num_padding = pattern_to_padding(num_pat); | ||
part.den_padding = pattern_to_padding(den_pat); | ||
part.num_padding = patternToPadding(num_pat); | ||
part.den_padding = patternToPadding(den_pat); | ||
@@ -468,0 +468,0 @@ if (part.den_pattern.length) { |
@@ -128,3 +128,3 @@ import round from './round'; | ||
// This copies the TEXT function which emits a #VALUE! error | ||
throw new Error('Date out of bounds'); | ||
return l10n.overflow; | ||
} | ||
@@ -131,0 +131,0 @@ else if (part.date) { |
@@ -5,7 +5,7 @@ { | ||
"author": "Borgar Þorsteinsson <borgar@borgar.net>", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"scripts": { | ||
"start": "wp --mode development --watch", | ||
"build": "wp --mode production", | ||
"dev": "wp --mode development --watch", | ||
"dev": "nodemon -w test -w lib -x 'SKIPTABLES=1 tape -r @babel/register 'test/*-test.js'|tap-min'", | ||
"lint": "eslint lib test", | ||
@@ -12,0 +12,0 @@ "test-all": "tape -r @babel/register 'test/*-test.js'", |
@@ -116,6 +116,8 @@ # numFmt | ||
<a name="numfmt" href="#numfmt">#</a> **numfmt**(pattern[, locale]) | ||
<a name="numfmt" href="#numfmt">#</a> **numfmt**(pattern[, locale[, suppressErrors]]) | ||
Constructs a new _formatter_ with the specified format pattern and locale. Pattern must be a string according to the [ECMA-376][ecma] number format. Locale is expected to be a [BCP 47][bcp] tag but if not specified it defaults to `en-US`. | ||
Constructs a new _formatter_ with the specified format pattern and locale. Pattern must be a string according to the [ECMA-376][ecma] number format. The locale argument is expected to be a [BCP 47][bcp] string tag but if not specified it defaults to `en-US`. | ||
The suppressError argument will prevent the parser and formatter from throwing errors (which they will normally do). Invalid patterns will produce a formatter that only emits `######`. | ||
<a name="numfmt" href="#formatter">#</a> _formatter_(value) | ||
@@ -142,5 +144,5 @@ | ||
<a name="numfmt.format" href="#numfmt.format">#</a> numfmt.**format**(pattern, value[, locale]) | ||
<a name="numfmt.format" href="#numfmt.format">#</a> numfmt.**format**(pattern, value[, locale[, suppressErrors]]) | ||
Parses the format pattern and formats the value according to the pattern, and optionally, locale. | ||
Parses the format pattern and formats the value according to the pattern, and optionally, locale. See definition [above](#numfmt). | ||
@@ -147,0 +149,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108257
1894
216