From 6d4d6356f5168ee93a1d7b52a4d70cf2e2e5fbd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=A0=95=EC=9D=80?= Date: Wed, 18 Nov 2020 18:56:37 +0900 Subject: [PATCH] feat : when you do not set a custom timeZone, the schedule will always be displayed the same. (#718) * style: add prettier config * fix: when you do not set a custom timeZone, the schedule will always be displayed the same * style: update code style --- .eslintrc | 3 +- .prettierrc | 16 ++++++++++ package-lock.json | 47 +++++++++++++++++++++++++++- package.json | 3 ++ src/js/common/timezone.js | 64 +++++++++++++++++++++------------------ 5 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 .prettierrc diff --git a/.eslintrc b/.eslintrc index 7ee9e4149..07592b96a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,6 @@ { - "extends": "tui", + "plugins": ["prettier"], + "extends": ["tui", "plugin:prettier/recommended"], "globals": { "chance": true, "moment": true diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..8f70b54ed --- /dev/null +++ b/.prettierrc @@ -0,0 +1,16 @@ +{ + "singleQuote": true, + "printWidth": 100, + "tabWidth": 4, + "useTabs": false, + "semi": true, + "quoteProps": "as-needed", + "jsxSingleQuote": false, + "trailingComma": "es5", + "arrowParens": "always", + "endOfLine": "lf", + "bracketSpacing": true, + "requirePragma": false, + "insertPragma": false, + "proseWrap": "preserve" +} diff --git a/package-lock.json b/package-lock.json index 8b4450cb1..f9f227652 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "tui-calendar", - "version": "1.12.13", + "version": "1.12.14", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3215,6 +3215,15 @@ } } }, + "eslint-config-prettier": { + "version": "6.15.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-6.15.0.tgz", + "integrity": "sha512-a1+kOYLR8wMGustcgAjdydMsQ2A/2ipRPwRKUmfYaSxc9ZPcrku080Ctl6zrZzZNs/U82MjSv+qKREkoq3bJaw==", + "dev": true, + "requires": { + "get-stdin": "^6.0.0" + } + }, "eslint-config-tui": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/eslint-config-tui/-/eslint-config-tui-1.0.3.tgz", @@ -3240,6 +3249,15 @@ "integrity": "sha1-VzO3CedR9LxA4x4cFpib0s377Jc=", "dev": true }, + "eslint-plugin-prettier": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-3.1.4.tgz", + "integrity": "sha512-jZDa8z76klRqo+TdGDTFJSavwbnWK2ZpqGKNZ+VvweMW516pDUMmQ2koXvxEE4JhzNvTv+radye/bWGBmA6jmg==", + "dev": true, + "requires": { + "prettier-linter-helpers": "^1.0.0" + } + }, "eslint-scope": { "version": "3.7.3", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-3.7.3.tgz", @@ -3610,6 +3628,12 @@ "integrity": "sha512-8UEa58QDLauDNfpbrX55Q9jrGHThw2ZMdOky5Gl1CDtVeJDPVrG4Jxx1N8jw2gkWaff5UUuX1KJd+9zGe2B+ZA==", "dev": true }, + "fast-diff": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", + "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==", + "dev": true + }, "fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -3926,6 +3950,12 @@ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", "dev": true }, + "get-stdin": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz", + "integrity": "sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g==", + "dev": true + }, "get-stream": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", @@ -7719,6 +7749,21 @@ "preprocess": "^3.0.2" } }, + "prettier": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.2.tgz", + "integrity": "sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==", + "dev": true + }, + "prettier-linter-helpers": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz", + "integrity": "sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==", + "dev": true, + "requires": { + "fast-diff": "^1.1.2" + } + }, "process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index ae4369cbb..9e3ecbc6f 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,11 @@ "devDependencies": { "css-loader": "^3.2.0", "eslint": "^4.19.1", + "eslint-config-prettier": "^6.15.0", "eslint-config-tui": "^1.0.3", "eslint-loader": "^2.0.0", "eslint-plugin-jasmine": "^2.10.1", + "eslint-plugin-prettier": "^3.1.4", "handlebars": "^4.7.6", "handlebars-template-loader": "^1.0.0", "istanbul-instrumenter-loader": "^3.0.1", @@ -64,6 +66,7 @@ "mini-css-extract-plugin": "^0.4.0", "optimize-css-assets-webpack-plugin": "^5.0.3", "preprocess-loader": "^0.3.0", + "prettier": "^2.1.2", "safe-umd-webpack-plugin": "^4.0.0", "stylus": "^0.54.5", "stylus-loader": "^3.0.2", diff --git a/src/js/common/timezone.js b/src/js/common/timezone.js index 782bb141f..5271df2e0 100644 --- a/src/js/common/timezone.js +++ b/src/js/common/timezone.js @@ -20,7 +20,7 @@ var getterMethods = [ 'getMilliseconds', 'getMinutes', 'getMonth', - 'getSeconds' + 'getSeconds', ]; var setterMethods = [ @@ -30,7 +30,7 @@ var setterMethods = [ 'setMilliseconds', 'setMinutes', 'setMonth', - 'setSeconds' + 'setSeconds', ]; /** @@ -65,10 +65,16 @@ function getCustomTimezoneOffset(timestamp) { * @returns {number} local time */ function getLocalTime(time) { - var timezoneOffset = getTimezoneOffset(time); - var customTimezoneOffset = getCustomTimezoneOffset(time); - var timezoneOffsetDiff = customTimezoneOffset ? 0 : nativeOffsetMs - timezoneOffset; - var localTime = time - customTimezoneOffset + timezoneOffset + timezoneOffsetDiff; + var timezoneOffset, customTimezoneOffset, timezoneOffsetDiff, localTime; + + if (!setByTimezoneOption) { + return time; + } + + timezoneOffset = getTimezoneOffset(time); + customTimezoneOffset = getCustomTimezoneOffset(time); + timezoneOffsetDiff = customTimezoneOffset ? 0 : nativeOffsetMs - timezoneOffset; + localTime = time - customTimezoneOffset + timezoneOffset + timezoneOffsetDiff; return localTime; } @@ -95,7 +101,7 @@ function createDateWithUTCTime(arg) { if (arg instanceof TZDate) { time = arg.getUTCTime(); - } else if ((typeof arg) === 'number') { + } else if (typeof arg === 'number') { time = arg; } else if (arg === null) { time = 0; @@ -116,7 +122,7 @@ function createDateAsLocalTime(arg) { if (arg instanceof Date) { time = arg.getTime(); - } else if ((typeof arg) === 'string') { + } else if (typeof arg === 'string') { time = Date.parse(arg); } else { throw new Error('Invalid Type'); @@ -133,7 +139,7 @@ function createDateAsLocalTime(arg) { * @returns {boolean} */ function useLocalTimeConverter(arg) { - return arg instanceof Date || (typeof arg) === 'string'; + return arg instanceof Date || typeof arg === 'string'; } /** @@ -163,7 +169,7 @@ function TZDate(date) { * Get milliseconds which is converted by timezone * @returns {number} milliseconds */ -TZDate.prototype.getTime = function() { +TZDate.prototype.getTime = function () { var time = this._date.getTime(); return time + getCustomTimezoneOffset(time) - getTimezoneOffset(time); @@ -173,7 +179,7 @@ TZDate.prototype.getTime = function() { * Get UTC milliseconds * @returns {number} milliseconds */ -TZDate.prototype.getUTCTime = function() { +TZDate.prototype.getUTCTime = function () { return this._date.getTime(); }; @@ -181,7 +187,7 @@ TZDate.prototype.getUTCTime = function() { * toUTCString * @returns {string} */ -TZDate.prototype.toUTCString = function() { +TZDate.prototype.toUTCString = function () { return this._date.toUTCString(); }; @@ -189,34 +195,34 @@ TZDate.prototype.toUTCString = function() { * to Date * @returns {Date} */ -TZDate.prototype.toDate = function() { +TZDate.prototype.toDate = function () { return this._date; }; -TZDate.prototype.valueOf = function() { +TZDate.prototype.valueOf = function () { return this.getTime(); }; -TZDate.prototype.addDate = function(day) { +TZDate.prototype.addDate = function (day) { this.setDate(this.getDate() + day); return this; }; -TZDate.prototype.addMinutes = function(minutes) { +TZDate.prototype.addMinutes = function (minutes) { this.setMinutes(this.getMinutes() + minutes); return this; }; -TZDate.prototype.addMilliseconds = function(milliseconds) { +TZDate.prototype.addMilliseconds = function (milliseconds) { this.setMilliseconds(this.getMilliseconds() + milliseconds); return this; }; /* eslint-disable max-params*/ -TZDate.prototype.setWithRaw = function(y, M, d, h, m, s, ms) { +TZDate.prototype.setWithRaw = function (y, M, d, h, m, s, ms) { this.setFullYear(y, M, d); this.setHours(h, m, s, ms); @@ -226,7 +232,7 @@ TZDate.prototype.setWithRaw = function(y, M, d, h, m, s, ms) { /** * @returns {TZDate} local time */ -TZDate.prototype.toLocalTime = function() { +TZDate.prototype.toLocalTime = function () { var time = this.getTime(); var utcTime = this.getUTCTime(); var diff = time - utcTime; @@ -234,14 +240,14 @@ TZDate.prototype.toLocalTime = function() { return new TZDate(utcTime - diff); }; -getterMethods.forEach(function(methodName) { - TZDate.prototype[methodName] = function() { +getterMethods.forEach(function (methodName) { + TZDate.prototype[methodName] = function () { return this._date[methodName].apply(this._date, arguments); }; }); -setterMethods.forEach(function(methodName) { - TZDate.prototype[methodName] = function() { +setterMethods.forEach(function (methodName) { + TZDate.prototype[methodName] = function () { this._date[methodName].apply(this._date, arguments); return this.getTime(); @@ -255,7 +261,7 @@ module.exports = { * Set offset * @param {number} offset - timezone offset based on minutes */ - setOffset: function(offset) { + setOffset: function (offset) { customOffsetMs = offset * MIN_TO_MS; }, @@ -263,7 +269,7 @@ module.exports = { * Set offset * @param {number} offset - timezone offset based on minutes */ - setOffsetByTimezoneOption: function(offset) { + setOffsetByTimezoneOption: function (offset) { this.setOffset(-offset); setByTimezoneOption = true; }, @@ -272,7 +278,7 @@ module.exports = { * Get offset in case of `setByTimezoneOption`. Or return 0. * @returns {number} timezone offset offset minutes */ - getOffset: function() { + getOffset: function () { if (setByTimezoneOption) { return customOffsetMs / MIN_TO_MS; } @@ -284,7 +290,7 @@ module.exports = { * Set a callback function to get timezone offset by timestamp * @param {function} callback - callback function */ - setOffsetCallback: function(callback) { + setOffsetCallback: function (callback) { timezoneOffsetCallback = callback; }, @@ -292,7 +298,7 @@ module.exports = { * (Use this method only for testing) * Reset system timezone and custom timezone */ - restoreOffset: function() { + restoreOffset: function () { customOffsetMs = getTimezoneOffset(); - } + }, };