mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-07 22:18:33 +00:00
feat: support object and array interpolation in bruno-common interpolate fn (#4519)
--------- Co-authored-by: Pooja Belaramani <pooja@usebruno.com> Co-authored-by: lohit jiddimani <lohitjiddimani@lohits-MacBook-Air.local> Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
import { flattenObject } from './index';
|
||||
|
||||
describe('flattenObject', () => {
|
||||
it('should flatten a simple object', () => {
|
||||
const input = { a: 1, b: { c: 2, d: { e: 3 } } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ a: 1, 'b.c': 2, 'b.d.e': 3 });
|
||||
});
|
||||
|
||||
it('should flatten an object with arrays', () => {
|
||||
const input = { a: 1, b: { c: [2, 3, 4], d: { e: 5 } } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ a: 1, 'b.c[0]': 2, 'b.c[1]': 3, 'b.c[2]': 4, 'b.d.e': 5 });
|
||||
});
|
||||
|
||||
it('should flatten an object with arrays having objects', () => {
|
||||
const input = { a: 1, b: { c: [{ d: 2 }, { e: 3 }], f: { g: 4 } } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ a: 1, 'b.c[0].d': 2, 'b.c[1].e': 3, 'b.f.g': 4 });
|
||||
});
|
||||
|
||||
it('should handle null values', () => {
|
||||
const input = { a: 1, b: { c: null, d: { e: 3 } } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ a: 1, 'b.c': null, 'b.d.e': 3 });
|
||||
});
|
||||
|
||||
it('should handle an empty object', () => {
|
||||
const input = {};
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({});
|
||||
});
|
||||
|
||||
it('should handle an object with nested empty objects', () => {
|
||||
const input = { a: { b: {}, c: { d: {} } } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({});
|
||||
});
|
||||
|
||||
it('should handle an object with duplicate keys - dot notation used to define the last duplicate key', () => {
|
||||
const input = { a: { b: 2 }, 'a.b': 1 };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ 'a.b': 1 });
|
||||
});
|
||||
|
||||
it('should handle an object with duplicate keys - inner object used to define the last duplicate key', () => {
|
||||
const input = { 'a.b': 1, a: { b: 2 } };
|
||||
const output = flattenObject(input);
|
||||
expect(output).toEqual({ 'a.b': 2 });
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
export const flattenObject = (obj: Record<string, any>, parentKey: string = ''): Record<string, any> => {
|
||||
return Object.entries(obj).reduce((acc: Record<string, any>, [key, value]: [string, any]) => {
|
||||
const newKey = parentKey ? (Array.isArray(obj) ? `${parentKey}[${key}]` : `${parentKey}.${key}`) : key;
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
Object.assign(acc, flattenObject(value, newKey));
|
||||
} else {
|
||||
acc[newKey] = value;
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
Reference in New Issue
Block a user