Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created September 19, 2019 19:54
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 tcrowe/89ffd619150c2d2fd32db5c0c601b46c to your computer and use it in GitHub Desktop.
Save tcrowe/89ffd619150c2d2fd32db5c0c601b46c to your computer and use it in GitHub Desktop.
extending the svelte store writable with your own methods
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