Skip to content

Commit

Permalink
Make RegExp.prototype.flags more spec-compliant
Browse files Browse the repository at this point in the history
Note that this still isn’t fully spec-compliant, since it shouldn’t be configurable.
  • Loading branch information
mathiasbynens committed Dec 13, 2014
1 parent 3e61627 commit 4bbbf4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 17 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,23 @@
if (!ES.TypeIsObject(this)) {
throw new TypeError('Method called on incompatible type: must be an object.');
}
var str = String(this);
return str.slice(str.lastIndexOf('/') + 1);
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;
};

Value.getter(RegExp.prototype, 'flags', regExpFlagsGetter);
Expand Down
14 changes: 13 additions & 1 deletion test/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,23 @@ describe('RegExp', function () {
});
});

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

describe('#flags', function () {
it('has the correct descriptor', function () {
expect(regexpFlagsDescriptor.configurable).to.equal(true);
expect(regexpFlagsDescriptor.enumerable).to.equal(false);
expect(regexpFlagsDescriptor.get instanceof Function).to.equal(true);
expect(regexpFlagsDescriptor.set).to.equal(undefined);
});

it('throws when not called on an object', function () {
var nonObjects = ['', false, true, 42, NaN, null, undefined];
nonObjects.forEach(function (nonObject) {
expect(function () { RegExp.prototype.flags.call(nonObject); }).to['throw'](TypeError);
expect(function () { testGenericRegExpFlags(nonObject); }).to['throw'](TypeError);
});
});

Expand Down

0 comments on commit 4bbbf4a

Please sign in to comment.