Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

Functions

Type aliases

IMerger

IMerger<T>: function

Represents the "merger" function that combines the new object, with the previous object. Overwritting existing keys

Type parameters

  • T: object

Type declaration

    • (previous_object: T, new_object: T): T
    • Parameters

      • previous_object: T
      • new_object: T

      Returns T

Functions

merge

  • merge<T>(previous_object: T, new_object: T): T
  • Returns the two objects merged with each other

    Type parameters

    • T

    Parameters

    • previous_object: T
    • new_object: T

    Returns T

merged

  • Represents a Writable Svelte Store for a Store that performs shallow delta updates to the current object

    Without Custom "Merger"

    import {merged} from "svelte-commons/lib/stores/shared";
    
    // The Store is initialized with an empty base object, which is immutable by default
    const store = merged({});
    
    // Will log every change to the Store
    store.subscribe(console.log);
    
    store.set({first_name: "John", last_name: "Smith"}); // logs: `{first_name: "John", last_name: "Smith"}`
    store.set({first_name: "Jane"}); // logs: `{first_name: "Jane", last_name: "Smith"}`

    With Custom "Merger"

    import {merged} from "svelte-commons/lib/stores/shared";
    
    const initial_data = {
        name: "James Bond",
        job: {
            title: "Spy",
            agency: "MI6",
            country: "England"
        }
    };
    
    function merger(a, b) {
        // By default, the default "Merger" only merges the top-level object keys, and spreads
        // all of them. For more performance, and to target nested keys, we can use a custom function
        const { name: a_name, job: a_job = {} } = a;
        const { name: b_name, job: b_job = {} } = b;
    
        return {
            name: b_name || a_name,
            job: {
                title: b_job.title || a_job.title,
                agency: b_job.agency || a_job.agency,
                country: b_job.country || a_job.country
            }
        };
    }
    
    // The Store is initialized with an empty base object, which is immutable by default
    const store = merged(initial_data, merger);
    
    // Will log every change to the Store
    store.subscribe(console.log); // logs: `{name:"James Bond",job:{title:"Spy",agency:"MI6",country:"England"}}`
    
    store.set({job: {agency: "FBI", country: "USA"}}); // logs: `{name:"James Bond",job:{title:"Spy",agency:"FBI",country:"USA"}}`

    Type parameters

    • T: object

    Parameters

    Returns Writable<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc