Skip to content

Commit 9367a41

Browse files
committed
Add functional set
1 parent a39dbf0 commit 9367a41

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ var _notevil = require("notevil");
3737

3838
var _notevil2 = _interopRequireDefault(_notevil);
3939

40+
var _set2 = require("lodash/fp/set");
41+
42+
var _set3 = _interopRequireDefault(_set2);
43+
4044
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
4145

4246
// Evaluates an expression in a safe way
@@ -583,6 +587,17 @@ function selectOrSet(projection, obj, valueToSet, type) {
583587
return value;
584588
}
585589

590+
function set(projection, obj, valueToSet, type) {
591+
if (!obj) {
592+
obj = this;
593+
}
594+
595+
// Support [] array syntax
596+
var parts = typeof projection === "string" ? _objectpath2.default.parse(projection) : projection;
597+
598+
return (0, _set3.default)(parts, valueToSet)(obj);
599+
}
600+
586601
var validateBySchema = function validateBySchema(schema, value) {
587602
return _tv2.default.validateResult(value, schema);
588603
};
@@ -687,5 +702,6 @@ exports.default = {
687702
selectOrSet: selectOrSet,
688703
getValueFromModel: getValueFromModel,
689704
getTitleByValue: getTitleByValue,
690-
removeEmpty: removeEmpty
705+
removeEmpty: removeEmpty,
706+
set: set
691707
};

src/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import isUndefined from "lodash/isUndefined";
66
import ObjectPath from "objectpath";
77
import tv4 from "tv4";
88
import notevil from "notevil";
9+
import _set from "lodash/fp/set";
910

1011
// Evaluates an expression in a safe way
1112
const safeEval = (condition, scope) => {
@@ -617,6 +618,20 @@ function selectOrSet(projection, obj, valueToSet, type) {
617618
return value;
618619
}
619620

621+
function set(projection, obj, valueToSet, type) {
622+
if (!obj) {
623+
obj = this;
624+
}
625+
626+
// Support [] array syntax
627+
const parts =
628+
typeof projection === "string"
629+
? ObjectPath.parse(projection)
630+
: projection;
631+
632+
return _set(parts, valueToSet)(obj);
633+
}
634+
620635
const validateBySchema = (schema, value) => tv4.validateResult(value, schema);
621636

622637
const validate = (form, value, getLocalizedString) => {
@@ -721,5 +736,6 @@ export default {
721736
selectOrSet,
722737
getValueFromModel,
723738
getTitleByValue,
724-
removeEmpty
739+
removeEmpty,
740+
set
725741
};

0 commit comments

Comments
 (0)