-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data-lake.ts: deal with setting non-flat data
- Loading branch information
1 parent
6b192f2
commit 6fbbfb7
Showing
2 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* The result of flattening complex data structures into simple types | ||
*/ | ||
interface FlattenedData { | ||
/** | ||
* The determined type of the flattened data | ||
*/ | ||
type: 'string' | 'number' | 'boolean' | 'object' | ||
/** | ||
* The resulting flattened value | ||
*/ | ||
value: string | number | boolean | object | Array<string | number> | ||
} | ||
|
||
/** | ||
* Type guard to check if a value is an array of numbers | ||
* @param {unknown[]} data The data to check | ||
* @returns {data is number[]} True if the array contains numbers | ||
*/ | ||
function isNumberArray(data: unknown[]): data is number[] { | ||
return typeof data[0] === 'number' | ||
} | ||
|
||
/** | ||
* Type guard to check if a value is an array of strings | ||
* @param {unknown[]} data The data to check | ||
* @returns {data is string[]} True if the array contains strings | ||
*/ | ||
function isStringArray(data: unknown[]): data is string[] { | ||
return typeof data[0] === 'string' | ||
} | ||
|
||
/** | ||
* Flattens complex data structures into simple types that can be stored in the data lake | ||
* @param {object | string | number | boolean | Array<string | number>} data The data to flatten | ||
* @param {(value: number, index: string) => void} [onArrayElement] Callback for handling individual array elements | ||
* @returns {FlattenedData | null} The flattened data structure or null if unable to flatten | ||
*/ | ||
export function flattenData( | ||
data: object | string | number | boolean | Array<string | number>, | ||
onArrayElement?: (value: number, index: string) => void | ||
): FlattenedData | null { | ||
const typeOfData = typeof data | ||
|
||
if (typeOfData !== 'object') { | ||
return { | ||
type: typeOfData as 'string' | 'number' | 'boolean', | ||
value: data, | ||
} | ||
} | ||
|
||
// Handle arrays | ||
if (Array.isArray(data)) { | ||
if (data.length === 0) return null | ||
|
||
if (isStringArray(data)) { | ||
return { | ||
type: 'string', | ||
value: data.join(''), | ||
} | ||
} | ||
|
||
if (isNumberArray(data) && onArrayElement) { | ||
// Handle array of numbers by calling the callback for each element | ||
data.forEach((value, index) => { | ||
onArrayElement(value, index.toString()) | ||
}) | ||
return null | ||
} | ||
} | ||
|
||
// Handle objects with special properties | ||
const objData = data as Record<string, unknown> | ||
|
||
if ('type' in objData && 'value' in objData) { | ||
return { | ||
type: typeof objData.type as 'string' | 'number' | 'boolean', | ||
value: objData.value as string | number | boolean, | ||
} | ||
} | ||
|
||
if ('bits' in objData) { | ||
return { | ||
type: typeof objData.bits as 'string' | 'number' | 'boolean', | ||
value: objData.bits as string | number | boolean, | ||
} | ||
} | ||
|
||
return null | ||
} |