{"id":28385754,"url":"https://github.com/beyonk-group/reorient","last_synced_at":"2025-06-26T12:31:21.878Z","repository":{"id":42223148,"uuid":"75094407","full_name":"beyonk-group/reorient","owner":"beyonk-group","description":"Transforms json from one format into another","archived":false,"fork":false,"pushed_at":"2023-03-04T03:01:05.000Z","size":287,"stargazers_count":7,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-03T20:45:54.658Z","etag":null,"topics":["assign","convert","flatmap","json","map","rejig","reorganise","reorganize","transform","transpose"],"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/beyonk-group.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":"2016-11-29T15:26:31.000Z","updated_at":"2022-02-01T21:20:26.000Z","dependencies_parsed_at":"2024-06-21T13:23:33.273Z","dependency_job_id":null,"html_url":"https://github.com/beyonk-group/reorient","commit_stats":null,"previous_names":["beyonk-group/reorient","desirable-objects/reorient"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/beyonk-group/reorient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyonk-group%2Freorient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyonk-group%2Freorient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyonk-group%2Freorient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyonk-group%2Freorient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beyonk-group","download_url":"https://codeload.github.com/beyonk-group/reorient/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyonk-group%2Freorient/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260546052,"owners_count":23025858,"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":["assign","convert","flatmap","json","map","rejig","reorganise","reorganize","transform","transpose"],"created_at":"2025-05-30T11:43:40.226Z","updated_at":"2025-06-26T12:31:21.870Z","avatar_url":"https://github.com/beyonk-group.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca href=\"https://beyonk.com\"\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/218949/144224348-1b3a20d5-d68e-4a7a-b6ac-6946f19f4a86.png\" width=\"198\" /\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n\u003c/a\u003e\n\n## Reorient\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) ![publish](https://github.com/beyonk-adventures/reorient/workflows/publish/badge.svg)\n\nTransforms an object from one form into another form much like (the now removed) [Hoek.transform](https://github.com/hapijs/hoek/blob/5.0.4/API.md#transformobj-transform-options), but allows methods as transformation values, which are called during transformation, and passed the original source object.\n\nThis allows for more useful transformations as shown in Usage.\n\n## Releases\n\n### v4.0.0\n\n* Adds validation as an option\n* Defaults `undefined` values\n\n### v3.0.0\n\nVersion 3.0.0 includes some major changes:\n* We now require `await transform()` rather than `transform()`\n* We took `transform` and its associated tests from `hoek`  and embedded it in the project, since it has been removed from their latest release.\n* Requires node 7 or above, For node \u003c 6, use version \u003c 3\n\n### \u003c v2.1.0\n\nVersion \u003c 2.1.0 does not use async/await\n\n## Documentation\n\nSee Hoek.transform docs (linked above) for basic usage, see below for advanced usage.\n\n## Usage\n\nTransformation from object to object\n\n```javascript\n    const { transform } = require('reorient')\n\n    const source = {\n      firstName: 'Antony',\n      lastName: 'Jones',\n      job: {\n        role: 'Developer'\n       }\n    }\n    \n    const buildFullName = function (data) {\n      return data.firstName + ' ' + data.lastName\n    }\n\n    const transforms = {\n      'fullName': buildFullName,\n      'job.title': 'job.role'\n    }\n\n    const result = await transform(source, transforms)\n    \n    // results in:\n    \n    result === {\n      fullName: 'Antony Jones',\n      job: {\n        title: 'Developer'\n      }\n    }\n```\n\nTransformation from object to array\n\n```javascript\n    const { transform } = require('reorient')\n\n    const source = {\n      firstName: 'Antony',\n      lastName: 'Jones',\n      job: {\n        role: 'Developer'\n       }\n    }\n    \n    const buildFullName = function (data) {\n      return data.firstName + ' ' + data.lastName\n    }\n\n    // to convert to an array, drop your 'destination' keys,\n    // and just pass an array of transformations\n    const transforms = [\n      buildFullName,\n      'job.role'\n    ]\n\n    const result = await transform(source, transforms)\n    \n    // results in:\n    \n    result === [\n      'Antony Jones',\n      'Developer'\n    ]\n```\n\n### Empty mappings\n\nIf you leave a mapping directive empty, it will simply map to null. This is probably more useful for arrays where you need a gap in an array, but it works on objects too, i.e. `someKey: null`\n\n```javascript\n    const { transform } = require('reorient')\n\n    const source = {\n      one: 'one',\n      two: 'not-supplied',\n      three: 'three' \n    }\n\n    const transforms = [\n      'one',\n      null,\n      'three'\n    ]\n\n    const result = await transform(source, transforms)\n    \n    // results in:\n    \n    result === [\n      'one',\n      null,\n      'three'\n    ]\n```\n\n### options\n\nreorient takes all the options hoek can take for\n [Hoek.transform](https://github.com/hapijs/hoek/blob/master/API.md#transformobj-transform-options), \n and in addition, has a few extra options:\n\n#### trim\n\nTrim trims all null, undefined, and void values (excluding false), as well as dropping empty objects.\n\nIt will do this for all values including nested values (deep)\n\n```javascript\n    const { transform } = require('reorient')\n\n    const source = {\n      firstName: 'Antony',\n      lastName: null,\n      job: {\n        role: undefined\n       }\n    }\n\n    const transforms = {\n      'firstName': 'firstName',\n      'job.role': 'job.role'\n    }\n\n    const result = await transform(source, transforms, { trim: true })\n\n    // results in:\n    \n    result === {\n      firstName: 'Antony'\n    }\n```\n\n#### default\n\nDefaulting of properties can be done on a per property basis by specifying a configuration object than simply a path.\n\nDefaults cannot be specified when using a function as a transform, you should do the defaulting in your function.\n\n```javascript\n    const { transform } = require('reorient')\n\n    const source = {\n      firstName: 'Antony',\n      contract: {\n        start: Date.now()\n      }\n    }\n\n    const defaultEndDate = new Date()\n    defaultEndDate.setMonth(defaultEndDate.getMonth() + 3)\n\n    const transforms = {\n      'firstName': 'firstName',\n      'employment.startDate': 'contract.start',\n      'employment.endDate': { path: 'contract.endDate', default: defaultEndDate }\n    }\n\n    const details = await transform(source, transforms)\n\n    // results in:\n    \n    details === {\n      firstName: 'Antony',\n      employment: {\n        startDate: 'Fri Aug 03 2017 22:23:10 GMT+0100 (BST)',\n        endDate: 'Sat Nov 03 2017 22:23:23 GMT+0000 (GMT)'\n      }\n    }\n```\n\n\n#### validating\n\nProperties can be passed a validator function, which is passed the value of the transformation, and the original source object\n\nThe validator function must return the field value if the field is valid, or throw an error with a validation message.\n\nThe example below uses [Joi](https://hapi.dev/module/joi/)\n\n```javascript\n    const { transform } = require('reorient')\n    const Joi = require('@hapi/joi')\n\n    async function validateNationalInsurance (value, source) {\n      const schema = Joi.string().pattern(/^[A-Z]{2}[0-9]{6}[A-Z]$/)\n      return schema.validateAsync(nationalInsurance)\n    }\n\n    const source = {\n      firstName: 'Antony',\n      nationalInsurance: 'AA332211B'\n    }\n\n    const transforms = {\n      'firstName': 'firstName',\n      'employment.endDate': { path: 'nationalInsurance', validate: validateNationalInsurance }\n    }\n\n    const details = await transform(source, transforms)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeyonk-group%2Freorient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeyonk-group%2Freorient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeyonk-group%2Freorient/lists"}