{"id":20153343,"url":"https://github.com/w3tecch/class-mapper","last_synced_at":"2025-04-09T21:32:58.556Z","repository":{"id":57199472,"uuid":"132187803","full_name":"w3tecch/class-mapper","owner":"w3tecch","description":"A easy to use way to map any ugly backend structures into clean TypeScript/ES6 models","archived":false,"fork":false,"pushed_at":"2019-01-31T12:56:33.000Z","size":770,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T11:38:51.567Z","etag":null,"topics":["annotations","clean","decorator","es6","javascipt","library","mapping","model","structure","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/w3tecch.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-05-04T20:49:49.000Z","updated_at":"2023-03-17T19:52:08.000Z","dependencies_parsed_at":"2022-09-16T14:51:14.235Z","dependency_job_id":null,"html_url":"https://github.com/w3tecch/class-mapper","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/w3tecch%2Fclass-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w3tecch%2Fclass-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w3tecch%2Fclass-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w3tecch%2Fclass-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/w3tecch","download_url":"https://codeload.github.com/w3tecch/class-mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248114908,"owners_count":21050138,"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":["annotations","clean","decorator","es6","javascipt","library","mapping","model","structure","typescript"],"created_at":"2024-11-13T23:18:39.196Z","updated_at":"2025-04-09T21:32:58.534Z","avatar_url":"https://github.com/w3tecch.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./icon.png\" alt=\"w3tec\" width=\"400\" /\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003eClass Mapper\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://david-dm.org/w3tecch/class-mapper\"\u003e\n    \u003cimg src=\"https://david-dm.org/w3tecch/class-mapper/status.svg?style=flat\" alt=\"dependency\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/w3tecch/class-mapper\"\u003e\n    \u003cimg src=\"https://travis-ci.org/w3tecch/class-mapper.svg?branch=master\" alt=\"travis\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://ci.appveyor.com/project/DaNautilus/class-mapper/branch/master\"\u003e\n    \u003cimg src=\"https://ci.appveyor.com/api/projects/status/jkcquiufl9us4epw/branch/master?svg=true\u0026passingText=windows%20passing\u0026pendingText=windows%20pending\u0026failingText=windows%20failing\" alt=\"appveyor\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/w3tecch/class-mapper\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/w3tecch/class-mapper/branch/master/graph/badge.svg\" alt=\"codecov\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cb\u003eA easy to use way to map any ugly backend structures into clean TypeScript/ES6 models.\u003c/b\u003e\u003c/br\u003e\n  \u003cspan\u003eInspired typestack \u003ca href=\"https://github.com/typestack/class-transformer\"\u003eclass-transformer\u003c/a\u003e\u003c/span\u003e\u003c/br\u003e\n  \u003csub\u003eMade with ❤️ by \u003ca href=\"https://github.com/w3tecch\"\u003ew3tech\u003c/a\u003e\u003c/sub\u003e\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\n![divider](./w3tec-divider.png)\n\n## ❯ Why\n\nAre you tired of ugly and weird backend structures which makes it challenging to work with in your application 🤯? Here comes a solution 🎉! Just use class-mapper to map all weird structures into TypeScript and ES6 models 👉 YOU 🤗 like to work with in your web frontend or Node.js application and not backend guys 🤪.\n\nTry it!! We are happy to hear your feedback or any kind of new features.\n\n![divider](./w3tec-divider.png)\n\n## ❯ Table of Contents\n\n- [Installation](#-installation)\n- [License](#-license)\n- [Methods](#-methods)\n\n![divider](./w3tec-divider.png)\n\n## ❯ Installation\n\n### Step 1: Get library via npm or yarn\n\n```shell\nnpm install class-mapper --save\n```\n\nor with yarn\n\n```shell\nyarn add class-mapper\n```\n\n### Step 2: Add library to your project\n\n#### Browser\n```html\n\u003chtml\u003e\n   \u003chead\u003e\n       \u003cscript src=\"node_modules/class-mapper/dist/class-mapper.js\"\u003e\u003c/script\u003e\n   \u003c/head\u003e\n\u003c/html\u003e\n```\n\n## ❯ Methods\n\n### mapClasses\n\nThis method maps a source class to your target class\n\n```typescript\nimport {mapClasses, MapFromSource, PropertyType} from 'class-mapper';\n\n/**\n * Source classes\n */\n\nabstract class SourcePersonModel {\n  public name1: string;\n  public name2: string;\n}\n\nclass SourceCarModel {\n  public attribute1: string;\n  public attribute2: string;\n}\n\nclass SourceCustomerModel extends SourcePersonModel {\n  public car1!: SourceCarModel[];\n}\n\nconst sourceUser: SourceCustomerModel = new SourceCustomerModel();\n\n/**\n * Target classes\n */\n\nabstract class TargetPersonModel {\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.name1)\n  public firstName!: string;\n\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.name2)\n  public lastName!: string;\n}\n\nclass TargetCarModel {\n  @MapFromSource((sourceCar: SourceCarModel) =\u003e sourceCar.attribute1)\n  public manufacturer!: string;\n\n  @MapFromSource(sourceCar: SourceCarModel) =\u003e sourceCar.attribute2)\n  public model!: string;\n}\n\nclass TargetCustomerModel extends TargetPersonModel {\n  @PropertyType(TargetCarModel)\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.car1)\n  public cars!: TargetCarModel[];\n}\n\nconst targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser);\n```\n\n### Using `groups` to exclude properties\n\nWith `groups` array, you can exclude properties from mapping. `MapFromSource` decorators with no `groups` option will always be mapped.\n\n```typescript\nimport {mapClasses, MapFromSource, PropertyType} from 'class-mapper';\n\n/**\n * Source class\n */\n\nabstract class SourcePersonModel {\n  public name1: string;\n  public name1: string;\n}\n\n/**\n * Target class\n */\n\nconst firstNameOnly = 'first-name-only';\nconst lastNameOnly = 'last-name-only';\n\nabstract class TargetPersonModel {\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.name1, { groups: [firstNameOnly] })\n  public firstName!: string;\n\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.name2, { groups: [lastNameOnly] })\n  public lastName!: string;\n}\n\nconst targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser, { groups: [lastNameOnly] });\n```\n\n### Using `enabled` to exclude properties\n\nWith `enabled` you can exclude conditionally properties. `MapFromSource` decorators with no `enabled` option will always be mapped.\n\n```typescript\nimport {mapClasses, MapFromSource, PropertyType} from 'class-mapper';\n\n/**\n * Source class\n */\n\nabstract class SourcePersonModel {\n  public name1: string;\n  public name1: string;\n}\n\n/**\n * Target class\n */\n\nabstract class TargetPersonModel {\n  @MapFromSource((sourceUser: SourcePersonModel) =\u003e sourceUser.name1, { enabled: (sourceUser: SourcePersonModel) =\u003e !!sourceUser.name1 })\n  public firstName!: string;\n\n  @MapFromSource(sourceUser =\u003e sourceUser.name2, { enabled: (sourceUser: SourcePersonModel) =\u003e !!sourceUser.name2 })\n  public lastName!: string;\n}\n\nconst targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser);\n```\n\n## ❯ License\n\n[MIT](/LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw3tecch%2Fclass-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fw3tecch%2Fclass-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw3tecch%2Fclass-mapper/lists"}