summaryrefslogtreecommitdiff
path: root/src/duration.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/duration.ts')
-rw-r--r--src/duration.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/duration.ts b/src/duration.ts
index 3d1a44c..e8dc7d1 100644
--- a/src/duration.ts
+++ b/src/duration.ts
@@ -12,6 +12,7 @@ export enum DurationUnit {
MINUTE,
HOUR,
}
+
const durationUnitMap: Record<string, DurationUnit> = {
ms: DurationUnit.MILLISECOND,
milliseconds: DurationUnit.MILLISECOND,
@@ -153,3 +154,26 @@ export const parse = (duration: string): E.Either<string, Duration> => {
E.map(build),
);
};
+
+export const transformDurations = (obj: any): E.Either<string, any> => {
+ const transform = (o: any): E.Either<string, any> => {
+ const entries = Object.entries(o);
+
+ for (let [key, value] of entries) {
+ if (key === "duration" && typeof value === "string") {
+ return parse(value);
+ } else if (typeof value === "object" && value !== null) {
+ const result = transform(value);
+ if (E.isLeft(result)) {
+ return result;
+ } else {
+ o[key] = result.right;
+ }
+ }
+ }
+
+ return E.right(o);
+ };
+
+ return transform(obj);
+}