Skip to content

Commit

Permalink
Merge pull request #13 from bettyblocks/feat/date-picker-PAGE-1557
Browse files Browse the repository at this point in the history
feat: prevent options being generated
  • Loading branch information
benjamin-rocks authored May 9, 2022
2 parents a4a155c + f80681c commit e70fa43
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
14 changes: 10 additions & 4 deletions src/prefabs/factories/component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import type { IdentityRecordBy } from '../../type-utils';
import type { PrefabComponent, PrefabReference } from '../types/component';

function isNotNullEntry <T, X>(entry: [T, X]): entry is [T, NonNullable<X>] {
const [, option] = entry;
return option !== null;
}

type RequiredAttrs = Omit<PrefabComponent, 'name' | 'descendants'>;
type UnresolvedAttributes = IdentityRecordBy<
RequiredAttrs,
'options',
[string]
[string],
'nullable'
>;

const resolveAttributes = (attrs: UnresolvedAttributes): RequiredAttrs => {
const options = Object.entries(attrs.options).map(([key, option]) =>
option(key),
);
const options = Object.entries(attrs.options)
.filter(isNotNullEntry)
.map(([key, option]) => option(key));

return {
...attrs,
Expand Down
16 changes: 12 additions & 4 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ type Singleton<T> = T extends Array<any> ? T[0] : T;
type Identities<T, Args extends Array<any>> = {
[K in keyof T]: Identity<T[K], Args>;
};
type IdentityRecords<T, Args extends Array<any>> = {
[K in keyof T]: Record<string, Identity<Singleton<T[K]>, Args>>;
type IdentityRecords<T, Args extends Array<any>, Features> = {
[K in keyof T]: Record<
string,
Features extends 'nullable'
? Identity<Singleton<T[K]>, Args> | null
: Identity<Singleton<T[K]>, Args>
>;
};
// type Singletons<T> = { [K in keyof T]: Singleton<T[K]> };

export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export type IdentityBy<T, K extends keyof T, ARGS extends Array<any>> = Omit<
T,
K
> &
Identities<Pick<T, K>, ARGS>;

type IdentityFeatures = 'nullable' | ''

export type IdentityRecordBy<
T,
K extends keyof T,
Args extends Array<any>,
> = Omit<T, K> & IdentityRecords<Pick<T, K>, Args>;
Features extends IdentityFeatures = '',
> = Omit<T, K> & IdentityRecords<Pick<T, K>, Args, Features>;
37 changes: 34 additions & 3 deletions tests/prefabs/factories/component.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import test from 'tape';
import { component, partial } from '../../../src/prefabs/factories/component';
import { variable, showIfTrue } from '../../../src/prefabs/factories/options';
import {
variable,
showIfTrue,
toggle,
} from '../../../src/prefabs/factories/options';

test('component builds empty component', (t) => {
const result = component('Text', {options: {}}, []);
const result = component('Text', { options: {} }, []);
const expected = {
name: 'Text',
options: [],
descendants: [],
type: 'COMPONENT'
type: 'COMPONENT',
};

t.deepEqual(result, expected);
t.end();
});

test('component ignores null options', (t) => {
const result = component(
'Text',
{
options: {
shouldNotExist: null,
shouldExist: toggle('ShouldExist', { value: false }),
},
},
[],
);
const expected = {
name: 'Text',
options: [{
label: 'ShouldExist',
key: 'shouldExist',
type: 'TOGGLE',
value: false
}],
descendants: [],
type: 'COMPONENT',
};

t.deepEqual(result, expected);
Expand Down

0 comments on commit e70fa43

Please sign in to comment.