@@ -6,6 +6,7 @@ const mappings = ['complete', 'sign', 'weeks', 'days', 'hours', 'minutes', 'seco
66const regex = /^(?:([-])\s*)?(?:(\d+)w\s*)?(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s\s*)?$/;
77const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/ig;
88const roundedRegex = /(\[\%([^\>\]]*)\:([^\]]*)\])/ig;
9+ const conditionalSimpleRegex = /(.*)\>(.*)/ig;
910const defaultRegex = /(\[\%([^\]]*)\])/ig;
1011
1112Number.prototype.padLeft = function (n, str) {
@@ -96,7 +97,7 @@ class time {
9697 * @returns {string}
9798 */
9899 static toHumanReadable(input, hoursPerDay = 8, format = time.defaultTimeFormat) {
99- let sign = parseInt(input) < 0 ? '-' : '', output = format, match;
100+ let sign = parseInt(input) < 0 ? '-' : '', output = format, match, conditionalMatch ;
100101 input = Math.abs(input);
101102
102103 let secondsInADay = 60 * 60 * hoursPerDay;
@@ -122,19 +123,25 @@ class time {
122123 inserts.seconds = ((input % secondsInADay) % secondsInAnHour) % secondsInAMinute;
123124 inserts.Seconds = inserts.seconds.padLeft(2, 0);
124125
126+ // rounded
127+ while ((match = roundedRegex.exec(format)) !== null) {
128+ if (match.index === roundedRegex.lastIndex) roundedRegex.lastIndex++;
129+ let time;
130+
131+ if ((conditionalMatch = conditionalSimpleRegex.exec(match[3])) !== null) {
132+ match[3] = conditionalMatch[1]
133+ }
134+
135+ time = Math.ceil(inserts[match[2]] * Math.pow(10, match[3])) / Math.pow(10, match[3]);
136+ output = output.replace(match[0], time !== 0 && conditionalMatch ? time + conditionalMatch[2] : '');
137+ }
138+
125139 // conditionals
126140 while ((match = conditionalRegex.exec(format)) !== null) {
127141 if (match.index === conditionalRegex.lastIndex) conditionalRegex.lastIndex++;
128142 output = output.replace(match[0], inserts[match[2]] > 0 ? inserts[match[2]] + match[3] : '');
129143 }
130144
131- // rounded
132- while ((match = roundedRegex.exec(format)) !== null) {
133- if (match.index === roundedRegex.lastIndex) roundedRegex.lastIndex++;
134- // console.log(match); process.exit();
135- output = output.replace(match[0], Math.ceil(inserts[match[2]] * Math.pow(10, match[3])) / Math.pow(10, match[3]));
136- }
137-
138145 // default
139146 format = output;
140147 while ((match = defaultRegex.exec(format)) !== null) {
0 commit comments