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/modules / ___dist / identifier / toIdentifier.js
Size: Mime:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toIdentifier = toIdentifier;
exports.default = void 0;

var _exotic = require("../exotic");

var _toComponentName = _interopRequireDefault(require("./toComponentName"));

var _uuid = _interopRequireDefault(require("./uuid"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable max-statements */

/* eslint-disable brace-style */

/* @lint ^ 1 is for tests, 1 is for readable long conditionals */
// import { toComponentName, toIdentity } from 'modules/view-container/deps'
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 ((0, _exotic.isObj)(instance) === false && (0, _exotic.isFunction)(instance) === false) {
    if ((0, _exotic.isString)(instance) || (0, _exotic.isNumber)(instance) || (0, _exotic.isBoolean)(instance)) {
      return instance;
    } else if (IS_TEST === true) {
      return '@@uuid--test';
    } else {
      return (0, _uuid.default)();
    }
  } else if (identityWeakmap.has(instance)) {
    return identityWeakmap.get(instance);
  } else {
    const hashedId = hash + (0, _toComponentName.default)(instance);

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

    const id = hashedId + (0, _uuid.default)();
    identityWeakmap.set(instance, id);
    return id;
  }
}

var _default = toIdentifier;
exports.default = _default;