Skip to content

Commit 75dc32e

Browse files
committed
fix: refactor reverseSafeSchema for improved readability and performance; update test to reflect changes in expected output
1 parent f5e9b68 commit 75dc32e

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

packages/sdk/src/utils/data.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -263,32 +263,25 @@ export const createZipFromObject = (obj: unknown): Promise<Uint8Array> => {
263263
);
264264
};
265265

266-
export const reverseSafeSchema = function (
266+
export const reverseSafeSchema = (
267267
schema?: Array<Record<'id', string>>
268-
) {
269-
if (!schema) {
270-
return {};
271-
}
272-
return schema.reduce((propsAndTypes, { id: propPathColonType }) => {
273-
// { id: 'nested.something:string' }
274-
const [key, value] = propPathColonType.split(':');
275-
// key = 'nested.something'
276-
// value = 'string'
277-
if (key.includes('.')) {
278-
const firstKey = key.split('.')[0];
279-
// firstKey = 'nested'
280-
const restOfKey = key.split('.').slice(1).join('.');
281-
// restOfKey = 'something'
282-
const withValue = `${restOfKey}:${value}`;
283-
// withValue = 'something:string'
284-
propsAndTypes[firstKey] = reverseSafeSchema([
285-
{
286-
id: withValue,
287-
},
288-
]);
289-
} else {
290-
propsAndTypes[key] = value;
268+
): Record<string, any> => {
269+
if (!schema) return {};
270+
271+
return schema.reduce((acc, { id }) => {
272+
const [path, type] = id.split(':');
273+
const keys = path.split('.');
274+
275+
let current = acc;
276+
for (let i = 0; i < keys.length - 1; i++) {
277+
current[keys[i]] ??= {};
278+
current = current[keys[i]];
291279
}
292-
return propsAndTypes;
280+
281+
const finalKey = keys[keys.length - 1];
282+
current[finalKey] =
283+
type === 'bool' ? false : type === 'f64' ? 1 : type;
284+
285+
return acc;
293286
}, {});
294287
};

packages/sdk/tests/e2e/dataProtectorCore/getProtectedData.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('dataProtectorCore.getProtectedData()', () => {
129129
expect(result[0].schema).toEqual({
130130
email: 'string',
131131
tag: {
132-
size: 'f64',
132+
size: 1,
133133
},
134134
});
135135
});

0 commit comments

Comments
 (0)