Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Birth of parseBool() #455

Merged
merged 26 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af96814
added common util folder, implemented isStringTruthy, added isPinned …
mle225 Apr 28, 2022
93dcbde
cast DEBUG to string to check
mle225 Apr 28, 2022
9a02c7a
Update isStringTruthy function signature
mle225 Apr 29, 2022
0b6bc9f
Update isStringTruthy truthy check
mle225 Apr 29, 2022
030f1b2
Update src/lib/common/utils.ts
mle225 Apr 29, 2022
dcd1485
modified isStringTruthy and added test cases
mle225 Apr 29, 2022
f7dfbc0
Refactored truthiness checker function
EthanThatOneKid May 19, 2022
69dd5f3
Updated tests for `determineTruthyString`
EthanThatOneKid May 19, 2022
f9db6ab
Updated import for `determineTruthyString`
EthanThatOneKid May 19, 2022
f53a6da
Merge branch 'main' into fix/422
EthanThatOneKid May 19, 2022
af8c952
Attempt number 1 at _Recent Events_ UI
EthanThatOneKid May 19, 2022
5745f3e
Added ICAL parser integration testing
EthanThatOneKid May 19, 2022
7dcc301
Merge branch 'add/ical-parse-integration-test' into fix/422
EthanThatOneKid May 19, 2022
f5ca7fb
Show sample events if in debug mode and no events currently exist
EthanThatOneKid May 19, 2022
877d807
Progress
EthanThatOneKid May 19, 2022
8432b35
Updated sample event data
EthanThatOneKid May 19, 2022
f6f85d1
This commit should fail the tests, but it won't
EthanThatOneKid May 19, 2022
ec895ff
Resolved <https://github.com/EthanThatOneKid/acmcsuf.com/pull/455#dis…
EthanThatOneKid May 19, 2022
a81e7f1
Revert "Attempt number 1 at _Recent Events_ UI"
EthanThatOneKid May 19, 2022
622e488
Renamed `determineTruthyString` to `parseBool`
EthanThatOneKid May 20, 2022
76602ae
Merge branch 'main' into fix/422
EthanThatOneKid May 20, 2022
2d0df3d
Revert "This commit should fail the tests, but it won't"
EthanThatOneKid May 21, 2022
7b88b6b
Included a case for "any non-falsy string" since any string passed to…
EthanThatOneKid Aug 29, 2022
88e7702
Merge branch 'main' into fix/422
EthanThatOneKid Aug 29, 2022
9e8decb
Refactored parseBool function
EthanThatOneKid Aug 29, 2022
4d40cb7
This is true progress.
EthanThatOneKid Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test, expect } from 'vitest';
import * as utils from './utils';

const TRUTHY_INPUT_DATA = ['tRuThY sTrInG', 'TRUE', String(true), String(1)];
for (const input of TRUTHY_INPUT_DATA) {
test(`correctly determines input value '${input}' as truthy`, () => {
expect(utils.parseBool(input)).toBe(true);
});
}

const FALSY_INPUT_DATA = ['FALSE', '', null, undefined, String(false), String(NaN), String(0)];
for (const input of FALSY_INPUT_DATA) {
test(`correctly determines input value '${input}' as falsy`, () => {
expect(utils.parseBool(input)).toBe(false);
});
}
10 changes: 10 additions & 0 deletions src/lib/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function parseBool(payload?: string | null, defaultValue = false): boolean {
jaasonw marked this conversation as resolved.
Show resolved Hide resolved
if (!payload) return defaultValue;

payload = payload.trim().toLowerCase();

// unlikely NaN will be passed, but included to be safe
const stringsOtherwiseTruthy = [0, false, NaN].map((v) => String(v).toLowerCase());
jaasonw marked this conversation as resolved.
Show resolved Hide resolved

return !stringsOtherwiseTruthy.includes(payload);
}
6 changes: 3 additions & 3 deletions src/lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from './acm-paths';
export * from './links';
export * from './officers';

import { parseBool } from '$lib/common/utils';

// DEBUG flag is activated when anything besides '', '0', or 'false' is selected
export const DEBUG = !['', '0', 'false', 'undefined'].includes(
String(import.meta.env.VITE_DEBUG).toLowerCase()
);
export const DEBUG = parseBool(import.meta.env.VITE_DEBUG);
5 changes: 5 additions & 0 deletions src/lib/ical/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as RRule from 'rrule/dist/es5/rrule.min.js';
import { Temporal } from '@js-temporal/polyfill';
import type { AcmPath } from '$lib/constants/acm-paths';
import { acmAlgo, acmCreate, acmDev, acmGeneral } from '$lib/constants/acm-paths';
import { parseBool } from '$lib/common/utils';

export interface AcmEvent {
month: string;
day: number;
time: string;
hasStarted: boolean;
hasEnded: boolean;
isPinned: boolean;
duration: string;
date: string;
location: string;
Expand Down Expand Up @@ -314,6 +316,8 @@ export function makeAcmEvent(
variables.get('ACM_LOCATION')
);

const isPinned = parseBool(variables.get('ACM_PINNED'));

const slug = makeEventSlug(title, dtStart);

const selfLink = makeEventLink(slug);
Expand Down Expand Up @@ -353,6 +357,7 @@ export function makeAcmEvent(
date,
hasStarted,
hasEnded,
isPinned,
duration,
location,
title,
Expand Down
10 changes: 10 additions & 0 deletions src/routes/events/_testdata/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const events = [
date: '2018-08-17T12:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'ACM & ACM-W Meeting',
Expand Down Expand Up @@ -35,6 +36,7 @@ export const events = [
date: '2018-08-17T17:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'ACM Board Meeting',
Expand Down Expand Up @@ -64,6 +66,7 @@ export const events = [
date: '2018-08-21T12:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'Personal Website Workshop Planning',
Expand Down Expand Up @@ -94,6 +97,7 @@ export const events = [
date: '2018-08-24T09:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'Leadcon',
Expand Down Expand Up @@ -123,6 +127,7 @@ export const events = [
date: '2018-09-10T17:30:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '15 minutes',
location: 'CSUF Pollack Library SGSR113',
title: 'Git Workshop Review',
Expand Down Expand Up @@ -154,6 +159,7 @@ export const events = [
date: '2018-09-14T19:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'Project Jam Meeting',
Expand Down Expand Up @@ -184,6 +190,7 @@ export const events = [
date: '2018-09-19T12:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'ROOM SGSR212 CSUF Pollak Library',
title: 'Personal Website Workshop Mock Presentation',
Expand Down Expand Up @@ -215,6 +222,7 @@ export const events = [
date: '2018-10-02T16:00:00-07:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'PLN 434',
title: 'Project Jam lead meeting',
Expand Down Expand Up @@ -246,6 +254,7 @@ export const events = [
date: '2019-02-05T18:30:00-08:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'Website Meeting',
Expand Down Expand Up @@ -275,6 +284,7 @@ export const events = [
date: '2019-02-06T10:30:00-08:00[America/Los_Angeles]',
hasStarted: true,
hasEnded: true,
isPinned: false,
duration: '0 minutes',
location: 'TBD',
title: 'Workshop meeting',
Expand Down
6 changes: 6 additions & 0 deletions src/routes/events/index.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RequestHandlerOutput } from '@sveltejs/kit/types/internal';
import type { AcmEvent } from '$lib/ical';
import { parse } from '$lib/ical';
import { DEBUG } from '$lib/constants';
import { events as SAMPLE_EVENTS } from '../../routes/events/_testdata/events';

// Constants
const caching = false; // Make this false to disable server-side caching in development.
Expand All @@ -16,6 +17,11 @@ let events: AcmEvent[] = [];
async function setCache(timestamp: number): Promise<AcmEvent[]> {
const data = await fetch(ICAL_TARGET_URL).then((response) => response.text());
events = parse(data, { maxEvents: DEBUG ? 10 : undefined });

if (DEBUG && events.length === 0) {
events = SAMPLE_EVENTS as AcmEvent[];
}

eventExpirationTimestamp = timestamp + expirationTimeout;
return events;
}
Expand Down