reuters-graphics / bluprint_graphics-kit

SvelteKit rig for graphics and newsapps
https://reuters-graphics.github.io/docs_graphics-kit/
21 stars 3 forks source link

Truthy values for props #62

Closed hobbes7878 closed 2 years ago

hobbes7878 commented 2 years ago

@man-shar raises in https://github.com/reuters-graphics/graphics-svelte-components/issues/35 that we're not setting stackBackground in a way that it can ever be set to false here, b/c we need to convert the string to a boolean for that prop to work (as a string, it's always true)

We do this a lot, and so I think it makes sense to create a simple utils function that handles turning string representations of booleans back into proper JS ones. So something like:

import truthyString from '$utils/truthyString';

truthyString('true') === true;
truthyString('False') === false;
truthyString('T') === true;
truthyString('0') === false;

The function can then be case insensitive, tolerate white space and deal with several different ways we may mean true or false.

And then we can use that in our component logic in Page.svelte like this:

<Scroller
  stackBackground="{truthyString(block.StackBackground)}"
/>

@MinamiFunakoshiTR, you want to take a crack at it in this repo?

Fixing this will close https://github.com/reuters-graphics/graphics-svelte-components/issues/35 and https://github.com/reuters-graphics/graphics-svelte-components/pull/34.

hobbes7878 commented 2 years ago

Should say, too, there are dozens of examples of libraries that do this on npm, but I still think let's roll our own.

Still, useful to look at some of em for reference for what ours should do, e.g.: https://www.npmjs.com/package/boolean

MinamiFunakoshiTR commented 2 years ago

Will work on this on a new branch

MinamiFunakoshiTR commented 2 years ago

Made the fix, see PR here: https://github.com/reuters-graphics/bluprint_graphics-kit/pull/63

@hobbes7878 let me know if you want to set up the logic for the string to Boolean conversion differently

MinamiFunakoshiTR commented 2 years ago

PR merged. Page.svelte now uses truthyString() to convert string into boolean.