Skip to content

Commit

Permalink
Fixed escaping chars in the time pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs committed Jul 10, 2018
1 parent 2e9beea commit b0c16ae
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 138 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kairos",
"version": "2.1.0",
"version": "2.1.1",
"description": "A non date-based time calculator",
"homepage": "https://github.com/kairos",
"repository": {
Expand Down
55 changes: 22 additions & 33 deletions build/kairos-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Kairos.js - A non date-based time calculator
* @author Rodrigo Gomes da Silva <[email protected]>
* @version v2.1.0
* @version v2.1.1
* @link https://github.com/kairos
* @license BSD-2-Clause
*/
Expand Down Expand Up @@ -552,13 +552,15 @@
}

var sign = instance.milliseconds >= 0,
hours = String(Math.abs(instance.getHours())),
minutes = String(Math.abs(instance.getMinutes())),
seconds = String(Math.abs(instance.getSeconds())),
milliseconds = String(Math.abs(instance.getMilliseconds()));
hours = String(Math.abs(instance.getHours())),
minutes = String(Math.abs(instance.getMinutes())),
seconds = String(Math.abs(instance.getSeconds())),
milliseconds = String(Math.abs(instance.getMilliseconds()));

var result = '',
hasOverflow = (hours.length > (pattern.match(/h/g) || []).length);
var result = '';
var escapedHourTokens = (pattern.match(/\\h/g) || []).length;
var hourTokens = ((pattern.match(/h/g) || []).length - escapedHourTokens);
var usedHourTokens = 0;

for (var i = pattern.length - 1; i >= 0; i--) {
var cur = pattern[i];
Expand All @@ -567,65 +569,52 @@
if (hasLeadingEscape) {
result = cur + result;
i--;
continue;
}

switch (cur) {
case TOKENS.SIGN:
if (!hasLeadingEscape) result = (sign ? '+' : '-') + result;
result = (sign ? '+' : '-') + result;
break;
case TOKENS.HOURS:
if (hasLeadingEscape && hours.length > 0) {
hours = hours.slice(0, hours.length - 1);
break;
}
usedHourTokens++;

var isLastHourToken = usedHourTokens === hourTokens;
var isOverflowing = isLastHourToken && hours.length > 1;

if (hasOverflow) {
if (allowOverflow) {
result = hours + result;
allowOverflow = false;
}
if (isOverflowing && allowOverflow) {
result = hours + result;
allowOverflow = false;
break;
}

result = (hours.slice(-1) || '0') + result;
if (hours.length > 0) {
hours = hours.slice(0, hours.length - 1);
}
break;
case TOKENS.MINUTES:
if (hasLeadingEscape && minutes.length > 0) {
minutes = minutes.slice(0, minutes.length - 1);
break;
}

result = (minutes.slice(-1) || '0') + result;
if (minutes.length > 0) {
minutes = minutes.slice(0, minutes.length - 1);
}
break;
case TOKENS.SECONDS:
if (hasLeadingEscape && seconds.length > 0) {
seconds = seconds.slice(0, seconds.length - 1);
break;
}

result = (seconds.slice(-1) || '0') + result;
if (seconds.length > 0) {
seconds = seconds.slice(0, seconds.length - 1);
}
break;
case TOKENS.MILLISECONDS:
if (hasLeadingEscape && milliseconds.length > 0) {
milliseconds = milliseconds.slice(0, milliseconds.length - 1);
break;
}

result = (milliseconds.slice(-1) || '0') + result;
if (milliseconds.length > 0) {
milliseconds = milliseconds.slice(0, milliseconds.length - 1);
}
break;
default:
if (!hasLeadingEscape) result = cur + result;
if (!hasLeadingEscape) {
result = cur + result;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions build/kairos-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 22 additions & 33 deletions build/kairos-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Kairos.js - A non date-based time calculator
* @author Rodrigo Gomes da Silva <[email protected]>
* @version v2.1.0
* @version v2.1.1
* @link https://github.com/kairos
* @license BSD-2-Clause
*/
Expand Down Expand Up @@ -531,13 +531,15 @@
}

var sign = instance.milliseconds >= 0,
hours = String(Math.abs(instance.getHours())),
minutes = String(Math.abs(instance.getMinutes())),
seconds = String(Math.abs(instance.getSeconds())),
milliseconds = String(Math.abs(instance.getMilliseconds()));
hours = String(Math.abs(instance.getHours())),
minutes = String(Math.abs(instance.getMinutes())),
seconds = String(Math.abs(instance.getSeconds())),
milliseconds = String(Math.abs(instance.getMilliseconds()));

var result = '',
hasOverflow = (hours.length > (pattern.match(/h/g) || []).length);
var result = '';
var escapedHourTokens = (pattern.match(/\\h/g) || []).length;
var hourTokens = ((pattern.match(/h/g) || []).length - escapedHourTokens);
var usedHourTokens = 0;

for (var i = pattern.length - 1; i >= 0; i--) {
var cur = pattern[i];
Expand All @@ -546,65 +548,52 @@
if (hasLeadingEscape) {
result = cur + result;
i--;
continue;
}

switch (cur) {
case TOKENS.SIGN:
if (!hasLeadingEscape) result = (sign ? '+' : '-') + result;
result = (sign ? '+' : '-') + result;
break;
case TOKENS.HOURS:
if (hasLeadingEscape && hours.length > 0) {
hours = hours.slice(0, hours.length - 1);
break;
}
usedHourTokens++;

var isLastHourToken = usedHourTokens === hourTokens;
var isOverflowing = isLastHourToken && hours.length > 1;

if (hasOverflow) {
if (allowOverflow) {
result = hours + result;
allowOverflow = false;
}
if (isOverflowing && allowOverflow) {
result = hours + result;
allowOverflow = false;
break;
}

result = (hours.slice(-1) || '0') + result;
if (hours.length > 0) {
hours = hours.slice(0, hours.length - 1);
}
break;
case TOKENS.MINUTES:
if (hasLeadingEscape && minutes.length > 0) {
minutes = minutes.slice(0, minutes.length - 1);
break;
}

result = (minutes.slice(-1) || '0') + result;
if (minutes.length > 0) {
minutes = minutes.slice(0, minutes.length - 1);
}
break;
case TOKENS.SECONDS:
if (hasLeadingEscape && seconds.length > 0) {
seconds = seconds.slice(0, seconds.length - 1);
break;
}

result = (seconds.slice(-1) || '0') + result;
if (seconds.length > 0) {
seconds = seconds.slice(0, seconds.length - 1);
}
break;
case TOKENS.MILLISECONDS:
if (hasLeadingEscape && milliseconds.length > 0) {
milliseconds = milliseconds.slice(0, milliseconds.length - 1);
break;
}

result = (milliseconds.slice(-1) || '0') + result;
if (milliseconds.length > 0) {
milliseconds = milliseconds.slice(0, milliseconds.length - 1);
}
break;
default:
if (!hasLeadingEscape) result = cur + result;
if (!hasLeadingEscape) {
result = cur + result;
}
}
}

Expand Down
Loading

0 comments on commit b0c16ae

Please sign in to comment.