We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow get and set by path
const get = (object, keys, defaultVal = null) => { // check if object actually exists if (typeof object === "object" && object !== null) { keys = Array.isArray(keys) ? keys : keys.replace(/(\[(\d)\])/g, ".$2").split("."); const value = keys.reduce( (obj, key) => (obj && obj[key] !== "undefined" ? obj[key] : null), object ); return value !== null && typeof value !== "undefined" ? value : defaultVal; } return defaultVal; }; const set = (obj = {}, keys, value) => { keys = Array.isArray(keys) ? keys : keys.replace(/(\[(\d)\])/g, ".$2").split("."); const key = keys[0]; if (keys.length > 1) { keys.shift(); obj[key] = set(obj[key], keys, value); } else { obj[key] = value; } return obj; }; const useEtters = initialState => { const [state, setState] = useState(initialState); return [ (keys, defaultVal) => get(state, keys, defaultVal), (keys, value) => setState(s => set(s, keys, value)) ]; };
The text was updated successfully, but these errors were encountered:
FYI typically getters/setters are collectively referred to as "accessors"
Sorry, something went wrong.
No branches or pull requests
allow get and set by path
The text was updated successfully, but these errors were encountered: