Skip to content

Commit

Permalink
Rename lift to def (thanks Ben)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAForbes committed Feb 13, 2024
1 parent a6e34df commit ab92f29
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/index.ts
Expand Up @@ -22,7 +22,7 @@ export type CoreAPI<N extends string, D extends Definition> = {
instance: InternalInstance<N, D, keyof D>,
options: MatchOptions<D, T>,
) => T
lift: <T>(
def: <T>(
fn: (x: InternalInstance<N, D, keyof D>) => T,
) => (x: InternalInstance<N, D, keyof D>) => T
}
Expand Down Expand Up @@ -103,7 +103,7 @@ function match(instance: any, options: any): any {
return options[instance.tag](instance.value)
}

function lift(fn: any): any {
function def(fn: any): any {
return (x: any) => fn(x)
}

Expand All @@ -122,7 +122,7 @@ export function type<N extends string, D extends Definition>(
patterns: {},
definition,
match,
lift,
def,
otherwise: (tagNames = tags) => otherwise(tagNames),
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"module": "./dist/sum-type.esm.js",
"unpkg": "./dist/sum-type.esm.js",
"scripts": {
"test": "npx tsc ./test/index.ts --noEmit && node --import tsx --test test/*.ts",
"test": "node --import tsx --test test/*.ts",
"dev": "node --watch --import tsx --test test/*.ts",
"build:bundle": "esbuild lib/index.ts --bundle --format=esm --sourcemap --allow-overwrite --outfile=./dist/sum-type.esm.js",
"build:types": "npx tsc ./test/index.ts --noEmit --esModuleInterop && npx tsc",
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -25,14 +25,14 @@ const loaded =
const loading =
Loaded.N(55)

const render = Loaded.lift( x =>
const render = Loaded.def( x =>
Loaded.bifold(
x
, x => `Loading: ${x}%`
, x => `Loaded: ${x}`
))

const transform = Loaded.lift( x =>
const transform = Loaded.def( x =>
Loaded.mapY(
x, x => x.toUpperCase()
))
Expand Down Expand Up @@ -386,7 +386,7 @@ instance.tag === 'Loaded' ? instance.value.title : 'No Title'
Resource.getLoaded(instance, 'No Title', x => x.title)
```

### `type.lift`
### `type.def`

Useful for defining a reusable function that accepts an instance of your type as an argument. Note you can do this manually with type utilities like `Instance` but doing it this way is may be preferable as the type of the parameter is inferred for you.

Expand All @@ -403,7 +403,7 @@ const render = (x: T.Instance<typeof Loaded> ) =>
```

```typescript
const render = Loaded.lift( x =>
const render = Loaded.def( x =>
Loaded.bifold(
x
, x => `Loading: ${x}%`
Expand Down Expand Up @@ -641,7 +641,7 @@ type Single = T.Instance<typeof ExampleResource.Loaded>
// | { type: 'ExampleResource', tag: 'Loaded', value: { id: string, title: string } }
```
Can be useful for annotating custom functions. But note `type.lift` can be more convenient as the `Instance` type is automatically inferred for you.
Can be useful for annotating custom functions. But note `type.def` can be more convenient as the `Instance` type is automatically inferred for you.
## FAQ
Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Expand Up @@ -390,12 +390,12 @@ test('readme', () => {
})


test('lift', () => {
test('def', () => {
type File = { file_id: string, file_url: string, file_ext: string, file_upname: string }

const FileResource = T.Resource<'FileResource', File>('FileResource')

const fn = FileResource.lift( x => {
const fn = FileResource.def( x => {
return x.tag
})

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -6,7 +6,8 @@
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "./dist",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["lib/**.ts"],
}

0 comments on commit ab92f29

Please sign in to comment.