Skip to content

Commit

Permalink
Add tests for time option
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed Jul 14, 2018
1 parent 1741317 commit 1b31067
Show file tree
Hide file tree
Showing 19 changed files with 1,060 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"form",
"input"
],
"version": "1.3.8",
"version": "1.4.0",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
210 changes: 199 additions & 11 deletions dist/cleave-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ return /******/ (function(modules) { // webpackBootstrap
var owner = this, pps = owner.properties;

// no need to use this lib
if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) {
if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) {
owner.onInput(pps.initValue);

return;
Expand All @@ -113,6 +113,7 @@ return /******/ (function(modules) { // webpackBootstrap

owner.initPhoneFormatter();
owner.initDateFormatter();
owner.initTimeFormatter();
owner.initNumeralFormatter();

// avoid touch input field if value is null
Expand Down Expand Up @@ -140,6 +141,19 @@ return /******/ (function(modules) { // webpackBootstrap
);
},

initTimeFormatter: function() {
var owner = this, pps = owner.properties;

if (!pps.time) {
return;
}

pps.timeFormatter = new Cleave.TimeFormatter(pps.timePattern);
pps.blocks = pps.timeFormatter.getBlocks();
pps.blocksLength = pps.blocks.length;
pps.maxLength = Cleave.Util.getMaxLength(pps.blocks);
},

initDateFormatter: function () {
var owner = this, pps = owner.properties;

Expand Down Expand Up @@ -282,6 +296,11 @@ return /******/ (function(modules) { // webpackBootstrap
value = pps.dateFormatter.getValidatedDate(value);
}

// time
if (pps.time) {
value = pps.timeFormatter.getValidatedTime(value);
}

// strip delimiters
value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters);

Expand Down Expand Up @@ -464,10 +483,11 @@ return /******/ (function(modules) { // webpackBootstrap

Cleave.NumeralFormatter = __webpack_require__(1);
Cleave.DateFormatter = __webpack_require__(2);
Cleave.PhoneFormatter = __webpack_require__(3);
Cleave.CreditCardDetector = __webpack_require__(4);
Cleave.Util = __webpack_require__(5);
Cleave.DefaultProperties = __webpack_require__(6);
Cleave.TimeFormatter = __webpack_require__(3);
Cleave.PhoneFormatter = __webpack_require__(4);
Cleave.CreditCardDetector = __webpack_require__(5);
Cleave.Util = __webpack_require__(6);
Cleave.DefaultProperties = __webpack_require__(7);

// for angular directive
((typeof global === 'object' && global) ? global : window)['Cleave'] = Cleave;
Expand Down Expand Up @@ -815,6 +835,168 @@ return /******/ (function(modules) { // webpackBootstrap

'use strict';

var TimeFormatter = function (timePattern) {
var owner = this;

owner.time = [];
owner.blocks = [];
owner.timePattern = timePattern;
owner.initBlocks();
};

TimeFormatter.prototype = {
initBlocks: function () {
var owner = this;
owner.timePattern.forEach(function () {
owner.blocks.push(2);
});
},

getISOFormatTime: function () {
var owner = this,
time = owner.time;

return time[2] ? (
owner.addLeadingZero(time[0]) + ':' + owner.addLeadingZero(time[1]) + ':' + owner.addLeadingZero(time[2])
) : '';
},

getBlocks: function () {
return this.blocks;
},

getValidatedTime: function (value) {
var owner = this, result = '';

value = value.replace(/[^\d]/g, '');

owner.blocks.forEach(function (length, index) {
if (value.length > 0) {
var sub = value.slice(0, length),
sub0 = sub.slice(0, 1),
rest = value.slice(length);

switch (owner.timePattern[index]) {

case 'h':
if (parseInt(sub0, 10) > 2) {
sub = '0' + sub0;
} else if (parseInt(sub, 10) > 23) {
sub = '23';
}

break;

case 'm':
case 's':
if (parseInt(sub0, 10) > 5) {
sub = '0' + sub0;
} else if (parseInt(sub, 10) > 60) {
sub = '60';
}
break;
}

result += sub;

// update remaining string
value = rest;
}
});

return this.getFixedTimeString(result);
},

getFixedTimeString: function (value) {
var owner = this, timePattern = owner.timePattern, time = [],
secondIndex = 0, minuteIndex = 0, hourIndex = 0,
secondStartIndex = 0, minuteStartIndex = 0, hourStartIndex = 0,
second, minute, hour;

if (value.length === 6) {
timePattern.forEach(function (type, index) {
switch (type) {
case 's':
secondIndex = index * 2;
break;
case 'm':
minuteIndex = index * 2;
break;
case 'h':
hourIndex = index * 2;
break;
}
});

hourStartIndex = hourIndex;
minuteStartIndex = minuteIndex;
secondStartIndex = secondIndex;

second = parseInt(value.slice(secondStartIndex, secondStartIndex + 2), 10);
minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10);
hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10);

time = this.getFixedTime(hour, minute, second);
}

if (value.length === 4 && owner.timePattern.indexOf('s') < 0) {
timePattern.forEach(function (type, index) {
switch (type) {
case 'm':
minuteIndex = index * 2;
break;
case 'h':
hourIndex = index * 2;
break;
}
});

hourStartIndex = hourIndex;
minuteStartIndex = minuteIndex;

second = 0;
minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10);
hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10);

time = this.getFixedTime(hour, minute, second);
}

owner.time = time;

return time.length === 0 ? value : timePattern.reduce(function (previous, current) {
switch (current) {
case 's':
return previous + owner.addLeadingZero(time[2]);
case 'm':
return previous + owner.addLeadingZero(time[1]);
case 'h':
return previous + owner.addLeadingZero(time[0]);
}
}, '');
},

getFixedTime: function (hour, minute, second) {
second = Math.min(parseInt(second || 0, 10), 60);
minute = Math.min(minute, 60);
hour = Math.min(hour, 60);

return [hour, minute, second];
},

addLeadingZero: function (number) {
return (number < 10 ? '0' : '') + number;
}
};

module.exports = TimeFormatter;


/***/ }),
/* 4 */
/***/ (function(module, exports) {

'use strict';

var PhoneFormatter = function (formatter, delimiter) {
var owner = this;

Expand Down Expand Up @@ -874,7 +1056,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ }),
/* 4 */
/* 5 */
/***/ (function(module, exports) {

'use strict';
Expand Down Expand Up @@ -977,7 +1159,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ }),
/* 5 */
/* 6 */
/***/ (function(module, exports) {

'use strict';
Expand Down Expand Up @@ -1198,7 +1380,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ }),
/* 6 */
/* 7 */
/***/ (function(module, exports) {

/* WEBPACK VAR INJECTION */(function(global) {'use strict';
Expand Down Expand Up @@ -1226,6 +1408,11 @@ return /******/ (function(modules) { // webpackBootstrap
target.phoneRegionCode = opts.phoneRegionCode || 'AU';
target.phoneFormatter = {};

// time
target.time = !!opts.time;
target.timePattern = opts.timePattern || ['h', 'm'];
target.timeFormatter = {};

// date
target.date = !!opts.date;
target.datePattern = opts.datePattern || ['d', 'm', 'Y'];
Expand Down Expand Up @@ -1257,9 +1444,10 @@ return /******/ (function(modules) { // webpackBootstrap
target.delimiter =
(opts.delimiter || opts.delimiter === '') ? opts.delimiter :
(opts.date ? '/' :
(opts.numeral ? ',' :
(opts.phone ? ' ' :
' ')));
(opts.time ? ':' :
(opts.numeral ? ',' :
(opts.phone ? ' ' :
' '))));
target.delimiterLength = target.delimiter.length;
target.delimiterLazyShow = !!opts.delimiterLazyShow;
target.delimiters = opts.delimiters || [];
Expand Down
4 changes: 2 additions & 2 deletions dist/cleave-angular.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 1b31067

Please sign in to comment.