@supernova-studio/model 0.58.27 → 0.59.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supernova-studio/model",
3
- "version": "0.58.27",
3
+ "version": "0.59.0",
4
4
  "description": "Supernova Data Models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -93,14 +93,32 @@ const stringTokenTypes: Set<DesignTokenType> = new Set([
93
93
  "FontWeight",
94
94
  ] satisfies DesignTokenType[]);
95
95
 
96
- export function areTokenTypesCompatible(lhs: DesignTokenType, rhs: DesignTokenType): boolean {
96
+ const fallbackNumberValue = 14;
97
+
98
+ export function areTokenTypesCompatible(
99
+ lhs: DesignTokenType,
100
+ rhs: DesignTokenType,
101
+ isNonCompatibleTypeChangesEnabled = false
102
+ ): boolean {
97
103
  if (lhs === rhs) return true;
98
104
  if (numberTokenTypes.has(lhs) && numberTokenTypes.has(rhs)) return true;
99
105
  if (stringTokenTypes.has(lhs) && stringTokenTypes.has(rhs)) return true;
100
106
 
107
+ if (isNonCompatibleTypeChangesEnabled && stringTokenTypes.has(lhs) && numberTokenTypes.has(rhs)) return true;
108
+
101
109
  return false;
102
110
  }
103
111
 
112
+ export function castStringToDimensionValue(lhs: DesignTokenType, rhs: DesignTokenType, value?: unknown) {
113
+ if (stringTokenTypes.has(lhs) && numberTokenTypes.has(rhs) && value) {
114
+ const newValue = Number.parseFloat(value?.toString() ?? "");
115
+ const measure = Number.isNaN(newValue) ? fallbackNumberValue : newValue;
116
+ return { unit: "Pixels", measure };
117
+ }
118
+
119
+ return value;
120
+ }
121
+
104
122
  //
105
123
  // Element category
106
124
  //
@@ -35,7 +35,7 @@ import {
35
35
  VisibilityTokenData,
36
36
  ZIndexTokenData,
37
37
  } from "./data";
38
- import { areTokenTypesCompatible, DesignTokenType } from "./raw-element";
38
+ import { areTokenTypesCompatible, castStringToDimensionValue, DesignTokenType } from "./raw-element";
39
39
 
40
40
  //
41
41
  // Base
@@ -279,15 +279,21 @@ export function extractTokenTypedData<T extends DesignTokenType, I extends Desig
279
279
 
280
280
  export function convertTokenTypedData<I extends DesignTokenType, O extends DesignTokenType>(
281
281
  source: DesignTokenTypedDataOfType<I>,
282
- type: O
282
+ type: O,
283
+ isNonCompatibleTypeChangesEnabled: boolean
283
284
  ): DesignTokenTypedDataOfType<O> {
284
- if (!areTokenTypesCompatible(source.type, type)) {
285
+ if (!areTokenTypesCompatible(source.type, type, isNonCompatibleTypeChangesEnabled)) {
285
286
  throw SupernovaException.invalidOperation(`Cannot convert token from ${source.type} to ${type}`);
286
287
  }
287
288
 
289
+ const data = source.data;
290
+ if (isNonCompatibleTypeChangesEnabled) {
291
+ data.value = castStringToDimensionValue(source.type, type, source.data.value);
292
+ }
293
+
288
294
  return {
289
- type: type,
290
- data: source.data,
295
+ type,
296
+ data,
291
297
  } as DesignTokenTypedDataOfType<O>;
292
298
  }
293
299
 
@@ -60,6 +60,7 @@ export const FeatureFlagsKeepAliases = z.object({
60
60
  isTypographyPropsKeepAliasesEnabled: z.boolean().default(false),
61
61
  isGradientPropsKeepAliasesEnabled: z.boolean().default(false),
62
62
  isShadowPropsKeepAliasesEnabled: z.boolean().default(false),
63
+ isNonCompatibleTypeChangesEnabled: z.boolean().default(false),
63
64
  });
64
65
  export type FeatureFlagsKeepAliases = z.infer<typeof FeatureFlagsKeepAliases>;
65
66
 
@@ -8,6 +8,7 @@ export const FlaggedFeature = z.enum([
8
8
  "TypographyPropsKeepAliases",
9
9
  "GradientPropsKeepAliases",
10
10
  "ShadowPropsKeepAliases",
11
+ "NonCompatibleTypeChanges",
11
12
  ]);
12
13
  export const FeatureFlagMap = z.record(FlaggedFeature, z.boolean());
13
14
  export const FeatureFlag = z.object({