Comparing version 0.5.7 to 0.5.8
# Changelog | ||
## 0.5.8 | ||
* Large perf improvements for `DateTime#toFormat()`, when using non-intl numbers | ||
## 0.5.7 | ||
@@ -4,0 +7,0 @@ |
{ | ||
"name": "luxon", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "Immutable date wrapper", | ||
@@ -5,0 +5,0 @@ "author": "Isaac Cambron", |
import * as English from './english'; | ||
import * as Formats from './formats'; | ||
import { padStart } from './util'; | ||
import { padStart, roundTo } from './util'; | ||
@@ -46,5 +46,4 @@ function stringifyTokens(splits, tokenToString) { | ||
static create(locale, opts = {}) { | ||
const fast = opts.fast; | ||
const formatOpts = Object.assign({}, { round: true }, opts); | ||
return new Formatter(locale, formatOpts, fast); | ||
return new Formatter(locale, formatOpts); | ||
} | ||
@@ -86,5 +85,4 @@ | ||
constructor(locale, formatOpts, fast) { | ||
constructor(locale, formatOpts) { | ||
this.opts = formatOpts; | ||
this.fast = fast; | ||
this.loc = locale; | ||
@@ -118,17 +116,15 @@ this.systemLoc = null; | ||
num(n, p = 0) { | ||
if (this.fast) { | ||
if (p > 0) { | ||
return padStart(n, p); | ||
} else { | ||
return n; | ||
} | ||
} else { | ||
const opts = Object.assign({}, this.opts); | ||
if (p > 0) { | ||
opts.padTo = p; | ||
} | ||
// we get some perf out of doing this here, annoyingly | ||
if (this.opts.forceSimple) { | ||
return padStart(n, p); | ||
} | ||
return this.loc.numberFormatter(opts).format(n); | ||
const opts = Object.assign({}, this.opts); | ||
if (p > 0) { | ||
opts.padTo = p; | ||
} | ||
return this.loc.numberFormatter(opts).format(n); | ||
} | ||
@@ -135,0 +131,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { hasFormatToParts, hasIntl, padStart } from './util'; | ||
import { hasFormatToParts, hasIntl, padStart, roundTo } from './util'; | ||
import * as English from './english'; | ||
@@ -76,2 +76,13 @@ import Settings from '../settings'; | ||
function supportsFastNumbers(loc) { | ||
if (loc.numberingSystem && loc.numberingSystem !== 'latn') { | ||
return false; | ||
} else { | ||
return loc.numberingSystem === 'latn' || !loc.locale || loc.locale.startsWith('en') || (hasIntl() && | ||
Intl.DateTimeFormat(loc.intl) | ||
.resolvedOptions() | ||
.numberingSystem === 'latn'); | ||
} | ||
} | ||
/** | ||
@@ -81,3 +92,3 @@ * @private | ||
class PolyNumberFormatter { | ||
class SimpleNumberFormatter { | ||
constructor(opts) { | ||
@@ -89,7 +100,13 @@ this.padTo = opts.padTo || 0; | ||
format(i) { | ||
const maybeRounded = this.round ? Math.round(i) : i; | ||
return padStart(maybeRounded.toString(), this.padTo); | ||
// to match the browser's numberformatter defaults | ||
const digits = this.round ? 0 : 3, | ||
rounded = roundTo(i, digits); | ||
return padStart(rounded, this.padTo); | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
class PolyDateFormatter { | ||
@@ -204,2 +221,3 @@ constructor(dt, intl, opts) { | ||
this.specifiedLocale = specifiedLocale; | ||
this.fastNumbers = supportsFastNumbers(this); | ||
} | ||
@@ -324,17 +342,23 @@ | ||
numberFormatter(opts = {}, intlOpts = {}) { | ||
if (hasIntl()) { | ||
const realIntlOpts = Object.assign({ useGrouping: false }, intlOpts); | ||
numberFormatter(opts = {}) { | ||
// this option is never used (the only caller short-circuits on it, but it seems safer to leave) | ||
// (in contrast, the || is used heavily) | ||
if (opts.forceSimple || this.fastNumbers) { | ||
return new SimpleNumberFormatter(opts); | ||
} else { | ||
if (hasIntl()) { | ||
const intlOpts = { useGrouping: false }; | ||
if (opts.padTo > 0) { | ||
realIntlOpts.minimumIntegerDigits = opts.padTo; | ||
} | ||
if (opts.padTo > 0) { | ||
intlOpts.minimumIntegerDigits = opts.padTo; | ||
} | ||
if (opts.round) { | ||
realIntlOpts.maximumFractionDigits = 0; | ||
if (opts.round) { | ||
intlOpts.maximumFractionDigits = 0; | ||
} | ||
return new Intl.NumberFormat(this.intl, intlOpts); | ||
} else { | ||
return new SimpleNumberFormatter(opts); | ||
} | ||
return new Intl.NumberFormat(this.intl, realIntlOpts); | ||
} else { | ||
return new PolyNumberFormatter(opts); | ||
} | ||
@@ -341,0 +365,0 @@ } |
@@ -80,3 +80,7 @@ /* | ||
export function padStart(input, n = 2) { | ||
return ('0'.repeat(n) + input).slice(-n); | ||
if (input.toString().length < n) { | ||
return ('0'.repeat(n) + input).slice(-n); | ||
} else { | ||
return input.toString(); | ||
} | ||
} | ||
@@ -93,2 +97,7 @@ | ||
export function roundTo(number, digits) { | ||
var factor = Math.pow(10, digits); | ||
return Math.round(number * factor) / factor; | ||
} | ||
// DATE BASICS | ||
@@ -95,0 +104,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
2208806
28197