Socket
Socket
Sign inDemoInstall

luxon

Package Overview
Dependencies
Maintainers
1
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

luxon - npm Package Compare versions

Comparing version 3.4.3 to 3.4.4

2

package.json
{
"name": "luxon",
"version": "3.4.3",
"version": "3.4.4",
"description": "Immutable date wrapper",

@@ -5,0 +5,0 @@ "author": "Isaac Cambron",

@@ -487,5 +487,6 @@ import { InvalidArgumentError, InvalidDurationError, InvalidUnitError } from "./errors.js";

* Returns a string representation of a Duration with all units included.
* To modify its behavior use the `listStyle` and any Intl.NumberFormat option, though `unitDisplay` is especially relevant.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
* @param opts - On option object to override the formatting. Accepts the same keys as the options parameter of the native `Int.NumberFormat` constructor, as well as `listStyle`.
* To modify its behavior, use `listStyle` and any Intl.NumberFormat option, though `unitDisplay` is especially relevant.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
* @param {Object} opts - Formatting options. Accepts the same keys as the options parameter of the native `Intl.NumberFormat` constructor, as well as `listStyle`.
* @param {string} [opts.listStyle='narrow'] - How to format the merged list. Corresponds to the `style` property of the options parameter of the native `Intl.ListFormat` constructor.
* @example

@@ -612,2 +613,14 @@ * ```js

/**
* Returns a string representation of this Duration appropriate for the REPL.
* @return {string}
*/
[Symbol.for("nodejs.util.inspect.custom")]() {
if (this.isValid) {
return `Duration { values: ${JSON.stringify(this.values)} }`;
} else {
return `Duration { Invalid, reason: ${this.invalidReason} }`;
}
}
/**
* Returns an milliseconds value of this Duration.

@@ -732,3 +745,3 @@ * @return {number}

* - negative lower-order units are converted to higher order units (there must be such a higher order unit, otherwise
* the overall value would be negative, see second example)
* the overall value would be negative, see third example)
* - fractional values for higher-order units are converted to lower-order units (if possible, see fourth example)

@@ -735,0 +748,0 @@ *

@@ -9,4 +9,6 @@ import {

isInteger,
isUndefined,
} from "./util.js";
import Invalid from "./invalid.js";
import { ConflictingSpecificationError } from "../errors.js";

@@ -23,3 +25,3 @@ const nonLeapLadder = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],

function dayOfWeek(year, month, day) {
export function dayOfWeek(year, month, day) {
const d = new Date(Date.UTC(year, month - 1, day));

@@ -47,2 +49,6 @@

export function isoWeekdayToLocal(isoWeekday, startOfWeek) {
return ((isoWeekday - startOfWeek + 7) % 7) + 1;
}
/**

@@ -52,8 +58,8 @@ * @private

export function gregorianToWeek(gregObj) {
export function gregorianToWeek(gregObj, minDaysInFirstWeek = 4, startOfWeek = 1) {
const { year, month, day } = gregObj,
ordinal = computeOrdinal(year, month, day),
weekday = dayOfWeek(year, month, day);
weekday = isoWeekdayToLocal(dayOfWeek(year, month, day), startOfWeek);
let weekNumber = Math.floor((ordinal - weekday + 10) / 7),
let weekNumber = Math.floor((ordinal - weekday + 14 - minDaysInFirstWeek) / 7),
weekYear;

@@ -63,4 +69,4 @@

weekYear = year - 1;
weekNumber = weeksInWeekYear(weekYear);
} else if (weekNumber > weeksInWeekYear(year)) {
weekNumber = weeksInWeekYear(weekYear, minDaysInFirstWeek, startOfWeek);
} else if (weekNumber > weeksInWeekYear(year, minDaysInFirstWeek, startOfWeek)) {
weekYear = year + 1;

@@ -75,8 +81,8 @@ weekNumber = 1;

export function weekToGregorian(weekData) {
export function weekToGregorian(weekData, minDaysInFirstWeek = 4, startOfWeek = 1) {
const { weekYear, weekNumber, weekday } = weekData,
weekdayOfJan4 = dayOfWeek(weekYear, 1, 4),
weekdayOfJan4 = isoWeekdayToLocal(dayOfWeek(weekYear, 1, minDaysInFirstWeek), startOfWeek),
yearInDays = daysInYear(weekYear);
let ordinal = weekNumber * 7 + weekday - weekdayOfJan4 - 3,
let ordinal = weekNumber * 7 + weekday - weekdayOfJan4 - 7 + minDaysInFirstWeek,
year;

@@ -110,5 +116,44 @@

export function hasInvalidWeekData(obj) {
/**
* Check if local week units like localWeekday are used in obj.
* If so, validates that they are not mixed with ISO week units and then copies them to the normal week unit properties.
* Modifies obj in-place!
* @param obj the object values
*/
export function usesLocalWeekValues(obj, loc) {
const hasLocaleWeekData =
!isUndefined(obj.localWeekday) ||
!isUndefined(obj.localWeekNumber) ||
!isUndefined(obj.localWeekYear);
if (hasLocaleWeekData) {
const hasIsoWeekData =
!isUndefined(obj.weekday) || !isUndefined(obj.weekNumber) || !isUndefined(obj.weekYear);
if (hasIsoWeekData) {
throw new ConflictingSpecificationError(
"Cannot mix locale-based week fields with ISO-based week fields"
);
}
if (!isUndefined(obj.localWeekday)) obj.weekday = obj.localWeekday;
if (!isUndefined(obj.localWeekNumber)) obj.weekNumber = obj.localWeekNumber;
if (!isUndefined(obj.localWeekYear)) obj.weekYear = obj.localWeekYear;
delete obj.localWeekday;
delete obj.localWeekNumber;
delete obj.localWeekYear;
return {
minDaysInFirstWeek: loc.getMinDaysInFirstWeek(),
startOfWeek: loc.getStartOfWeek(),
};
} else {
return { minDaysInFirstWeek: 4, startOfWeek: 1 };
}
}
export function hasInvalidWeekData(obj, minDaysInFirstWeek = 4, startOfWeek = 1) {
const validYear = isInteger(obj.weekYear),
validWeek = integerBetween(obj.weekNumber, 1, weeksInWeekYear(obj.weekYear)),
validWeek = integerBetween(
obj.weekNumber,
1,
weeksInWeekYear(obj.weekYear, minDaysInFirstWeek, startOfWeek)
),
validWeekday = integerBetween(obj.weekday, 1, 7);

@@ -119,3 +164,3 @@

} else if (!validWeek) {
return unitOutOfRange("week", obj.week);
return unitOutOfRange("week", obj.weekNumber);
} else if (!validWeekday) {

@@ -122,0 +167,0 @@ return unitOutOfRange("weekday", obj.weekday);

@@ -340,2 +340,10 @@ import * as English from "./english.js";

return this.num(dt.weekNumber, 2);
case "n":
return this.num(dt.localWeekNumber);
case "nn":
return this.num(dt.localWeekNumber, 2);
case "ii":
return this.num(dt.localWeekYear.toString().slice(-2), 2);
case "iiii":
return this.num(dt.localWeekYear, 4);
case "o":

@@ -342,0 +350,0 @@ return this.num(dt.ordinal);

@@ -1,2 +0,2 @@

import { padStart, roundTo, hasRelative, formatOffset } from "./util.js";
import { hasLocaleWeekInfo, hasRelative, padStart, roundTo, validateWeekSettings } from "./util.js";
import * as English from "./english.js";

@@ -64,2 +64,14 @@ import Settings from "../settings.js";

let weekInfoCache = {};
function getCachedWeekInfo(locString) {
let data = weekInfoCache[locString];
if (!data) {
const locale = new Intl.Locale(locString);
// browsers currently implement this as a property, but spec says it should be a getter function
data = "getWeekInfo" in locale ? locale.getWeekInfo() : locale.weekInfo;
weekInfoCache[locString] = data;
}
return data;
}
function parseLocaleString(localeStr) {

@@ -309,2 +321,8 @@ // I really want to avoid writing a BCP 47 parser

const fallbackWeekSettings = {
firstDay: 1,
minimalDays: 4,
weekend: [6, 7],
};
/**

@@ -316,6 +334,12 @@ * @private

static fromOpts(opts) {
return Locale.create(opts.locale, opts.numberingSystem, opts.outputCalendar, opts.defaultToEN);
return Locale.create(
opts.locale,
opts.numberingSystem,
opts.outputCalendar,
opts.weekSettings,
opts.defaultToEN
);
}
static create(locale, numberingSystem, outputCalendar, defaultToEN = false) {
static create(locale, numberingSystem, outputCalendar, weekSettings, defaultToEN = false) {
const specifiedLocale = locale || Settings.defaultLocale;

@@ -326,3 +350,4 @@ // the system locale is useful for human readable strings but annoying for parsing/formatting known formats

const outputCalendarR = outputCalendar || Settings.defaultOutputCalendar;
return new Locale(localeR, numberingSystemR, outputCalendarR, specifiedLocale);
const weekSettingsR = validateWeekSettings(weekSettings) || Settings.defaultWeekSettings;
return new Locale(localeR, numberingSystemR, outputCalendarR, weekSettingsR, specifiedLocale);
}

@@ -337,7 +362,7 @@

static fromObject({ locale, numberingSystem, outputCalendar } = {}) {
return Locale.create(locale, numberingSystem, outputCalendar);
static fromObject({ locale, numberingSystem, outputCalendar, weekSettings } = {}) {
return Locale.create(locale, numberingSystem, outputCalendar, weekSettings);
}
constructor(locale, numbering, outputCalendar, specifiedLocale) {
constructor(locale, numbering, outputCalendar, weekSettings, specifiedLocale) {
const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] = parseLocaleString(locale);

@@ -348,2 +373,3 @@

this.outputCalendar = outputCalendar || parsedOutputCalendar || null;
this.weekSettings = weekSettings;
this.intl = intlConfigString(this.locale, this.numberingSystem, this.outputCalendar);

@@ -384,2 +410,3 @@

alts.outputCalendar || this.outputCalendar,
validateWeekSettings(alts.weekSettings) || this.weekSettings,
alts.defaultToEN || false

@@ -493,2 +520,24 @@ );

getWeekSettings() {
if (this.weekSettings) {
return this.weekSettings;
} else if (!hasLocaleWeekInfo()) {
return fallbackWeekSettings;
} else {
return getCachedWeekInfo(this.locale);
}
}
getStartOfWeek() {
return this.getWeekSettings().firstDay;
}
getMinDaysInFirstWeek() {
return this.getWeekSettings().minimalDays;
}
getWeekendDays() {
return this.getWeekSettings().weekend;
}
equals(other) {

@@ -495,0 +544,0 @@ return (

@@ -9,2 +9,3 @@ /*

import Settings from "../settings.js";
import { dayOfWeek, isoWeekdayToLocal } from "./conversions.js";

@@ -47,2 +48,14 @@ /**

export function hasLocaleWeekInfo() {
try {
return (
typeof Intl !== "undefined" &&
!!Intl.Locale &&
("weekInfo" in Intl.Locale.prototype || "getWeekInfo" in Intl.Locale.prototype)
);
} catch (e) {
return false;
}
}
// OBJECTS AND ARRAYS

@@ -81,2 +94,24 @@

export function validateWeekSettings(settings) {
if (settings == null) {
return null;
} else if (typeof settings !== "object") {
throw new InvalidArgumentError("Week settings must be an object");
} else {
if (
!integerBetween(settings.firstDay, 1, 7) ||
!integerBetween(settings.minimalDays, 1, 7) ||
!Array.isArray(settings.weekend) ||
settings.weekend.some((v) => !integerBetween(v, 1, 7))
) {
throw new InvalidArgumentError("Invalid week settings");
}
return {
firstDay: settings.firstDay,
minimalDays: settings.minimalDays,
weekend: Array.from(settings.weekend),
};
}
}
// NUMBERS AND STRINGS

@@ -180,14 +215,14 @@

export function weeksInWeekYear(weekYear) {
const p1 =
(weekYear +
Math.floor(weekYear / 4) -
Math.floor(weekYear / 100) +
Math.floor(weekYear / 400)) %
7,
last = weekYear - 1,
p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) + Math.floor(last / 400)) % 7;
return p1 === 4 || p2 === 3 ? 53 : 52;
// adapted from moment.js: https://github.com/moment/moment/blob/000ac1800e620f770f4eb31b5ae908f6167b0ab2/src/lib/units/week-calendar-utils.js
function firstWeekOffset(year, minDaysInFirstWeek, startOfWeek) {
const fwdlw = isoWeekdayToLocal(dayOfWeek(year, 1, minDaysInFirstWeek), startOfWeek);
return -fwdlw + minDaysInFirstWeek - 1;
}
export function weeksInWeekYear(weekYear, minDaysInFirstWeek = 4, startOfWeek = 1) {
const weekOffset = firstWeekOffset(weekYear, minDaysInFirstWeek, startOfWeek);
const weekOffsetNext = firstWeekOffset(weekYear + 1, minDaysInFirstWeek, startOfWeek);
return (daysInYear(weekYear) - weekOffset + weekOffsetNext) / 7;
}
export function untruncateYear(year) {

@@ -194,0 +229,0 @@ if (year > 99) {

@@ -7,3 +7,3 @@ import DateTime from "./datetime.js";

import { hasRelative } from "./impl/util.js";
import { hasLocaleWeekInfo, hasRelative } from "./impl/util.js";

@@ -53,2 +53,37 @@ /**

/**
* Get the weekday on which the week starts according to the given locale.
* @param {Object} opts - options
* @param {string} [opts.locale] - the locale code
* @param {string} [opts.locObj=null] - an existing locale object to use
* @returns {number} the start of the week, 1 for Monday through 7 for Sunday
*/
static getStartOfWeek({ locale = null, locObj = null } = {}) {
return (locObj || Locale.create(locale)).getStartOfWeek();
}
/**
* Get the minimum number of days necessary in a week before it is considered part of the next year according
* to the given locale.
* @param {Object} opts - options
* @param {string} [opts.locale] - the locale code
* @param {string} [opts.locObj=null] - an existing locale object to use
* @returns {number}
*/
static getMinimumDaysInFirstWeek({ locale = null, locObj = null } = {}) {
return (locObj || Locale.create(locale)).getMinDaysInFirstWeek();
}
/**
* Get the weekdays, which are considered the weekend according to the given locale
* @param {Object} opts - options
* @param {string} [opts.locale] - the locale code
* @param {string} [opts.locObj=null] - an existing locale object to use
* @returns {number[]} an array of weekdays, 1 for Monday through 7 for Sunday
*/
static getWeekendWeekdays({ locale = null, locObj = null } = {}) {
// copy the array, because we cache it internally
return (locObj || Locale.create(locale)).getWeekendDays().slice();
}
/**
* Return an array of standalone month names.

@@ -165,8 +200,9 @@ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

* * `relative`: whether this environment supports relative time formatting
* @example Info.features() //=> { relative: false }
* * `localeWeek`: whether this environment supports different weekdays for the start of the week based on the locale
* @example Info.features() //=> { relative: false, localeWeek: true }
* @return {Object}
*/
static features() {
return { relative: hasRelative() };
return { relative: hasRelative(), localeWeek: hasLocaleWeekInfo() };
}
}

@@ -237,8 +237,16 @@ import DateTime, { friendlyDateTime } from "./datetime.js";

* @param {string} [unit='milliseconds'] - the unit of time to count.
* @param {Object} opts - options
* @param {boolean} [opts.useLocaleWeeks=false] - If true, use weeks based on the locale, i.e. use the locale-dependent start of the week; this operation will always use the locale of the start DateTime
* @return {number}
*/
count(unit = "milliseconds") {
count(unit = "milliseconds", opts) {
if (!this.isValid) return NaN;
const start = this.start.startOf(unit),
end = this.end.startOf(unit);
const start = this.start.startOf(unit, opts);
let end;
if (opts?.useLocaleWeeks) {
end = this.end.reconfigure({ locale: start.locale });
} else {
end = this.end;
}
end = end.startOf(unit, opts);
return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf());

@@ -316,3 +324,3 @@ }

.filter((d) => this.contains(d))
.sort(),
.sort((a, b) => a.toMillis() - b.toMillis()),
results = [];

@@ -537,2 +545,14 @@ let { s } = this,

/**
* Returns a string representation of this Interval appropriate for the REPL.
* @return {string}
*/
[Symbol.for("nodejs.util.inspect.custom")]() {
if (this.isValid) {
return `Interval { start: ${this.s.toISO()}, end: ${this.e.toISO()} }`;
} else {
return `Interval { Invalid, reason: ${this.invalidReason} }`;
}
}
/**
* Returns a localized string representing this Interval. Accepts the same options as the

@@ -539,0 +559,0 @@ * Intl.DateTimeFormat constructor and any presets defined by Luxon, such as

@@ -12,3 +12,3 @@ import DateTime from "./datetime.js";

const VERSION = "3.4.3";
const VERSION = "3.4.4";

@@ -15,0 +15,0 @@ export {

{
"type": "module",
"version": "3.4.3"
"version": "3.4.4"
}

@@ -6,2 +6,3 @@ import SystemZone from "./zones/systemZone.js";

import { normalizeZone } from "./impl/zoneUtil.js";
import { validateWeekSettings } from "./impl/util.js";

@@ -14,3 +15,4 @@ let now = () => Date.now(),

twoDigitCutoffYear = 60,
throwOnInvalid;
throwOnInvalid,
defaultWeekSettings = null;

@@ -107,2 +109,27 @@ /**

/**
* @typedef {Object} WeekSettings
* @property {number} firstDay
* @property {number} minimalDays
* @property {number[]} weekend
*/
/**
* @return {WeekSettings|null}
*/
static get defaultWeekSettings() {
return defaultWeekSettings;
}
/**
* Allows overriding the default locale week settings, i.e. the start of the week, the weekend and
* how many days are required in the first week of a year.
* Does not affect existing instances.
*
* @param {WeekSettings|null} weekSettings
*/
static set defaultWeekSettings(weekSettings) {
defaultWeekSettings = validateWeekSettings(weekSettings);
}
/**
* Get the cutoff year after which a string encoding a year as two digits is interpreted to occur in the current century.

@@ -109,0 +136,0 @@ * @type {number}

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc