├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /index.js -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Nate Wienert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## reapp-component 2 | 3 | Component is a tiny, no-dependency library designed for top-down applications. 4 | 5 | It's essentially a factory, that provides two things on it's factories: 6 | 7 | - Decorators through `addDecorator` 8 | - Dependency Injection through `addStatics` 9 | 10 | So essentially Dependency Injection and Decorations. 11 | 12 | In reapp it's optional, but we found it helpful when creating large apps, for 13 | times when you want to add a mixin to every class, or have commonly used ones. 14 | As well as for injecting stuff like stores, etc. 15 | 16 | Because React apps use gradual controller-view -> view trees 17 | DI is typically very simple and more akin to global variables. Decorators are helpful 18 | for medium to large scale apps. They can help you have default mixins, and automate 19 | other tasks you'd normally do all over the place. 20 | 21 | ### Usage 22 | 23 | ```js 24 | var c1 = Component(); 25 | 26 | c1.addDecorator(spec => { 27 | spec.decorated = true; 28 | return spec; 29 | }); 30 | 31 | c1.addStatics('hello', 'world'); 32 | 33 | assert(c1.hello === 'world'); 34 | assert(c1({}) === { decorated: true }) 35 | ``` 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "reapp-component", 3 | "version": "1.0.2", 4 | "description": "ultra simple dependency injection and statics/globals", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "babel src --out-dir . --experimental" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "github.com/reapp/reapp-component" 12 | }, 13 | "author": "Nate Wienert", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/reapp/reapp-component/issues" 17 | }, 18 | "homepage": "https://github.com/reapp/reapp-component", 19 | "devDependencies": { 20 | "babel": "^4.7.16" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | function Component() { 2 | var decorators = []; 3 | var factory = function(spec) { 4 | decorators.forEach(decorator => { 5 | spec = decorator(spec); 6 | }); 7 | return spec; 8 | }; 9 | 10 | factory.addDecorator = function(decorator) { 11 | decorators.push(decorator); 12 | }; 13 | 14 | factory.addStatics = function(name, statics) { 15 | if (!statics) 16 | this._addStatics(name); 17 | else { 18 | var obj = {}; 19 | obj[name] = statics; 20 | this._addStatics(obj); 21 | } 22 | }; 23 | 24 | factory._addStatics = function(obj) { 25 | if (Object.prototype.toString.call(obj) !== '[object Object]') 26 | throw new Error('Must provide an object to statics'); 27 | 28 | Object.keys(obj).forEach(key => { 29 | if (key === 'addStatics' || key === '_addStatics' || key === 'addDecorator') 30 | throw new Error('Cannot overwrite addStatics or addDecorator keys'); 31 | 32 | this[key] = obj[key]; 33 | }); 34 | }; 35 | 36 | return factory; 37 | } 38 | 39 | module.exports = Component; --------------------------------------------------------------------------------