-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag.js
153 lines (126 loc) · 3.94 KB
/
flag.js
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
144
145
146
147
148
149
150
151
152
153
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(["require"], function (require) {
var module = factory();
if (require.specified('khoaijs')) {
require(['khoaijs'], function (Khoai) {
Khoai.Flag = module;
});
}
root.Flag = module;
return module;
});
} else {
var module = factory();
if (root.Khoai) {
root.Khoai.Flag = module;
}
root.Flag = module;
}
}(this, function () {
"use strict";
function Flag() {
this._flags = {};
}
/**
* Check if a flag is exists
* @param {string} name
* @return {boolean}
*/
Flag.prototype.hasFlag = function (name) {
return this._flags && this._flags.hasOwnProperty(name);
};
/**
* Set a flag
* @param {string} name
* @param {boolean} [is_active=true] Flag status, default is True
* @return {Flag}
*/
Flag.prototype.flag = function (name, is_active) {
is_active = (is_active || typeof is_active == 'undefined') ? true : Boolean(is_active);
if (!this._flags) {
this._flags = {};
}
if (name instanceof Array) {
for (var i = 0, len = name.length; i < len; i++) {
this._flags[name[i]] = is_active;
}
} else if (name instanceof Object) {
for (var tmp_name in name) {
if (name.hasOwnProperty(tmp_name)) {
this._flags[tmp_name] = Boolean(name[tmp_name]);
}
}
} else {
this._flags[name] = is_active;
}
return this;
};
/**
* Get flag status is on (true) or off (false)
* @param {string} name
* @return {boolean} true -> on, false -> off
*/
Flag.prototype.flagStatus = function (name) {
return this._flags && this._flags.hasOwnProperty(name) && Boolean(this._flags[name]);
};
/**
* Check if a flag is exists and it's status is on
* @param {string} name
* @return {boolean}
*/
Flag.prototype.isFlagged = function (name) {
return true === this.flagStatus(name);
};
/**
*
* @param {string|Array} flags
* @param {boolean} [status] Missing - On/off when current flag's status is off/on.
* Boolean - On/off when status is true/false
*
* @return {Flag}
*/
Flag.prototype.toggleFlag = function (flags, status) {
if (flags instanceof Array) {
for (var index in flags) {
this.toggleFlag(flags[index], status);
}
return this;
}
if (typeof status != 'undefined') {
this.flag(flags, Boolean(status));
} else {
this.flag(flags, !this.isFlagged(flags));
}
return this;
};
/**
*
* @return {Flag}
*/
Flag.prototype.resetFlagStatus = function () {
this._flags = {};
return this;
};
Flag.mixin = function (destObject) {
var props = ['hasFlag', 'flag', 'flagStatus', 'isFlagged', 'toggleFlag', 'resetFlagStatus'];
for (var i = 0; i < props.length; i++) {
if (typeof destObject === 'function') {
destObject.prototype[props[i]] = Flag.prototype[props[i]];
} else {
destObject[props[i]] = Flag.prototype[props[i]];
}
}
return destObject;
};
var static_instance = new Flag();
var methods = ['hasFlag', 'flag', 'flagStatus', 'isFlagged', 'toggleFlag', 'resetFlagStatus'];
for (var i = 0, len = methods.length; i < len; i++) {
Flag[methods[i]] = (function (method) {
return function () {
return static_instance[method].apply(static_instance, Array.prototype.slice.call(arguments));
};
})(methods[i]);
}
return Flag;
}));