Comparing version 1.0.1 to 1.0.2
@@ -1,4 +0,20 @@ | ||
export declare function encode(byteArrayArg: Uint8Array): string; | ||
/** | ||
* Encode binary data to base45 | ||
* @param byteArrayArg An array of bytes to encode | ||
* @returns a base45-encoded string | ||
*/ | ||
export declare function encode(byteArrayArg: Uint8Array | number[]): string; | ||
/** | ||
* Decode a base45-encoded string | ||
* @param utf8StringArg A base45-encoded string | ||
* @returns a typed array containing the decoded data | ||
*/ | ||
export declare function decode(utf8StringArg: string): Uint8Array; | ||
/** | ||
* Same as decode, but returns a string instead of a typed array. | ||
* If the base45-encoded data was not valid UTF-8, throws an error. | ||
* @param utf8StringArg base45-encoded string representing an utf8 string | ||
* @returns the decoded string | ||
*/ | ||
export declare function decodeToUtf8String(utf8StringArg: string): string; | ||
//# sourceMappingURL=base45.d.ts.map |
@@ -9,4 +9,9 @@ const baseSize = 45; | ||
const decoding = Object.fromEntries(encoding.map((l, i) => [l, i])); | ||
/** | ||
* Encode binary data to base45 | ||
* @param byteArrayArg An array of bytes to encode | ||
* @returns a base45-encoded string | ||
*/ | ||
export function encode(byteArrayArg) { | ||
const wholeChunkCount = Math.trunc(byteArrayArg.length / chunkSize); | ||
const wholeChunkCount = (byteArrayArg.length / chunkSize | 0); | ||
const resultSize = wholeChunkCount * encodedChunkSize + (byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
@@ -32,2 +37,7 @@ const result = new Array(resultSize); | ||
; | ||
/** | ||
* Decode a base45-encoded string | ||
* @param utf8StringArg A base45-encoded string | ||
* @returns a typed array containing the decoded data | ||
*/ | ||
export function decode(utf8StringArg) { | ||
@@ -38,8 +48,9 @@ if (utf8StringArg.length === 0) | ||
if (remainderSize === 1) | ||
throw new Error("utf8StringArg has incorrect length."); | ||
throw new Error(`A string of length ${utf8StringArg.length} is not valid base45: ${utf8StringArg}`); | ||
const buffer = new Uint8Array(utf8StringArg.length); | ||
for (let i = 0; i < utf8StringArg.length; ++i) { | ||
const found = decoding[utf8StringArg[i]]; | ||
const char = utf8StringArg[i]; | ||
const found = decoding[char]; | ||
if (found === undefined) | ||
throw new Error(`Invalid character at position ${i}.`); | ||
throw new Error(`Invalid character '${char}' at position ${i}.`); | ||
buffer[i] = found; | ||
@@ -60,2 +71,8 @@ } | ||
} | ||
/** | ||
* Same as decode, but returns a string instead of a typed array. | ||
* If the base45-encoded data was not valid UTF-8, throws an error. | ||
* @param utf8StringArg base45-encoded string representing an utf8 string | ||
* @returns the decoded string | ||
*/ | ||
export function decodeToUtf8String(utf8StringArg) { | ||
@@ -62,0 +79,0 @@ return new TextDecoder().decode(decode(utf8StringArg)); |
@@ -7,4 +7,4 @@ { | ||
"types": "./lib/base45.d.ts", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/ehn-digital-green-development/base45-js", | ||
"version": "1.0.2", | ||
"homepage": "https://github.com/lovasoe/base45-ts", | ||
"author": { | ||
@@ -18,2 +18,5 @@ "name": "Dirk-Willem van Gulik", | ||
"email": "steve.kellaway@mefitihe.com" | ||
}, | ||
{ | ||
"name": "Ophir LOJKINE" | ||
} | ||
@@ -23,6 +26,6 @@ ], | ||
"type": "git", | ||
"url": "https://github.com/iehn-digital-green-development/base45-js" | ||
"url": "https://github.com/lovasoa/base45-ts" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ehn-digital-green-development/base45-js/issues" | ||
"url": "https://github.com/lovasoa/base45-ts/issues" | ||
}, | ||
@@ -32,3 +35,3 @@ "licenses": [ | ||
"type": "Apache-2.0", | ||
"url": "https://github.com/ehn-digital-green-development/base45/blob/master/LICENSE-ASF-2.0.txt" | ||
"url": "https://github.com/lovasoa/base45-ts/blob/master/LICENSE-ASF-2.0.txt" | ||
} | ||
@@ -50,3 +53,4 @@ ], | ||
"dgc", | ||
"ehn" | ||
"ehn", | ||
"QR code" | ||
], | ||
@@ -56,2 +60,2 @@ "vsTest": { | ||
} | ||
} | ||
} |
@@ -0,6 +1,10 @@ | ||
[![npm](https://img.shields.io/npm/v/base45-ts)](https://www.npmjs.com/package/base45-ts) | ||
# base45 typescript implementation | ||
Simple typescript base45 (charset of Qr codes, alphanumeric mode) encoder/decoder. | ||
Run as `npm test` to get an idea of what it does. | ||
This library exports an ES module, together with the required type annotations. | ||
Typical use: | ||
## Example | ||
@@ -20,1 +24,53 @@ ```ts | ||
``` | ||
## Exported functions | ||
### decode | ||
```ts | ||
decode(utf8StringArg: string): Uint8Array | ||
``` | ||
Decode a base45-encoded string | ||
#### Parameters | ||
- utf8StringArg, `string`: A base45-encoded string | ||
#### Returns | ||
An `Uint8Array` containing the decoded data | ||
### decodeToUtf8String | ||
```ts | ||
decodeToUtf8String(utf8StringArg: string): string | ||
``` | ||
Same as [decode](#decode), but returns a string instead of a typed array. If the base45-encoded data was not valid UTF-8, throws an error. | ||
#### Parameters | ||
- utf8StringArg, `string`: base45-encoded string representing an utf8 string | ||
#### Returns | ||
the decoded `string` | ||
### encode | ||
```ts | ||
encode(byteArrayArg: Uint8Array | number[]): string | ||
``` | ||
Encode binary data to base45 | ||
#### Parameters | ||
- byteArrayArg: `Uint8Array | number[]` An array of bytes to encode | ||
#### Returns | ||
a base45-encoded `string` | ||
## Development | ||
This library doesn't have any dependency. | ||
- Build the code with `npm run build` | ||
- Test with `npm test` |
@@ -11,4 +11,9 @@ const baseSize = 45; | ||
export function encode(byteArrayArg: Uint8Array): string { | ||
const wholeChunkCount = Math.trunc(byteArrayArg.length / chunkSize); | ||
/** | ||
* Encode binary data to base45 | ||
* @param byteArrayArg An array of bytes to encode | ||
* @returns a base45-encoded string | ||
*/ | ||
export function encode(byteArrayArg: Uint8Array | number[]): string { | ||
const wholeChunkCount = (byteArrayArg.length / chunkSize | 0); | ||
const resultSize = wholeChunkCount * encodedChunkSize + (byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
@@ -37,2 +42,7 @@ | ||
/** | ||
* Decode a base45-encoded string | ||
* @param utf8StringArg A base45-encoded string | ||
* @returns a typed array containing the decoded data | ||
*/ | ||
export function decode(utf8StringArg: string): Uint8Array { | ||
@@ -43,9 +53,10 @@ if (utf8StringArg.length === 0) return new Uint8Array; | ||
if (remainderSize === 1) | ||
throw new Error("utf8StringArg has incorrect length."); | ||
throw new Error(`A string of length ${utf8StringArg.length} is not valid base45: ${utf8StringArg}`); | ||
const buffer = new Uint8Array(utf8StringArg.length); | ||
for (let i = 0; i < utf8StringArg.length; ++i) { | ||
const found = decoding[utf8StringArg[i]]; | ||
const char = utf8StringArg[i]; | ||
const found = decoding[char]; | ||
if (found === undefined) | ||
throw new Error(`Invalid character at position ${i}.`); | ||
throw new Error(`Invalid character '${char}' at position ${i}.`); | ||
buffer[i] = found; | ||
@@ -70,5 +81,11 @@ } | ||
export function decodeToUtf8String(utf8StringArg: string) { | ||
/** | ||
* Same as decode, but returns a string instead of a typed array. | ||
* If the base45-encoded data was not valid UTF-8, throws an error. | ||
* @param utf8StringArg base45-encoded string representing an utf8 string | ||
* @returns the decoded string | ||
*/ | ||
export function decodeToUtf8String(utf8StringArg: string): string { | ||
return new TextDecoder().decode(decode(utf8StringArg)); | ||
} | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33185
234
75
1