Add flow to array reducer
This commit is contained in:
parent
59b75a3ea3
commit
bd8640917a
|
@ -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();
|
||||
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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<ArrayReducerFactory>(reducerStructureDescriptor: ReducerStructure) {
|
||||
return(state: any[] = [], { type, payload = []}: ArrayReducerAction = {}): any[] => {
|
||||
switch(type) {
|
||||
case 'BLARG3':
|
||||
return [ state, ...payload ];
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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}),
|
||||
};
|
Loading…
Reference in New Issue