aboutsummaryrefslogtreecommitdiff
path: root/date-fns/src/_lib/setUTCISODay
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/_lib/setUTCISODay
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/_lib/setUTCISODay')
-rw-r--r--date-fns/src/_lib/setUTCISODay/index.js27
-rw-r--r--date-fns/src/_lib/setUTCISODay/test.js79
2 files changed, 106 insertions, 0 deletions
diff --git a/date-fns/src/_lib/setUTCISODay/index.js b/date-fns/src/_lib/setUTCISODay/index.js
new file mode 100644
index 0000000..cbd2fbc
--- /dev/null
+++ b/date-fns/src/_lib/setUTCISODay/index.js
@@ -0,0 +1,27 @@
+import toInteger from '../toInteger/index'
+import toDate from '../../toDate/index'
+import requiredArgs from '../requiredArgs/index'
+
+// This function will be a part of public API when UTC function will be implemented.
+// See issue: https://github.com/date-fns/date-fns/issues/376
+export default function setUTCISODay(dirtyDate, dirtyDay) {
+ requiredArgs(2, arguments)
+
+ var day = toInteger(dirtyDay)
+
+ if (day % 7 === 0) {
+ day = day - 7
+ }
+
+ var weekStartsOn = 1
+ var date = toDate(dirtyDate)
+ var currentDay = date.getUTCDay()
+
+ var remainder = day % 7
+ var dayIndex = (remainder + 7) % 7
+
+ var diff = (dayIndex < weekStartsOn ? 7 : 0) + day - currentDay
+
+ date.setUTCDate(date.getUTCDate() + diff)
+ return date
+}
diff --git a/date-fns/src/_lib/setUTCISODay/test.js b/date-fns/src/_lib/setUTCISODay/test.js
new file mode 100644
index 0000000..11b6019
--- /dev/null
+++ b/date-fns/src/_lib/setUTCISODay/test.js
@@ -0,0 +1,79 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import setUTCISODay from '.'
+
+describe('setUTCISODay', function () {
+ it('sets the day of the ISO week', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 3)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 3)))
+ })
+
+ it('sets the day to Sunday of this ISO week if the index is 7', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 7)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 7)))
+ })
+
+ describe('the day index is more than 7', function () {
+ it('sets the day of the next ISO week', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 8)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 8)))
+ })
+
+ it('sets the day of another ISO week in the future', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 21)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 21)))
+ })
+ })
+
+ describe('the day index is less than 1', function () {
+ it('sets the day of the last ISO week', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 0)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 7 /* Aug */, 31)))
+ })
+
+ it('set the day of another ISO week in the past', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), -13)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 7 /* Aug */, 18)))
+ })
+ })
+
+ it('accepts a timestamp', function () {
+ var result = setUTCISODay(
+ new Date(Date.UTC(2014, 8 /* Sep */, 1)).getTime(),
+ 3
+ )
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 3)))
+ })
+
+ it('converts a fractional number to an integer', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), 3.33)
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 3)))
+ })
+
+ it('implicitly converts number arguments', function () {
+ var result = setUTCISODay(new Date(Date.UTC(2014, 8 /* Sep */, 1)), '3')
+ assert.deepEqual(result, new Date(Date.UTC(2014, 8 /* Sep */, 3)))
+ })
+
+ it('does not mutate the original date', function () {
+ var date = new Date(Date.UTC(2014, 8 /* Sep */, 1))
+ setUTCISODay(date, 3)
+ assert.deepEqual(date, new Date(Date.UTC(2014, 8 /* Sep */, 1)))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function () {
+ var result = setUTCISODay(new Date(NaN), 3)
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('returns `Invalid Date` if the given amount is NaN', function () {
+ var result = setUTCISODay(new Date(2014, 8 /* Sep */, 1), NaN)
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function () {
+ assert.throws(setUTCISODay.bind(null, 1), TypeError)
+ })
+})