UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

71 lines (54 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NameGenerator = void 0; var _invariant = _interopRequireDefault(require("../invariant.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ function escapeInvalidIdentifierCharacters(s) { let res = ""; for (let c of s) if (c >= "0" && c <= "9" || c >= "a" && c <= "z" || c >= "A" && c <= "Z") res += c;else res += "_" + c.charCodeAt(0); return res; } const base62characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; function base62encode(n) { (0, _invariant.default)((n | 0) === n && n >= 0); if (n === 0) return "0"; let s = ""; while (n > 0) { let f = n % base62characters.length; s = base62characters[f] + s; n = (n - f) / base62characters.length; } return s; } class NameGenerator { constructor(forbiddenNames, debugNames, uniqueSuffix, prefix) { this.prefix = prefix; this.uidCounter = 0; this.debugNames = debugNames; this.forbiddenNames = forbiddenNames; this.uniqueSuffix = uniqueSuffix; } generate(debugSuffix) { let id; do { id = this.prefix + base62encode(this.uidCounter++); if (this.uniqueSuffix.length > 0) id += this.uniqueSuffix; if (this.debugNames) { if (debugSuffix) id += "_" + escapeInvalidIdentifierCharacters(debugSuffix);else id += "_"; } } while (this.forbiddenNames.has(id)); return id; } } exports.NameGenerator = NameGenerator; //# sourceMappingURL=NameGenerator.js.map