jsbin/lib/features.js
Remy Sharp a2565aeae7 Support feature checks in templates
The template needs the user object (expected to be the session user), and usage is:

```html
{{#feature user "alpha"}}You are alpha user{{/feature}}
```
2014-01-14 16:45:59 +00:00

59 lines
1.3 KiB
JavaScript

/**
* usage:
*
* var features = require('./features');
*
* if (features('alpha', req)) {
* // do alpha features
* }
*
* // as an express route handler - if it fails the feature test then
* // the user will see a 404 (in this case, you can catch /cool-feature, and
* // express will call the second handler)
* app.get('/cool-feature', features.route('alpha'), function (req, res) {
* res.render('cool-feature');
* });
*
* // as a hbs template (defined in ./hbs.js)
* {{#feature user "alpha"}}You are part of the cool gang{{/feature}}
*/
'use strict';
var Features = require('feature-gateway'),
undefsafe = require('undefsafe');
var teamjsbin = ['rem', 'allouis', 'yandle'];
var alphausers = teamjsbin.concat([]);
var flags = {
alpha: function (req) {
var name = undefsafe(req, 'session.user.name');
if (name) {
return alphausers.indexOf(name) !== -1;
}
return false;
},
beta: function (req) {
return undefsafe(req, 'session.user.beta');
},
pro: function (req) {
return undefsafe(req, 'session.user.pro');
},
teamjsbin: function (req) {
var name = undefsafe(req, 'session.user.name');
if (name) {
return teamjsbin.indexOf(name) !== -1;
}
return false;
}
};
var features = module.exports = new Features(flags);
features.log = function () {
};