Skip to content

Commit 8b8c66a

Browse files
committed
add string.pad and util.formatTimecode
1 parent 1e7404a commit 8b8c66a

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Functional.moon

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ _string = {
285285
when "B" then args[i] and 1 or 0 -- trueish/falsish as 1 and 0
286286
else (opts..type_)\format args[i]
287287

288+
pad: (str, charCnt, padStr = "0", right = false) ->
289+
repCnt = charCnt - #str
290+
return str if repCnt < 1
291+
padding = padStr\rep(math.ceil repCnt / #padStr)\sub 1, repCnt
292+
return right and str .. padding or padding .. str
293+
288294
toNumbers: (base, ...) ->
289295
numbers, n = {}, 1
290296
if type(base) != "number"
@@ -537,6 +543,20 @@ _function = {
537543
_util = {
538544
equals: DependencyControl.UnitTestSuite.UnitTest.equals
539545
itemsEqual: DependencyControl.UnitTestSuite.UnitTest.itemsEqual
546+
547+
formatTimecode: (time, format) ->
548+
splitTime = (time, div) ->
549+
split = time % div
550+
return split, (time - split) / div
551+
552+
splits = {}
553+
splits.f, time = splitTime time, 1000
554+
splits.s, time = splitTime time, 60
555+
splits.m, time = splitTime time, 60
556+
splits.h, time = splitTime time, 24
557+
558+
return _re.replace format, "(h+|m+|s+|f+)", (flag) ->
559+
_string.pad tostring(splits[flag\sub 1, 1]), #flag
540560
}
541561

542562
_unicode = {

0 commit comments

Comments
 (0)