diff --git a/src/redux-arg/generateReducer.js b/src/redux-arg/generateReducer.js index 1639d9f..2aa5f01 100644 --- a/src/redux-arg/generateReducer.js +++ b/src/redux-arg/generateReducer.js @@ -6,8 +6,6 @@ import { PROP_TYPES } from './constants'; export function buildReducers(structure) { - //TODO - allow for building single primitive reducers - const tmp = combineReducers(reduce(structure, (memo, propValue, propName) => { const { structure } = propValue(); diff --git a/src/redux-arg/reducers.js b/src/redux-arg/reducers.js index e269930..b2feb98 100644 --- a/src/redux-arg/reducers.js +++ b/src/redux-arg/reducers.js @@ -10,61 +10,6 @@ import { compose } from 'ramda'; -type ObjectReducerAction = { - type: string, - payload?: Object, -}; -type ObjectReducer = (Object) => (Object, ObjectReducerAction) => Object; - -type Primitive = string | number; -type PrimitiveReducerAction = { - type: string, - payload?: string | number, -}; -type PrimitiveReducer = (Primitive) => (Primitive, PrimitiveReducerAction) => Primitive; - -type ArrayReducerAction = { - type: string, - payload?: Array, - index?: number, -}; -type ArrayReducer = (Array) => (Array, ArrayReducerAction) => Array; - -const REDUCER_FUNCTIONS = { - objectReducer(reducerStructureDescriptor): ObjectReducer { - const { structure } = reducerStructureDescriptor(); - return (state = determineDefaults(reducerStructureDescriptor)(structure), { type, payload } = {}) => { - switch (type) { - case 'BLARG!': - return { state, ...reducerStructureDescriptor, ...payload }; - default: - return state; - } - } - }, - primitiveReducer(reducerStructureDescriptor): PrimitiveReducer { - const { structure } = reducerStructureDescriptor(); - return (state = determineDefaults(reducerStructureDescriptor)(structure), { type, payload } = {}) => { - switch(type) { - case 'BLARG2': - return { state, ...structure, ...payload }; - default: - return state; - } - } - }, - arrayReducer(reducerStructureDescriptor): ArrayReducer { - return (state = [], { type, payload } = {}) => { - switch(type) { - case 'BLARG3': - return { state, ...reducerStructureDescriptor, ...payload }; - default: - return state; - } - } - } -}; - const DEFAULTS_FUNCTIONS = new Map([ [PROP_TYPES._shape, objectDefaults], [PROP_TYPES._array, arrayDefaults], diff --git a/src/redux-arg/reducers/arrayReducer.js b/src/redux-arg/reducers/arrayReducer.js new file mode 100644 index 0000000..b5df5fc --- /dev/null +++ b/src/redux-arg/reducers/arrayReducer.js @@ -0,0 +1,21 @@ +//@flow +import type { ReducerStructure } from '../structure'; + +export type ArrayReducerAction = { + type: string, + payload?: any[], + index?: number, +}; +export type ArrayReducerFactory = (structure: Object) => ArrayReducer; +export type ArrayReducer = (state: any[], action: ArrayReducerAction) => any[]; + +export function arrayReducer(reducerStructureDescriptor: ReducerStructure) { + return(state: any[] = [], { type, payload = []}: ArrayReducerAction = {}): any[] => { + switch(type) { + case 'BLARG3': + return [ state, ...payload ]; + default: + return state; + } + } +} diff --git a/src/redux-arg/reducers/objectReducer.js b/src/redux-arg/reducers/objectReducer.js new file mode 100644 index 0000000..f090448 --- /dev/null +++ b/src/redux-arg/reducers/objectReducer.js @@ -0,0 +1,19 @@ +//@flow +type ObjectReducerAction = { + type: string, + payload?: Object, +}; +type ObjectReducer = (Object) => (Object, ObjectReducerAction) => Object; + +export function objectReducer(reducerStructureDescriptor: Object): ObjectReducer { + const { structure } = reducerStructureDescriptor(); + return (state = determineDefaults(reducerStructureDescriptor)(structure), { type, payload } = {}) => { + switch (type) { + case 'BLARG!': + return { state, ...reducerStructureDescriptor, ...payload }; + default: + return state; + } + } +} + diff --git a/src/redux-arg/reducers/primitiveReducer.js b/src/redux-arg/reducers/primitiveReducer.js new file mode 100644 index 0000000..3d7de3e --- /dev/null +++ b/src/redux-arg/reducers/primitiveReducer.js @@ -0,0 +1,19 @@ +//@flow +type Primitive = string | number; +type PrimitiveReducerAction = { + type: string, + payload?: string | number, +}; +type PrimitiveReducer = (Primitive) => (Primitive, PrimitiveReducerAction) => Primitive; + +export function primitiveReducer(reducerStructureDescriptor): PrimitiveReducer { + const { structure } = reducerStructureDescriptor(); + return (state = determineDefaults(reducerStructureDescriptor)(structure), { type, payload } = {}) => { + switch(type) { + case 'BLARG2': + return { state, ...structure, ...payload }; + default: + return state; + } + } +} diff --git a/src/redux-arg/structure.js b/src/redux-arg/structure.js new file mode 100644 index 0000000..a4a2794 --- /dev/null +++ b/src/redux-arg/structure.js @@ -0,0 +1,37 @@ +//@flow + +//============================== +// Flow types +//============================== +export type ReducerStructure = { + [key: string]: StructureType +} + +export type StructureType = (structure: ?ReducerStructure) => { + type: string, + structure: [StructureType | ReducerStructure] +}; + +//============================== +// Structure +//============================== +export const PROP_TYPES = { + _string: '_string', + _number: '_number', + _reducer: '_reducer', + _shape: '_shape', + _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 }), + arrayOf: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._array, structure }), + reducer: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._reducer, structure }), + shape: (structure: ReducerStructure) => () => ({ type: PROP_TYPES._shape, structure}), +};