-
Notifications
You must be signed in to change notification settings - Fork 935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introducing "dynamic mode" allowing enable/disable on the fly #156
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7cb2cc
Adding "dynamic mode", splitting out enabled/disable logic into it's …
111b74d
adding an example server to demonstrate dynamic mode
d5e68c0
adding tests for able.js
9ea71b6
fixing bugs found after trying out many examples
55d9af5
adding special case for '*' wildcard to enable/disable
6aba343
making changes to ensure we don't break the API
61092e9
adding ability to retrieve dynamic flag externally
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* in-memory cache | ||
*/ | ||
|
||
var enabled = []; | ||
var disabled = []; | ||
|
||
/** | ||
* Checks if the specified `ns` is enabled. | ||
* | ||
* @param {String} ns Wildcards are not supported here | ||
* @returns {Boolean} | ||
*/ | ||
|
||
exports.enabled = function (ns) { | ||
if (find(disabled, ns, true) > -1) return false; | ||
if (find(enabled, ns, true) > -1) return true; | ||
return false; | ||
}; | ||
|
||
/** | ||
* Destroys the lists of enabled/disabled. (primarilly for testing purposes) | ||
*/ | ||
|
||
exports.clear = function () { | ||
disabled.length = enabled.length = 0; // truncates w/o destroying | ||
}; | ||
|
||
/** | ||
* Outputs the list of enable/disabled in a single string. | ||
* | ||
* @returns {String} | ||
*/ | ||
|
||
exports.stringify = function () { | ||
var e = enabled.map(function (item) { | ||
return item.string; | ||
}); | ||
|
||
var d = disabled.map(function (item) { | ||
return '-' + item.string; | ||
}); | ||
|
||
return e.concat(d).join(','); | ||
}; | ||
|
||
/** | ||
* Parses a list and enables/disables accordingly. | ||
* | ||
* This list can either be passed from the user directly, or from .stringify() | ||
* | ||
* @param {String} str | ||
*/ | ||
|
||
exports.parse = function (str) { | ||
if (!str) return []; | ||
return (str || '').trim().split(/[\s,]+/).filter(function (item) { | ||
return !!item; | ||
}); | ||
}; | ||
|
||
/** | ||
* Enables the specified `ns`. | ||
* | ||
* @param {String} ns | ||
*/ | ||
|
||
exports.enable = function (ns) { | ||
// special case, empty the current list and allow it to append | ||
if ('*' == ns) exports.clear(); | ||
|
||
prune(disabled, ns); | ||
if (find(enabled, ns) > -1) return; | ||
|
||
enabled.push({ | ||
string: ns, | ||
regex: regex(ns) | ||
}); | ||
}; | ||
|
||
/** | ||
* Disables the specified `ns`. | ||
* | ||
* @param {String} ns | ||
*/ | ||
|
||
exports.disable = function (ns) { | ||
// special case, empty the current list (since default is to allow nothing) | ||
if ('*' == ns) return exports.clear(); | ||
|
||
prune(enabled, ns); | ||
if (find(disabled, ns) > -1) return; | ||
|
||
disabled.push({ | ||
string: ns, | ||
regex: regex(ns) | ||
}); | ||
}; | ||
|
||
/** | ||
* Searches for the given `ns` in the `arr`. | ||
* | ||
* By default, it only matches on the raw string, but if `regex` is set, it will | ||
* match via the `RegExp` instead. | ||
* | ||
* Returns the index of the match, or -1 if not found. | ||
* | ||
* @param {Array} arr | ||
* @param {String} ns | ||
* @param {Boolean} [regex] | ||
* @returns {Number} | ||
*/ | ||
|
||
function find(arr, ns, regex) { | ||
var ret = -1; | ||
arr.some(function (item, x) { | ||
if (regex ? item.regex.test(ns) : ns === item.string) { | ||
ret = x; | ||
return true; | ||
} | ||
}); | ||
return ret; | ||
} | ||
|
||
/** | ||
* Wraps around `find(...)`, but also removes the found item. | ||
* | ||
* @param {Array} arr | ||
* @param {String} ns | ||
* @param {Boolean} [regex] | ||
*/ | ||
|
||
function prune(arr, ns, regex) { | ||
var x = find(arr, ns, regex); | ||
if (x > -1) arr.splice(x, 1); | ||
} | ||
|
||
/** | ||
* Converts a raw `ns` into a `RegExp`. | ||
* | ||
* @param {String} ns | ||
* @returns {RegExp} | ||
*/ | ||
|
||
function regex(ns) { | ||
var pattern = ns.replace(/\*/g, '.*?'); | ||
return new RegExp('^' + pattern + '$'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not supporting wildcards here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been a really long time... but I think it was just for simplicity. Imagine I've enabled
mocha*
but disabledmocha:runner
, what would you expectenabled("mocha*")
to return? (since there is a clear exception to this rule that makes it so the wildcard technically can't be true 100% of the time)