Added type annotations to structure

This commit is contained in:
Kai Moseley 2016-11-29 12:30:26 +00:00
parent bd8640917a
commit 6d9c887486
5 changed files with 48 additions and 39 deletions

View File

@ -6,11 +6,6 @@ export const PROP_TYPES = {
_array: '_array',
};
export const TYPE_DEFAULTS = new Map([
[PROP_TYPES._string, ''],
[PROP_TYPES._number, 0],
]);
export const Types = {
string: () => ({ type: PROP_TYPES._string, structure: PROP_TYPES._string }),
number: () => ({ type: PROP_TYPES._number, structure: PROP_TYPES._number }),

View File

@ -1,10 +1,12 @@
//@flow
import type { ReducerStructure } from './structure';
import { combineReducers } from 'redux';
import { reduce, find } from 'lodash';
import { createReducer } from './reducers';
import { PROP_TYPES } from './constants';
export function buildReducers(structure) {
export function buildReducers(structure: ReducerStructure) {
const tmp = combineReducers(reduce(structure, (memo, propValue, propName) => {
const { structure } = propValue();

View File

@ -3,12 +3,11 @@ import {
PROP_TYPES,
TYPE_DEFAULTS,
} from './constants';
import {
reduce
} from 'lodash';
import {
compose
} from 'ramda';
import { reduce } from 'lodash';
import { compose } from 'ramda';
import { primitiveReducer } from './reducers/primitiveReducer';
import { objectReducer } from './reducers/objectReducer';
import { arrayReducer } from './reducers/arrayReducer';
const DEFAULTS_FUNCTIONS = new Map([
[PROP_TYPES._shape, objectDefaults],
@ -36,9 +35,9 @@ function determineReducerType(reducerDescriptor) {
const { structure } = reducerDescriptor();
const { type } = structure();
let reducerFn = REDUCER_FUNCTIONS.primitiveReducer;
if (type === PROP_TYPES._shape) reducerFn = REDUCER_FUNCTIONS.objectReducer;
if (type === PROP_TYPES._array) reducerFn = REDUCER_FUNCTIONS.arrayReducer;
let reducerFn = primitiveReducer;
if (type === PROP_TYPES._shape) reducerFn = objectReducer;
if (type === PROP_TYPES._array) reducerFn = arrayReducer;
return {
reducerFn,
reducerStructureDescriptor: structure,

View File

@ -1,17 +1,18 @@
//@flow
type Primitive = string | number;
type PrimitiveReducerAction = {
type: string,
payload?: string | number,
};
type PrimitiveReducer = (Primitive) => (Primitive, PrimitiveReducerAction) => Primitive;
import type { ReducerStructure } from '../structure';
export function primitiveReducer(reducerStructureDescriptor): PrimitiveReducer {
const { structure } = reducerStructureDescriptor();
return (state = determineDefaults(reducerStructureDescriptor)(structure), { type, payload } = {}) => {
export type ObjectReducerAction = {
type: string,
payload?: Object,
};
export type ObjectReducerFactory = (structure: Object) => ObjectReducer;
export type ObjectReducer = (state: Object, action: ObjectReducerAction) => Object;
export function arrayReducer<ArrayReducerFactory>(reducerStructureDescriptor: ReducerStructure) {
return(state: Object = calculateDefaults(reducerSructure), { type, payload = {}}: ObjectReducerAction = {}): Object => {
switch(type) {
case 'BLARG2':
return { state, ...structure, ...payload };
case 'BLARG3':
return [ state, ...payload ];
default:
return state;
}

View File

@ -4,13 +4,30 @@
// Flow types
//==============================
export type ReducerStructure = {
[key: string]: StructureType
[key: string]: StructureType|PrimitiveType,
}
export type StructureType = () => {
type: string,
structure: StructureType|ReducerStructure
};
export type PrimitiveType = () => {
type: string,
structure: $Keys<typeof PROP_TYPES>,
defaultValue: ?any,
};
export type TypesObject = {
[key: string]: CreateStructure|CreateStringType|CreateNumberType
}
export type StructureType = (structure: ?ReducerStructure) => {
type: string,
structure: [StructureType | ReducerStructure]
};
export type TypesObjectDefaults = {
[key: string]: mixed|TypesArrayDefaults,
}
export type TypesArrayDefaults = Array<mixed>|Array<TypesObjectDefaults>;
type CreateStringType = (defaultValue: string) => PrimitiveType;
type CreateNumberType = (defaultValue: number) => PrimitiveType;
type CreateStructure = (structure: ReducerStructure, defaultValue: TypesArrayDefaults|TypesObjectDefaults) => StructureType;
//==============================
// Structure
@ -23,14 +40,9 @@ export const PROP_TYPES = {
_array: '_array',
};
export const TYPE_DEFAULTS = new Map([
[PROP_TYPES._string, ''],
[PROP_TYPES._number, 0],
]);
export const Types = {
string: () => ({ type: PROP_TYPES._string, structure: PROP_TYPES._string }),
number: () => ({ type: PROP_TYPES._number, structure: PROP_TYPES._number }),
export const Types: TypesObject = {
string: (defaultValue: string = '') => () => ({ type: PROP_TYPES._string, structure: PROP_TYPES._string, defaultValue }),
number: (defaultValue: number = 0) => () => ({ type: PROP_TYPES._number, structure: PROP_TYPES._number, defaultValue }),
arrayOf: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._array, structure }),
reducer: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._reducer, structure }),
shape: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._shape, structure}),