Skip to content

Commit

Permalink
Added TimeFormatter (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
wex authored and nosir committed Jul 14, 2018
1 parent 2cadf9c commit 1741317
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Cleave.prototype = {
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 @@ -57,6 +57,7 @@ Cleave.prototype = {

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

// avoid touch input field if value is null
Expand Down Expand Up @@ -84,6 +85,19 @@ Cleave.prototype = {
);
},

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 @@ -226,6 +240,11 @@ Cleave.prototype = {
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 @@ -408,6 +427,7 @@ Cleave.prototype = {

Cleave.NumeralFormatter = require('../src/shortcuts/NumeralFormatter');
Cleave.DateFormatter = require('../src/shortcuts/DateFormatter');
Cleave.TimeFormatter = require('../src/shortcuts/TimeFormatter');
Cleave.PhoneFormatter = require('../src/shortcuts/PhoneFormatter');
Cleave.CreditCardDetector = require('../src/shortcuts/CreditCardDetector');
Cleave.Util = require('../src/utils/Util');
Expand Down
5 changes: 5 additions & 0 deletions src/common/DefaultProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var DefaultProperties = {
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
157 changes: 157 additions & 0 deletions src/shortcuts/TimeFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
'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 (value) {
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;

0 comments on commit 1741317

Please sign in to comment.