Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit a93a815

Browse files
Implemented weekdayRange function
1 parent e0ddf63 commit a93a815

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

test/weekdayRange.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,41 @@
55

66
var assert = require('assert');
77
var weekdayRange = require('../weekdayRange');
8+
var vanillaGetUTCDay = Date.prototype.getUTCDay;
9+
var vanillaGetDay = Date.prototype.getDay;
810

9-
describe('weekdayRange(wd1, wd2, gmt)', function () {
11+
describe('hooks', function() {
1012

11-
var tests = [
12-
];
13+
before(function() {
14+
Date.prototype.getDay = function() { return 6; } // Setting local weekday as SAT(6)
15+
Date.prototype.getUTCDay = function() { return 5; } // Setting UTC weekday as FRI(5)
16+
});
1317

14-
tests.forEach(function (test) {
15-
var expected = test.pop();
16-
it('should return `' + expected +'` for "' + test.join('", "') + '"', function () {
17-
assert.equal(expected, weekdayRange(test[0], test[1], test[2]));
18-
});
18+
after(function() {
19+
Date.prototype.getUTCDay = vanillaGetUTCDay;
20+
Date.prototype.getDay = vanillaGetDay;
1921
});
2022

23+
describe('weekdayRange(wd1, wd2, gmt)', function () {
24+
25+
var tests = [
26+
["MON", "FRI", false],
27+
["MON", "FRI", "GMT", true],
28+
["SAT", true],
29+
["SAT", "GMT", false],
30+
["FRI", "MON", true],
31+
["SAT", "MON", "GMT", false],
32+
["SOME", "RANDOM", false],
33+
["RANDOM", false],
34+
["RANDOM", "VALUE", "IST", false],
35+
];
36+
37+
tests.forEach(function (test) {
38+
var expected = test.pop();
39+
it('should return `' + expected +'` for "' + test.join('", "') + '"', function () {
40+
assert.equal(expected, weekdayRange(test[0], test[1], test[2]));
41+
});
42+
});
43+
44+
});
2145
});

weekdayRange.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,33 @@ module.exports = weekdayRange;
4848
* @return {Boolean}
4949
*/
5050

51+
const dayOrder = { "SUN": 0, "MON": 1, "TUE": 2, "WED": 3, "THU": 4, "FRI": 5, "SAT": 6 };
52+
5153
function weekdayRange (wd1, wd2, gmt) {
52-
// TODO: implement me!
53-
return false;
54+
55+
var useGMTzone = (wd2 == "GMT" || gmt == "GMT"),
56+
todaysDay = getTodaysDay(useGMTzone),
57+
wd1Index = dayOrder[wd1] || -1,
58+
wd2Index = dayOrder[wd2] || -1,
59+
result = false;
60+
61+
if (wd2Index < 0) {
62+
result = (todaysDay == wd1Index);
63+
} else {
64+
if (wd1Index <= wd2Index) {
65+
result = valueInRange(wd1Index, todaysDay, wd2Index);
66+
} else {
67+
result = valueInRange(wd1Index, todaysDay, 6) || valueInRange(0, todaysDay, wd2Index);
68+
}
69+
}
70+
return result;
71+
}
72+
73+
function getTodaysDay (gmt) {
74+
return (gmt ? (new Date()).getUTCDay() : (new Date()).getDay());
75+
}
76+
77+
// start <= value <= finish
78+
function valueInRange (start, value, finish) {
79+
return (start <= value) && (value <= finish);
5480
}

0 commit comments

Comments
 (0)