Skip to content

Commit

Permalink
avoid returning empty {} object from validateStudioTokensExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
macintoshhelper committed Jun 11, 2024
1 parent fbfc83a commit 6300bec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const output = {
describe('validateStudioTokensExtensions', () => {
it('strips empty extension', () => {
const validatedTokens = validateStudioTokensExtensions(tokens.blue as SingleToken);
expect(validatedTokens).toEqual({});
expect(validatedTokens).toEqual(undefined);
});
it('skips no extensions', () => {
const validatedTokens = validateStudioTokensExtensions(tokens.green as SingleToken);
Expand All @@ -76,7 +76,7 @@ describe('validateStudioTokensExtensions', () => {
});
it('strips empty modify', () => {
const validatedTokens = validateStudioTokensExtensions(tokens.red as SingleToken);
expect(validatedTokens).toEqual({});
expect(validatedTokens).toEqual(undefined);
});
it('keeps valid modify', () => {
const validatedTokens = validateStudioTokensExtensions(tokens.valid as SingleToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { SingleToken } from '@/types/tokens';
type StudioTokensExtension = NonNullable<SingleToken['$extensions']>['studio.tokens'];

export default function validateStudioTokensExtensions(payload: SingleToken): StudioTokensExtension {
let studioTokensExtension;
if (typeof payload?.$extensions?.['studio.tokens'] !== 'undefined') {
studioTokensExtension = Object.keys(payload?.$extensions?.['studio.tokens']).reduce((accExt, k) => {
const studioTokensExtension = Object.keys(payload?.$extensions?.['studio.tokens']).reduce((accExt, k) => {
const extension = (payload?.$extensions?.['studio.tokens'] || {})[k];
if (extension && Object.values(extension).length > 0) {
accExt[k] = extension;
}

return accExt;
}, {});
if (Object.keys(studioTokensExtension).length > 0) {
return studioTokensExtension;
}
}

return studioTokensExtension;
return undefined;
}

0 comments on commit 6300bec

Please sign in to comment.