From ef1c0e32d62c5ef6e0ebde2491487e20361d88c2 Mon Sep 17 00:00:00 2001 From: Floofies Date: Fri, 25 Aug 2017 11:29:24 -0400 Subject: [PATCH] Added RegExp flag cloning support --- src/differentia.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/differentia.js b/src/differentia.js index f230655..eb4bf68 100755 --- a/src/differentia.js +++ b/src/differentia.js @@ -8,6 +8,12 @@ var differentia = (function () { if (typeof module === "undefined") { var module = { exports: null }; } + // Checks if certain RegExp props are supported. + var supportedRegExpProps = { + sticky: "sticky" in RegExp.prototype, + unicode: "unicode" in RegExp.prototype, + flags: "flags" in RegExp.prototype + }; /** * assert - Logs or throws an Error if `boolean` is false, * If `boolean` is `true`, nothing happens. @@ -240,7 +246,17 @@ var differentia = (function () { if (state.isContainer) { if (state.currentValue instanceof RegExp) { // Clone a Regular Expression - state.tuple.clone[state.accessor] = new RegExp(state.currentValue.source); + var flags = ""; + if (supportedRegExpProps.flags) { + flags = state.currentValue.flags; + } else { + if (state.currentValue.global) flags += "g"; + if (state.currentValue.ignorecase) flags += "i"; + if (state.currentValue.multiline) flags += "m"; + if (supportedRegExpProps.sticky && state.currentValue.sticky) flags += "y"; + if (supportedRegExpProps.unicode && state.currentValue.unicode) flags += "u"; + } + state.tuple.clone[state.accessor] = new RegExp(state.currentValue.source, flags); } else { if (state.existing !== null) { state.tuple.clone[state.accessor] = state.existing.clone; @@ -257,12 +273,6 @@ var differentia = (function () { } } }; - // Checks for which "newer" RegExp properties are supported. - var supportedRegExpProps = { - sticky: "sticky" in RegExp.prototype, - unicode: "unicode" in RegExp.prototype, - flags: "flags" in RegExp.prototype - }; strategies.diff = { interface: function (subject, compare, search = null) { if (search === null && getContainerLength(subject) !== getContainerLength(compare)) {