-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(frontend/utils): typescript
- Loading branch information
Showing
10 changed files
with
126 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @module timed | ||
* @submodule timed-utils | ||
* @public | ||
*/ | ||
import moment, { type Duration } from "moment"; | ||
|
||
interface Groups { | ||
days?: number; | ||
hours: number; | ||
minutes: number; | ||
seconds: number; | ||
microseconds?: number; | ||
} | ||
|
||
/** | ||
* Converts a django duration string to a moment duration | ||
*/ | ||
export default function parseDjangoDuration(str: string): Duration | null { | ||
if (!str) { | ||
return null; | ||
} | ||
|
||
const re = | ||
/^(?<days>-?\d+)?\s?(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})(?<microseconds>\.\d{6})?$/; | ||
|
||
const matches = str.match(re) as { | ||
groups: Groups; | ||
} | null; | ||
|
||
if (!matches) { | ||
return null; | ||
} | ||
|
||
const { | ||
days: _days, | ||
hours, | ||
minutes, | ||
seconds, | ||
microseconds: _microseconds, | ||
} = matches.groups; | ||
|
||
const [days, microseconds] = [_days, _microseconds].map( | ||
(v) => Number(v) || 0 | ||
) as [number, number]; | ||
|
||
return moment.duration({ | ||
days, | ||
hours, | ||
minutes, | ||
seconds, | ||
milliseconds: microseconds * 1000, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
frontend/app/utils/serialize-moment.js → frontend/app/utils/serialize-moment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import moment from "moment"; | ||
import moment, { type Moment, type MomentInput } from "moment"; | ||
|
||
export const DATE_FORMAT = "YYYY-MM-DD"; | ||
|
||
export function serializeMoment(momentObject) { | ||
export function serializeMoment(momentObject: Moment) { | ||
if (momentObject) { | ||
momentObject = moment(momentObject); | ||
} | ||
return (momentObject && momentObject.format(DATE_FORMAT)) || null; | ||
} | ||
export function deserializeMoment(momentString) { | ||
export function deserializeMoment(momentString: MomentInput) { | ||
return (momentString && moment(momentString, DATE_FORMAT)) || null; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
type ExcludeNullOrUndefined<T> = T extends null | undefined ? never : T; | ||
|
||
type CleanedObject<T extends object> = { | ||
[K in keyof T as ExcludeNullOrUndefined<T[K]> extends never | ||
? never | ||
: K]: ExcludeNullOrUndefined<T[K]>; | ||
}; | ||
|
||
const notNullOrUndefined = <T>(value: T): value is ExcludeNullOrUndefined<T> => | ||
value !== null && value !== undefined; | ||
|
||
export const cleanParams = <T extends object>(params: T): CleanedObject<T> => { | ||
const cleanedEntries = Object.entries(params).filter(([, value]) => | ||
notNullOrUndefined(value) | ||
); | ||
return Object.fromEntries(cleanedEntries) as CleanedObject<T>; | ||
}; | ||
|
||
export const toQueryString = (params: Record<string, unknown>): string => | ||
Object.entries(params) | ||
.map( | ||
([key, value]) => | ||
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}` | ||
) | ||
.join("&"); |