├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | npm-debug.log* 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.12" 3 | - "4" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test:cov" 7 | after_script: "npm i -g codecov.io && cat ./coverage/lcov.info | codecov" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # choo-redirect [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Redirect a view to another view. 6 | 7 | ## Usage 8 | ```js 9 | var redirect = require('choo-redirect') 10 | var html = require('choo/html') 11 | var choo = require('choo') 12 | 13 | var app = choo() 14 | 15 | app.route('/', redirect('/welcome')) 16 | app.route('/welcome', welcomeView) 17 | 18 | app.mount('body') 19 | 20 | function welcomeView () { 21 | return html` 22 | 23 |
welcome!
24 | 25 | ` 26 | } 27 | ``` 28 | 29 | ## API 30 | ### view = redirect(route, rootSelector?) 31 | Create a new view that redirects to a `route`. Can take an optional second 32 | argument for the root selector which is used as the entry point of the 33 | application. Defaults to `document.body`. 34 | 35 | ## Installation 36 | ```sh 37 | $ npm install choo-redirect 38 | ``` 39 | 40 | ## License 41 | [MIT](https://tldrlegal.com/license/mit-license) 42 | 43 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 44 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 45 | [2]: https://img.shields.io/npm/v/choo-redirect.svg?style=flat-square 46 | [3]: https://npmjs.org/package/choo-redirect 47 | [4]: https://img.shields.io/travis/yoshuawuyts/choo-redirect/master.svg?style=flat-square 48 | [5]: https://travis-ci.org/yoshuawuyts/choo-redirect 49 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/choo-redirect/master.svg?style=flat-square 50 | [7]: https://codecov.io/github/yoshuawuyts/choo-redirect 51 | [8]: http://img.shields.io/npm/dm/choo-redirect.svg?style=flat-square 52 | [9]: https://npmjs.org/package/choo-redirect 53 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 54 | [11]: https://github.com/feross/standard 55 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var redirect = require('.') 2 | var html = require('choo/html') 3 | var choo = require('choo') 4 | 5 | var app = choo() 6 | 7 | app.route('/', redirect('/welcome')) 8 | app.route('/welcome', welcomeView) 9 | 10 | app.mount('body') 11 | 12 | function welcomeView () { 13 | return html` 14 | 15 |
welcome!
16 | Test 17 | 18 | ` 19 | } 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | 3 | module.exports = redirect 4 | 5 | // Redirect a view to another view 6 | // (str, str) -> HTMLNode 7 | function redirect (url, rootSelector) { 8 | rootSelector = rootSelector || 'body' 9 | 10 | assert.equal(typeof url, 'string') 11 | assert.equal(typeof rootSelector, 'string') 12 | 13 | return function redirectView (state, emit) { 14 | emit('replaceState', url) 15 | 16 | // return the exact same tree that's being rendered right now so nothing is 17 | // changed :tada: 18 | var tree = document.querySelector(rootSelector) 19 | var node = document.createElement('div') 20 | node.isSameNode = function (el) { 21 | return el === tree 22 | } 23 | return node 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "choo-redirect", 3 | "version": "2.0.0", 4 | "description": "Redirect a view to another view", 5 | "main": "index.js", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "test": "standard && npm run deps && NODE_ENV=test node test", 9 | "test:cov": "standard && npm run deps && NODE_ENV=test istanbul cover test.js" 10 | }, 11 | "repository": "yoshuawuyts/choo-redirect", 12 | "keywords": [ 13 | "redirect", 14 | "choo", 15 | "view", 16 | "bel", 17 | "framework" 18 | ], 19 | "license": "MIT", 20 | "dependencies": {}, 21 | "devDependencies": { 22 | "choo": "^5.4.0", 23 | "dependency-check": "^2.6.0", 24 | "istanbul": "^0.4.5", 25 | "standard": "^8.6.0", 26 | "tape": "^4.6.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const chooRedirect = require('./') 3 | 4 | test('should assert input types', function (t) { 5 | t.plan(1) 6 | t.throws(chooRedirect) 7 | }) 8 | --------------------------------------------------------------------------------