From cba4db64393743a1662abac8010533b12e67cb8f Mon Sep 17 00:00:00 2001 From: Heena Kwag Date: Mon, 9 Dec 2019 12:11:01 +0900 Subject: [PATCH] chore: apply code review --- src/js/calendar/index.js | 3 +-- src/js/calendar/layerBody/date.js | 28 ++++++++++------------------ src/js/dateTimeFormatter.js | 17 ++++++++++------- src/js/datepicker/index.js | 2 +- src/js/rangeModel/index.js | 10 +++------- src/js/util.js | 28 ++++++++++++++++++++++++++++ test/calendar/calendar.spec.js | 22 ++++++++++++++++++++++ 7 files changed, 75 insertions(+), 35 deletions(-) diff --git a/src/js/calendar/index.js b/src/js/calendar/index.js index 68ed8f8..b1abfa9 100644 --- a/src/js/calendar/index.js +++ b/src/js/calendar/index.js @@ -12,7 +12,6 @@ var hasClass = require('tui-code-snippet/domUtil/hasClass'); var removeClass = require('tui-code-snippet/domUtil/removeClass'); var removeElement = require('tui-code-snippet/domUtil/removeElement'); var extend = require('tui-code-snippet/object/extend'); -var sendHostname = require('tui-code-snippet/request/sendHostname'); var Header = require('./header'); var Body = require('./body'); @@ -174,7 +173,7 @@ var Calendar = defineClass( }); if (options.usageStatistics) { - sendHostname('date-picker', 'UA-129987462-1'); + util.sendHostName(); } }, diff --git a/src/js/calendar/layerBody/date.js b/src/js/calendar/layerBody/date.js index acaf222..24382e1 100644 --- a/src/js/calendar/layerBody/date.js +++ b/src/js/calendar/layerBody/date.js @@ -72,17 +72,14 @@ var DateLayer = defineClass( var weekNumber = 0; var weeksCount = 6; // Fix for no changing height var weeks = []; + var dates, i; for (; weekNumber < weeksCount; weekNumber += 1) { - weeks.push(this._getWeek(year, month, [ - dateUtil.getDateOfWeek(year, month, weekNumber, 0), - dateUtil.getDateOfWeek(year, month, weekNumber, 1), - dateUtil.getDateOfWeek(year, month, weekNumber, 2), - dateUtil.getDateOfWeek(year, month, weekNumber, 3), - dateUtil.getDateOfWeek(year, month, weekNumber, 4), - dateUtil.getDateOfWeek(year, month, weekNumber, 5), - dateUtil.getDateOfWeek(year, month, weekNumber, 6) - ])); + dates = []; + for (i = 0; i < 7; i += 1) { + dates.push(dateUtil.getDateOfWeek(year, month, weekNumber, i)); + } + weeks.push(this._getWeek(year, month, dates)); } return weeks; @@ -115,15 +112,10 @@ var DateLayer = defineClass( className += ' tui-calendar-next-month'; } - switch (date.getDay()) { - case 0: - className += ' tui-calendar-sun'; - break; - case 6: - className += ' tui-calendar-sat'; - break; - default: - break; + if (date.getDay() === 0) { + className += ' tui-calendar-sun'; + } else if (date.getDay() === 6) { + className += ' tui-calendar-sat'; } contexts.push({ diff --git a/src/js/dateTimeFormatter.js b/src/js/dateTimeFormatter.js index 8b574df..c89bc06 100644 --- a/src/js/dateTimeFormatter.js +++ b/src/js/dateTimeFormatter.js @@ -9,6 +9,7 @@ var inArray = require('tui-code-snippet/array/inArray'); var forEachArray = require('tui-code-snippet/collection/forEachArray'); var defineClass = require('tui-code-snippet/defineClass/defineClass'); +var util = require('./util'); var dateUtil = require('./dateUtil'); var constants = require('./constants'); var localeTexts = require('./localeTexts'); @@ -148,15 +149,17 @@ var DateTimeFormatter = defineClass( var matchedKeys = this._rawStr.match(rFormableKeys); var keyOrder = []; - forEachArray(matchedKeys, function(key, index) { - if (key[0] !== '\\') { - if (!/m/i.test(key)) { - key = key.toLowerCase(); - } + matchedKeys = util.filter(matchedKeys, function(key) { + return key[0] !== '\\'; + }); - regExpStr += mapForConverting[key].expression + '[\\D\\s]*'; - keyOrder[index] = mapForConverting[key].type; + forEachArray(matchedKeys, function(key, index) { + if (!/m/i.test(key)) { + key = key.toLowerCase(); } + + regExpStr += mapForConverting[key].expression + '[\\D\\s]*'; + keyOrder[index] = mapForConverting[key].type; }); // This formatter does not allow additional numbers at the end of string. diff --git a/src/js/datepicker/index.js b/src/js/datepicker/index.js index 39ee504..ff54662 100644 --- a/src/js/datepicker/index.js +++ b/src/js/datepicker/index.js @@ -437,7 +437,7 @@ var DatePicker = defineClass( _changePicker: function(target) { var btnSelector = '.' + CLASS_NAME_SELECTOR_BUTTON; var selectedBtn = closest(target, btnSelector); - var isDateElement = selectedBtn.querySelector(SELECTOR_DATE_ICO); + var isDateElement = !!selectedBtn.querySelector(SELECTOR_DATE_ICO); if (isDateElement) { this._calendar.show(); diff --git a/src/js/rangeModel/index.js b/src/js/rangeModel/index.js index 669c732..0e813c3 100644 --- a/src/js/rangeModel/index.js +++ b/src/js/rangeModel/index.js @@ -10,6 +10,7 @@ var defineClass = require('tui-code-snippet/defineClass/defineClass'); var isNumber = require('tui-code-snippet/type/isNumber'); var Range = require('./range'); +var util = require('../util'); /** * @class @@ -131,8 +132,6 @@ var RangeModel = defineClass( * @param {number} [end] - End */ exclude: function(start, end) { - var result = []; - if (!isNumber(end)) { end = start; } @@ -155,12 +154,9 @@ var RangeModel = defineClass( ); // Reduce empty ranges - forEachArray(this._ranges, function(range) { - if (!range.isEmpty()) { - result.push(range); - } + this._ranges = util.filter(this._ranges, function(range) { + return !range.isEmpty(); }); - this._ranges = result; }, /** diff --git a/src/js/util.js b/src/js/util.js index c59f348..bbbef2b 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -5,7 +5,9 @@ 'use strict'; +var forEachArray = require('tui-code-snippet/collection/forEachArray'); var isHTMLNode = require('tui-code-snippet/type/isHTMLNode'); +var sendHostname = require('tui-code-snippet/request/sendHostname'); var currentId = 0; @@ -52,6 +54,32 @@ var utils = { currentId += 1; return currentId; + }, + + /** + * Create a new array with all elements that pass the test implemented by the provided function. + * @param {Array} arr - Array that will be traversed + * @param {function} iteratee - iteratee callback function + * @returns {Array} + */ + filter: function(arr, iteratee) { + var result = []; + + forEachArray(arr, function(item) { + if (iteratee(item)) { + result.push(item); + } + }); + + return result; + }, + + /** + * Send hostname for GA + * @ignore + */ + sendHostName: function() { + sendHostname('date-picker', 'UA-129987462-1'); } }; diff --git a/test/calendar/calendar.spec.js b/test/calendar/calendar.spec.js index f8817b4..d28cc70 100644 --- a/test/calendar/calendar.spec.js +++ b/test/calendar/calendar.spec.js @@ -5,6 +5,7 @@ 'use strict'; var Calendar = require('../../src/js/calendar'); +var util = require('../../src/js/util'); describe('Calendar', function() { describe('Api', function() { @@ -209,4 +210,25 @@ describe('Calendar', function() { expect(nCalendar._body).toBeNull(); }); }); + + describe('usageStatistics', function() { + var nCalendar; + it('should send hostname by default', function() { + spyOn(util, 'sendHostName'); + nCalendar = new Calendar(document.createElement('div')); + + expect(util.sendHostName).toHaveBeenCalled(); + }); + + it('should not send hostname on usageStatistics option false', function() { + spyOn(util, 'sendHostName'); + nCalendar = new Calendar(document.createElement('div'), {usageStatistics: false}); + + expect(util.sendHostName).not.toHaveBeenCalled(); + }); + + afterEach(function() { + nCalendar.destroy(); + }); + }); });