{"id":22220548,"url":"https://github.com/drawbotics/morphey","last_synced_at":"2025-03-25T07:41:30.202Z","repository":{"id":57302920,"uuid":"82844054","full_name":"Drawbotics/morphey","owner":"Drawbotics","description":"A small utility to transform keys and values of an object using a template.","archived":false,"fork":false,"pushed_at":"2018-03-27T08:34:59.000Z","size":49,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":7,"default_branch":"dev","last_synced_at":"2025-01-30T07:12:36.153Z","etag":null,"topics":["javscript","keys","nodejs","utility"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Drawbotics.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}},"created_at":"2017-02-22T19:27:05.000Z","updated_at":"2018-03-27T08:35:01.000Z","dependencies_parsed_at":"2022-08-29T09:51:56.857Z","dependency_job_id":null,"html_url":"https://github.com/Drawbotics/morphey","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fmorphey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fmorphey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fmorphey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fmorphey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Drawbotics","download_url":"https://codeload.github.com/Drawbotics/morphey/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245422921,"owners_count":20612725,"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":["javscript","keys","nodejs","utility"],"created_at":"2024-12-02T23:09:03.128Z","updated_at":"2025-03-25T07:41:30.176Z","avatar_url":"https://github.com/Drawbotics.png","language":"JavaScript","readme":"# Morphey\n\nA small utility to change keys and values of an object according to another object that describes the transformations. It's important to notice that this function **doesn't mutate the original object** and returns a new one instead.\n\n[![npm version](https://img.shields.io/npm/v/morphey.svg?style=flat-square)](https://www.npmjs.com/package/morphey)\n[![build status](https://img.shields.io/travis/larsbs/morphey/master.svg?style=flat-square)](https://travis-ci.org/larsbs/morphey)\n\n\n## Installation\n\nInstall it using yarn:\n\n```bash\n$ yarn add morphey\n```\n\nOr, if you're an old school guy, using npm:\n\n```bash\n$ npm install --save morphey\n```\n\n\n## Example\n\nA basic usage example:\n\n```js\nimport morphey, { fromKey, fromValue } from 'morphey';  // or const morphey = require('morphey');\n\n\n// translations can be a function or an object. If it's a function, the original\n// object would be passed as the first arg.\nconst translations = (obj) =\u003e {\n  foo: fromKey('bar'),\n  test: fromKey('old').using((v) =\u003e v * 2),\n  'deep.x': fromKey('deepX'),\n  'deep.y': fromKey('deep.deep.Y'),\n  computed: fromValue(obj.deepX * obj.deep.deep.Y),\n};\nconst initialObject = {\n  bar: 123,\n  old: 10,\n  deepX: 2,\n  deep: {\n    deep: { Y: 3 },\n  },\n};\nconst final = morphey(initialObject, translations);\n\nconsole.log(final);  // will print\nconst f = {\n  foo: 123,\n  test: 20,\n  deep: {\n    x: 2,\n    y: 3,\n  },\n  computed: 6,\n};\n```\n\n\n## Motivation\n\nThis small utility allows to change keys and values of an object according to a description of those changes. This can be useful in multiple situations. For example, when you need to communicate with an external service through JSON but the keys the service is expecting are different than the one you want to use, when you want to filter some keys from one object, when you want to sanitize values or compute some of the them on the fly, etc.\n\nBut, what's the point of having those transformations in a description object? Well, first of all it makes easier to know what's happening at first glance. Second, this provides an unified way of doing this operation that can be shared across the entire team and across the entire code base.\n\nBasically, it's easier to understand this:\n\n```js\nconst translations = (obj) =\u003e {\n  foo: fromKey('bar'),\n  test: fromKey('old').using((v) =\u003e v * 2),\n  'deep.x': fromKey('deepX'),\n  'deep.y': fromKey('deep.deep.Y'),\n};\nconst final = morphey(initialObject, translations);\n```\n\nThan this:\n\n```js\nconst final = Object.keys(initialObject).reduce((memo, k) =\u003e {\n  if (k === 'bar') {\n    return { ...memo, foo: initialObject[k] };\n  }\n  else if (k === 'old') {\n    return { ...memo, test: initialObject[k] * 2 };\n  }\n  else if (k === 'deepX') {\n    return { ...memo, deep: { ...memo.deep, x: initialObject[k] } };\n  }\n  // It's not possible to easily do deep.y\n}, {});\n```\n\nAnd all of this without taking into account that every developer in the team can have his own way of doing this or the need to repeat this code everytime someone is going to change the shape of an object.\n\n\n## API\n\n#### `morphey(obj, morphs)`\n\n\u003e Creates a new object from `obj` using the transformations in `morphs`.\n\n - **Parameters**\n  - `obj` *Object*: The object to use as a base for the new object.\n  - `morphs` *Object|Function*: The transformations to apply to the base object for obtaining the new object.\n - **Returns**\n  - *Object*: A new object resulting of applying the transformations to the base object.\n\nTransformations descriptions are objects with keys as the new names of the properties of the resulting object and values that describe where to find the data in the original object. This data finding process can be specified using two methods.\n\n\n##### `fromKey`\n\n\u003e In progress\n\n\n##### `fromValue`\n\n\u003e In progress\n\n\n## License\n\nMIT. See [LICENSE](LICENSE) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fmorphey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrawbotics%2Fmorphey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fmorphey/lists"}