Repository URL to install this package:
|
Version:
3.0.0 ▾
|
import { fliphash, stringify } from 'chain-able-boost'
import {
IDENTIFIER_REFERENCE_REGISTRY,
IDENTIFIER_STRING_REGISTRY,
} from './IDENTIFIER_REGISTRY'
import { uuid } from './uuid'
/**
* @example for why `id` in part of a variable name sucks
* @see https://bitbucket.org/skava-admin/skreact/commits/fbdc931637a385f99a802f9cce5ef3c7c8953632?at=dev
*/
/**
* different than the other to identifiers -.-
* value-equals-identity
* one day, easy to wicked optimize this => trie
*/
function toIdentityHash(
small: Object | string,
ignoreReference: boolean | Object = false
): string {
// `a === b`
if (ignoreReference === false && IDENTIFIER_STRING_REGISTRY.has(small)) {
return IDENTIFIER_STRING_REGISTRY.get(small)
}
const string = stringify(small)
if (IDENTIFIER_REFERENCE_REGISTRY.has(string)) {
return IDENTIFIER_REFERENCE_REGISTRY.get(string)
}
const unique = uuid() + string
const hashstring = '__' + fliphash(unique)
IDENTIFIER_REFERENCE_REGISTRY.set(string, hashstring)
IDENTIFIER_STRING_REGISTRY.set(small, hashstring)
return hashstring
}
export default toIdentityHash
export { toIdentityHash }