Skip to content

Commit

Permalink
refactor: use custom match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
waynevanson committed Aug 27, 2023
1 parent bd566d6 commit c89a0f6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
22 changes: 7 additions & 15 deletions .vault/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
"type": "split",
"children": [
{
"id": "783a819f9359185b",
"id": "a69a62ec0b11ace7",
"type": "tabs",
"children": [
{
"id": "07a69522aff335a3",
"id": "87c79401221f8db2",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Sample 02.md",
"mode": "source",
"source": true
}
"type": "empty",
"state": {}
}
}
]
Expand Down Expand Up @@ -85,7 +81,6 @@
"state": {
"type": "backlink",
"state": {
"file": "Sample 02.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -102,7 +97,6 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Sample 02.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -124,9 +118,7 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "Sample 02.md"
}
"state": {}
}
}
],
Expand All @@ -147,11 +139,11 @@
"command-palette:Open command palette": false
}
},
"active": "07a69522aff335a3",
"active": "87c79401221f8db2",
"lastOpenFiles": [
"Sample 02.md",
"data.md",
"Sample 01.md",
"Sample 02.md",
"d.md",
"NonObject.md",
"Yello world.md",
Expand Down
24 changes: 12 additions & 12 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"id": "data-entry",
"name": "Data Entry",
"version": "7.0.0",
"minAppVersion": "0.15.0",
"description": "Create forms that save data simply; the data view of data entry",
"author": "Wayne Van Son",
"authorUrl": "https://github.com/waynevanson",
"fundingUrl": {
"GitHub Sponsors": "https://github.com/sponsors/waynevanson"
},
"isDesktopOnly": false
}
"id": "data-entry",
"name": "Data Entry",
"version": "7.0.0",
"minAppVersion": "0.15.0",
"description": "Create forms that save data simply; the data view of data entry",
"author": "Wayne Van Son",
"authorUrl": "https://github.com/waynevanson",
"fundingUrl": {
"GitHub Sponsors": "https://github.com/sponsors/waynevanson"
},
"isDesktopOnly": false
}
20 changes: 20 additions & 0 deletions packages/data-entry/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,23 @@ export const configuration = pipe(
}),
),
);

export const match =
<
T extends Record<string, unknown>,
CS extends T extends unknown
? {
[P in keyof T]: (argument: T[P]) => unknown;
}
: never,
>(
cases: CS,
) =>
(sum: T): ReturnType<CS[keyof CS]> => {
const name = Object.keys(sum)[0] as keyof T;
const value = sum[name];
const fn = cases[name as keyof CS];
return fn(value) as never;
};

pipe({ one: 'two' }, match({ one: () => '' }));
37 changes: 17 additions & 20 deletions packages/data-entry/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import * as React from 'react';
import { StrictMode } from 'react';
import { Root, createRoot } from 'react-dom/client';
import { configuration } from './common';
import { configuration, match } from './common';
import { Application } from './components';
import { ApplicationProvider } from './components/context';
import { useTheme } from './components/material';
Expand Down Expand Up @@ -78,32 +78,29 @@ export class MainPlugin extends Plugin {
),
either.bindW('schema', ({ config }) =>
pipe(
'inline' in config.schema
? either.right(config.schema.inline)
: pipe(
getFrontmatterByPath(config.schema.file.path),
either.map(
(json) =>
//@ts-expect-error
json[config.schema.file.frontmatter] as never,
),
config.schema,
match({
inline: (inline) => either.right(inline),
file: ({ path, frontmatter }) =>
pipe(
getFrontmatterByPath(path),
either.map((json) => json[frontmatter] as never),
),
}),
),
),
either.bindW('uischema', ({ config }) =>
pipe(
option.fromNullable(config?.uischema),
option.traverse(either.Applicative)((uischema) =>
'inline' in uischema
? either.right(uischema.inline)
: pipe(
getFrontmatterByPath(uischema.file.path),
either.map(
(json) =>
//@ts-expect-error
json[config.uischema.file.frontmatter] as never,
),
option.traverse(either.Applicative)(
match({
inline: (inline) => either.right(inline),
file: ({ frontmatter, path }) =>
pipe(
getFrontmatterByPath(path),
either.map((json) => json[frontmatter] as never),
),
}),
),
either.map(option.toNullable),
),
Expand Down

0 comments on commit c89a0f6

Please sign in to comment.