UNPKG

3.62 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * This package exports a single function, {@link timeAgo},\n * which describes the time elapsed between a given date and the current date\n * in a human readable format (for example, `10 minutes ago`, `in 3 seconds`).\n *\n * @packageDocumentation\n */\n\n/**\n * `timeAgo` returns a string describing the time elapsed between\n * a given date and the current time at which the function is called.\n *\n * @remarks\n * `timeAgo` only supports the `en_US` locale.\n *\n * The following table describes `timeAgo`'s output.\n *\n * ```\n * | Time elapsed | Past output | Future output |\n * | --------------------- | ----------------- | ---------------- |\n * | < 1 second | `just now` | `just now` |\n * | < 1 minute | `N second(s) ago` | `in N second(s)` |\n * | < 1 hour | `N minute(s) ago` | `in N minute(s)` |\n * | < 1 day | `N hour(s) ago` | `in N hour(s)` |\n * | < 1 month (30.5 days) | `N day(s) ago` | `in N day(s)` |\n * | < 1 year (365 days) | `N month(s) ago` | `in N month(s)` |\n * | > 1 year | `N year(s) ago` | `in N year(s)` |\n * ```\n *\n * @example\n * Basic usage:\n *\n * ```typescript\n * import { timeAgo } from 'short-time-ago';\n *\n * const myDate = new Date();\n * const description = timeAgo(myDate);\n *\n * // Output: `just now`.\n * console.log(description);\n * ```\n *\n * @example\n * Specifying a custom current date with the `now` parameter:\n *\n * ```typescript\n * import { timeAgo } from 'short-time-ago';\n *\n * const myDate = new Date('2019-01-01T00:00:00.000Z');\n * const now = new Date('2019-01-01T00:01:00.000Z');\n * const description = timeAgo(myDate, now);\n *\n * // Output: `1 minute ago`.\n * console.log(description);\n * ```\n *\n * ```typescript\n * import { timeAgo } from 'short-time-ago';\n *\n * const myDate = new Date('2019-01-02T00:00:00.000Z');\n * const now = new Date('2019-01-01T00:00:00.000Z');\n * const description = timeAgo(myDate, now);\n *\n * // Output: `in 1 day`.\n * console.log(description);\n * ```\n *\n * @param date - a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}\n * @param now - the current date (optional, defaults to `new Date()`)\n */\nexport function timeAgo(date: Date, now?: Date): string {\n const units = [\n ['year', 365 * 24 * 60 * 60 * 1000],\n ['month', 30.5 * 24 * 60 * 60 * 1000],\n ['day', 24 * 60 * 60 * 1000],\n ['hour', 60 * 60 * 1000],\n ['minute', 60 * 1000],\n ['second', 1000],\n ] as const;\n\n const diff = (now || new Date()).getTime() - date.getTime();\n const elapsed = Math.abs(diff);\n\n for (const [name, size] of units) {\n const value = Math.floor(elapsed / size);\n if (value > 0) {\n const plural = value > 1 ? 's' : '';\n const description = `${value} ${name}${plural}`;\n return diff > 0 ? `${description} ago` : `in ${description}`;\n }\n }\n\n return 'just now';\n}\n"],"names":["date","now","diff","Date","getTime","elapsed","Math","abs","name","value","floor","description"],"mappings":"sCAsEwBA,EAAYC,GAahC,IAZA,IASMC,GAAQD,GAAO,IAAIE,MAAQC,UAAYJ,EAAKI,UAC5CC,EAAUC,KAAKC,IAAIL,SAVX,CACV,CAAC,OAAQ,SACT,CAAC,QAAS,SACV,CAAC,MAAO,OACR,CAAC,OAAQ,MACT,CAAC,SAAU,KACX,CAAC,SAAU,qBAMmB,YAAtBM,OACFC,EAAQH,KAAKI,MAAML,QACzB,GAAII,EAAQ,EAAG,CACX,IACME,EAAiBF,MAASD,GADjBC,EAAQ,EAAI,IAAM,IAEjC,OAAOP,EAAO,EAAOS,eAA0BA,GAIvD,MAAO"}
\No newline at end of file