From 73c649a0419a8ff8b67182a3256de3a0b1778eb0 Mon Sep 17 00:00:00 2001 From: Richard Frost Date: Wed, 8 May 2024 18:43:05 -0600 Subject: [PATCH] :recycle: Add timeForFileName helper function --- src/script/lib/helper.ts | 8 ++++++++ test/spec/lib/helper.spec.ts | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/script/lib/helper.ts b/src/script/lib/helper.ts index f6242279..9ecd1153 100644 --- a/src/script/lib/helper.ts +++ b/src/script/lib/helper.ts @@ -214,6 +214,14 @@ export function stringArray(data: string | string[]): string[] { return data; } +export function timeForFileName() { + const padded = (num: number) => { return ('0' + num).slice(-2); }; + const date = new Date; + const today = `${date.getFullYear()}-${padded(date.getMonth()+1)}-${padded(date.getDate())}`; + const time = `${padded(date.getHours())}${padded(date.getMinutes())}${padded(date.getSeconds())}`; + return `${today}_${time}`; +} + export function upperCaseFirst(str: string, lowerCaseRest: boolean = true): string { let value = str.charAt(0).toUpperCase(); value += lowerCaseRest ? str.toLowerCase().slice(1) : str.slice(1); diff --git a/test/spec/lib/helper.spec.ts b/test/spec/lib/helper.spec.ts index 752e0c02..f2ab771a 100644 --- a/test/spec/lib/helper.spec.ts +++ b/test/spec/lib/helper.spec.ts @@ -10,6 +10,7 @@ import { numberToBoolean, removeFromArray, secondsToHMS, + timeForFileName, } from '@APF/lib/helper'; const array = ['a', 'needle', 'in', 'a', 'large', 'haystack']; @@ -189,4 +190,10 @@ describe('Helper', function() { expect(secondsToHMS(10818.5)).to.eql('03:00:18.500'); }); }); + + describe('timeForFileName()', function() { + it('Returns time string', function() { + expect(timeForFileName()).to.match(/\d{4}-\d{2}-\d{2}_\d{6}/); + }); + }); });