-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfeature-flags.js
More file actions
143 lines (116 loc) · 3.75 KB
/
feature-flags.js
File metadata and controls
143 lines (116 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*! Javascript Feature Flags
https://github.com/jayf/javascript-feature-flags
MIT License 2013
*/
var ff = ff || {};
/*
Setting a global variable, ff, here.
You can change this! It can be any name,
but change it again at the bottom to match
what you have here.
*/
(function(app, options) {
"use strict";
// defaults
app.methods = options && options.methods ||
[
'query',
'hash',
'cookie',
'domain'
];
app.keyName = options && options.keyName ||'ff';
app.testDomain = options && options.testDomain || 'localhost';
app.testDomainFlag = options && options.testDomainFlag || 'debug';
app.loc = window.location;
app.flags = [];
// returns true if flag is set, false otherwise
// example: if ( ff.flag('Shuggie') ) { console.log('Otis!'); }
app.flag = function( f ) {
return false || app.flags.indexOf( f ) > -1;
};
// all methods for setting flags
app.setFlags = {
// flags stored in browser cookie named app.keyName
// cookie can be single value, or CSV, like: Brubeck,Coltrane,Monk
cookie: function() {
var neq = app.keyName + '=',
neql = neq.length,
value = null,
ca = document.cookie.split(';'),
cal = ca.length,
i;
for( i=0; i < cal; i++ ) {
var c = ca[i],
cl = c.length;
while ( c.charAt(0) === ' ' ) {
c = c.substring( 1, cl );
}
if ( c.indexOf(neq) === 0 ) {
value = c.substring( neql, cl );
}
}
if ( value ) {
this._addFlag( value.split(',') );
}
},
// flag for page on a test domain
domain: function() {
if ( app.loc.hostname === app.testDomain ) {
this._addFlag(app.testDomainFlag);
}
},
// flag in URL fragment that starts with app.keyName
// fragment format is CSV after app.keyName: ff,Colins,Clinton,Hazel
hash: function() {
var h = app.loc.hash,
ki = h.indexOf( app.keyName );
if ( ki > -1 ) {
var s = h.substring( ki + app.keyName.length + 1 );
this._addFlag( s.split(',') );
}
},
// flag in URL query string, app.keyName is parameter
// both ?ff=Elvin&ff=Ringo and ?ff=Jones,Starr work
query: function() {
var s = app.loc.search,
a = s.substring(1).split('&'),
al = a.length,
i;
for ( i=0; i < al; i++ ) {
var p = a[i].split('=');
if ( p[0] === app.keyName && p[1] ) {
this._addFlag( p[1].split(',') );
}
}
},
// private, adds each new flag to app.flags array
_addFlag: function(f) {
if ( typeof(f) === 'string' ) {
app.flags.push(f);
} else {
var i;
for ( i in f ) {
app.flags.push( f[i] );
}
}
}
};
// call each desired method in app.setFlags
var m;
for ( m in app.methods ) {
app.setFlags[app.methods[m]]();
}
})( ff );
/*
If you change the ff variable name at top,
change it in the line above.
You also could attach this to an existing global,
just pass the function YO.ff
You can override any of the following:
(ff, {
keyName: 'ff',
testDomain: 'localhost',
testDomainFlag: 'debug',
methods: ['cookie', 'domain', 'hash', 'query'] })
*/