Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/identifier / src / toIdentifier.ts
Size: Mime:
/* eslint-disable max-statements */
/* eslint-disable brace-style */
/* @lint ^ 1 is for tests, 1 is for readable long conditionals */
import {
  isObj,
  isFunction,
  isString,
  isNumber,
  isBoolean,
  hasOwnProp,
} from 'exotic'
// import { toComponentName, toIdentity } from 'view-container/deps'
import toComponentName from './toComponentName'
import uuid from './uuid'

let identityWeakmap = new WeakMap()
const IS_TEST = process.env.NODE_ENV === 'test'

/**
 * @todo hashnumber here
 * @todo id based on CLASS + INSTANCE
 * @todo selectors ^
 * @todo use this to also cache identifiers for things alternative from weakmap
 *
 * @param {Object | string | number} [optionalClassOrInstance=undefined]
 * @param  {string | *} [hash='']
 * @return {String}
 */
function toIdentifier(instance, hash = '') {
  if (isObj(instance) === false && isFunction(instance) === false) {
    if (isString(instance) || isNumber(instance) || isBoolean(instance)) {
      return instance
    } else if (IS_TEST === true) {
      return '@@uuid--test'
    } else {
      return uuid()
    }
  } else if (identityWeakmap.has(instance)) {
    return identityWeakmap.get(instance)
  } else {
    const hashedId = hash + toComponentName(instance)

    if (IS_TEST === true) {
      return '@@uuid--test--' + hashedId
    }

    const id = hashedId + uuid()
    identityWeakmap.set(instance, id)
    return id
  }
}

export { toIdentifier }
export default toIdentifier