Created
September 19, 2019 19:54
-
-
Save tcrowe/89ffd619150c2d2fd32db5c0c601b46c to your computer and use it in GitHub Desktop.
extending the svelte store writable with your own methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { writable, get } = require("svelte/store"); | |
const merge = require("lodash/merge"); | |
/** | |
* A svelte store writable with two extra methods | |
* | |
* Usage: | |
* const store = customStore({ name: "Erasmus" }); | |
* store.merge({ name: "Billy" }); | |
* store.reset(); | |
* | |
* @param defaultValue | |
*/ | |
function customStore(defaultValue){ | |
const op = writable(defaultValue) | |
op.merge = obj => op.set(merge(get(op), obj)); | |
op.reset = () => op.set(defaultValue); | |
return op; | |
} | |
modex.exports = customStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment