{"id":20258686,"url":"https://github.com/47ng/codec","last_synced_at":"2025-07-27T12:36:40.806Z","repository":{"id":36949079,"uuid":"233364398","full_name":"47ng/codec","owner":"47ng","description":"Universal conversion of Uint8Array from/into UTF-8, base64url and hex in the browser and Node.js","archived":false,"fork":false,"pushed_at":"2023-03-04T05:39:27.000Z","size":948,"stargazers_count":6,"open_issues_count":12,"forks_count":0,"subscribers_count":2,"default_branch":"next","last_synced_at":"2025-04-07T16:39:44.523Z","etag":null,"topics":["base64url","browser","decoding","encoding","hex","nodejs","uint8array","utf-8"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/47ng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["franky47"],"liberapay":"francoisbest","custom":["https://paypal.me/francoisbest?locale.x=fr_FR"]}},"created_at":"2020-01-12T08:58:36.000Z","updated_at":"2024-08-15T11:25:58.000Z","dependencies_parsed_at":"2024-06-18T21:29:17.992Z","dependency_job_id":"884a601f-e3da-46f7-8a20-d7dd064c13c6","html_url":"https://github.com/47ng/codec","commit_stats":{"total_commits":187,"total_committers":4,"mean_commits":46.75,"dds":0.267379679144385,"last_synced_commit":"39c94ef5d7f931d00480b11cb2400c948f2061e9"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":"47ng/typescript-library-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/47ng%2Fcodec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/47ng%2Fcodec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/47ng%2Fcodec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/47ng%2Fcodec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/47ng","download_url":"https://codeload.github.com/47ng/codec/tar.gz/refs/heads/next","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248322581,"owners_count":21084337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base64url","browser","decoding","encoding","hex","nodejs","uint8array","utf-8"],"created_at":"2024-11-14T11:10:32.948Z","updated_at":"2025-04-11T01:11:36.477Z","avatar_url":"https://github.com/47ng.png","language":"TypeScript","funding_links":["https://github.com/sponsors/franky47","https://liberapay.com/francoisbest","https://paypal.me/francoisbest?locale.x=fr_FR"],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\u003ccode\u003e@47ng/codec\u003c/code\u003e\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![NPM](https://img.shields.io/npm/v/@47ng/codec?color=red)](https://www.npmjs.com/package/@47ng/codec)\n[![MIT License](https://img.shields.io/github/license/47ng/codec.svg?color=blue)](https://github.com/47ng/codec/blob/master/LICENSE)\n![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/47ng/codec/Continuous%20Integration/next?label=CI)\n[![Coverage Status](https://coveralls.io/repos/github/47ng/codec/badge.svg?branch=next)](https://coveralls.io/github/47ng/codec?branch=next)\n![NPM Downloads](https://img.shields.io/npm/dm/@47ng/codec)\n\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n  Universal conversion of Uint8Array from/into UTF-8, base64url and hex in the browser and Node.js\n\u003c/p\u003e\n\n## Features\n\nAvailable codecs:\n\n- `utf8` - UTF-8\n- `b64` - Base 64 URL ([RFC-4648, Section 5](https://tools.ietf.org/html/rfc4648#section-5))\n- `hex` - Hexadecimal, lowercase\n- Simple conversion between string representation\n- Encoding detection\n\n\u003e Watch out !\n\u003e `utf8` uses a different convention than the other codecs (encode and decode\n\u003e are swapped), to reflect how `TextEncoder` and `TextDecoder` behave.\n\n## Installation\n\n```shell\n$ yarn add @47ng/codec\n# or\n$ npm i @47ng/codec\n```\n\n## Support\n\n- Node.js: \u003e=11.x\n- Browser: See [caniuse](https://caniuse.com/#feat=textencoder) for `TextEncoder` / `TextDecoder`\n\n## Examples\n\n```ts\nimport { b64, hex, utf8 } from '@47ng/codec'\n\nb64.encode(utf8.encode('Hello, World !')) // SGVsbG8sIFdvcmxkICE=\nhex.encode(b64.decode('SGVsbG8sIFdvcmxkICE=')) // 48656c6c6f2c20576f726c642021\nutf8.decode(hex.decode('48656c6c6f2c20576f726c642021')) // Hello, World !\n\nb64.decode('SGVsbG8sIFdvcmxkICE=') // \u003cBuffer 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 20 21\u003e\n```\n\n## Documentation\n\n### UTF-8\n\n- `utf8.encode`: Convert an UTF-8 string into an array of bytes (Uint8Array)\n- `utf8.decode`: Convert an array of bytes into an UTF-8 string\n\nExamples:\n\n```ts\nimport { utf8 } from '@47ng/codec'\n\nconst uint8Array = utf8.encode('Hello, World!')\n// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]\n\nconst backToText = utf8.decode(uint8Array)\n// 'Hello, World!'\n```\n\n### Base 64\n\n- `b64.encode`: Convert an array of bytes into a base64url string\n- `b64.decode`: Convert a base64 string into an array of bytes\n- `b64.urlSafe`: Convert a standard base64 string to base64url\n- `b64.urlUnsafe`: Convert a base64url string to standard base64 dictionary\n\n\u003e _**Note**_: For decoding, any dictionary is supported, and trailing padding (`=`) is optional.\n\nExamples:\n\n```ts\nimport { b64, utf8 } from '@47ng/codec'\n\nconst uint8Array = b64.decode('8J-Ri_CfjI0')\n// Uint8Array [240, 159, 145, 139, 240, 159, 140, 141]\n\n// Encoding always emits padding\nconst backToBase64 = b64.encode(uint8Array)\n// '8J-Ri_CfjI0='\n\nconst asText = utf8.decode(uint8Array)\n// '👋🌍'\n```\n\n### Hex\n\n- `hex.encode`: Convert an array of bytes into a hex string\n- `hex.decode`: Convert a hex string into an array of bytes\n\n\u003e _**Note**_: Decoding accepts any case (lowercase, uppercase, mixed).\n\nExamples:\n\n```ts\nimport { hex } from '@47ng/codec'\n\nconst uint8Array = hex.decode('48656C6C6F2C20576f726c642021')\n// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]\n\n// Encoding is always lowercase\nconst backToBase64 = hex.encode(uint8Array)\n// '48656c6c6f2c20576f726c642021'\n```\n\n### Utilities\n\nThe library exports convenience methods for converting from one string\nrepresentation to another:\n\n```ts\nimport {\n  hexToBase64url,\n  base64ToHex,\n  utf8ToBase64,\n  base64toUTF8,\n  utf8ToHex,\n  hexToUTF8\n} from '@47ng/codec'\n\nbase64ToHex('SGVsbG8sIFdvcmxkICE=')\n// 48656c6c6f2c20576f726c642021\n\nhexToBase64url('48656c6c6f2c20576f726c642021')\n// SGVsbG8sIFdvcmxkICE=\n\nutf8ToBase64('Hello, World !')\n// SGVsbG8sIFdvcmxkICE=\n\nbase64toUTF8('SGVsbG8sIFdvcmxkICE=')\n// Hello, World !\n\nutf8ToHex('Hello, World !')\n// 48656c6c6f2c20576f726c642021\n\nhexToUTF8('48656c6c6f2c20576f726c642021')\n// Hello, World !\n```\n\nIt also exports encoder / decoder objects with strong TypeScript types, to help you\nbuild your own encoders \u0026 decoders:\n\n```ts\nimport { encoders, decoders, Encoding } from '@47ng/codec'\n\n// type Encoding = 'base64' | 'utf8' | 'hex'\n\nfunction convert(\n  input: string,\n  inputEncoding: Encoding,\n  outputEncoding: Encoding\n): string {\n  const decoder = decoders[inputEncoding]\n  const encoder = encoders[outputEncoding]\n  return encoder(decoder(input))\n}\n\nconvert('Hello, World!', 'utf8', 'base64')\n// SGVsbG8sIFdvcmxkICE\n\nconvert('Hello, World!', 'utf8', 'hex')\n// 48656c6c6f2c20576f726c642021\n```\n\nYou can detect the encoding of a string:\n\n```ts\nimport { detectEncoding } from '@47ng/codec'\n\ndetectEncoding('baadf00dcafebabe') // hex\n\n// Both variants of base64 are detected\ndetectEncoding('SGVs+G8s/FdvcmxkICE=') // base64\ndetectEncoding('SGVs-G8s_FdvcmxkICE=') // base64\n\ndetectEncoding('not hex not base64') // utf8\n```\n\n## License\n\n[MIT](https://github.com/47ng/codec/blob/master/LICENSE) - Made with ❤️ by\n[François Best](https://francoisbest.com).\n\nUsing this package at work ? [Sponsor me](https://github.com/sponsors/franky47) to help with support and maintenance.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F47ng%2Fcodec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F47ng%2Fcodec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F47ng%2Fcodec/lists"}