Skip to content

Commit

Permalink
Add functional set
Browse files Browse the repository at this point in the history
  • Loading branch information
psamim committed Jun 13, 2019
1 parent a39dbf0 commit 9367a41
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var _notevil = require("notevil");

var _notevil2 = _interopRequireDefault(_notevil);

var _set2 = require("lodash/fp/set");

var _set3 = _interopRequireDefault(_set2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function set(projection, obj, valueToSet, type) {
if (!obj) {
obj = this;
}

// Support [] array syntax
var parts = typeof projection === "string" ? _objectpath2.default.parse(projection) : projection;

return (0, _set3.default)(parts, valueToSet)(obj);
}

var validateBySchema = function validateBySchema(schema, value) {
return _tv2.default.validateResult(value, schema);
};
Expand Down Expand Up @@ -687,5 +702,6 @@ exports.default = {
selectOrSet: selectOrSet,
getValueFromModel: getValueFromModel,
getTitleByValue: getTitleByValue,
removeEmpty: removeEmpty
removeEmpty: removeEmpty,
set: set
};
18 changes: 17 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isUndefined from "lodash/isUndefined";
import ObjectPath from "objectpath";
import tv4 from "tv4";
import notevil from "notevil";
import _set from "lodash/fp/set";

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

function set(projection, obj, valueToSet, type) {
if (!obj) {
obj = this;
}

// Support [] array syntax
const parts =
typeof projection === "string"
? ObjectPath.parse(projection)
: projection;

return _set(parts, valueToSet)(obj);
}

const validateBySchema = (schema, value) => tv4.validateResult(value, schema);

const validate = (form, value, getLocalizedString) => {
Expand Down Expand Up @@ -721,5 +736,6 @@ export default {
selectOrSet,
getValueFromModel,
getTitleByValue,
removeEmpty
removeEmpty,
set
};

0 comments on commit 9367a41

Please sign in to comment.