Skip to content

Commit

Permalink
chore: apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
heenakwag committed Dec 12, 2019
1 parent 63f4450 commit cba4db6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/js/calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -174,7 +173,7 @@ var Calendar = defineClass(
});

if (options.usageStatistics) {
sendHostname('date-picker', 'UA-129987462-1');
util.sendHostName();
}
},

Expand Down
28 changes: 10 additions & 18 deletions src/js/calendar/layerBody/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down
17 changes: 10 additions & 7 deletions src/js/dateTimeFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/js/datepicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 3 additions & 7 deletions src/js/rangeModel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,8 +132,6 @@ var RangeModel = defineClass(
* @param {number} [end] - End
*/
exclude: function(start, end) {
var result = [];

if (!isNumber(end)) {
end = start;
}
Expand All @@ -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;
},

/**
Expand Down
28 changes: 28 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
};

Expand Down
22 changes: 22 additions & 0 deletions test/calendar/calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

var Calendar = require('../../src/js/calendar');
var util = require('../../src/js/util');

describe('Calendar', function() {
describe('Api', function() {
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit cba4db6

Please sign in to comment.