From e5df5349b6c191efdbb8f2c1cb3e5c6a2e0b7672 Mon Sep 17 00:00:00 2001 From: big nutty <3515180-bignutty@users.noreply.gitlab.com> Date: Wed, 26 Feb 2025 13:35:27 +0000 Subject: [PATCH] Revert "move to utils package xid" This reverts commit 938e07a03f1f19223f790e76f21b5446f6677ff6 --- labscore/cardstack/stack.js | 4 +-- labscore/utils/hash.js | 53 +++++++++++++++++++++++++++++++++++++ package.json | 3 +-- 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 labscore/utils/hash.js diff --git a/labscore/cardstack/stack.js b/labscore/cardstack/stack.js index eacd34c..27a2947 100644 --- a/labscore/cardstack/stack.js +++ b/labscore/cardstack/stack.js @@ -16,7 +16,7 @@ const { ResolveCallbackTypes } = require("./constants"); const {InteractiveComponentTypes, DEFAULT_BUTTON_STYLES} = require("#cardstack/constants"); -const {xid} = require("utils"); +const {Xid} = require("#utils/hash"); /** * Stores all active card stacks @@ -338,7 +338,7 @@ class DynamicCardStack { let component = { type: MessageComponentTypes.BUTTON, // id/XID is used for dynamically generated components via BUTTON_GENERATOR - customId: button.customId ? id + "/" + xid(button.customId) : id, + customId: button.customId ? id + "/" + Xid(button.customId) : id, style: button.style || 2, disabled: disabled } diff --git a/labscore/utils/hash.js b/labscore/utils/hash.js new file mode 100644 index 0000000..6d49d77 --- /dev/null +++ b/labscore/utils/hash.js @@ -0,0 +1,53 @@ +// Adapted from https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/Xid.java +const START_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const CHARS = START_CHARS + "0123456789"; +const START_RADIX = START_CHARS.length; +const RADIX = CHARS.length; + +/** + * A simple utility for shortening identifiers in a stable way. Generates + * short substitution strings deterministically, using a compact + * (1 to 6 characters in length) repesentation of a 32-bit hash of the key. + * The string is suitable to be used as a JavaScript or CSS identifier. + * Collisions are possible but unlikely, depending on the underlying hash algorithm used. + * + * This substitution scheme uses case-sensitive names for maximum + * compression. Digits are also allowed in all but the first character of a + * class name. There are a few characters allowed by the CSS grammar that we + * choose not to use (e.g. the underscore and hyphen), to keep names simple. + * + * Xid should maintain as minimal dependencies as possible to ease its + * integration with other tools, such as server side HTML generators. + * + * (https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/Xid.java#L18-L32) + * + * @param input Input + * @returns {string} Xid + */ +module.exports.Xid = (input) => { + if(typeof(input)==="string"){ + var h = 0, i, c; + if (input.length === 0) return h; + for (i = 0; i < input.length; i++) { + c = input.charCodeAt(i); + h = ((h << 5) - h) + c; + h |= 0; + } + if(h<=0) h = h*-1; + input = h; + } + + const buf = new Array(6); + let len = 0; + + let l = input - Math.floor(Number.MIN_SAFE_INTEGER); + buf[len++] = START_CHARS.charAt(Math.floor(l % START_RADIX)); + input = Math.floor(l / START_RADIX); + + while (input > 0) { + buf[len++] = CHARS.charAt(input % RADIX); + input = Math.floor(input / RADIX); + } + + return buf.slice(0, len).reverse().join(""); +} \ No newline at end of file diff --git a/package.json b/package.json index e8bea51..bb19d49 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,7 @@ "dotenv": "^16.4.7", "emoji-aware": "^3.1.0", "eventemitter3": "^5.0.1", - "superagent": "^10.1.1", - "utils": "gitlab:bignutty/utils" + "superagent": "^10.1.1" }, "imports": { "#api": "./labscore/api/index.js",