summaryrefslogtreecommitdiff
path: root/date-fns/src/differenceInWeeks
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2021-08-23 16:46:06 -0300
committerSebastian <sebasjm@gmail.com>2021-08-23 16:48:30 -0300
commit38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch)
tree453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/differenceInWeeks
parentf26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff)
downloadnode-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip
added web vendorsHEADmaster
Diffstat (limited to 'date-fns/src/differenceInWeeks')
-rw-r--r--date-fns/src/differenceInWeeks/benchmark.js23
-rw-r--r--date-fns/src/differenceInWeeks/index.d.ts4
-rw-r--r--date-fns/src/differenceInWeeks/index.js.flow55
-rw-r--r--date-fns/src/differenceInWeeks/index.ts55
-rw-r--r--date-fns/src/differenceInWeeks/test.ts104
5 files changed, 241 insertions, 0 deletions
diff --git a/date-fns/src/differenceInWeeks/benchmark.js b/date-fns/src/differenceInWeeks/benchmark.js
new file mode 100644
index 0000000..c29909c
--- /dev/null
+++ b/date-fns/src/differenceInWeeks/benchmark.js
@@ -0,0 +1,23 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import differenceInWeeks from '.'
+import moment from 'moment'
+
+suite('differenceInWeeks', function () {
+ benchmark('date-fns', function () {
+ return differenceInWeeks(this.dateA, this.dateB)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.momentA.diff(this.momentB, 'week')
+ })
+}, {
+ setup: function () {
+ this.dateA = new Date()
+ this.momentA = moment()
+ this.dateB = new Date(this.dateA.getTime() + 604800000)
+ this.momentB = this.momentA.clone().add(7, 'days')
+ }
+})
diff --git a/date-fns/src/differenceInWeeks/index.d.ts b/date-fns/src/differenceInWeeks/index.d.ts
new file mode 100644
index 0000000..a42614c
--- /dev/null
+++ b/date-fns/src/differenceInWeeks/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { differenceInWeeks } from 'date-fns'
+export default differenceInWeeks
diff --git a/date-fns/src/differenceInWeeks/index.js.flow b/date-fns/src/differenceInWeeks/index.js.flow
new file mode 100644
index 0000000..c799819
--- /dev/null
+++ b/date-fns/src/differenceInWeeks/index.js.flow
@@ -0,0 +1,55 @@
+// @flow
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+export type Interval = {
+ start: Date | number,
+ end: Date | number,
+}
+
+export type Locale = {
+ code?: string,
+ formatDistance?: (...args: Array<any>) => any,
+ formatRelative?: (...args: Array<any>) => any,
+ localize?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ formatLong?: {
+ date: (...args: Array<any>) => any,
+ time: (...args: Array<any>) => any,
+ dateTime: (...args: Array<any>) => any,
+ },
+ match?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ options?: {
+ weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
+ firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
+ },
+}
+
+export type Duration = {
+ years?: number,
+ months?: number,
+ weeks?: number,
+ days?: number,
+ hours?: number,
+ minutes?: number,
+ seconds?: number,
+}
+
+export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6
+
+declare module.exports: (
+ dateLeft: Date | number,
+ dateRight: Date | number
+) => number
diff --git a/date-fns/src/differenceInWeeks/index.ts b/date-fns/src/differenceInWeeks/index.ts
new file mode 100644
index 0000000..350da2f
--- /dev/null
+++ b/date-fns/src/differenceInWeeks/index.ts
@@ -0,0 +1,55 @@
+import differenceInDays from '../differenceInDays/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name differenceInWeeks
+ * @category Week Helpers
+ * @summary Get the number of full weeks between the given dates.
+ *
+ * @description
+ * Get the number of full weeks between two dates. Fractional weeks are
+ * truncated towards zero.
+ *
+ * One "full week" is the distance between a local time in one day to the same
+ * local time 7 days earlier or later. A full week can sometimes be less than
+ * or more than 7*24 hours if a daylight savings change happens between two dates.
+ *
+ * To ignore DST and only measure exact 7*24-hour periods, use this instead:
+ * `Math.floor(differenceInHours(dateLeft, dateRight)/(7*24))|0`.
+ *
+ *
+ * ### v2.0.0 breaking changes:
+ *
+ * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
+ *
+ * @param {Date|Number} dateLeft - the later date
+ * @param {Date|Number} dateRight - the earlier date
+ * @returns {Number} the number of full weeks
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // How many full weeks are between 5 July 2014 and 20 July 2014?
+ * const result = differenceInWeeks(new Date(2014, 6, 20), new Date(2014, 6, 5))
+ * //=> 2
+ *
+ * // How many full weeks are between
+ * // 1 March 2020 0:00 and 6 June 2020 0:00 ?
+ * // Note: because local time is used, the
+ * // result will always be 8 weeks (54 days),
+ * // even if DST starts and the period has
+ * // only 54*24-1 hours.
+ * const result = differenceInWeeks(
+ * new Date(2020, 5, 1),
+ * new Date(2020, 2, 6)
+ * )
+ * //=> 8
+ */
+export default function differenceInWeeks(
+ dirtyDateLeft: Date | number,
+ dirtyDateRight: Date | number
+): number {
+ requiredArgs(2, arguments)
+
+ const diff = differenceInDays(dirtyDateLeft, dirtyDateRight) / 7
+ return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
+}
diff --git a/date-fns/src/differenceInWeeks/test.ts b/date-fns/src/differenceInWeeks/test.ts
new file mode 100644
index 0000000..6e8e05e
--- /dev/null
+++ b/date-fns/src/differenceInWeeks/test.ts
@@ -0,0 +1,104 @@
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import differenceInWeeks from '.'
+
+describe('differenceInWeeks', function () {
+ it('returns the number of full weeks between the given dates', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 6 /* Jul */, 8, 18, 0),
+ new Date(2014, 5 /* Jun */, 29, 6, 0)
+ )
+ assert(result === 1)
+ })
+
+ it('returns a negative number if the time value of the first date is smaller', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 5 /* Jun */, 29, 6, 0),
+ new Date(2014, 6 /* Jul */, 8, 18, 0)
+ )
+ assert(result === -1)
+ })
+
+ it('accepts timestamps', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 6 /* Jul */, 12).getTime(),
+ new Date(2014, 6 /* Jul */, 2).getTime()
+ )
+ assert(result === 1)
+ })
+
+ describe('edge cases', function () {
+ it('the difference is less than a week, but the given dates are in different calendar weeks', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 6 /* Jul */, 6),
+ new Date(2014, 6 /* Jul */, 5)
+ )
+ assert(result === 0)
+ })
+
+ it('the same for the swapped dates', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 6 /* Jul */, 5),
+ new Date(2014, 6 /* Jul */, 6)
+ )
+ assert(result === 0)
+ })
+
+ it('days of weeks of the given dates are the same', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 6 /* Jul */, 9),
+ new Date(2014, 6 /* Jul */, 2)
+ )
+ assert(result === 1)
+ })
+
+ it('the given dates are the same', function () {
+ const result = differenceInWeeks(
+ new Date(2014, 8 /* Sep */, 5, 0, 0),
+ new Date(2014, 8 /* Sep */, 5, 0, 0)
+ )
+ assert(result === 0)
+ })
+
+ it('does not return -0 when the given dates are the same', () => {
+ function isNegativeZero(x: number): boolean {
+ return x === 0 && 1 / x < 0
+ }
+
+ const result = differenceInWeeks(
+ new Date(2014, 8 /* Sep */, 5, 0, 0),
+ new Date(2014, 8 /* Sep */, 5, 0, 0)
+ )
+
+ const resultIsNegative = isNegativeZero(result)
+ assert(resultIsNegative === false)
+ })
+ })
+
+ it('returns NaN if the first date is `Invalid Date`', function () {
+ const result = differenceInWeeks(
+ new Date(NaN),
+ new Date(2017, 0 /* Jan */, 1)
+ )
+ assert(isNaN(result))
+ })
+
+ it('returns NaN if the second date is `Invalid Date`', function () {
+ const result = differenceInWeeks(
+ new Date(2017, 0 /* Jan */, 1),
+ new Date(NaN)
+ )
+ assert(isNaN(result))
+ })
+
+ it('returns NaN if the both dates are `Invalid Date`', function () {
+ const result = differenceInWeeks(new Date(NaN), new Date(NaN))
+ assert(isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function () {
+ assert.throws(differenceInWeeks.bind(null), TypeError)
+ assert.throws(differenceInWeeks.bind(null, 1), TypeError)
+ })
+})