MCPcopy Create free account
hub / github.com/dfinity/orbit / normalize

Function normalize

apps/wallet/src/utils/helper.utils.ts:534–637  ·  view source on GitHub ↗
(data: unknown)

Source from the content-addressed store, hash-verified

532
533 const seen = new WeakSet();
534 const normalize = (data: unknown): unknown => {
535 // Handles circular references by returning a string '[Circular Reference]' when a circular reference is found.
536 if (typeof data === 'object' && data !== null) {
537 if (seen.has(data)) {
538 return '[Circular Reference]';
539 }
540
541 seen.add(data);
542 }
543
544 if (data === null || data === undefined) {
545 return removeUndefinedOrNull ? undefined : data;
546 }
547
548 if (typeof data === 'function') {
549 return !removeFunctions ? '[Function]' : undefined;
550 }
551
552 if (typeof data === 'bigint') {
553 return Number(data);
554 }
555
556 if (data instanceof Principal) {
557 return data.toText();
558 }
559
560 if (data instanceof Date) {
561 return data.toISOString();
562 }
563
564 if (data instanceof ArrayBuffer) {
565 if (removeEmptyLists && data.byteLength === 0) {
566 return undefined;
567 }
568
569 return transformBufferAsHex ? arrayBufferToHex(data) : Array.from(new Uint8Array(data));
570 }
571
572 if (data instanceof Uint8Array) {
573 if (removeEmptyLists && data.length === 0) {
574 return undefined;
575 }
576
577 return Array.from(data);
578 }
579
580 if (Array.isArray(data)) {
581 if (removeEmptyLists && data.length === 0) {
582 return undefined;
583 }
584
585 return data
586 .map(value => normalize(value))
587 .filter(data => (removeUndefinedOrNull ? data !== undefined : true));
588 }
589
590 if (data instanceof Map) {
591 const result: Record<string, unknown> = {};

Callers 1

transformDataFunction · 0.85

Calls 5

arrayBufferToHexFunction · 0.90
addMethod · 0.80
mapMethod · 0.80
keysMethod · 0.80
fromMethod · 0.45

Tested by

no test coverage detected