Skip to content

Commit

Permalink
Start guessing countries
Browse files Browse the repository at this point in the history
This change only recognises cases where the location is a country name or code.

Refs #31
  • Loading branch information
thewilkybarkid committed Jul 9, 2024
1 parent 952ee24 commit 607d5a2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d3-dsv": "^3.0.1",
"d3-time-format": "^4.1.0",
"effect": "^3.3.5",
"i18n-iso-countries": "^7.11.2",
"iso-639-1": "^3.1.2"
},
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions src/data/users.json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { HttpClient, HttpClientRequest, HttpClientResponse, Terminal } from '@effect/platform'
import { NodeTerminal } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { Config, Effect, Redacted } from 'effect'
import { Array, Config, Effect, Option, Redacted } from 'effect'
import * as Iso3166 from '../lib/Iso3166.js'
import * as Temporal from '../lib/Temporal.js'

const Users = Schema.Array(
Expand All @@ -12,6 +13,15 @@ const Users = Schema.Array(
}),
)

const Output = Schema.Array(
Schema.Struct({
careerStage: Schema.OptionFromUndefinedOr(Schema.Literal('early', 'mid', 'late')),
location: Schema.OptionFromUndefinedOr(Schema.String),
country: Schema.OptionFromUndefinedOr(Iso3166.Alpha2CodeSchema),
timestamp: Temporal.InstantFromStringSchema,
}),
)

const program = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
const token = yield* Config.redacted('PREREVIEW_REVIEWS_DATA_TOKEN')
Expand All @@ -26,7 +36,12 @@ const program = Effect.gen(function* () {
Effect.scoped,
)

const encoded = yield* Schema.encode(Schema.parseJson(Users))(data)
const transformedData = Array.map(data, user => ({
...user,
country: Option.flatMap(user.location, Iso3166.guessCountry),
}))

const encoded = yield* Schema.encode(Schema.parseJson(Output))(transformedData)

yield* terminal.display(encoded)
})
Expand Down
13 changes: 13 additions & 0 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Schema } from '@effect/schema'
import { Option, type Predicate } from 'effect'
import iso3166 from 'i18n-iso-countries'

export type Alpha2Code = iso3166.Alpha2Code

export const isAlpha2Code: Predicate.Refinement<unknown, Alpha2Code> = (u): u is Alpha2Code =>
typeof u === 'string' && u in iso3166.getAlpha2Codes()

export const Alpha2CodeSchema: Schema.Schema<Alpha2Code, string> = Schema.String.pipe(Schema.filter(isAlpha2Code))

export const guessCountry = (location: string): Option.Option<Alpha2Code> =>
Option.fromNullable(iso3166.getAlpha2Code(location, 'en')).pipe(Option.filter(isAlpha2Code))
4 changes: 2 additions & 2 deletions src/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ function usersByCareerStage({ width } = {}) {
```js
function usersByLocation() {
return Inputs.table(
usersInTimePeriod.flatMap(user => (user.location ? { location: user.location } : [])),
{ header: { location: 'Location' }, sort: 'location' },
usersInTimePeriod.flatMap(user => (user.location ? { location: user.location, country: user.country } : [])),
{ header: { country: 'Country', location: 'Location' }, sort: 'location' },
)
}
```
Expand Down

0 comments on commit 607d5a2

Please sign in to comment.