Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zhige Yu
scientia
Commits
37bfa7fa
Commit
37bfa7fa
authored
Nov 25, 2020
by
Zhige Yu
Browse files
Add comments on dateNeutralized function and utility types
parent
0896eeda
Pipeline
#165669
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/constants/types.ts
View file @
37bfa7fa
...
...
@@ -295,6 +295,25 @@ export enum LinkTitleError {
export
type
SubscriptionLevel
=
1
|
2
|
3
/*****************************************
* # Utility Types # *
* Behold! The power of TypeScript :) *
*****************************************/
/**
* JavaScript does not allow us to use enum as dictionary keys by default.
* Make it happy.
*/
export
type
EnumDictionary
<
T
extends
string
|
symbol
|
number
,
U
>
=
{
[
K
in
T
]:
U
;
};
\ No newline at end of file
}
/**
* A union type contains all keys in type T, whose corresponding value extends type O.
*
* i.e. All keys of type O
*/
export
type
AllKeysExtends
<
T
,
O
>
=
{
[
K
in
keyof
T
]:
T
[
K
]
extends
O
?
K
:
never
}[
keyof
T
]
\ No newline at end of file
src/utils/functions.ts
View file @
37bfa7fa
import
{
AllKeysExtends
}
from
"
constants/types
"
import
moment
from
"
moment
"
export
function
titleCase
(
string
:
string
)
{
...
...
@@ -85,15 +86,27 @@ export function emailFromUsername(username: string): string {
return
`
${
username
}
@ic.ac.uk`
}
/**
* TODO: not implemented
*
* Show the full cohort string of the given cohortID
* i.e. 'c3' => 'Computing - Year 3'
* @param cohortID cohort id of the student (e.g. 'c3')
*/
export
function
showCohort
(
cohortID
:
string
):
string
{
return
"
Computing - Year 3
"
}
export
type
AllKeysExtends
<
T
,
O
>
=
{
[
K
in
keyof
T
]:
T
[
K
]
extends
O
?
K
:
never
}[
keyof
T
]
/**
* Neutralize an object by parsing specified date members into Date objects.
*
* Can be used to neutralize the response object from the api request,
* as the raw response object stores all Dates members as strings.
*
* This function would only work on the keys that are declared to be of 'Date' type.
* @param dict The object to be neutralized
* @param dateKeys The keys you wish to neutralize.
*/
export
function
dateNeutralized
<
T
>
(
dict
:
T
,
...
dateKeys
:
AllKeysExtends
<
T
,
Date
>
[]):
T
{
for
(
const
k
of
dateKeys
)
{
const
date
=
dict
[
k
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment