{"id":20906156,"url":"https://github.com/distributedlife/the-great-mutator-immutable","last_synced_at":"2025-06-13T13:01:59.036Z","repository":{"id":141573045,"uuid":"96201175","full_name":"distributedlife/the-great-mutator-immutable","owner":"distributedlife","description":"Describe mutations, get results! With batching and a change log. With ImmutableJS support.","archived":false,"fork":false,"pushed_at":"2017-07-04T10:09:41.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-19T14:24:02.207Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/distributedlife.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-04T09:33:57.000Z","updated_at":"2017-07-04T10:09:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"0656e26e-bb00-4471-ba83-cf9f99b33091","html_url":"https://github.com/distributedlife/the-great-mutator-immutable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator-immutable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator-immutable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator-immutable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator-immutable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distributedlife","download_url":"https://codeload.github.com/distributedlife/the-great-mutator-immutable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243297558,"owners_count":20268799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T13:29:16.044Z","updated_at":"2025-03-12T21:29:11.376Z","avatar_url":"https://github.com/distributedlife.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Great Mutator\n\n\u003e Describe mutations, get results! With batching and a change log.\n\nThe great mutator is an wrapper around a state tree. It could be called a store. It's really an [immutable](https://github.com/facebook/immutable-js) object.\n\nYou can use this wrapper to describe mutations that will be staged until applied as a single change.\n\nChoose your backing structure:\n\n```javascript\nimport theGreatMutator from 'the-great-mutator';\n```\n\nAnd create a great mutator.\n\n```javascript\nconst initialState = {\n  top: 'level',\n  such: {\n    nested: 'wow'\n  },\n  array: [{id: 1. prop: true}],\n  counter: 0\n};\n\nconst state = theGreatMutator(initialState);\n```\n\n# Mutating\nWith this wrapper you can send in your mutations, like this:\n\n```javascript\nstate.mutate({ top: 'changed' });\nstate.get('top'); //level\n```\n\nExcept that our state has not changed yet. It's only staged.\n\n```javascript\nstate.applyPendingMerges();\nstate.get('top'); //changed\n```\n\nThis also works:\n\n```javascript\nstate.mutate(['such.nested', 'very'])\nstate.get('such.nested'); //wow\nstate.applyPendingMerges();\nstate.get('such.nested'); //very\n```\n\nAnd you can apply multiple mutations at once. Using any of the given syntax.\n\n```javascript\nstate.mutate([\n  ['such.nested', 'very'],\n  [{top: 'changed' }]\n]);\n```\n\nIt can handle modifying arrays in clever ways:\n\n```javascript\n// This replaces the existing value of 'array'\nstate.mutate(['array', [1,2,3,4]]);\n\n// This pushes a record onto the array\nstate.mutate(['array+', { this: 'element', is: 'pushed', onto: 'the', array: true }]);\n\n// This replaces the element with the matching id.\nstate.mutate(['array!', { id:1, prop: false }]);\n\n// While this only modifies what is passed in.\nstate.mutate(['array:4', { prop: false }])\n\n// And you could have done this:\nstate.mutate(['array:4.prop', false])\n```\n\nYou can pass in a function:\n\n```javascript\nconst increment = (current) =\u003e current + 1;\n\nstate.mutate(['counter', increment]);\nstate.get('counter') //1\nstate.applyPendingMerges();\nstate.get('counter') //2\n```\n\nBut it also considers things that have been staged. So this works too:\n\n```javascript\nstate.mutate(['counter', 0]);\nstate.applyPendingMerges();\nstate.get('counter') //0\n\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.get('counter') //0\n\nstate.applyPendingMerges();\nstate.get('counter') //3\n```\n\nAs do promises:\n\n```javascript\nstate.mutate(['top', Promise.resolve('only applied on resolution and after batching')]);\nstate.get('top') //changed\nstate.applyPendingMerges();\nstate.get('top') //only applied on resolution and after batching\n```\n\n# Things you should know\n\n- It won't mutate `_id`, even if you ask nicely.\n- It relies on array elements having an `id` property.\n\n# Reading Values\n\nYou can get the root object (immutable) by asking for `all`. Or read a specific value using `get`. Get uses [ok-selector-immutable](https://github.com/distributedlife/ok-selector-immutable) under the hood so you can use `dot.strings.to.state` as well as `reference.arrays:1.items`.\n\n```javascript\nstate.all();\nstate.get('path.to.some.state');\n```\n\nThe value returned by `all` or `get` will be with be `immutable`. In the case of literals as immutable returns those as literals.\n\n# Getting the changes\n\nThe `flushChanges` method returns an array of changes as well as emptying out the change array. Changes are the result of `applyPendingMerges` calls.\n\n```javascript\nstate.mutate(['counter', 0]);\nstate.flushChanges() // []\nstate.applyPendingMerges();\nstate.get('counter') //0\nstate.flushChanges() // [{counter: 0}]\n\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.flushChanges() // []\nstate.applyPendingMerges();\nstate.flushChanges() // [{counter: 3}]\n\nstate.mutate(['counter', increment]);\nstate.applyPendingMerges();\nstate.mutate(['counter', increment]);\nstate.applyPendingMerges();\nstate.mutate(['counter', increment]);\nstate.applyPendingMerges();\nstate.flushChanges() // [{counter: 4}, {counter: 5}, {counter: 6}]\n```\n\n## Disabling change recording\nYou can disable recording of changes by passing in configuration option as the second parameter of `the great mutator` constructor. The `flushChanges` method always returns an empty array.\n\n```javascript\nconst state = theGreatMutator({}, { trackChanges; false });\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.mutate(['counter', increment]);\nstate.applyPendingMerges();\nstate.flushChanges() // []\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fthe-great-mutator-immutable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistributedlife%2Fthe-great-mutator-immutable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fthe-great-mutator-immutable/lists"}