UNPKG

@cartbc/codecs-core

Version:

Core types and helpers for encoding and decoding byte arrays on Cartes

93 lines (58 loc) 4.93 kB
<!-- BADGES/ --> [![npm][npm-image]][npm-url] [![npm-downloads][npm-downloads-image]][npm-url] [![semantic-release][semantic-release-image]][semantic-release-url] <br /> [![code-style-prettier][code-style-prettier-image]][code-style-prettier-url] <br /> <!-- [code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square [code-style-prettier-url]: https://github.com/prettier/prettier [npm-downloads-image]: https://img.shields.io/npm/dm/@cartbc/codecs-core/downloads [npm-image]: https://img.shields.io/npm/v/@cartbc/codecs-core [npm-url]: https://www.npmjs.com/package/@cartbc/codecs-core [semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg [semantic-release-url]: https://github.com/semantic-release/semantic-release --> <span class="badge-githubworkflow"><a href="https://github.com/cartallum/badges/actions?query=workflow%3Acodecs-core" title="View the status of this project's GitHub Workflow: codecs-core"><img src="https://github.com/bevry/badges/workflows/codecs-core/badge.svg" alt="Status of the GitHub Workflow: codes-core" /></a></span> <span class="badge-npmversion"><a href="https://npmjs.org/package/badges" title="View this project on NPM"><img src="https://img.shields.io/npm/v/badges.svg" alt="NPM version" /></a></span> <span class="badge-npmdownloads"><a href="https://npmjs.org/package/badges" title="View this project on NPM"><img src="https://img.shields.io/npm/dm/badges.svg" alt="NPM downloads" /></a></span> <br class="badge-separator" /> <span class="badge-thanksdev"><a href="https://thanks.dev/u/gh/balupton" title="Donate to this project using ThanksDev"><img src="https://img.shields.io/badge/thanksdev-donate-yellow.svg" alt="ThanksDev donate button" /></a></span> <span class="badge-patreon"><a href="https://patreon.com/bevry" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span> <span class="badge-flattr"><a href="https://flattr.com/profile/balupton" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span> <span class="badge-liberapay"><a href="https://liberapay.com/bevry" title="Donate to this project using Liberapay"><img src="https://img.shields.io/badge/liberapay-donate-yellow.svg" alt="Liberapay donate button" /></a></span> <span class="badge-buymeacoffee"><a href="https://buymeacoffee.com/balupton" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span> <span class="badge-opencollective"><a href="https://opencollective.com/bevry" title="Donate to this project using Open Collective"><img src="https://img.shields.io/badge/open%20collective-donate-yellow.svg" alt="Open Collective donate button" /></a></span> <span class="badge-crypto"><a href="https://bevry.me/crypto" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span> <span class="badge-paypal"><a href="https://bevry.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span> <span class="badge-wishlist"><a href="https://bevry.me/wishlist" title="Buy an item on our wishlist for us"><img src="https://img.shields.io/badge/wishlist-donate-yellow.svg" alt="Wishlist browse button" /></a></span> <!-- /BADGES --> <!-- DESCRIPTION/ --> # @cartbc/codecs-core This package contains the core types and function for encoding and decoding data structures on Cartes. It can be used standalone, but it is also exported as part of the Cartes JavaScript SDK [`@cartbc/cartes-web3.js`](https://github.com/cartallum/cartes-web3.js/tree/master/packages/library). <!-- /DESCRIPTION --> ## Types ### `bytes` Concatenates an array of `Uint8Array`s into a single `Uint8Array`. Reuses the original byte array when applicable. ### `codec` Defines an offset in bytes ## Functions ### `assertions` Asserts that a given byte array is not empty ### `combine-codec` Combines an encoder and a decoder into a codec. The encoder and decoder must have the same fixed size, max size and description. If a description is provided, it will override the encoder and decoder descriptions. ### `fix-codec` Creates a fixed-size encoder from a given encoder. @param encoder - The encoder to wrap into a fixed-size encoder. @param fixedBytes - The fixed number of bytes to write. @param description - A custom description for the encoder. ### `map-codec` Converts an encoder A to a encoder B by mapping their values. ### `reverse-codec` Reverses the bytes of a fixed-size encoder.