Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / exotic   js

Repository URL to install this package:

Version: 2.0.8 

/ src / types / NATIVE / isNative.ts

import { matchNative } from '../../deps'
import NATIVE_FLAT_PAIRS_LIST from './CONSTANTS/NATIVE_NAMES_LIST'
import { functionToString } from './CONSTANTS/PROTOTYPE_METHODS'

/**
 * @desc based on isNative from react-fibers, based on isNative() from Lodash
 * @since 4.0.6
 * @memberOf is
 * @func isNative
 *
 * @param {*} x value to check
 * @return {boolean}
 *
 * {@link https://esdiscuss.org/topic/spec-feedback-on-rev-6#content-2 esdiscuss-functiontostring}
 * {@link https://github.com/lodash/lodash/issues/2185 lodash-functiontostring-issue}
 * {@link http://tc39.github.io/Function-prototype-toString-revision/ functiontostring-emca}
 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString Function.toString}
 *
 * @see {@link Function.toString}
 * @see {@link functiontostring-emca}
 * @see {@link lodash-functiontostring-issue}
 * @see {@link esdiscuss-functiontostring}
 *
 * @example
 *
 * isNative(Array.prototype.push)
 * //=> true
 *
 * isNative(function normalFunction() {})
 * //=> false
 *
 */
function isNativeFunction(x: any): x is Function {
  try {
    const source = functionToString.call(x)
    return matchNative.test(source)
  } catch (err) {
    return false
  }
}

// Native - much faster than try catch function toString regexp matching
// though it would not include ALL **METHODS** (Object Properties)
// eslint-disable-next-line
const isBuiltIn = (x: any) =>
  NATIVE_FLAT_PAIRS_LIST.indexOf(x) !== -1 || isNativeFunction(x)

const isNative = isBuiltIn

export { isNativeFunction, isBuiltIn, isNative }