{"id":23159203,"url":"https://github.com/jogemu/promisemut","last_synced_at":"2025-04-04T18:42:10.447Z","repository":{"id":210976295,"uuid":"727915624","full_name":"jogemu/promisemut","owner":"jogemu","description":"Chain instructions for mutating the fulfillment value of a not yet constructed promise.","archived":false,"fork":false,"pushed_at":"2024-06-08T20:31:24.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-10T22:35:14.850Z","etag":null,"topics":["js-promise","mutator"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jogemu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-12-05T20:55:31.000Z","updated_at":"2024-06-08T20:31:27.000Z","dependencies_parsed_at":"2023-12-05T21:10:16.952Z","dependency_job_id":"b4519054-a5ee-48a1-a786-3c7136d5007e","html_url":"https://github.com/jogemu/promisemut","commit_stats":null,"previous_names":["jogemu/promisemut"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jogemu%2Fpromisemut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jogemu%2Fpromisemut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jogemu%2Fpromisemut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jogemu%2Fpromisemut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jogemu","download_url":"https://codeload.github.com/jogemu/promisemut/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234844,"owners_count":20905852,"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":["js-promise","mutator"],"created_at":"2024-12-17T22:32:45.077Z","updated_at":"2025-04-04T18:42:10.422Z","avatar_url":"https://github.com/jogemu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# promisemut\n\nChain instructions for mutating the fulfillment value of a not yet constructed promise. This package allows to easily break promise chains into smaller, more manageable functions. Mutators for objects are described intuitively by using an object as a template. It is possible to perform array operations directly on array promises.\n\nAlthough this could be implemented without promises, they make sense here because of the intuitive data flow. Furthermore, it allows a mutator to return a promise.\n\n## Getting started\n\nLet's make the following code return the modified object instead. It may be necessary to get familiar with [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions).\n\n```\nPromise.resolve({ age: 0 }).then(\n  (value) =\u003e value.age += 1\n).then(console.log)\n// Output: 1\n```\n\nThe function can be simply wrapped in `mutate`.\n\n```\nimport resolve from 'promisemut'\n\nPromise.resolve({ age: 0 }).then(\n  resolve.mutate((value) =\u003e value.age += 1)\n).then(console.log)\n// Output: { age: 1 }\n```\n\nSince this `assign`s a new value to `age` it can be described through a template object that has an independent function for each key.\n\n```\nimport resolve from 'promisemut'\n\nPromise.resolve({ age: 0 }).then(\n  resolve.assign({\n    age: (value) =\u003e value + 1\n  })\n).then(console.log)\n// Output: { age: 1 }\n```\n\nIt is simple to make the function easily reusable.\n\n```\nlet increaseAge = resolve.assign({\n  age: (value) =\u003e value + 1\n})\n\nincreaseAge({ age: 0 }).then(console.log)\n// Output: { age: 1 }\n```\n\nThe template can access the object as well.\n\n```\nlet needsMaintenance = resolve.assign({\n  maintained: (value, key, item) =\u003e item.age \u003c 1\n})\n\nneedsMaintenance({ age: 0 }).then(console.log)\n// Output: { age: 0, maintained: true }\n```\n\nThe two previous functions can be easily combined.\n\n```\nlet needsMaintenanceSoon = increaseAge.then(needsMaintenance)\n\nneedsMaintenanceSoon({ age: 0 }).then(console.log)\n// Output: { age: 1, maintained: false }\n```\n\nIn case the original object should not be modified, use `then` instead of `assign`.\n\n### Arrays\n\n```\nlet array = resolve\n  .filter('age')   // [ {age: 1}, {age: 2} ]\n  .reverse()       // [ {age: 2}, {age: 1} ]\n  .map(o =\u003e o.age) // [ 2, 1 ]\n  .sort()          // [ 1, 2 ]\n  .map({\n    age: (...[,,a]) =\u003e a,\n    valid: true\n  })               // [ { age: 1, valid: true }, { age: 2, valid: true } ]\n  .then(console.log)\n\narray([{age: 0}, {age: 1}, {age: 2}])\n```\n\nIt is possible to `map` objects as well (index = key).\n\n## Advanced\n\nMany advantages of this approach only become apparent when the mutator requires arguments that do not traverse through the promise chain.\n\n```\nlet increaseAge = increment =\u003e resolve.assign({\n  age: (value) =\u003e value + increment\n})\n\npromise.then(increaseAge(2)).then(console.log)\n```\n\nThere is direct access to the `resolve` function as well. It can be called like `promise.then(resolve.then(fn))` to allow to work with a fulfilled like it is a promise. All other functions are built on that and support to be called directly on resolve like `resolve.mutate(fn).mutate(fn)` instead of `resolve.then(mutate(fn).then(mutate(fn)))`. The value resolve receives can be accessed later in the chain with `then2`. This works similarly to the argument of the mutator above but this time the argument is dynamic.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjogemu%2Fpromisemut","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjogemu%2Fpromisemut","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjogemu%2Fpromisemut/lists"}