schemaglobin
Advanced tools
Comparing version 5.1.1 to 5.2.0
@@ -36,7 +36,27 @@ /** Is a value a date? */ | ||
export declare const formatWhen: (targetDate: PossibleDate, currentDate?: PossibleDate) => string; | ||
/** Return when a date happened, e.g. `10 days` or `2 hours` */ | ||
/** | ||
* Return when a date happened, e.g. `10 days` or `2 hours` or `-1 week` | ||
* @param targetDate The date when the thing will happen. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
export declare const formatUntil: (targetDate: PossibleDate, currentDate?: PossibleDate) => string; | ||
/** Return when a date will happen, e.g. `10 days` or `2 hours` */ | ||
/** | ||
* Return a compact version of when a date happened, e.g. `10d` or `2h` or `-1w` | ||
* @param targetDate The date when the thing will happen. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
export declare const formatShortUntil: (targetDate: PossibleDate, currentDate?: PossibleDate) => string; | ||
/** | ||
* Return when a date will happen, e.g. `10 days` or `2 hours` or `-1 week` | ||
* @param targetDate The date when the thing happened. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
export declare const formatAgo: (targetDate: PossibleDate, currentDate?: PossibleDate) => string; | ||
/** | ||
* Return a compact version of when a date will happen, e.g. `10d` or `2h` or `-1w` | ||
* @param targetDate The date when the thing happened. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
export declare const formatShortAgo: (targetDate: PossibleDate, currentDate?: PossibleDate) => string; | ||
/** Format a date in the browser locale. */ | ||
export declare const formatDate: (date: PossibleDate, options?: Intl.DateTimeFormatOptions | undefined) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatDate = exports.formatAgo = exports.formatUntil = exports.formatWhen = exports.weeksAgo = exports.weeksUntil = exports.daysAgo = exports.daysUntil = exports.secondsAgo = exports.secondsUntil = exports.addHours = exports.addDays = exports.getMonday = exports.getMidnight = exports.getDow = exports.days = exports.getYmd = exports.makeDate = exports.isDate = void 0; | ||
exports.formatDate = exports.formatShortAgo = exports.formatAgo = exports.formatShortUntil = exports.formatUntil = exports.formatWhen = exports.weeksAgo = exports.weeksUntil = exports.daysAgo = exports.daysUntil = exports.secondsAgo = exports.secondsUntil = exports.addHours = exports.addDays = exports.getMonday = exports.getMidnight = exports.getDow = exports.days = exports.getYmd = exports.makeDate = exports.isDate = void 0; | ||
const number_1 = require("./number"); | ||
/** Is a value a date? */ | ||
@@ -57,3 +58,7 @@ exports.isDate = (v) => v instanceof Date; | ||
exports.weeksAgo = (targetDate, currentDate = new Date()) => 0 - exports.weeksUntil(targetDate, currentDate); | ||
/** Get information about the difference between two dates. */ | ||
/** | ||
* Get information about the difference between two dates. | ||
* - Used by `formatWhen()` and `formatAgo()` etc | ||
* @returns Tuple in `[amount, units]` format, e.g. `[3, "days"]` or `[-16, "hours"]` or `[1, "week"]` | ||
*/ | ||
const diffDates = (targetDate, currentDate = new Date()) => { | ||
@@ -63,19 +68,31 @@ const seconds = (exports.makeDate(targetDate).getTime() - exports.makeDate(currentDate).getTime()) / 1000; | ||
// Up to 99 seconds, e.g. '22 seconds ago' | ||
if (abs < 99) | ||
return [Math.round(seconds), "second"]; | ||
if (abs < 99) { | ||
const num = Math.round(seconds); | ||
return [num, num === 1 ? "second" : "seconds"]; | ||
} | ||
// Up to one hour — show minutes, e.g. '18 minutes ago' | ||
if (abs < 3600) | ||
return [Math.round(seconds / 60), "minute"]; | ||
if (abs < 3600) { | ||
const num = Math.round(seconds / 60); | ||
return [num, num === 1 ? "minute" : "minutes"]; | ||
} | ||
// Up to 24 hours — show hours, e.g. '23 hours ago' | ||
if (abs < 86400) | ||
return [Math.round(seconds / 3600), "hour"]; | ||
if (abs < 86400) { | ||
const num = Math.round(seconds / 3600); | ||
return [num, num === 1 ? "hour" : "hours"]; | ||
} | ||
// Up to 2 weeks — show days, e.g. '13 days ago' | ||
if (abs < 1209600) | ||
return [Math.round(seconds / 86400), "day"]; | ||
if (abs < 1209600) { | ||
const num = Math.round(seconds / 86400); | ||
return [num, num === 1 ? "day" : "days"]; | ||
} | ||
// Up to 2 months — show weeks, e.g. '6 weeks ago' | ||
if (abs < 5184000) | ||
return [Math.round(seconds / 604800), "week"]; | ||
if (abs < 5184000) { | ||
const num = Math.round(seconds / 604800); | ||
return [num, num === 1 ? "week" : "weeks"]; | ||
} | ||
// Up to 18 months — show months, e.g. '6 months ago' | ||
if (abs < 46656000) | ||
return [Math.round(seconds / 2592000), "month"]; | ||
if (abs < 46656000) { | ||
const num = Math.round(seconds / 2592000); | ||
return [num, num === 1 ? "month" : "months"]; | ||
} | ||
// Above 18 months — show years, e.g. '2 years ago' | ||
@@ -87,4 +104,4 @@ return [Math.round(seconds / 31536000), "year"]; | ||
const [amount, unit] = diffDates(targetDate, currentDate); | ||
// Special case for equality. | ||
if (unit === "second" && amount > -20 && amount < 20) | ||
// Special case for rough equality. | ||
if (unit === "second" && amount > -30 && amount < 30) | ||
return "just now"; | ||
@@ -94,19 +111,42 @@ // Return either `in 22 days` or `1 hour ago` | ||
const abs = Math.abs(amount); | ||
const units = `${unit}${abs === 1 ? "" : "s"}`; | ||
return future ? `in ${abs} ${units}` : `${abs} ${units} ago`; | ||
const str = number_1.formatNumber(abs); | ||
return future ? `in ${str} ${unit}` : `${str} ${unit} ago`; | ||
}; | ||
/** Return when a date happened, e.g. `10 days` or `2 hours` */ | ||
/** | ||
* Return when a date happened, e.g. `10 days` or `2 hours` or `-1 week` | ||
* @param targetDate The date when the thing will happen. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
exports.formatUntil = (targetDate, currentDate = new Date()) => { | ||
const [amount, unit] = diffDates(targetDate, currentDate); | ||
const units = `${unit}${amount === 1 ? "" : "s"}`; | ||
return `${amount} ${units}`; | ||
const [amount, units] = diffDates(targetDate, currentDate); | ||
return `${number_1.formatNumber(amount)} ${units}`; | ||
}; | ||
/** Return when a date will happen, e.g. `10 days` or `2 hours` */ | ||
/** | ||
* Return a compact version of when a date happened, e.g. `10d` or `2h` or `-1w` | ||
* @param targetDate The date when the thing will happen. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
exports.formatShortUntil = (targetDate, currentDate = new Date()) => { | ||
const [amount, units] = diffDates(targetDate, currentDate); | ||
return `${number_1.formatNumber(amount)}${units.substr(0, 1)}`; | ||
}; | ||
/** | ||
* Return when a date will happen, e.g. `10 days` or `2 hours` or `-1 week` | ||
* @param targetDate The date when the thing happened. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
exports.formatAgo = (targetDate, currentDate = new Date()) => { | ||
const [amount, unit] = diffDates(targetDate, currentDate); | ||
const invert = 0 - amount; | ||
const units = `${unit}${invert === 1 ? "" : "s"}`; | ||
return `${invert} ${units}`; | ||
const [amount, units] = diffDates(currentDate, targetDate); | ||
return `${number_1.formatNumber(amount)} ${units}`; | ||
}; | ||
/** | ||
* Return a compact version of when a date will happen, e.g. `10d` or `2h` or `-1w` | ||
* @param targetDate The date when the thing happened. | ||
* @param currentDate Today's date (or a different date to measure from). | ||
*/ | ||
exports.formatShortAgo = (targetDate, currentDate = new Date()) => { | ||
const [amount, units] = diffDates(currentDate, targetDate); | ||
return `${number_1.formatNumber(amount)} ${units}`; | ||
}; | ||
/** Format a date in the browser locale. */ | ||
exports.formatDate = (date, options) => new Intl.DateTimeFormat(undefined, options).format(exports.makeDate(date)); |
{ | ||
"name": "schemaglobin", | ||
"description": "Validate user-entered data.", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"repository": "https://github.com/dhoulb/schemaglobin", | ||
@@ -6,0 +6,0 @@ "author": "Dave Houlbrooke <dave@shax.com>", |
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
149214
2788