Skip to content

Commit

Permalink
Merge branch 'timeFormat' of https://github.com/andrewdormi/cleave.js
Browse files Browse the repository at this point in the history
…into andrewdormi-timeFormat
  • Loading branch information
nosir committed Dec 16, 2018
2 parents 8432a62 + dc7f5f0 commit 861c80c
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 14 deletions.
13 changes: 13 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ new Cleave('.my-input', {
['s', 'm', 'h']: 37:56:14
```

### `timeFormat`

A `String` value indicates time format.

**Default value** `"24"`

```js
new Cleave('.my-input', {
time: true,
timeFormat: "12"
});
```

You can also custom the [delimiter](#delimiter) for time

## Numerals
Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"name": "cleave.js",
"title": "cleave.js",
"description":
"JavaScript library for formatting input text content when you are typing",
"keywords": ["cleave", "javascript", "html", "format", "form", "input"],
"description": "JavaScript library for formatting input text content when you are typing",
"keywords": [
"cleave",
"javascript",
"html",
"format",
"form",
"input"
],
"version": "1.4.4",
"files": ["src", "dist", "react.js"],
"files": [
"src",
"dist",
"react.js"
],
"author": {
"name": "Max Huang",
"url": "http://github.com/nosir",
Expand Down
2 changes: 1 addition & 1 deletion src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Cleave.prototype = {
return;
}

pps.timeFormatter = new Cleave.TimeFormatter(pps.timePattern);
pps.timeFormatter = new Cleave.TimeFormatter(pps.timePattern, pps.timeFormat);
pps.blocks = pps.timeFormatter.getBlocks();
pps.blocksLength = pps.blocks.length;
pps.maxLength = Cleave.Util.getMaxLength(pps.blocks);
Expand Down
2 changes: 1 addition & 1 deletion src/Cleave.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var cleaveReactClass = CreateReactClass({
return;
}

pps.timeFormatter = new TimeFormatter(pps.timePattern);
pps.timeFormatter = new TimeFormatter(pps.timePattern, pps.timeFormat);
pps.blocks = pps.timeFormatter.getBlocks();
pps.blocksLength = pps.blocks.length;
pps.maxLength = Util.getMaxLength(pps.blocks);
Expand Down
1 change: 1 addition & 0 deletions src/common/DefaultProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var DefaultProperties = {
// time
target.time = !!opts.time;
target.timePattern = opts.timePattern || ['h', 'm', 's'];
target.timeFormat = opts.timeFormat || '24';
target.timeFormatter = {};

// date
Expand Down
36 changes: 29 additions & 7 deletions src/shortcuts/TimeFormatter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

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

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

Expand All @@ -30,11 +31,32 @@ TimeFormatter.prototype = {
return this.blocks;
},

getTimeFormatOptions: function () {
var owner = this;
if (owner.timeFormat === '12') {
return {
maxHourFirstDigit: 1,
maxHours: 11,
maxMinutesFirstDigit: 5,
maxMinutes: 60
};
}

return {
maxHourFirstDigit: 2,
maxHours: 23,
maxMinutesFirstDigit: 5,
maxMinutes: 60
};
},

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

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

var timeFormatOptions = owner.getTimeFormatOptions();

owner.blocks.forEach(function (length, index) {
if (value.length > 0) {
var sub = value.slice(0, length),
Expand All @@ -44,20 +66,20 @@ TimeFormatter.prototype = {
switch (owner.timePattern[index]) {

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

break;

case 'm':
case 's':
if (parseInt(sub0, 10) > 5) {
if (parseInt(sub0, 10) > timeFormatOptions.maxMinutesFirstDigit) {
sub = '0' + sub0;
} else if (parseInt(sub, 10) > 60) {
sub = '60';
} else if (parseInt(sub, 10) > timeFormatOptions.maxMinutes) {
sub = timeFormatOptions.maxMinutes + '';
}
break;
}
Expand Down
59 changes: 59 additions & 0 deletions test/fixtures/time.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,64 @@
"0918"
]
]
},
{
"timeFormat": "12",
"timePattern": [
"h",
"m"
],
"time": [
[
"0718",
"0718"
],
[
"2018",
"0218"
],
[
"0008",
"0008"
],
[
"9018",
"0918"
]
]
},
{
"timeFormat": "12",
"timePattern": [
"h",
"m",
"s"
],
"time": [
[
"0718",
"0718"
],
[
"2518",
"0218"
],
[
"071899",
"071809"
],
[
"232323000",
"022323"
],
[
"2323230",
"022323"
],
[
"2370700",
"020707"
]
]
}
]
2 changes: 1 addition & 1 deletion test/unit/TimeFormatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var timeGroups = require('../fixtures/time.json');
describe('TimeFormatter', function () {
_.each(timeGroups, function (timeGroup) {
describe('pattern: ' + timeGroup.timePattern.join(', '), function () {
var timeFormatter = new TimeFormatter(timeGroup.timePattern);
var timeFormatter = new TimeFormatter(timeGroup.timePattern, timeGroup.timeFormat);

_.each(timeGroup.time, function (time) {
it('should convert time ' + time[0] + ' to ' + time[1], function () {
Expand Down

0 comments on commit 861c80c

Please sign in to comment.