Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions packages/react-form-renderer/src/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ export const TO_STRING = {}.toString;

const isObject = (obj) => typeof obj === 'object' && TO_STRING.call(obj) === '[object Object]' && obj !== null;

const stringify = (args) => {
let arr = [];
let value;
let options = args;
if (typeof options === 'number') {
options = options.toString();
const stringify = (value) => {
if (value === null) {
return 'null';
}

for (let k in options) {
if (HAS_PROP.call(options, k)) {
value = options[k];
arr.push(k, isValidElement(value) ? stringify(value.props) : isObject(value) ? stringify(value) : value.toString());
}
if (typeof value === 'boolean') {
return value ? 'true' : 'false';
}

if (!value) {
return '';
}

if (typeof value === 'string') {
return value;
}

if (typeof value === 'number') {
return value.toString();
}

return JSON.stringify(arr);
return JSON.stringify(value);
};

export const memoize = (func) => {
Expand All @@ -31,7 +37,7 @@ export const memoize = (func) => {
}

return (value, allValues, ...options) => {
const key = stringify(value, allValues);
const key = stringify(allValues);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do a try catch around the block if for some reason someone passes circular structure and the try/catch fails. Maybe with a console.error but do not crash the entire runtime.

return HAS_PROP.call(func.cache, key) ? func.cache[key] : (func.cache[key] = func(value, allValues, ...options));
};
};
Expand Down