Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
WIE_202021_CSBillero11
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bernard Roland (Student Com20)
WIE_202021_CSBillero11
Commits
7ea6f852
Commit
7ea6f852
authored
3 years ago
by
Bernard Roland (Student Com20)
Browse files
Options
Downloads
Patches
Plain Diff
Augmented the date formatting
parent
70c12a1d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
client/src/timely.test.ts
+51
-15
51 additions, 15 deletions
client/src/timely.test.ts
client/src/timely.ts
+61
-4
61 additions, 4 deletions
client/src/timely.ts
with
112 additions
and
19 deletions
client/src/timely.test.ts
+
51
−
15
View file @
7ea6f852
import
{
format
Tim
e
,
formatDuration
,
formatRelativeTime
,
addTime
,
subtractTime
}
from
'
timely
'
;
import
{
format
Dat
e
,
formatDuration
,
formatRelativeTime
,
addTime
,
subtractTime
}
from
'
timely
'
;
test
(
'
small durations format as `moments`
'
,
()
=>
{
expect
(
formatDuration
(
10
*
1000
)).
toEqual
(
'
moments
'
);
...
...
@@ -25,8 +25,8 @@ test('one and a quarter second is rounded to one second', () => {
expect
(
formatDuration
(
1250
,
'
second
'
)).
toEqual
(
'
one second
'
);
});
test
(
'
one and a half second is rounded to
two
seconds
'
,
()
=>
{
expect
(
formatDuration
(
1500
,
'
second
'
)).
toEqual
(
'
2
second
s
'
);
test
(
'
one and a half second is rounded to
one
seconds
'
,
()
=>
{
expect
(
formatDuration
(
1500
,
'
second
'
)).
toEqual
(
'
one
second
'
);
});
test
(
'
ten seconds can be formated as expected
'
,
()
=>
{
...
...
@@ -82,35 +82,71 @@ test('ten months can be formated as expected', () => {
});
test
(
'
1st December 2020 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
2020-12-01
'
))).
toEqual
(
'
1st December 2020
'
);
expect
(
format
Dat
e
(
new
Date
(
'
2020-12-01
'
))).
toEqual
(
'
1st December 2020
'
);
});
test
(
'
2nd January 1900 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
1900-01-02
'
))).
toEqual
(
'
2nd January 1900
'
);
expect
(
format
Dat
e
(
new
Date
(
'
1900-01-02
'
))).
toEqual
(
'
2nd January 1900
'
);
});
test
(
'
3rd April 2001 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
2001-04-03
'
))).
toEqual
(
'
3rd April 2001
'
);
expect
(
format
Dat
e
(
new
Date
(
'
2001-04-03
'
))).
toEqual
(
'
3rd April 2001
'
);
});
test
(
'
12th May 2021 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
2021-05-12
'
))).
toEqual
(
'
12th May 2021
'
);
expect
(
format
Dat
e
(
new
Date
(
'
2021-05-12
'
))).
toEqual
(
'
12th May 2021
'
);
});
test
(
'
21st September 2015 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
2015-09-21
'
))).
toEqual
(
'
21st September 2015
'
);
expect
(
format
Dat
e
(
new
Date
(
'
2015-09-21
'
))).
toEqual
(
'
21st September 2015
'
);
});
test
(
'
22nd October 1800 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
1800-10-22
'
))).
toEqual
(
'
22nd October 1800
'
);
expect
(
format
Dat
e
(
new
Date
(
'
1800-10-22
'
))).
toEqual
(
'
22nd October 1800
'
);
});
test
(
'
23rd February 2000 is formated as expected
'
,
()
=>
{
expect
(
format
Tim
e
(
new
Date
(
'
2000-02-23
'
))).
toEqual
(
'
23rd February 2000
'
);
expect
(
format
Dat
e
(
new
Date
(
'
2000-02-23
'
))).
toEqual
(
'
23rd February 2000
'
);
});
test
(
'
31st July 1500 is formated as expected
'
,
()
=>
{
expect
(
formatTime
(
new
Date
(
'
1500-07-31
'
))).
toEqual
(
'
31st July 1500
'
);
expect
(
formatDate
(
new
Date
(
'
1500-07-31
'
))).
toEqual
(
'
31st July 1500
'
);
});
test
(
'
date format precision can be month
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31
'
),
'
month
'
)).
toEqual
(
'
July 2000
'
);
});
test
(
'
date format precision can be year
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31
'
),
'
year
'
)).
toEqual
(
'
2000
'
);
});
test
(
'
date format precision can be hour
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31T12:42:02.121
'
),
'
hour
'
)).
toEqual
(
'
31st July 2000 12:00
'
);
});
test
(
'
date format precision can be minute
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31T12:42:02.121
'
),
'
minute
'
)).
toEqual
(
'
31st July 2000 12:42
'
);
});
test
(
'
date format precision can be second
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31T12:42:02.121
'
),
'
second
'
)).
toEqual
(
'
31st July 2000 12:42:02
'
);
});
test
(
'
date format precision can be millisecond
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-31T12:42:02.121
'
),
'
millisecond
'
)).
toEqual
(
'
31st July 2000 12:42:02.121
'
);
});
test
(
'
date format can include full weekday
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-30
'
),
'
day
'
,
'
full
'
)).
toEqual
(
'
Sunday, 30th July 2000
'
);
});
test
(
'
date format can include short weekday
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-22
'
),
'
day
'
,
'
short
'
)).
toEqual
(
'
Sat, 22nd July 2000
'
);
});
test
(
'
date format can include only the weekday
'
,
()
=>
{
expect
(
formatDate
(
new
Date
(
'
2000-07-26
'
),
'
none
'
,
'
full
'
)).
toEqual
(
'
Wednesday
'
);
});
test
(
'
adding one minute works as expected
'
,
()
=>
{
...
...
@@ -218,9 +254,9 @@ test('one and a quarter minutes ago gets rounded to one minute', () => {
.
toEqual
(
'
one minute ago
'
);
});
test
(
'
one and a half minutes ago gets rounded to
two
minute
s
'
,
()
=>
{
test
(
'
one and a half minutes ago gets rounded to
one
minute
'
,
()
=>
{
expect
(
formatRelativeTime
(
new
Date
(
'
2021-04-22T00:00:00
'
),
new
Date
(
'
2021-04-22T00:01:30
'
)))
.
toEqual
(
'
2
minute
s
ago
'
);
.
toEqual
(
'
one
minute ago
'
);
});
test
(
'
one day ago formats as expected
'
,
()
=>
{
...
...
@@ -273,9 +309,9 @@ test('in one and a quarter minutes gets rounded to one minute', () => {
.
toEqual
(
'
in one minute
'
);
});
test
(
'
in one and a half minutes gets rounded to
two
minute
s
'
,
()
=>
{
test
(
'
in one and a half minutes gets rounded to
one
minute
'
,
()
=>
{
expect
(
formatRelativeTime
(
new
Date
(
'
2021-04-22T00:01:30
'
),
new
Date
(
'
2021-04-22T00:00:00
'
)))
.
toEqual
(
'
in
2
minute
s
'
);
.
toEqual
(
'
in
one
minute
'
);
});
test
(
'
in one day formats as expected
'
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
client/src/timely.ts
+
61
−
4
View file @
7ea6f852
const
UNITS
=
{
'
none
'
:
Infinity
,
'
year
'
:
1000
*
60
*
60
*
24
*
365.2425
,
'
month
'
:
1000
*
60
*
60
*
24
*
30
,
'
week
'
:
1000
*
60
*
60
*
24
*
7
,
...
...
@@ -13,7 +14,7 @@ const UNITS = {
type
Unit
=
(
keyof
typeof
UNITS
);
function
formatAmount
(
amount
:
number
,
base
:
string
):
string
{
amount
=
Math
.
round
(
amount
);
amount
=
Math
.
floor
(
amount
);
if
(
amount
===
0
)
{
return
'
zero
'
+
base
+
'
s
'
;
}
else
if
(
amount
===
1
)
{
...
...
@@ -35,7 +36,7 @@ export function formatDuration(millis: number, precision: Unit = 'minute'): stri
}
function
formatOrdinal
(
value
:
number
):
string
{
value
=
Math
.
round
(
value
);
value
=
Math
.
floor
(
value
);
if
(
value
===
1
||
(
value
%
10
===
1
&&
value
>
20
))
{
return
value
.
toString
()
+
'
st
'
;
}
else
if
(
value
===
2
||
(
value
%
10
===
2
&&
value
>
20
))
{
...
...
@@ -47,6 +48,14 @@ function formatOrdinal(value: number): string {
}
}
function
formatNumber
(
value
:
number
,
places
=
2
,
padding
=
'
0
'
):
string
{
let
result
=
Math
.
floor
(
value
).
toString
();
while
(
result
.
length
<
places
)
{
result
=
padding
+
result
;
}
return
result
;
}
const
MONTHS
=
[
'
January
'
,
'
February
'
,
...
...
@@ -62,8 +71,56 @@ const MONTHS = [
'
December
'
,
];
export
function
formatTime
(
date
:
Date
):
string
{
return
formatOrdinal
(
date
.
getDate
())
+
'
'
+
MONTHS
[
date
.
getMonth
()]
+
'
'
+
date
.
getFullYear
().
toString
();
const
WEEKDAYS
=
[
'
Sunday
'
,
'
Monday
'
,
'
Tuesday
'
,
'
Wednesday
'
,
'
Thursday
'
,
'
Friday
'
,
'
Saturday
'
,
];
export
function
formatDate
(
date
:
Date
,
precision
:
Unit
=
'
day
'
,
weekday
?:
'
short
'
|
'
full
'
):
string
{
let
result
=
''
;
if
(
weekday
===
'
short
'
)
{
result
+=
WEEKDAYS
[
date
.
getDay
()].
substr
(
0
,
3
);
}
else
if
(
weekday
===
'
full
'
)
{
result
+=
WEEKDAYS
[
date
.
getDay
()];
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
day
'
])
{
if
(
result
.
length
!==
0
)
{
result
+=
'
,
'
;
}
result
+=
formatOrdinal
(
date
.
getDate
());
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
month
'
])
{
if
(
result
.
length
!==
0
)
{
result
+=
'
'
;
}
result
+=
MONTHS
[
date
.
getMonth
()];
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
year
'
])
{
if
(
result
.
length
!==
0
)
{
result
+=
'
'
;
}
result
+=
date
.
getFullYear
().
toString
();
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
hour
'
])
{
result
+=
'
'
+
formatNumber
(
date
.
getHours
());
if
(
UNITS
[
precision
]
<=
UNITS
[
'
minute
'
])
{
result
+=
'
:
'
+
formatNumber
(
date
.
getMinutes
());
}
else
{
result
+=
'
:00
'
;
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
second
'
])
{
result
+=
'
:
'
+
formatNumber
(
date
.
getSeconds
());
}
if
(
UNITS
[
precision
]
<=
UNITS
[
'
millisecond
'
])
{
result
+=
'
.
'
+
formatNumber
(
date
.
getMilliseconds
(),
3
);
}
}
return
result
;
}
export
function
formatRelativeTime
(
target
:
Date
,
origin
=
new
Date
(),
precision
?:
Unit
):
string
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment