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

Commit

Permalink
fix: Use @guardian/tsconfig (#390)
Browse files Browse the repository at this point in the history
* use `@guardian/tsconfig`
* Use `Date.toLocaleString` instead of an array of month names
* Update src/cookies/getCookie.ts

Co-authored-by: Max Duval <[email protected]>
  • Loading branch information
sndrs and mxdvl authored Aug 24, 2022
1 parent d5953f4 commit c5b6652
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 59 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@guardian/eslint-config-typescript": "1.0.2",
"@guardian/pkgu": "0.6.2",
"@guardian/prettier": "2.0.0",
"@guardian/tsconfig": "0.1.4",
"@semantic-release/github": "8.0.5",
"@types/jest": "27.4.1",
"@types/node": "16.11.47",
Expand Down
8 changes: 4 additions & 4 deletions src/cookies/getCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const getCookie = ({
const memoizedCookie = memoizedCookies.get(name);
if (memoizedCookie) return memoizedCookie;

const cookieVal = getCookieValues(name);
const [value] = getCookieValues(name);

if (cookieVal.length > 0) {
if (value) {
if (shouldMemoize) {
memoizedCookies.set(name, cookieVal[0]);
memoizedCookies.set(name, value);
}
return cookieVal[0];
return value;
}
return null;
};
5 changes: 4 additions & 1 deletion src/cookies/setCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export const setCookie = ({

// If the cookie is already memoized we want to replace its value
if (memoizedCookies.has(name)) {
memoizedCookies.set(name, getCookieValues(name)[0]);
const [value] = getCookieValues(name);
if (value) {
memoizedCookies.set(name, value);
}
}
};
5 changes: 4 additions & 1 deletion src/cookies/setSessionCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const setSessionCookie = ({

// If the cookie is already memoized we want to replace its value
if (memoizedCookies.has(name)) {
memoizedCookies.set(name, getCookieValues(name)[0]);
const [value] = getCookieValues(name);
if (value) {
memoizedCookies.set(name, value);
}
}
};
36 changes: 3 additions & 33 deletions src/datetime/timeAgo.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
type Unit = 's' | 'm' | 'h' | 'd';

const shortMonth = (month: number): string =>
[
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
][month];

const longMonth = (month: number): string =>
[
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
][month];

const pad = (n: number): number | string => n.toString().padStart(2, '0');

const isWithin24Hours = (date: Date): boolean => {
Expand Down Expand Up @@ -137,7 +105,9 @@ export const timeAgo = (
// Simple date - "9 Nov 2019"
return [
then.getDate(),
verbose ? longMonth(then.getMonth()) : shortMonth(then.getMonth()),
verbose
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
then.getFullYear(),
].join(' ');
}
Expand Down
6 changes: 3 additions & 3 deletions src/loadScript/loadScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ describe('loadScript', () => {
referrerPolicy: 'no-referrer',
className: 'u6ytfiuyoibnpoim',
});
expect(document.scripts[0].async).toBeTruthy();
expect(document.scripts[0].referrerPolicy).toBe('no-referrer');
expect(document.scripts[0].className).toBe('u6ytfiuyoibnpoim');
expect(document.scripts[0]?.async).toBeTruthy();
expect(document.scripts[0]?.referrerPolicy).toBe('no-referrer');
expect(document.scripts[0]?.className).toBe('u6ytfiuyoibnpoim');
});

it('rejects if the script fails to load', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/loadScript/loadScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export const loadScript = (
script.onerror = reject;

const ref = document.scripts[0];
ref.parentNode?.insertBefore(script, ref);
ref?.parentNode?.insertBefore(script, ref);
});
27 changes: 11 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"resolveJsonModule": true,
"strict": true,
"target": "ES2020"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "coverage"],
"ts-node": { "compilerOptions": { "module": "commonjs" } }
"extends": "@guardian/tsconfig",
"compilerOptions": {
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"noEmit": true,
"target": "ES2020"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "coverage"],
"ts-node": { "compilerOptions": { "module": "commonjs" } }
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@
resolved "https://registry.yarnpkg.com/@guardian/prettier/-/prettier-2.0.0.tgz#2d213e78fab5973285ef5e4f077d3439ec661f49"
integrity sha512-xzihXsXQGERmbl6PSfC17o8h/R8SVO2DVHY7npxzSdKhQdGSRivWvIKOOsroEBoO5RsBMqeo2NJ+VSWQtugViw==

"@guardian/[email protected]":
version "0.1.4"
resolved "https://registry.npmjs.org/@guardian/tsconfig/-/tsconfig-0.1.4.tgz#6f9b78f36b14dbe9507658e7d8bb49b61398b641"
integrity sha512-QLHYlDSUksxQw3hzbAFhdC/NS7RMlw7fsdg0fL2CaOeGmOSwEmPJTbsqJAp3KxvpCuLK3ZtCSDnOY7sLcuQ+Ig==

"@humanwhocodes/config-array@^0.10.4":
version "0.10.4"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.4.tgz#01e7366e57d2ad104feea63e72248f22015c520c"
Expand Down

0 comments on commit c5b6652

Please sign in to comment.