From 4186cc9d9a7533f78d88be976f0a8a2757604fc5 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 10 Dec 2014 00:40:41 -0800 Subject: [PATCH] Implementation. --- index.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..0590ad8 --- /dev/null +++ b/index.js @@ -0,0 +1,36 @@ +'use strict'; + +var flagsGetter = function flags() { + var str = String(this); + return str.slice(str.lastIndexOf('/') + 1); +}; + +var supportsDescriptors = (function () { + if (!Object.defineProperty) { + return false; + } + try { + Object.defineProperty({}, 'x', {}); + return true; + } catch (e) { /* this is IE 8. */ + return false; + } +}()); + +var flags = Function.call.bind(flagsGetter); + +flags.shim = function flagsShim() { + if (!supportsDescriptors) { + throw new TypeError('RegExp.prototype.flags requires a true ES5 environment that supports property descriptors'); + } + if (/a/mig.flags !== 'gim') { + Object.defineProperty(RegExp.prototype, 'flags', { + configurable: true, + enumerable: false, + get: flagsGetter + }); + } + return flags; +}; + +module.exports = flags;