Skip to content

Commit

Permalink
Update minilo get() method to fix CSP unsafe-eval issue
Browse files Browse the repository at this point in the history
(cherry picked from commit 4511fae)
  • Loading branch information
VRuzhentsov authored and ghettovoice committed Apr 20, 2020
1 parent 9fab04d commit fc898f0
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/util/minilo.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,22 @@ export function* range (start, end, step = 1) {
}
}

export function get (object, path, defaultValue) {
// eslint-disable-next-line no-new-func
let fn = new Function('object', `try { return object.${path} } catch (e) {}`)
return coalesce(fn(object), defaultValue)
/**
* https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get
*
* @param obj
* @param path
* @param defaultValue
* @return {*}
*/
export function get (obj, path, defaultValue) {
const travel = regexp =>
String.prototype.split
.call(path, regexp)
.filter(Boolean)
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj)
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/)
return result === undefined || result === obj ? defaultValue : result
}

export function includes (array, value, comparator = isEqual) {
Expand Down

0 comments on commit fc898f0

Please sign in to comment.