From e2fedb26a1382da7ab1deb46e9c5d77b186aa8cf Mon Sep 17 00:00:00 2001 From: gras Date: Thu, 18 Apr 2024 21:40:42 +0200 Subject: [PATCH] Updated zeroPrefix to get rid of deprecation warning and to make it more generic --- src/common/Utils.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 38ca9acf..fcdc2d95 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -56,12 +56,16 @@ export class Utils { return c; } - //TODO: use zeroPad which works for longer values? - static zeroPrefix(value: string | number, length: number): string { - const ret = '00000' + value; - return ret.substr(ret.length - length); + static zeroPrefix(number: any, length: number): string { + if (!isNaN(number)) { + const zerosToAdd = Math.max(length - String(number).length, 0); + return '0'.repeat(zerosToAdd) + number; + } else { + return '0'.repeat(number); + } } + /** * Checks if the two input (let them be objects or arrays or just primitives) are equal */ @@ -186,7 +190,7 @@ export class Utils { gps.GPSDateStamp && gps.GPSTimeStamp) { //else use exif.gps.GPS*Stamp if available //GPS timestamp is always UTC (+00:00) - UTCTimestamp = gps.GPSDateStamp.replaceAll(':', '-') + " " + gps.GPSTimeStamp.map((num: any) => Utils.zeroPad(num ,2)).join(':'); + UTCTimestamp = gps.GPSDateStamp.replaceAll(':', '-') + " " + gps.GPSTimeStamp.map((num: any) => Utils.zeroPrefix(num ,2)).join(':'); } if (UTCTimestamp && timestamp) { //offset in minutes is the difference between gps timestamp and given timestamp @@ -202,22 +206,13 @@ export class Utils { if (-720 <= offsetMinutes && offsetMinutes <= 840) { //valid offset is within -12 and +14 hrs (https://en.wikipedia.org/wiki/List_of_UTC_offsets) return (offsetMinutes < 0 ? "-" : "+") + //leading +/- - Utils.zeroPad(Math.trunc(Math.abs(offsetMinutes) / 60), 2) + ":" + //zeropadded hours and ':' - Utils.zeroPad((Math.abs(offsetMinutes) % 60), 2); //zeropadded minutes + Utils.zeroPrefix(Math.trunc(Math.abs(offsetMinutes) / 60), 2) + ":" + //zeropadded hours and ':' + Utils.zeroPrefix((Math.abs(offsetMinutes) % 60), 2); //zeropadded minutes } else { return undefined; } } - static zeroPad(number: any, length: number): string { - if (!isNaN(number)) { - const zerosToAdd = Math.max(length - String(number).length, 0); - return '0'.repeat(zerosToAdd) + number; - } else { - return '0'.repeat(number); - } - } - static getOffsetMinutes(offsetString: string) { //Convert offset string (+HH:MM or -HH:MM) into a minute value const regex = /^([+-](0[0-9]|1[0-4]):[0-5][0-9])$/; //checks if offset is between -14:00 and +14:00. //-12:00 is the lowest valid UTC-offset, but we allow down to -14 for efficiency