mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-04-22 21:26:23 +05:30
chore: getJsonHeaders as util function since reusable
This commit is contained in:
parent
61610a0acd
commit
f6e1c13751
1 changed files with 18 additions and 0 deletions
18
src/utils/json.ts
Normal file
18
src/utils/json.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* Collects all unique keys from an array of row objects, preserving first-encountered order.
|
||||
* Handles sparse rows where different rows may have different keys.
|
||||
*
|
||||
* @param rows - Array of flattened row objects
|
||||
* @returns Array of unique header strings in insertion order
|
||||
*
|
||||
* @example
|
||||
* getJsonHeaders([{ a: '1' }, { a: '2', b: '3' }]) // → ['a', 'b']
|
||||
*/
|
||||
export function getJsonHeaders(rows: Record<string, string>[]): string[] {
|
||||
return Array.from(
|
||||
rows.reduce<Set<string>>((set, row) => {
|
||||
Object.keys(row).forEach((key) => set.add(key));
|
||||
return set;
|
||||
}, new Set())
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue