Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
is_native.js43 linesDownload Raw Back to js
1/* Code to check if the object or function is native JS2 * Author: John-David Dalton3 */4(function() {5 6  // Used to resolve the internal `[[Class]]` of values7  let toString = Object.prototype.toString;8 9  // Used to resolve the decompiled source of functions10  let fnToString = Function.prototype.toString;11 12  // Used to detect host constructors (Safari > 4; really typed array specific)13  let reHostCtor = /^\[object .+?Constructor\]$/;14 15  // Compile a regexp using a common native method as a template.16  // We chose `Object#toString` because there's a good chance it is not being mucked with.17  let reNative = RegExp('^' +18    // Coerce `Object#toString` to a string19    String(toString)20    // Escape any special regexp characters21      .replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&')22    // Replace mentions of `toString` with `.*?` to keep the template generic.23    // Replace thing like `for ...` to support environments like Rhino which add extra info24    // such as method arity.25      .replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'26  );27 28  function isNative(value) {29    let type = typeof value;30    return type == 'function'31      // Use `Function#toString` to bypass the value's own `toString` method32      // and avoid being faked out.33      ? reNative.test(fnToString.call(value))34      // Fallback to a host object check because some environments will represent35      // things like typed arrays as DOM methods which may not conform to the36      // normal native pattern.37      : (value && type == 'object' && reHostCtor.test(toString.call(value))) || false;38  }39 40  // export however you want41  module.exports = isNative;42}());43