From c951f46d130c72a660ce3ecf88b9861b18129fb3 Mon Sep 17 00:00:00 2001 From: Deide Date: Fri, 22 Apr 2016 18:42:31 +0100 Subject: [PATCH 1/2] Parse RegExp to clone instead of using `eval` --- lazy.js | 23 ++++++++++++++++++++--- package.json | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lazy.js b/lazy.js index 1c39d8f..57b91bb 100644 --- a/lazy.js +++ b/lazy.js @@ -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' }] @@ -6111,7 +6111,24 @@ * @returns {RegExp} */ function cloneRegex(pattern) { - return eval("" + pattern + (!pattern.global ? "g" : "")); + var flags, global, patternStr; + if (typeof pattern === "string") { + patternStr = pattern; + flags = pattern.substr(pattern.lastIndexOf("/") + 1); + global = flags.indexOf("g") >= 0 ? "" : "g"; + } + else { + patternStr = pattern.toString(); + // No widespread RegExp.prototype.flags, unfortuantely; + // We use the string approach instead! + flags = patternStr.substr(patternStr.lastIndexOf("/") + 1); + global = Boolean(pattern.global) ? "" : "g"; + } + var res = new RegExp( + patternStr.slice(1, -(flags.length + 1)), + flags + global + ); + return res; }; /** diff --git a/package.json b/package.json index 9d6da76..a00203e 100644 --- a/package.json +++ b/package.json @@ -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", From 45dcea48f329dfd2524cb2f202cb55baee1a20dd Mon Sep 17 00:00:00 2001 From: Deide Date: Sat, 23 Apr 2016 12:14:05 +0100 Subject: [PATCH 2/2] Don't clone RegExp if global flag exists already --- lazy.js | 34 +++++++++++++++++----------------- lazy.node.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lazy.js b/lazy.js index 57b91bb..f44b727 100644 --- a/lazy.js +++ b/lazy.js @@ -6104,32 +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) { - var flags, global, patternStr; - if (typeof pattern === "string") { - patternStr = pattern; - flags = pattern.substr(pattern.lastIndexOf("/") + 1); - global = flags.indexOf("g") >= 0 ? "" : "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.toString(); - // No widespread RegExp.prototype.flags, unfortuantely; - // We use the string approach instead! - flags = patternStr.substr(patternStr.lastIndexOf("/") + 1); - global = Boolean(pattern.global) ? "" : "g"; + patternStr = pattern; } - var res = new RegExp( - patternStr.slice(1, -(flags.length + 1)), - flags + global - ); - return res; - }; + 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. diff --git a/lazy.node.js b/lazy.node.js index 2fb4a9e..1704312 100644 --- a/lazy.node.js +++ b/lazy.node.js @@ -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) {