Created
March 21, 2023 19:17
-
-
Save gordonbrander/9362f9b19c2f7771f9736c88f6191d2c to your computer and use it in GitHub Desktop.
freeze.js - immutable JS object helpers
This file contains 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
// 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