Last active
August 16, 2021 20:14
-
-
Save mvcds/951da277927e7cdd30494ba6bad949d2 to your computer and use it in GitHub Desktop.
Testing data or rules 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//hard-coded data giving by business (probably) to "play safe" | |
const defaultConfiguration = { | |
alcohol: 21, | |
softDrugs: Infinity, | |
hardDrugs: Infinity, | |
drive: 21, | |
adult: 21 | |
} | |
// ts-magic, creates an interface-like type definition | |
// it uses a value as template, infering the types correctly | |
type Configuration = typeof defaultConfiguration | |
//hard-coded data giving by each country's law | |
//(not checked for accuracy) | |
//the Partial<T> allow us to not have to define values for all Configuration members | |
const dataset: Record<string, Partial<Configuration>> = { | |
[Country.Brazil]: { alcohol: 18, drive: 18, adult: 18 }, | |
[Country.SaudiArabia]: { alcohol: Infinity, adult: Infinity }, | |
[Country.Netherlands]: { drive: 16, softDrugs: 18 }, | |
[Country.USA]: { drive: 16, adult: 18 } | |
} | |
// js-magic, merges the default and the argument-related record | |
// in a way that the client code always will have some values | |
// even when the record does not especify them | |
export const getCountryConfiguration = ( | |
country: Country | |
): Configuration => ({ | |
...defaultConfiguration, | |
...dataset[country] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment