{"id":13509074,"url":"https://github.com/CocktailJS/traits-decorator","last_synced_at":"2025-03-30T13:31:24.102Z","repository":{"id":33999009,"uuid":"37753520","full_name":"CocktailJS/traits-decorator","owner":"CocktailJS","description":"Traits with decorators","archived":false,"fork":false,"pushed_at":"2016-08-25T21:06:12.000Z","size":36,"stargazers_count":137,"open_issues_count":1,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-29T20:05:40.119Z","etag":null,"topics":["decorators","es7","nodejs","trait","traits-decorator"],"latest_commit_sha":null,"homepage":"","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/CocktailJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-20T01:26:40.000Z","updated_at":"2023-12-06T19:08:06.000Z","dependencies_parsed_at":"2022-07-13T21:34:55.484Z","dependency_job_id":null,"html_url":"https://github.com/CocktailJS/traits-decorator","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CocktailJS%2Ftraits-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CocktailJS%2Ftraits-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CocktailJS%2Ftraits-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CocktailJS%2Ftraits-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CocktailJS","download_url":"https://codeload.github.com/CocktailJS/traits-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246323875,"owners_count":20759043,"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":["decorators","es7","nodejs","trait","traits-decorator"],"created_at":"2024-08-01T02:01:02.630Z","updated_at":"2025-03-30T13:31:23.421Z","avatar_url":"https://github.com/CocktailJS.png","language":"JavaScript","readme":"# traits-decorator\n\n[![npm version](https://badge.fury.io/js/traits-decorator.svg)](http://badge.fury.io/js/traits-decorator)\n[![Build Status](https://travis-ci.org/CocktailJS/traits-decorator.svg)](https://travis-ci.org/CocktailJS/traits-decorator)\n[![bitHound Score](https://www.bithound.io/github/CocktailJS/traits-decorator/badges/score.svg)](https://www.bithound.io/github/CocktailJS/traits-decorator)\n\nExperimental library to apply Traits with ES7 decorators.\n\n## Install\n\n\u003e using npm\n\n```\nnpm i -S traits-decorator\n```\n\n\n\u003e using git repository\n \n```\nnpm i -S git://github.com/cocktailjs/traits-decorator\n```\n\n## API\n\n## Decorators\n\n### @traits(Trait1, ...TraitN)\nApplicable to `class` definition. It will apply all the given Traits to the class.\n\n```js\n@traits(TExample) class MyClass {}\n```\n\n### @requires(description1, ...descriptionN)\nApplicable to a method defined in a Trait. The decorator **does nothing** but it serves as a documentation to reflect what method / property the method needs access to.\n\n```js\nclass TFoo {\n\n    @requires('bar: {String}')\n    fooBar() {\n        console.log('foo,' + this.bar);\n    }\n}\n```\n\n## Bindings\n\n### excludes(Method1, ...MethodN)\nApplicable to Trait definition in '@traits'. It will exclude the given method names from the Trait.\n\n```js\n@traits(TExample::excludes('foo', 'bar')) \nclass MyClass {}\n```\n\n### alias(aliases: {})\nApplicable to Trait definition in '@traits'. It will alias the method defined in the Trait with the `key` as the `value` .\n\n```js\n@traits(TExample::alias({baz: 'parentBaz'}))\nclass MyClass {}\n```\n\n### as({alias: {}, excludes: []})\nApplicable to Trait definition in '@traits'. It will apply aliases and excluded methods from the Trait\n\n```js\n@traits( TExample::as({alias: {baz: 'parentBaz'}, excludes:['foo', 'bar'] }) )\nclass MyClass {}\n```\n\n\n## Usage\nBasically, we have a few Traits (classes) TFirst, TLast and we combine and apply them by using `traits` decorator:\n\n\u003e example.js\n\n```js\n'use strict';\n\nimport { traits, excludes, alias, requires }  from 'traits-decorator'\n\nclass TFirst {\n\n    @requires('collection:[]')\n    first() {\n        return this.collection[0];\n    }\n\n}\n\nclass TLast {\n    \n    @requires('collection:[]')\n    last() {\n        let collection = this.collection;\n        let l = collection.length;\n        return collection[l-1];\n    }\n\n    justAnother() {}\n\n    foo() {\n        console.log('from TLast\\'s foo');\n    }\n}\n\n//composing a Trait with others\n@traits( TFirst, TLast::excludes('foo', 'justAnother') )\nclass TEnum {\n\n    foo() {\n        console.log('enum foo')\n    }\n}\n\n//apply trait TEnum\n@traits(TEnum::alias({ foo: 'enumFoo' }) )\nclass MyClass {\n\n    constructor (collection = []) {\n        this.collection = collection\n    }\n}\n\nlet obj = new MyClass([1,2,3])\n\nconsole.log(obj.first()) // 1\n\nobj.enumFoo() // enum foo\n\n```\n\n\nIn order to run the `example.js` we need babel and since we are using some experimental functionality, decorators (@traits) and bindOperator (::) we need to use the `--stage 0`.\n\n\n```\nbabel-node --stage 0 example.js\n```\n\n\n## Update\n@mixins decorator has been removed. If you want to use `mixins` please use [mixins-decorator](https://www.npmjs.com/package/mixins-decorator) package.\n\n## License MIT\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCocktailJS%2Ftraits-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCocktailJS%2Ftraits-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCocktailJS%2Ftraits-decorator/lists"}