Skip to content
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

Avoid eval in cloneRegex #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1559,8 +1559,8 @@
*
* @examples
* Lazy([1, 2, 2, 3, 3, 3]).uniq() // sequence: [1, 2, 3]
* Lazy([{ name: 'mike' },
* { name: 'sarah' },
* Lazy([{ name: 'mike' },
* { name: 'sarah' },
* { name: 'mike' }
* ]).uniq('name')
* // sequence: [{ name: 'mike' }, { name: 'sarah' }]
Expand Down Expand Up @@ -6104,15 +6104,32 @@
}

/**
* "Clones" a regular expression (but makes it always global).
* "Clones" a regular expression, but ensures it is always global.
* Will return the passed RegExp if global is already set.
*
* @private
* @param {RegExp|string} pattern
* @returns {RegExp}
*/
function cloneRegex(pattern) {
return eval("" + pattern + (!pattern.global ? "g" : ""));
};
var patternStr, lsi, flags, global;
if (pattern instanceof RegExp) {
// Just return the passed in RegExp
if (pattern.global)
return pattern;
patternStr = pattern.toString();
}
else {
patternStr = pattern;
}
lsi = patternStr.lastIndexOf("/");
// No widespread RegExp.prototype.flags, unfortuantely;
// We use the string approach for both argument types!
flags = patternStr.substring(lsi + 1);
global = flags.indexOf("g") >= 0 ? "" : "g";

return new RegExp(patternStr.substring(1, lsi), flags + global);
}

/**
* A collection of unique elements.
Expand Down
2 changes: 1 addition & 1 deletion lazy.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ if (typeof Stream.Readable !== "undefined") {

this.sequence = sequence;
this.started = false;

// Find delimiter on a (parent) sequence object if set
while (sequence) {
if (sequence.delimiter) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"underscore": "1.8.3"
},
"scripts": {
"test": "npm run-script 'autodoc' && npm run-script 'jasmine' && npm run-script 'amd' && npm run-script 'promise'",
"test": "npm run-script autodoc && npm run-script jasmine && npm run-script amd && npm run-script promise",
"autodoc": "autodoc -t --variable Lazy lazy.js",
"jasmine": "jasmine-node spec/node_spec.js",
"amd": "node spec/amd_spec.js",
Expand Down