Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
Created March 21, 2023 19:17
Show Gist options
  • Save gordonbrander/9362f9b19c2f7771f9736c88f6191d2c to your computer and use it in GitHub Desktop.
Save gordonbrander/9362f9b19c2f7771f9736c88f6191d2c to your computer and use it in GitHub Desktop.
freeze.js - immutable JS object helpers
// Shorthand for Object.freeze
export const freeze = Object.freeze
// Define an immutable model intializer.
// Objects are frozen on the way out.
export const Model = init => flags => freeze(init(flags))
// Put a value to a key, returning a new frozen object
export const put = (state, key, val) => freeze({...state, [key]: val})
// Append an array to another array.
// Returns a frozen array.
export const append = (a, b) => freeze([...a, ...b])
// Merge a patch onto a state.
// Returns a new frozen state.
export const merge = (state, patch) => freeze({...state, ...patch})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment