Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created August 27, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruprict/f1f497b547944ca2d50e to your computer and use it in GitHub Desktop.
Save ruprict/f1f497b547944ca2d50e to your computer and use it in GitHub Desktop.
function hasValue(value) {
return !!value
|| value === 0
|| value === false
}
/**
* @description
* getSubset returns an object with the same structure as the original object passed in, but contains
* only the specified paths and only if those paths have "value" (truth-y values, 0 or false).
*
* @param {Object} obj The object from which to create a subset.
* @param {String[]} paths An array of paths e.g. ['deeply.nested.key'], that should be included in the subset.
*
* @return {Object} An object that contains only the specified paths if they hold something of value.
*/
export function getSubset(obj, paths) {
if (!paths) return obj
let subset = {};
let latestState= obj.computedStates[obj.computedStates.length -1].state;
paths.forEach((path) => {
const keys = path.split('.')
const length = keys.length
const lastIndex = length - 1
let index = 0
let value = latestState
let nested = subset
// Retrieve value specified by path
while (value && index < length) {
value = value[keys[index++]]
}
// Add to subset if the specified path is defined and hasValue
if (index === length && hasValue(value)) {
keys.forEach((key, i) => {
if (i === lastIndex) {
nested[key] = value
} else if (!nested[key]) {
nested[key] = {}
}
nested = nested[key]
})
}
});
return subset
}
export default function devToolsFilter(paths) {
if (typeof paths === 'string') paths = [paths]
return (storage) => ({
...storage,
put: (key, state, callback) => {
storage.put(key, getSubset(state, paths), callback)
}
})
}
@ngokevin
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment