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

Function transformData

apps/wallet/src/utils/helper.utils.ts:517–655  ·  view source on GitHub ↗
(
  input: unknown,
  opts: {
    removeUndefinedOrNull?: boolean;
    removeEmptyLists?: boolean;
    removeFunctions?: boolean;
    transformBufferAsHex?: boolean;
  } = {},
)

Source from the content-addressed store, hash-verified

515 * @returns The transformed data.
516 */
517export const transformData = (
518 input: unknown,
519 opts: {
520 removeUndefinedOrNull?: boolean;
521 removeEmptyLists?: boolean;
522 removeFunctions?: boolean;
523 transformBufferAsHex?: boolean;
524 } = {},
525): unknown => {
526 const {
527 removeEmptyLists = false,
528 removeUndefinedOrNull = true,
529 removeFunctions = true,
530 transformBufferAsHex = true,
531 } = opts;
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;

Callers 2

useFormFunction · 0.90

Calls 2

normalizeFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected