Skip to content
This repository has been archived by the owner on Sep 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #10 from Floofies/master
Browse files Browse the repository at this point in the history
Added RegExp flag cloning support
  • Loading branch information
Floofies authored Aug 25, 2017
2 parents fe0f96b + ef1c0e3 commit 3fc647b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/differentia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down

0 comments on commit 3fc647b

Please sign in to comment.