-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
264 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
packages/export-variables-rest-api/src/__tests__/modifyData.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { updateApiResponse } from "../modifyData"; | ||
|
||
describe("updateApiResponse", () => { | ||
test("filter out remote collections and variables", () => { | ||
const input = { | ||
variableCollections: { | ||
"VariableCollectionId:1": { | ||
defaultModeId: "1:0", | ||
id: "VariableCollectionId:1", | ||
name: "Remote collection", | ||
remote: true, | ||
modes: [{ modeId: "1:0", name: "Mode" }], | ||
key: "123456", | ||
hiddenFromPublishing: true, | ||
variableIds: ["VariableID:1"], | ||
}, | ||
"VariableCollectionId:2": { | ||
defaultModeId: "2:0", | ||
id: "VariableCollectionId:2", | ||
name: "Local collection", | ||
remote: false, | ||
modes: [{ modeId: "2:0", name: "Mode" }], | ||
key: "223456", | ||
hiddenFromPublishing: false, | ||
variableIds: ["VariableID:2"], | ||
}, | ||
}, | ||
variables: { | ||
"VariableID:1": { | ||
id: "VariableID:1", | ||
name: "Color 1", | ||
remote: true, | ||
key: "09876", | ||
variableCollectionId: "VariableCollectionId:1", | ||
resolvedType: "COLOR", | ||
description: "", | ||
hiddenFromPublishing: true, | ||
valuesByMode: { | ||
"1:0": { | ||
r: 1, | ||
g: 1, | ||
b: 1, | ||
a: 1, | ||
}, | ||
}, | ||
scopes: ["ALL_SCOPES"], | ||
codeSyntax: {}, | ||
}, | ||
"VariableID:2": { | ||
id: "VariableID:2", | ||
name: "Color", | ||
remote: false, | ||
key: "98766", | ||
variableCollectionId: "VariableCollectionId:2", | ||
resolvedType: "COLOR", | ||
description: "", | ||
hiddenFromPublishing: false, | ||
valuesByMode: { | ||
"1:0": { | ||
r: 1, | ||
g: 1, | ||
b: 1, | ||
a: 1, | ||
}, | ||
}, | ||
scopes: ["ALL_SCOPES"], | ||
codeSyntax: {}, | ||
}, | ||
}, | ||
}; | ||
expect( | ||
Object.values(input.variableCollections).every((c) => c.remote === false) | ||
).toBeFalsy(); | ||
expect( | ||
Object.values(input.variables).every((c) => c.remote === false) | ||
).toBeFalsy(); | ||
|
||
const output = updateApiResponse(input); | ||
expect( | ||
Object.values(output.variableCollections).every((c) => c.remote === false) | ||
).toBeTruthy(); | ||
expect( | ||
Object.values(output.variables).every((c) => c.remote === false) | ||
).toBeTruthy(); | ||
}); | ||
test("append default to variable name when used as group name, but only in the same collection", () => { | ||
const input = { | ||
variableCollections: { | ||
"VariableCollectionId:1": { | ||
defaultModeId: "1:0", | ||
id: "VariableCollectionId:1", | ||
name: "Remote collection", | ||
remote: true, | ||
modes: [{ modeId: "1:0", name: "Mode" }], | ||
key: "123456", | ||
hiddenFromPublishing: true, | ||
variableIds: ["VariableID:1", "VariableID:2"], | ||
}, | ||
"VariableCollectionId:2": { | ||
defaultModeId: "2:0", | ||
id: "VariableCollectionId:2", | ||
name: "Local collection", | ||
remote: false, | ||
modes: [{ modeId: "2:0", name: "Mode" }], | ||
key: "223456", | ||
hiddenFromPublishing: false, | ||
variableIds: ["VariableID:3"], | ||
}, | ||
}, | ||
variables: { | ||
"VariableID:1": { | ||
id: "VariableID:1", | ||
name: "Color", | ||
remote: false, | ||
key: "09876", | ||
variableCollectionId: "VariableCollectionId:1", | ||
resolvedType: "COLOR", | ||
description: "", | ||
hiddenFromPublishing: false, | ||
valuesByMode: { | ||
"1:0": { | ||
r: 1, | ||
g: 1, | ||
b: 1, | ||
a: 1, | ||
}, | ||
}, | ||
scopes: ["ALL_SCOPES"], | ||
codeSyntax: {}, | ||
}, | ||
"VariableID:2": { | ||
id: "VariableID:2", | ||
name: "Color/100", | ||
remote: false, | ||
key: "98766", | ||
variableCollectionId: "VariableCollectionId:1", | ||
resolvedType: "COLOR", | ||
description: "", | ||
hiddenFromPublishing: false, | ||
valuesByMode: { | ||
"1:0": { | ||
r: 1, | ||
g: 1, | ||
b: 1, | ||
a: 1, | ||
}, | ||
}, | ||
scopes: ["ALL_SCOPES"], | ||
codeSyntax: {}, | ||
}, | ||
"VariableID:3": { | ||
id: "VariableID:3", | ||
name: "Color", | ||
remote: false, | ||
key: "98766", | ||
variableCollectionId: "VariableCollectionId:2", | ||
resolvedType: "COLOR", | ||
description: "", | ||
hiddenFromPublishing: false, | ||
valuesByMode: { | ||
"1:0": { | ||
r: 1, | ||
g: 1, | ||
b: 1, | ||
a: 1, | ||
}, | ||
}, | ||
scopes: ["ALL_SCOPES"], | ||
codeSyntax: {}, | ||
}, | ||
}, | ||
}; | ||
expect(input.variables["VariableID:1"].variableCollectionId).toEqual( | ||
input.variables["VariableID:2"].variableCollectionId | ||
); | ||
expect( | ||
input.variables["VariableID:2"].name.startsWith( | ||
input.variables["VariableID:1"].name | ||
) | ||
).toBeTruthy(); | ||
expect(input.variables["VariableID:3"].variableCollectionId).not.toEqual( | ||
input.variables["VariableID:2"].variableCollectionId | ||
); | ||
expect( | ||
input.variables["VariableID:2"].name.startsWith( | ||
input.variables["VariableID:3"].name | ||
) | ||
).toBeTruthy(); | ||
|
||
const output = updateApiResponse(input); | ||
expect(output.variables["VariableID:1"].name).toEqual("Color/default"); | ||
expect(output.variables["VariableID:3"].name).not.toEqual("Color/default"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Filter out remote variables and collections, add "/default" suffix to any | ||
* variable (in the same collection group) which also served as group name. | ||
* | ||
* e.g. when "Black" and "Black/100" both exist, variable with "Black" name | ||
* will be modified to be "Black/default". | ||
* | ||
* @param {*} data Figma variables REST API response's `meta` data | ||
* @returns new data with remote filtered out and variable name modified | ||
*/ | ||
export function updateApiResponse(data) { | ||
// New data object | ||
const newData = { variableCollections: {}, variables: {} }; | ||
|
||
// Find all non-remote collections | ||
const nonRemoteCollections = Object.values(data.variableCollections).filter( | ||
(c) => !c.remote | ||
); | ||
// Add collection to new object.variableCollections | ||
nonRemoteCollections.forEach((c) => (newData.variableCollections[c.id] = c)); | ||
|
||
// Filter and group non-remote variables by collections | ||
const variablesByGroup = {}; | ||
for (const v of Object.values(data.variables)) { | ||
if (v.remote) { | ||
continue; | ||
} | ||
const collectionId = v.variableCollectionId; | ||
if (variablesByGroup[collectionId] === undefined) { | ||
variablesByGroup[collectionId] = []; | ||
} | ||
variablesByGroup[collectionId].push(v); | ||
} | ||
|
||
// In each collection group | ||
for (const variablesInCollection of Object.values(variablesByGroup)) { | ||
// Sort variables by name | ||
variablesInCollection.sort((a, b) => a.name.localeCompare(b.name)); | ||
|
||
// Iterate variables, compare name with previous name | ||
let prevVariable = undefined; | ||
for (let index = 0; index < variablesInCollection.length; index++) { | ||
const element = variablesInCollection[index]; | ||
newData.variables[element.id] = element; | ||
|
||
if (prevVariable !== undefined) { | ||
// If name has exactly one more "/", previous one needs to be appended a "default" | ||
const lastSlashIndex = element.name.lastIndexOf("/"); | ||
if (lastSlashIndex !== -1) { | ||
if (element.name.substring(0, lastSlashIndex) === prevVariable.name) { | ||
prevVariable.name = prevVariable.name + "/default"; | ||
} | ||
} | ||
} | ||
prevVariable = element; | ||
} | ||
} | ||
|
||
// return new object | ||
return newData; | ||
} |