Skip to content

Commit be2f3ec

Browse files
committed
Added possibility to escape chars in formatTimeDiff
1 parent e5acad5 commit be2f3ec

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Executes Javascript, Typescript Scripts.
2626
<!--
2727
### **WORK IN PROGRESS**
2828
-->
29+
### **WORK IN PROGRESS**
30+
31+
* (@klein0r) Added possibility to escape chars in formatTimeDiff
32+
2933
### 8.9.2 (2025-04-27)
3034

3135
* (@GermanBluefox) Updated packages for GUI

docs/en/javascript.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,8 @@ formatTimeDiff(milliseconds, format);
12991299
* ss, сс(cyrillic) - full seconds, e.g. "05"
13001300
* s, с(cyrillic) - short seconds, e.g., "5"
13011301

1302+
You can use escape charachter `\` to avoid the replacement. e.g. `DD \Day\s, h \hour\s, m \minute, ss \second\s`
1303+
13021304
#### Example
13031305

13041306
```js

lib/sandbox.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,50 +3537,60 @@ function sandBox(script, name, verbose, debug, context) {
35373537
const neg = diff < 0;
35383538
diff = Math.abs(diff);
35393539

3540-
if (/DD|TT|ДД|D|T|Д/.test(text)) {
3540+
if (/(?<!\\)(D|T|Д)/.test(text)) {
35413541
const days = Math.floor(diff / day);
35423542

3543-
text = text.replace(/DD|TT|ДД/, days < 10 ? `0${days}` : days);
3544-
text = text.replace(/D|T|Д/, days);
3543+
text = text
3544+
.replace(/(?<!\\)(DD|TT|ДД)/g, days < 10 ? `0${days}` : days)
3545+
.replace(/(?<!\\)(D|T|Д)/g, days);
35453546

35463547
sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, days=${days})`, 'debug');
35473548

35483549
diff -= days * day;
35493550
}
35503551

3551-
if (/hh|SS|чч|h|S|ч/.test(text)) {
3552+
if (/(?<!\\)(h|S|ч)/.test(text)) {
35523553
const hours = Math.floor(diff / hour);
35533554

3554-
text = text.replace(/hh|SS|чч/, hours < 10 ? `0${hours}` : hours);
3555-
text = text.replace(/h|S|ч/, hours);
3555+
text = text
3556+
.replace(/(?<!\\)(hh|SS|чч)/g, hours < 10 ? `0${hours}` : hours)
3557+
.replace(/(?<!\\)(h|S|ч)/g, hours);
35563558

35573559
sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, hours=${hours})`, 'debug');
35583560

35593561
diff -= hours * hour;
35603562
}
35613563

3562-
if (/mm|мм|m|м/.test(text)) {
3564+
if (/(?<!\\)(m|м)/.test(text)) {
35633565
const minutes = Math.floor(diff / minute);
35643566

3565-
text = text.replace(/mm|мм/, minutes < 10 ? `0${minutes}` : minutes);
3566-
text = text.replace(/m|м/, minutes);
3567+
text = text
3568+
.replace(/(?<!\\)(mm|мм)/g, minutes < 10 ? `0${minutes}` : minutes)
3569+
.replace(/(?<!\\)(m|м)/g, minutes);
35673570

35683571
sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, minutes=${minutes})`, 'debug');
35693572

35703573
diff -= minutes * minute;
35713574
}
35723575

3573-
if (/ss|сс|мм|s|с/.test(text)) {
3576+
if (/(?<!\\)(s|с)/.test(text)) {
35743577
const seconds = Math.floor(diff / second);
35753578

3576-
text = text.replace(/ss|сс/, seconds < 10 ? `0${seconds}` : seconds);
3577-
text = text.replace(/s|с/, seconds);
3579+
text = text
3580+
.replace(/(?<!\\)(ss|сс)/g, seconds < 10 ? `0${seconds}` : seconds)
3581+
.replace(/(?<!\\)(s|с)/g, seconds);
35783582

35793583
sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, seconds=${seconds})`, 'debug');
35803584

35813585
diff -= seconds * second;
35823586
}
35833587

3588+
text = text
3589+
.replace(/\\(D|T|Д)/g, '$1')
3590+
.replace(/\\(h|S|ч)/g, '$1')
3591+
.replace(/\\(m|м)/g, '$1')
3592+
.replace(/\\(s|с)/g, '$1');
3593+
35843594
sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text})`, 'debug');
35853595

35863596
return neg ? `-${text}` : text;

test/testFunctions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,8 @@ describe.only('Test JS', function () {
19461946
source: `createState('test_formatTimeDiff', { type: 'string', role: 'json', read: true, write: false }, () => {\n` +
19471947
` const diff1 = formatTimeDiff(172800000 + 10800000 + 540000 + 15000, 'hh:mm:ss');\n` +
19481948
` const diff2 = formatTimeDiff((172800000 + 10800000 + 540000 + 15000) * -1, 'mm:ss');\n` +
1949-
` setState('test_formatTimeDiff', { val: JSON.stringify({ diff1, diff2 }), ack: true });\n` +
1949+
` const diff3 = formatTimeDiff(374501000, 'DD \\Day\\s, h \\hour\\s, m \\minute, ss \\second\\s');\n` +
1950+
` setState('test_formatTimeDiff', { val: JSON.stringify({ diff1, diff2, diff3 }), ack: true });\n` +
19501951
`});`,
19511952
},
19521953
native: {},
@@ -1961,6 +1962,9 @@ describe.only('Test JS', function () {
19611962
expect(obj.diff2).to.be.a('string');
19621963
expect(obj.diff2).to.be.equal('-3069:15');
19631964

1965+
expect(obj.diff3).to.be.a('string');
1966+
expect(obj.diff3).to.be.equal('04 Days, 8 hours, 1 minute, 41 seconds');
1967+
19641968
removeStateChangedHandler(onStateChanged);
19651969
done();
19661970
}

0 commit comments

Comments
 (0)