Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

fix: Use @guardian/tsconfig #390

Merged
merged 10 commits into from
Aug 24, 2022
Merged

fix: Use @guardian/tsconfig #390

merged 10 commits into from
Aug 24, 2022

Conversation

sndrs
Copy link
Member

@sndrs sndrs commented Aug 23, 2022

What does this change?

Adds @guardian/tsconfig to the typscript config

Why?

It's stricter (in a good way) and preps the code for import to https://github.com/guardian/csnx

@sndrs sndrs requested a review from a team as a code owner August 23, 2022 09:17
@sndrs sndrs enabled auto-merge (squash) August 23, 2022 09:17
@github-actions
Copy link

yarn.lock changes

Click to toggle table visibility
Name Status Previous Current
@guardian/tsconfig ADDED - 0.1.4

@sndrs sndrs force-pushed the use-guardian-tsconfig branch from 1a95c61 to 1e9c9fb Compare August 23, 2022 09:18
@coveralls
Copy link

coveralls commented Aug 23, 2022

Coverage Status

Coverage remained the same at 100.0% when pulling e7ea101 on use-guardian-tsconfig into d5953f4 on main.

ob6160
ob6160 previously approved these changes Aug 23, 2022
Copy link

@ob6160 ob6160 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work 👍

src/cookies/getCookie.ts Outdated Show resolved Hide resolved
@@ -137,7 +105,9 @@ export const timeAgo = (
// Simple date - "9 Nov 2019"
return [
then.getDate(),
verbose ? longMonth(then.getMonth()) : shortMonth(then.getMonth()),
verbose
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah! a lot nicer :)

Comment on lines +109 to +110
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
Copy link
Member Author

@sndrs sndrs Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliverlloyd how do you feel about these? guardian is at 98.99% support (greater than es module support). are they going to have wierd unexpected effects?

Copy link
Contributor

@JamieB-gu JamieB-gu Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the other functions in this file return English strings, and the style guide is British English, is it possible we want to use 'en-GB' here?

Suggested change
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
? then.toLocaleString('en-GB', { month: 'long' })
: then.toLocaleString('en-GB', { month: 'short' }),

I think using 'default' will use the browser/system's default locale, and will therefore give the month in the language for that locale? For example, in French if the system is set to French. You can test this with the following snippet of code:

const d = new Date();

d.toLocaleString('default', { month: 'long' });

in Node by setting the LANG environment variable:

LANG=fr_FR node

or in a browser by changing the macOS system language to e.g. French, restarting your browser, and then pulling up a console (tested in Firefox and Safari).

Copy link
Contributor

@mxdvl mxdvl Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely agree with @JamieB-gu here! I would be tempted to move this to a separate PR, as it’s possibly a breaking change, or at least most definitly a feature.

I am one such user who would not want my dates in French if I’m browsing an english-language website.

My suggestion for the existing code would be:

const longMonth = (month: number): string =>
	([
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December',
	] as const)[month] ?? "Unknown";
	
const shortMonth = (month: number) => longMonth(month).slice(0,3);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great point – 'en-GB' should resolve that though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think then.toLocaleString('en-GB', { month: 'short' }) best respects the design guide

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that toLocaleString was not well supported in browsers, but it seems like that’s not the case, there’s only missing data for about 3% of browsers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support is available for everything that supports es modules afaict?

have a ☕ first
@sndrs sndrs requested review from oliverlloyd and mxdvl August 23, 2022 09:38
@sndrs sndrs disabled auto-merge August 23, 2022 09:39
mxdvl
mxdvl previously approved these changes Aug 23, 2022
Copy link
Contributor

@mxdvl mxdvl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but I would not change the behaviour of timeAgo as part of this change.

src/cookies/getCookie.ts Outdated Show resolved Hide resolved
src/cookies/setCookie.ts Outdated Show resolved Hide resolved
src/cookies/setSessionCookie.ts Outdated Show resolved Hide resolved
Comment on lines +109 to +110
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
Copy link
Contributor

@mxdvl mxdvl Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely agree with @JamieB-gu here! I would be tempted to move this to a separate PR, as it’s possibly a breaking change, or at least most definitly a feature.

I am one such user who would not want my dates in French if I’m browsing an english-language website.

My suggestion for the existing code would be:

const longMonth = (month: number): string =>
	([
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December',
	] as const)[month] ?? "Unknown";
	
const shortMonth = (month: number) => longMonth(month).slice(0,3);

tsconfig.json Outdated Show resolved Hide resolved
@mxdvl mxdvl dismissed their stale review August 23, 2022 11:56

I don’t think this should go in as-is, because of the timeAgo changes.

@sndrs
Copy link
Member Author

sndrs commented Aug 23, 2022

I don’t think this should go in as-is, because of the timeAgo changes.

what do you think needs to change to make them ok? the ?? "Unknown" is only there because as far as i know TS doesn't have a type for 0-11. that makes it a branch of code we can't test, and given the month index comes from getMonth, a branch we'll never use. it's only there because getMonth returns a number in ts.

if the en-GB @JamieB-gu mentioned returns the same strings, shouldn't we be ok? what would you think we'd need to address in a subsequent PR?

Copy link
Contributor

@mxdvl mxdvl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, with the great support of toLocaleString, I think this is good to go!

Comment on lines +109 to +110
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that toLocaleString was not well supported in browsers, but it seems like that’s not the case, there’s only missing data for about 3% of browsers.

@sndrs sndrs merged commit c5b6652 into main Aug 24, 2022
@sndrs sndrs deleted the use-guardian-tsconfig branch August 24, 2022 09:34
@github-actions
Copy link

🎉 This PR is included in version 7.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@JamieB-gu
Copy link
Contributor

JamieB-gu commented Aug 24, 2022

@sndrs did we want to change the toLocaleString call to specify en-GB? I think, with this merged, the code now on main will show datetimes in different languages if any of the consumers update to it? I may have missed something though 🤔

@sndrs
Copy link
Member Author

sndrs commented Aug 24, 2022

Doh! Yes I completely forgot to implement it!

sndrs added a commit that referenced this pull request Aug 24, 2022
@sndrs
Copy link
Member Author

sndrs commented Aug 24, 2022

@JamieB-gu correct locale went out in https://github.com/guardian/libs/releases/tag/v7.1.3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants