Skip to content

Commit

Permalink
♻️ Add timeForFileName helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfrost committed May 9, 2024
1 parent 899cfe4 commit 73c649a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/script/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions test/spec/lib/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
numberToBoolean,
removeFromArray,
secondsToHMS,
timeForFileName,
} from '@APF/lib/helper';

const array = ['a', 'needle', 'in', 'a', 'large', 'haystack'];
Expand Down Expand Up @@ -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}/);
});
});
});

0 comments on commit 73c649a

Please sign in to comment.