{"id":20906144,"url":"https://github.com/distributedlife/the-great-mutator","last_synced_at":"2026-04-25T13:40:17.313Z","repository":{"id":65478854,"uuid":"67421621","full_name":"distributedlife/the-great-mutator","owner":"distributedlife","description":"Describe mutations, get results! With batching and a change log.","archived":false,"fork":false,"pushed_at":"2017-09-27T12:57:28.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-03-25T15:11:11.437Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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}},"created_at":"2016-09-05T13:04:58.000Z","updated_at":"2017-07-04T09:47:41.000Z","dependencies_parsed_at":"2023-01-25T08:05:13.562Z","dependency_job_id":null,"html_url":"https://github.com/distributedlife/the-great-mutator","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fthe-great-mutator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distributedlife","download_url":"https://codeload.github.com/distributedlife/the-great-mutator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243297542,"owners_count":20268795,"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:13.626Z","updated_at":"2025-12-27T17:27:16.837Z","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 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 by asking for `all`. Or read a specific value using `get`. Get uses [ok-selector](https://github.com/distributedlife/ok-selector) 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\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","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistributedlife%2Fthe-great-mutator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fthe-great-mutator/lists"}