X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=12c206ff63849ec0303c37a7ce8c41fffc5c29bb;hb=09379a0da5842414aa5835130a101aec11e724d5;hp=cff63715719b07d254b045c133a9be2170e01291;hpb=89448b6160fe75bd2dc10e409ba6c0988768a154;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index cff63715..12c206ff 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -211,55 +211,8 @@ export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] return timeSeries.map(timeSeriesItem => timeSeriesItem.value) } -type CloneableData = - | number - | string - | boolean - | null - | undefined - | Date - | CloneableData[] - | { [key: string]: CloneableData } - -type FormatKey = (key: string) => string - -const deepClone = ( - value: I, - formatKey?: FormatKey, - refs: Map = new Map() -): O => { - const ref = refs.get(value) - if (ref !== undefined) { - return ref - } - if (Array.isArray(value)) { - const clone: CloneableData[] = [] - refs.set(value, clone as O) - for (let i = 0; i < value.length; i++) { - clone[i] = deepClone(value[i], formatKey, refs) - } - return clone as O - } - if (value instanceof Date) { - return new Date(value.getTime()) as O - } - if (typeof value !== 'object' || value === null) { - return value as unknown as O - } - const clone: Record = {} - refs.set(value, clone as O) - for (const key of Object.keys(value)) { - clone[typeof formatKey === 'function' ? formatKey(key) : key] = deepClone( - value[key], - formatKey, - refs - ) - } - return clone as O -} - export const clone = (object: T): T => { - return deepClone(object as CloneableData) as T + return structuredClone(object) } /**