Support any type in the pushOrRemove array method

This commit is contained in:
Sancha Pereira 2018-08-24 14:41:53 +01:00
parent 1445408e11
commit 537a2f5e30
3 changed files with 30 additions and 3 deletions

View File

@ -53,6 +53,8 @@
"lodash.keys": "^4.2.0", "lodash.keys": "^4.2.0",
"lodash.map": "^4.6.0", "lodash.map": "^4.6.0",
"lodash.omit": "^4.5.0", "lodash.omit": "^4.5.0",
"lodash.findindex": "^4.6.0",
"lodash.isequal": "^4.5.0",
"redux": "4.0.0" "redux": "4.0.0"
} }
} }

View File

@ -73,12 +73,36 @@ describe('arrayReducer', () => {
describe('pushOrRemove', () => { describe('pushOrRemove', () => {
const { pushOrRemove } = DEFAULT_ARRAY_BEHAVIORS; const { pushOrRemove } = DEFAULT_ARRAY_BEHAVIORS;
it('should push the payload onto the end of the array', () => { it('should push the primitive payload onto the end of the array', () => {
expect(pushOrRemove.reducer([1,2,3], 4)).toEqual([1,2,3,4]); expect(pushOrRemove.reducer([1,2,3], 4)).toEqual([1,2,3,4]);
}); });
it('should remove the payload from the array', () => { it('should remove the primitive payload from the array', () => {
expect(pushOrRemove.reducer([1,2,3], 2)).toEqual([1,3]); expect(pushOrRemove.reducer([1,2,3], 2)).toEqual([1,3]);
}); });
it('should push the object payload onto the end of the array', () => {
let users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false }
];
let expected_users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
expect(pushOrRemove.reducer(users, { 'user': 'pebbles', 'active': true })).toEqual(expected_users);
});
it('should remove the object payload from the array', () => {
let users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
let expected_users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
expect(pushOrRemove.reducer(users, { 'user': 'fred', 'active': false })).toEqual(expected_users);
});
}); });
describe('pop', () => { describe('pop', () => {

View File

@ -59,6 +59,7 @@ import { createReducerBehaviors } from "../reducers";
import { updateAtIndex, removeAtIndex } from "../utils/arrayUtils"; import { updateAtIndex, removeAtIndex } from "../utils/arrayUtils";
import { PROP_TYPES } from "../structure"; import { PROP_TYPES } from "../structure";
import { isCombinedAction, getApplicableCombinedActions } from "./batchUpdates"; import { isCombinedAction, getApplicableCombinedActions } from "./batchUpdates";
import { findIndex, isEqual } from "lodash/fp";
const reduce = require("lodash/fp/reduce").convert({ cap: false }); const reduce = require("lodash/fp/reduce").convert({ cap: false });
@ -138,7 +139,7 @@ export const DEFAULT_ARRAY_BEHAVIORS: ArrayReducerBehaviorsConfig = {
}, },
pushOrRemove: { pushOrRemove: {
reducer(state, payload) { reducer(state, payload) {
let index = state.indexOf(payload); let index = findIndex(isEqual(payload))(state);
if(index == -1) { if(index == -1) {
return [...state, payload]; return [...state, payload];
} }