Repository: iamkun/dayjs
Pull request #270 (2018-07-06 13:16:13)

Add setting the day of the week

codecov-io: # [Codecov](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=h1) Report
> Merging [#270](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=desc) into [master](https://codecov.io/gh/iamkun/dayjs/commit/89cdce1f3165041cdb15410b9450ca84d1ae1c7d?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/iamkun/dayjs/pull/270/graphs/tree.svg?width=650&src=pr&token=3SZslYi4Y3&height=150)](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #270   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          30     30           
  Lines         376    383    +7     
  Branches       52     52           
=====================================
+ Hits          376    383    +7
```


| [Impacted Files](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.js](https://codecov.io/gh/iamkun/dayjs/pull/270/diff?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100% <100%> (ø)` | :arrow_up: |
| [src/utils.js](https://codecov.io/gh/iamkun/dayjs/pull/270/diff?src=pr&el=tree#diff-c3JjL3V0aWxzLmpz) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=footer). Last update [89cdce1...de603a2](https://codecov.io/gh/iamkun/dayjs/pull/270?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

iamkun: @bas080 Great, thanks, I missed this. Plus, would you mind change this commit to a bugfix: instead of feat: cause a feat: would trigger a minor update

Patch:
diff --git a/src/index.js b/src/index.js
index 2c79674..0972982 100644
--- a/src/index.js
+++ b/src/index.js
@@ -195,6 +195,9 @@ class Dayjs {
   $set(units, int) { // private set
     const unit = Utils.prettyUnit(units)
     switch (unit) {
+      case C.D:
+        Utils.setDay(this.$d, int)
+        break
       case C.DATE:
         this.$d.setDate(int)
         break
diff --git a/src/utils.js b/src/utils.js
index f993ae9..a70a09d 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -40,7 +40,17 @@ const prettyUnit = (u) => {
 
 const isUndefined = s => s === undefined
 
+const setDay = (date, day) => {
+  const currentDay = date.getDay()
+  const distance = day - currentDay
+
+  date.setDate(date.getDate() + distance)
+
+  return date
+}
+
 export default {
+  setDay,
   padStart,
   padZoneStr,
   monthDiff,


Test patch:
diff --git a/test/get-set.test.js b/test/get-set.test.js
index db3ab75..af5676a 100644
--- a/test/get-set.test.js
+++ b/test/get-set.test.js
@@ -46,6 +46,10 @@ it('Set Day', () => {
   expect(dayjs().set('date', 30).valueOf()).toBe(moment().set('date', 30).valueOf())
 })
 
+it('Set Day of Week', () => {
+  expect(dayjs().set('day', 0).valueOf()).toBe(moment().set('day', 0).valueOf())
+})
+
 it('Set Month', () => {
   expect(dayjs().set('month', 11).valueOf()).toBe(moment().set('month', 11).valueOf())
 })
diff --git a/test/utils.test.js b/test/utils.test.js
index 5a4dbaa..b66f52e 100644
--- a/test/utils.test.js
+++ b/test/utils.test.js
@@ -26,3 +26,9 @@ it('PadStart', () => {
   expect(padStart(1, 2, '0')).toBe('01')
   expect(padStart(0, 2, '0')).toBe('00')
 })
+
+it('SetDate', () => {
+  const day = Math.floor((Math.random() * 7) % 6)
+
+  expect(Utils.setDay((new Date()), day).getDay()).toBe(day)
+})