@webstudio-is/sdk 0.92.0 → 0.94.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -10,3 +10,4 @@ export * from "./schema/style-source-selections";
10
10
  export * from "./schema/styles";
11
11
  export * from "./schema/deployment";
12
12
  export * from "./instances-utils";
13
+ export * from "./scope";
package/lib/scope.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ export const createScope = (occupiedIdentifiers = []) => {
3
+ const freeIndexByPreferredName = /* @__PURE__ */ new Map();
4
+ const scopedNameByIdMap = /* @__PURE__ */ new Map();
5
+ for (const identifier of occupiedIdentifiers) {
6
+ freeIndexByPreferredName.set(identifier, 1);
7
+ }
8
+ const getName = (id, preferredName) => {
9
+ const cachedName = scopedNameByIdMap.get(id);
10
+ if (cachedName !== void 0) {
11
+ return cachedName;
12
+ }
13
+ const index = freeIndexByPreferredName.get(preferredName);
14
+ freeIndexByPreferredName.set(preferredName, (index ?? 0) + 1);
15
+ let scopedName = preferredName;
16
+ if (index !== void 0) {
17
+ scopedName = `${preferredName}_${index}`;
18
+ }
19
+ scopedNameByIdMap.set(id, scopedName);
20
+ return scopedName;
21
+ };
22
+ return {
23
+ getName
24
+ };
25
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ import { expect, test } from "@jest/globals";
3
+ import { createScope } from "./scope";
4
+ test("use variable name for specific id and suffix on collision", () => {
5
+ const scope = createScope();
6
+ expect(scope.getName("1", "myName")).toEqual("myName");
7
+ expect(scope.getName("2", "myName")).toEqual("myName_1");
8
+ expect(scope.getName("1", "myName")).toEqual("myName");
9
+ });
10
+ test("allow to predefine already occupied identifiers", () => {
11
+ const scope = createScope(["myName", "anotherName"]);
12
+ expect(scope.getName("1", "myName")).toEqual("myName_1");
13
+ expect(scope.getName("2", "anotherName")).toEqual("anotherName_1");
14
+ expect(scope.getName("3", "newName")).toEqual("newName");
15
+ });
@@ -9,3 +9,4 @@ export * from "./schema/style-source-selections";
9
9
  export * from "./schema/styles";
10
10
  export * from "./schema/deployment";
11
11
  export * from "./instances-utils";
12
+ export * from "./scope";
@@ -0,0 +1,16 @@
1
+ export type Scope = {
2
+ /**
3
+ * Accepts unique id to identify specific variable
4
+ * and preferred name to use it as variable name
5
+ * or suffix if already used.
6
+ */
7
+ getName(id: string, preferredName: string): string;
8
+ };
9
+ /**
10
+ * Utility to maintain unique variable when generate code.
11
+ * Single scope is shared for generated module for simplicity.
12
+ *
13
+ * occupiedIdentifiers parameter prevents collision with hardcoded
14
+ * identifiers.
15
+ */
16
+ export declare const createScope: (occupiedIdentifiers?: string[]) => Scope;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/sdk",
3
- "version": "0.92.0",
3
+ "version": "0.94.0",
4
4
  "description": "Webstudio site data schema",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -18,8 +18,8 @@
18
18
  "sideEffects": false,
19
19
  "dependencies": {
20
20
  "zod": "^3.21.4",
21
- "@webstudio-is/fonts": "^0.92.0",
22
- "@webstudio-is/css-engine": "^0.92.0"
21
+ "@webstudio-is/fonts": "^0.94.0",
22
+ "@webstudio-is/css-engine": "^0.94.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@jest/globals": "^29.6.4",