MCPcopy Index your code
hub / github.com/plotly/plotly.py / randstr

Function randstr

js/src/widget.ts:2017–2059  ·  view source on GitHub ↗
(
  existing?: { [k: string]: any },
  bits?: number,
  base?: number,
  _recursion?: number
)

Source from the content-addressed store, hash-verified

2015}
2016
2017function randstr(
2018 existing?: { [k: string]: any },
2019 bits?: number,
2020 base?: number,
2021 _recursion?: number
2022): string {
2023 if (!base) base = 16;
2024 if (bits === undefined) bits = 24;
2025 if (bits <= 0) return "0";
2026
2027 var digits = Math.log(Math.pow(2, bits)) / Math.log(base);
2028 var res = "";
2029 var i, b, x;
2030
2031 for (i = 2; digits === Infinity; i *= 2) {
2032 digits = (Math.log(Math.pow(2, bits / i)) / Math.log(base)) * i;
2033 }
2034
2035 var rem = digits - Math.floor(digits);
2036
2037 for (i = 0; i < Math.floor(digits); i++) {
2038 x = Math.floor(Math.random() * base).toString(base);
2039 res = x + res;
2040 }
2041
2042 if (rem) {
2043 b = Math.pow(base, rem);
2044 x = Math.floor(Math.random() * b).toString(base);
2045 res = x + res;
2046 }
2047
2048 var parsed = parseInt(res, base);
2049 if (
2050 (existing && existing[res]) ||
2051 (parsed !== Infinity && parsed >= Math.pow(2, bits))
2052 ) {
2053 if (_recursion > 10) {
2054 console.warn("randstr failed uniqueness");
2055 return res;
2056 }
2057 return randstr(existing, bits, base, (_recursion || 0) + 1);
2058 } else return res;
2059}
2060
2061export default () => {
2062 let model;

Callers 1

perform_renderMethod · 0.85

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected