Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 84a4a68

Browse files
committed
Wrapping get and toPath to be compatible with latest Lodash
1 parent 3450962 commit 84a4a68

File tree

12 files changed

+54
-16
lines changed

12 files changed

+54
-16
lines changed

src/actions/field-actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _get from 'lodash/get';
1+
import _get from '../utils/get';
22
import mapValues from '../utils/map-values';
33

44
import actionTypes from '../action-types';

src/actions/model-actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _get from 'lodash/get';
1+
import _get from '../utils/get';
22
import endsWith from 'lodash/endsWith';
33
import isEqual from 'lodash/isEqual';
44
import icepick from 'icepick';

src/components/control-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, createElement, cloneElement, PropTypes } from 'react';
22
import connect from 'react-redux/lib/components/connect';
3-
import _get from 'lodash/get';
3+
import _get from '../utils/get';
44
import merge from 'lodash/merge';
55

66
import { invertValidity, getFieldFromState, getValidity } from '../utils';

src/components/errors-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component, PropTypes } from 'react';
22
import connect from 'react-redux/lib/components/connect';
3-
import _get from 'lodash/get';
3+
import _get from '../utils/get';
44
import map from 'lodash/map';
55
import compact from 'lodash/compact';
66
import iteratee from 'lodash/iteratee';

src/components/field-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component, PropTypes } from 'react';
22
import shallowCompare from 'react/lib/shallowCompare';
33
import connect from 'react-redux/lib/components/connect';
44

5-
import _get from 'lodash/get';
5+
import _get from '../utils/get';
66
import identity from 'lodash/identity';
77
import isEqual from 'lodash/isEqual';
88
import omit from 'lodash/omit';

src/components/form-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component, PropTypes } from 'react';
22
import connect from 'react-redux/lib/components/connect';
3-
import _get from 'lodash/get';
3+
import _get from '../utils/get';
44
import mapValues from '../utils/map-values';
55
import merge from 'lodash/merge';
66

src/reducers/form-reducer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import _get from 'lodash/get';
1+
import _get from '../utils/get';
22
import every from 'lodash/every';
33
import icepick from 'icepick';
44
import isBoolean from 'lodash/isBoolean';
55
import isEqual from 'lodash/isEqual';
66
import isPlainObject from 'lodash/isPlainObject';
77
import map from 'lodash/map';
88
import mapValues from '../utils/map-values';
9-
import toPath from 'lodash/toPath';
9+
import toPath from '../utils/to-path';
1010
import startsWith from 'lodash/startsWith';
1111
import flatten from 'flat';
1212

@@ -282,7 +282,9 @@ function _createFormReducer(model, initialState) {
282282

283283
case actionTypes.SET_FIELDS_VALIDITY:
284284
return map(action.fieldsValidity, (fieldValidity, field) =>
285-
actions.setValidity(`${model}.${field}`, fieldValidity, action.options)
285+
actions.setValidity(field.length
286+
? `${model}.${field}`
287+
: model, fieldValidity, action.options)
286288
).reduce(formReducer, state);
287289

288290
case actionTypes.SET_ERRORS: {

src/reducers/model-reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import _get from 'lodash/get';
1+
import _get from '../utils/get';
22
import icepick from 'icepick';
33
import isEqual from 'lodash/isEqual';
4-
import toPath from 'lodash/toPath';
4+
import toPath from '../utils/to-path';
55

66
import actionTypes from '../action-types';
77

src/utils/get.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import _get from 'lodash/get';
2+
import endsWith from 'lodash/endsWith';
3+
4+
export default function get(object, path, defaultValue) {
5+
let modelString = path;
6+
7+
if (endsWith(modelString, '.')) {
8+
modelString = modelString.slice(0, -1);
9+
} else if (endsWith(modelString, '[]')) {
10+
modelString = modelString.slice(0, -2);
11+
}
12+
13+
return _get(object, modelString, defaultValue);
14+
}

src/utils/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import isPlainObject from 'lodash/isPlainObject';
44
import every from 'lodash/every';
55
import some from 'lodash/some';
66
import findKey from 'lodash/findKey';
7-
import get from 'lodash/get';
8-
import toPath from 'lodash/toPath';
7+
import _get from '../utils/get';
8+
import toPath from '../utils/to-path';
99
import startsWith from 'lodash/startsWith';
1010
import memoize from 'lodash/memoize';
1111

@@ -64,7 +64,7 @@ const getFormStateKey = memoize((state, model) => {
6464
function getForm(state, model) {
6565
const formStateKey = getFormStateKey(state, model);
6666

67-
return get(state, formStateKey);
67+
return _get(state, formStateKey);
6868
}
6969

7070
function getFieldFromState(state, model) {
@@ -74,6 +74,7 @@ function getFieldFromState(state, model) {
7474

7575
const formPath = toPath(form.model);
7676
const fieldPath = toPath(model).slice(formPath.length);
77+
7778
return getField(form, fieldPath.length ? [fieldPath.join('.')] : []);
7879
}
7980

@@ -119,6 +120,12 @@ function isInvalid(errors) {
119120
return !!errors;
120121
}
121122

123+
function getModelPath(model, field = '') {
124+
return field.length
125+
? `${model}.${field}`
126+
: model;
127+
}
128+
122129
export {
123130
isFocused,
124131
isMulti,
@@ -133,4 +140,5 @@ export {
133140
getFieldFromState,
134141
invertValidators,
135142
invertValidity,
143+
getModelPath,
136144
};

0 commit comments

Comments
 (0)