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

Match the spec properly #3

Merged
merged 2 commits into from
Dec 13, 2014
Merged
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
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
'use strict';

var flagsGetter = function flags() {
var str = String(this);
return str.slice(str.lastIndexOf('/') + 1);
if (this != null && this !== Object(this)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this !== Object(this) { is sufficient; no need to check for null or undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it’s not necessary. Did you see the commit message of the commit that introduced this change?

Speed up the “is object” check in case of null or undefined

I’m fine with removing it; just want to make sure we’re on the same page here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's true. Sure, why not keep it.

throw new TypeError('RegExp.prototype.flags getter called on non-object');
}
var result = '';
if (this.global) {
result += 'g';
}
if (this.ignoreCase) {
result += 'i';
}
if (this.multiline) {
result += 'm';
}
if (this.unicode) {
result += 'u';
}
if (this.sticky) {
result += 'y';
}
return result;
};

var supportsDescriptors = (function () {
Expand Down
29 changes: 25 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ var getRegexLiteral = function (stringRegex) {
} catch (e) {}
};

flags.shim();
var descriptor = Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags');
var testGenericFlags = function (object) {
return descriptor.get.call(object);
};

test('works as a function', function (t) {
t.equal(flags(/a/g), 'g', 'flags(/a/g) !== "g"');
t.equal(flags(/a/gmi), 'gim', 'flags(/a/gmi) !== "gim"');
Expand Down Expand Up @@ -43,15 +49,13 @@ test('works as a function', function (t) {
var nonObjects = ['', false, true, 42, NaN, null, undefined];
st.plan(nonObjects.length);
nonObjects.forEach(function (nonObject) {
st.throws(function () { RegExp.prototype.flags.call(nonObject); }, TypeError);
st.throws(function () { flags.call(nonObject); }, TypeError);
});
});
t.end();
});

test('shims properly', function (t) {
flags.shim();

t.equal((/a/g).flags, 'g', '(/a/g).flags !== "g"');
t.equal((/a/gmi).flags, 'gim', '(/a/gmi).flags !== "gim"');
t.equal(new RegExp('a', 'gmi').flags, 'gim', 'new RegExp("a", "gmi").flags !== "gim"');
Expand Down Expand Up @@ -79,12 +83,29 @@ test('shims properly', function (t) {
st.end();
});

t.test('has the correct descriptor', function (st) {
st.equal(descriptor.configurable, true);
st.equal(descriptor.enumerable, false);
st.equal(typeof descriptor.get, 'function');
st.equal(descriptor.set, undefined);
st.end();
});

t.test('throws properly', function (st) {
var nonObjects = ['', false, true, 42, NaN, null, undefined];
st.plan(nonObjects.length);
nonObjects.forEach(function (nonObject) {
st.throws(function () { RegExp.prototype.flags.call(nonObject); }, TypeError);
st.throws(function () { testGenericFlags(nonObject); }, TypeError);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary; the original is functionally equivalent and imo reads more clearly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary: paulmillr/es6-shim#310 (comment) RegExp.prototype.flags.call({}) throws TypeError: call is not a function since flags is a getter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhhh that makes sense. Thanks for clarifying.

});
});

t.test('generic flags', function (st) {
st.equal(testGenericFlags({}), '');
st.equal(testGenericFlags({ ignoreCase: true }), 'i');
st.equal(testGenericFlags({ global: 0, sticky: 1, unicode: 1 }), 'uy');
st.equal(testGenericFlags({ __proto__: { multiline: true } }), 'm');
st.end();
});

t.end();
});