Skip to content

Commit e6a657f

Browse files
committed
modernize js
prefer const and let to var; prefer arrow functions; prefer strict comparison; guarantee reference to `this` in `each` method using second parameter selfRef
1 parent 9c4a7bd commit e6a657f

File tree

1 file changed

+60
-71
lines changed

1 file changed

+60
-71
lines changed

jqClock.js

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@
3333
//THE FOLLOWING EXTENSION OF THE DATE PROTOTYPE WAS TAKEN FROM: https://stackoverflow.com/a/26778394/394921
3434
if (!Date.prototype.hasOwnProperty("stdTimezoneOffset")) {
3535
Date.prototype.stdTimezoneOffset = function () {
36-
var fy = this.getFullYear();
36+
const fy = this.getFullYear();
3737
if (!Date.prototype.stdTimezoneOffset.cache.hasOwnProperty(fy)) {
38-
var maxOffset = new Date(fy, 0, 1).getTimezoneOffset();
39-
var monthsTestOrder = [6, 7, 5, 8, 4, 9, 3, 10, 2, 11, 1];
40-
41-
for (var mi = 0; mi < 12; mi++) {
42-
var offset = new Date(
43-
fy,
44-
monthsTestOrder[mi],
45-
1
46-
).getTimezoneOffset();
47-
if (offset != maxOffset) {
38+
let maxOffset = new Date(fy, 0, 1).getTimezoneOffset();
39+
const monthsTestOrder = [6, 7, 5, 8, 4, 9, 3, 10, 2, 11, 1];
40+
41+
for (let mi = 0; mi < 12; mi++) {
42+
const offset = new Date(fy, monthsTestOrder[mi], 1).getTimezoneOffset();
43+
if (offset !== maxOffset) {
4844
maxOffset = Math.max(maxOffset, offset);
4945
break;
5046
}
@@ -63,27 +59,28 @@ if (!Date.prototype.hasOwnProperty("isDST")) {
6359
if (!Date.prototype.hasOwnProperty("isLeapYear")) {
6460
//source: https://stackoverflow.com/a/26426761/394921
6561
Date.prototype.isLeapYear = function () {
66-
var year = this.getFullYear();
67-
if ((year & 3) != 0) return false;
68-
return year % 100 != 0 || year % 400 == 0;
62+
const year = this.getFullYear();
63+
if ((year & 3) !== 0) return false;
64+
return year % 100 !== 0 || year % 400 === 0;
6965
};
7066
}
7167
if (!Date.prototype.hasOwnProperty("getDOY")) {
7268
// Get Day of Year
73-
//source: https://stackoverflow.com/a/26426761/394921
74-
//maybe can use the solution [here](https://stackoverflow.com/a/28919172/394921) also: Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
69+
// source: https://stackoverflow.com/a/26426761/394921
70+
// maybe can use the solution [here](https://stackoverflow.com/a/28919172/394921) also:
71+
// Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
7572
Date.prototype.getDOY = function () {
76-
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
77-
var mn = this.getMonth();
78-
var dn = this.getDate();
79-
var dayOfYear = dayCount[mn] + dn;
73+
const dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
74+
const mn = this.getMonth();
75+
const dn = this.getDate();
76+
let dayOfYear = dayCount[mn] + dn;
8077
if (mn > 1 && this.isLeapYear()) dayOfYear++;
8178
return dayOfYear;
8279
};
8380
}
8481
if (!Date.prototype.hasOwnProperty("daysInMonth")) {
85-
//Get number of days in the current month
86-
//source: https://stackoverflow.com/questions/1184334/get-number-days-in-a-specified-month-using-javascript#comment36681053_1464716
82+
// Get number of days in the current month
83+
// source: https://stackoverflow.com/questions/1184334/get-number-days-in-a-specified-month-using-javascript#comment36681053_1464716
8784
Date.prototype.daysInMonth = function () {
8885
return [
8986
31,
@@ -104,15 +101,17 @@ if (!Date.prototype.hasOwnProperty("daysInMonth")) {
104101
if (!Date.prototype.hasOwnProperty("getWOY")) {
105102
//Get Week Number in the Year
106103
//source: https://stackoverflow.com/a/6117889/394921
107-
Date.prototype.getWOY = function (getY) {
108-
var d = new Date(+this);
109-
d.setHours(0, 0, 0, 0);
110-
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
104+
Date.prototype.getWOY = function (getY = false) {
105+
const d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
106+
d.setUTCHours(0, 0, 0, 0);
107+
let dayNum = d.getUTCDay() || 7;
108+
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
111109
if (getY) {
112-
return d.getFullYear();
110+
return d.getUTCFullYear();
113111
} else {
112+
const yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
114113
return Math.ceil(
115-
((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7
114+
((d - yearStart) / 8.64e7 + 1) / 7
116115
);
117116
}
118117
};
@@ -351,31 +350,23 @@ if (!Number.prototype.map) {
351350
Object.freeze($.clock);
352351

353352
//_jqClock contains references to each clock's settimeouts
354-
var _jqClock = _jqClock || {};
353+
let _jqClock = _jqClock || {};
355354

356355
$.fn.clock = function (options) {
357356
let _this = this;
358357

359-
this.initialize = function () {
360-
return this;
361-
};
358+
this.initialize = () => _this;
362359

363-
this.destroy = () => {
364-
return _this.each((idx,selfRef) => {
365-
pluginMethods["destroy"](selfRef);
366-
});
367-
};
368-
this.stop = () => {
369-
return _this.each((idx,selfRef) => {
370-
pluginMethods["stop"](selfRef);
371-
});
372-
};
360+
this.destroy = () => _this.each((idx,selfRef) => {
361+
pluginMethods["destroy"](selfRef);
362+
});
363+
this.stop = () => _this.each((idx,selfRef) => {
364+
pluginMethods["stop"](selfRef);
365+
});
373366

374-
this.start = () => {
375-
return _this.each((idx,selfRef) => {
376-
pluginMethods["start"](selfRef);
377-
});
378-
};
367+
this.start = () => _this.each((idx,selfRef) => {
368+
pluginMethods["start"](selfRef);
369+
});
379370

380371
const dateFormatCharacters = {
381372
//DAY
@@ -571,30 +562,30 @@ if (!Number.prototype.map) {
571562
current_options !== undefined &&
572563
_jqClock.hasOwnProperty(el_id) === false
573564
) {
574-
_jqClock[el_id] = setTimeout(function () {
565+
_jqClock[el_id] = setTimeout(() => {
575566
_updateClock($(selfRef));
576567
}, current_options.rate);
577568
}
578569
}
579570
};
580571

581572
/* Define some helper functions */
582-
let _newGuid = () => {
573+
const _newGuid = () => {
583574
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
584-
.replace(/[xy]/g, function (c) {
585-
let r = (Math.random() * 16) | 0,
586-
v = c == "x" ? r : (r & 0x3) | 0x8;
575+
.replace(/[xy]/g, (c) => {
576+
const r = (Math.random() * 16) | 0,
577+
v = c === "x" ? r : (r & 0x3) | 0x8;
587578
return v.toString(16);
588579
})
589580
.toUpperCase();
590581
},
591582
_ordSuffix = (ord) => {
592583
let ord_suffix = ""; //st, nd, rd, th
593-
if (ord === 1 || (ord % 10 === 1 && ord != 11)) {
584+
if (ord === 1 || (ord % 10 === 1 && ord !== 11)) {
594585
ord_suffix = "st";
595-
} else if (ord === 2 || (ord % 10 === 2 && ord != 12)) {
586+
} else if (ord === 2 || (ord % 10 === 2 && ord !== 12)) {
596587
ord_suffix = "nd";
597-
} else if (ord === 3 || (ord % 10 === 3 && ord != 13)) {
588+
} else if (ord === 3 || (ord % 10 === 3 && ord !== 13)) {
598589
ord_suffix = "rd";
599590
} else {
600591
ord_suffix = "th";
@@ -721,10 +712,10 @@ if (!Number.prototype.map) {
721712
: true;
722713
options.dateFormat =
723714
options.dateFormat ||
724-
(options.langSet == "en" ? "l, F j, Y" : "l, j F Y");
715+
(options.langSet === "en" ? "l, F j, Y" : "l, j F Y");
725716
options.timeFormat =
726717
options.timeFormat ||
727-
(options.langSet == "en" ? "h:i:s A" : "H:i:s");
718+
(options.langSet === "en" ? "h:i:s A" : "H:i:s");
728719
options.timezone = options.timezone || "localsystimezone"; //should only really be passed in when a server timestamp is passed
729720
options.isDST = options.hasOwnProperty("isDST")
730721
? options.isDST
@@ -739,7 +730,7 @@ if (!Number.prototype.map) {
739730
}
740731
if (typeof options.calendar === "string") {
741732
options.calendar = Boolean(
742-
options.calendar == "false" ? false : true
733+
options.calendar === "false" ? false : true
743734
);
744735
} else if (typeof options.calendar !== "boolean") {
745736
options.calendar = Boolean(options.calendar); //do our best to get a boolean value
@@ -755,7 +746,7 @@ if (!Number.prototype.map) {
755746
}
756747
if (typeof options.isDST === "string") {
757748
options.isDST = Boolean(
758-
options.isDST == "true" ? true : false
749+
options.isDST === "true" ? true : false
759750
);
760751
} else if (typeof options.isDST !== "boolean") {
761752
options.isDST = Boolean(options.isDST);
@@ -769,12 +760,12 @@ if (!Number.prototype.map) {
769760
let pos = n + 1;
770761
let str = forDateStr ? myoptions.dateFormat : myoptions.timeFormat;
771762
while (pos < str.length) {
772-
if (str.charAt(pos) == "%") {
763+
if (str.charAt(pos) === "%") {
773764
break;
774765
}
775766
pos++;
776767
}
777-
if (pos > n + 1 && pos != str.length) {
768+
if (pos > n + 1 && pos !== str.length) {
778769
currStr += str.substring(n + 1, pos);
779770
n += pos - n;
780771
} else {
@@ -783,7 +774,7 @@ if (!Number.prototype.map) {
783774
return [ currStr, n ];
784775
},
785776
seemsToBePHPTimestamp = ( options, sysDateObj ) => {
786-
let digitCountDiff =
777+
const digitCountDiff =
787778
(sysDateObj.getTime() + "").length -
788779
(options.timestamp + "").length;
789780
return digitCountDiff > 2;
@@ -831,13 +822,12 @@ if (!Number.prototype.map) {
831822
}
832823
};
833824

834-
this.each(() => {
825+
this.each((idx, selfRef) => {
835826
if (typeof options === "undefined" || typeof options === "object") {
836827
//this is useful only for client timestamps...
837828
//used immediately for the default value of options.isDST...
838-
let highPrecisionTimestamp =
839-
performance.timeOrigin + performance.now();
840-
let sysDateObj = new Date(highPrecisionTimestamp);
829+
const highPrecisionTimestamp = performance.timeOrigin + performance.now();
830+
const sysDateObj = new Date(highPrecisionTimestamp);
841831
//TODO: if server timestamp is passed in and options.isDST is not, then options.isDST isn't any good...
842832
// no use using a client timestamps check for DST when a server timestamp is passed!
843833

@@ -851,7 +841,7 @@ if (!Number.prototype.map) {
851841
options.tzOffset = sysDateObj.getTimezoneOffset();
852842

853843
//divide by 60 to get hours from minutes
854-
let tzOffset = options.tzOffset / 60;
844+
const tzOffset = options.tzOffset / 60;
855845

856846
/* **********************************************
857847
* If we are using the current client timestamp,
@@ -869,15 +859,15 @@ if (!Number.prototype.map) {
869859
*/
870860

871861
//IF A TIMESTAMP HAS BEEN PASSED IN
872-
if (options.timestamp != "localsystime") {
862+
if (options.timestamp !== "localsystime") {
873863
if ( seemsToBePHPTimestamp( options, sysDateObj ) ) {
874864
options = normalizePHPTimestamp( options, sysDateObj );
875865
}
876866
else {
877867
options.sysdiff = options.timestamp - sysDateObj.getTime();
878868
/* ARE THE NEXT FEW LINES AT ALL USEFUL??? */
879869
//options.timezone has most probably not been set, let's do some guesswork
880-
if (options.timezone == "localsystimezone") {
870+
if (options.timezone === "localsystimezone") {
881871
options = initTimezone( options, tzOffset );
882872
}
883873
/* MIGHT WANT TO DOUBLE CHECK IF THE PRECEDING LOGIC IS AT ALL USEFUL... */
@@ -887,7 +877,7 @@ if (!Number.prototype.map) {
887877
//OTHERWISE IF NO TIMESTAMP HAS BEEN PASSED IN
888878
else {
889879
//options.timezone has most probably not been set, let's do some guesswork
890-
if (options.timezone == "localsystimezone") {
880+
if (options.timezone === "localsystimezone") {
891881
options = initTimezone( options, tzOffset );
892882
}
893883
}
@@ -898,7 +888,6 @@ if (!Number.prototype.map) {
898888

899889
initInstance( this, options );
900890
} else if (typeof options === "string") {
901-
let selfRef = this;
902891
if( options in pluginMethods ) {
903892
pluginMethods[options]( selfRef );
904893
} else {

0 commit comments

Comments
 (0)