Skip to content

Commit

Permalink
refactor: move json-stringify-fields to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
luqven committed Jun 13, 2024
1 parent 543c8f9 commit 45e9bf1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
29 changes: 6 additions & 23 deletions src/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { SourceSelect } from '../SourceSelect/';
import packageJson from '../../../package.json';
import { UploadButton } from '../UploadButton/UploadButton';
import './Dialog.css';
import { stringifyJsonFields } from '../../helpers/utils';

interface DialogProps {
sdk: DialogExtensionSDK;
Expand Down Expand Up @@ -306,7 +307,11 @@ export default class Dialog extends Component<DialogProps, DialogState> {
? assets.data
: [assets.data];
assetObjects = assetsArray.map((asset: any) => {
this.stringifyJsonFields(asset);
stringifyJsonFields(asset, [
'custom_fields',
'tags',
'colors.dominant_colors',
]);
// TODO: add more explicit types for `asset`
return {
src: asset.attributes.origin_path,
Expand All @@ -320,28 +325,6 @@ export default class Dialog extends Component<DialogProps, DialogState> {
}
};

/*
* Stringifies all JSON field values within the asset.attribute object
*/
stringifyJsonFields = (asset: AssetProps) => {
const replaceNullWithEmptyString = (_: any, value: any) =>
value === null ? '' : value;
asset.attributes.custom_fields = JSON.stringify(
asset.attributes.custom_fields,
replaceNullWithEmptyString,
);
asset.attributes.tags = JSON.stringify(
asset.attributes.tags,
replaceNullWithEmptyString,
);
if (asset.attributes.colors?.dominant_colors) {
asset.attributes.colors.dominant_colors = JSON.stringify(
asset.attributes.colors?.dominant_colors,
replaceNullWithEmptyString,
);
}
};

/*
* Constructs an array of imgix image URL from the selected source in the
* application Dialog component
Expand Down
67 changes: 67 additions & 0 deletions src/helpers/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
groupParamsByKey,
paramsReducer,
removeParams,
replaceNullWithEmptyString,
stringifyJsonFields,
} from './utils';

describe('groupParamsByKey', () => {
Expand Down Expand Up @@ -195,3 +197,68 @@ describe('paramsReducer', () => {
});
});
});

describe('stringifyJsonFields', () => {
it('should stringify specified fields', () => {
const input = {
field1: { a: 1, b: 2 },
field2: { c: 3, d: 4 },
};
const fields = ['field1', 'field2'];
const output = stringifyJsonFields(input, fields);

expect(output.field1).toBe(
JSON.stringify({ a: 1, b: 2 }, replaceNullWithEmptyString),
);
expect(output.field2).toBe(
JSON.stringify({ c: 3, d: 4 }, replaceNullWithEmptyString),
);
});

it('should not change fields that are not specified', () => {
const input = {
field1: { a: 1, b: 2 },
field2: { c: 3, d: 4 },
field3: { e: 5, f: 6 },
};
const fields = ['field1'];
const output = stringifyJsonFields(input, fields);

expect(output.field1).toBe(
JSON.stringify({ a: 1, b: 2 }, replaceNullWithEmptyString),
);
expect(output.field2).toEqual({ c: 3, d: 4 });
expect(output.field3).toEqual({ e: 5, f: 6 });
});

it('should handle fields with null values correctly', () => {
const input = {
field1: { a: 1, b: null },
};
const fields = ['field1'];
const output = stringifyJsonFields(input, fields);

expect(output.field1).toBe(
JSON.stringify({ a: 1, b: '' }, replaceNullWithEmptyString),
);
});

it('should not modify the input object if no fields are specified', () => {
const input = {
field1: { a: 1, b: 2 },
};
const output = stringifyJsonFields(input, []);

expect(output).toEqual(input);
});

it('should not modify the input object if specified fields do not exist', () => {
const input = {
field1: { a: 1, b: 2 },
};
const fields = ['field2'];
const output = stringifyJsonFields(input, fields);

expect(output).toEqual(input);
});
});
28 changes: 28 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,31 @@ export const paramsReducer = (
return removeParams(existingParams, newParams);
}
};

/**
* Stringifies value to '' if the value is null
*/
export const replaceNullWithEmptyString = (_: any, value: any) =>
value === null ? '' : value;

/*
* Stringifies all JSON field values within the asset.attribute object
*/
export const stringifyJsonFields = (
object: Record<string, any>,
fields: string[] = [],
): Record<string, any> => {
const normalizedObject = { ...object };

for (const field of fields) {
if (!normalizedObject[field]) {
continue;
}
normalizedObject[field] = JSON.stringify(
normalizedObject[field],
replaceNullWithEmptyString,
);
}

return normalizedObject;
};

0 comments on commit 45e9bf1

Please sign in to comment.