Skip to content

Commit

Permalink
Merge pull request #31 from pelotom/optional-creation-args
Browse files Browse the repository at this point in the history
Make creation function argument optional for variants of type {}
  • Loading branch information
pelotom authored Apr 22, 2018
2 parents 3d3e523 + b8f9c2c commit ecae215
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import { unionize, ofType } from 'unionize'

// Define a record mapping tag literals to value types
const Action = unionize({
ADD_TODO: ofType<{ id: string; text: string }>(),
SET_VISIBILITY_FILTER: ofType<'SHOW_ALL' | 'SHOW_ACTIVE' | 'SHOW_COMPLETED'>(),
TOGGLE_TODO: ofType<{ id: string }>()
ADD_TODO: ofType<{ id: string; text: string }>(),
SET_VISIBILITY_FILTER: ofType<'SHOW_ALL' | 'SHOW_ACTIVE' | 'SHOW_COMPLETED'>(),
TOGGLE_TODO: ofType<{ id: string }>(),
CLEAR_TODOS: {}, // For "empty" types, just use {}
},
// optionally override tag and/or value property names
{
Expand All @@ -39,6 +40,7 @@ type Action =
| { type: ADD_TODO; payload: { id: string; text: string } }
| { type: SET_VISIBILITY_FILTER; payload: 'SHOW_ALL' | 'SHOW_ACTIVE' | 'SHOW_COMPLETED' }
| { type: TOGGLE_TODO; payload: { id: string } }
| { type: CLEAR_TODOS; payload: {} }
```
Having done that, you now have at your disposal:
Expand All @@ -48,6 +50,7 @@ Having done that, you now have at your disposal:
```ts
store.dispatch(Action.ADD_TODO({ id: 'c819bbc1', text: 'Take out the trash' }));
store.dispatch(Action.SET_VISIBILITY_FILTER('SHOW_COMPLETED'));
store.dispatch(Action.CLEAR_TODOS()); // no argument required if value type is {}
```

#### Match expressions
Expand Down
4 changes: 4 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('merged', () => {
const Foo = unionize({
x: ofType<{ n: number }>(),
y: ofType<{ s: string }>(),
z: ofType<{}>(),
});

let foo: typeof Foo._Union;
Expand All @@ -17,6 +18,7 @@ describe('merged', () => {
tag: 'x',
n: 3,
});
expect(Foo.z()).toEqual(Foo.z({}));
});

it('predicates', () => {
Expand All @@ -34,6 +36,7 @@ describe('merged', () => {
Foo.match({
x: ({ n }) => n + 9,
y: ({ s }) => s.length,
z: () => 42,
})(foo),
).toBe(12);
expect(
Expand All @@ -49,6 +52,7 @@ describe('merged', () => {
Foo.match(foo, {
x: ({ n }) => n + 9,
y: ({ s }) => s.length,
z: () => 42,
}),
).toBe(12);
expect(
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export type Unionized<Record, TaggedRecord> = {
} & Creators<Record, TaggedRecord>;

export type Creators<Record, TaggedRecord> = {
[T in keyof Record]: (value: Record[T]) => TaggedRecord[keyof TaggedRecord]
[T in keyof Record]: {} extends Record[T]
? ((value?: {}) => TaggedRecord[keyof TaggedRecord])
: ((value: Record[T]) => TaggedRecord[keyof TaggedRecord])
};

export type Predicates<TaggedRecord> = {
Expand Down Expand Up @@ -88,8 +90,8 @@ export function unionize<Record>(record: Record, config?: { value?: string; tag?

const creators = {} as Creators<Record, any>;
for (const tag in record) {
creators[tag] = (value: any) =>
valProp ? { [tagProp]: tag, [valProp]: value } : { ...value, [tagProp]: tag };
creators[tag] = ((value = {}) =>
valProp ? { [tagProp]: tag, [valProp]: value } : { ...value, [tagProp]: tag }) as any;
}

const is = {} as Predicates<any>;
Expand Down

0 comments on commit ecae215

Please sign in to comment.