Repository URL to install this package:
|
Version:
3.0.0 ▾
|
/* 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