es-string-html-methods
Version:
An ES-spec-compliant shim/polyfill/replacement for the Annex B String.prototype HTML methods that works as far down as ES3
33 lines (26 loc) • 854 B
JavaScript
'use strict';
var Call = require('es-abstract/2024/Call');
var callBound = require('call-bound');
var $match = callBound('String.prototype.match');
var $toLowerCase = callBound('String.prototype.toLowerCase');
var safeConcat = require('safe-array-concat');
module.exports = function polyfillHelper(property, implementation) {
return function polyfill() {
var method = String.prototype[property];
if (typeof method !== 'function') {
return implementation;
}
var output = Call(method, '', [' " ']);
var quotesCount = safeConcat($match(output, /"/g)).length;
if (output !== $toLowerCase(output) || quotesCount > 2) {
return implementation;
}
try {
// node <= 0.10 does not throw on a nullish receiver
Call(method, null);
Call(method, undefined);
return implementation;
} catch (e) { /**/ }
return method;
};
};