UNPKG

@metaplex-foundation/umi-serializers-core

Version:

Core types and helpers for UMI serializers

1 lines 2.4 kB
{"version":3,"file":"bytes.cjs","sources":["../../src/bytes.ts"],"sourcesContent":["/**\n * Concatenates an array of `Uint8Array`s into a single `Uint8Array`.\n * @category Utils\n */\nexport const mergeBytes = (bytesArr: Uint8Array[]): Uint8Array => {\n const totalLength = bytesArr.reduce((total, arr) => total + arr.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n bytesArr.forEach((arr) => {\n result.set(arr, offset);\n offset += arr.length;\n });\n return result;\n};\n\n/**\n * Pads a `Uint8Array` with zeroes to the specified length.\n * If the array is longer than the specified length, it is returned as-is.\n * @category Utils\n */\nexport const padBytes = (bytes: Uint8Array, length: number): Uint8Array => {\n if (bytes.length >= length) return bytes;\n const paddedBytes = new Uint8Array(length).fill(0);\n paddedBytes.set(bytes);\n return paddedBytes;\n};\n\n/**\n * Fixes a `Uint8Array` to the specified length.\n * If the array is longer than the specified length, it is truncated.\n * If the array is shorter than the specified length, it is padded with zeroes.\n * @category Utils\n */\nexport const fixBytes = (bytes: Uint8Array, length: number): Uint8Array =>\n padBytes(bytes.slice(0, length), length);\n"],"names":["mergeBytes","bytesArr","totalLength","reduce","total","arr","length","result","Uint8Array","offset","forEach","set","padBytes","bytes","paddedBytes","fill","fixBytes","slice"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACaA,MAAAA,UAAU,GAAIC,QAAsB,IAAiB;AAChE,EAAA,MAAMC,WAAW,GAAGD,QAAQ,CAACE,MAAM,CAAC,CAACC,KAAK,EAAEC,GAAG,KAAKD,KAAK,GAAGC,GAAG,CAACC,MAAM,EAAE,CAAC,CAAC,CAAA;AAC1E,EAAA,MAAMC,MAAM,GAAG,IAAIC,UAAU,CAACN,WAAW,CAAC,CAAA;EAC1C,IAAIO,MAAM,GAAG,CAAC,CAAA;AACdR,EAAAA,QAAQ,CAACS,OAAO,CAAEL,GAAG,IAAK;AACxBE,IAAAA,MAAM,CAACI,GAAG,CAACN,GAAG,EAAEI,MAAM,CAAC,CAAA;IACvBA,MAAM,IAAIJ,GAAG,CAACC,MAAM,CAAA;AACtB,GAAC,CAAC,CAAA;AACF,EAAA,OAAOC,MAAM,CAAA;AACf,EAAC;;AAED;AACA;AACA;AACA;AACA;MACaK,QAAQ,GAAG,CAACC,KAAiB,EAAEP,MAAc,KAAiB;AACzE,EAAA,IAAIO,KAAK,CAACP,MAAM,IAAIA,MAAM,EAAE,OAAOO,KAAK,CAAA;EACxC,MAAMC,WAAW,GAAG,IAAIN,UAAU,CAACF,MAAM,CAAC,CAACS,IAAI,CAAC,CAAC,CAAC,CAAA;AAClDD,EAAAA,WAAW,CAACH,GAAG,CAACE,KAAK,CAAC,CAAA;AACtB,EAAA,OAAOC,WAAW,CAAA;AACpB,EAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,QAAQ,GAAG,CAACH,KAAiB,EAAEP,MAAc,KACxDM,QAAQ,CAACC,KAAK,CAACI,KAAK,CAAC,CAAC,EAAEX,MAAM,CAAC,EAAEA,MAAM;;;;;;"}