Compare commits
4 Commits
8055f6203e
...
f7dbe70888
Author | SHA1 | Date |
---|---|---|
qntm | f7dbe70888 | |
qntm | e9fb76dbb6 | |
qntm | 20b477d5f9 | |
qntm | d02e93dcf2 |
|
@ -53,7 +53,7 @@ const uint8Array3 = decodeL(l) // Uint8Array [ 3, 192 ]
|
||||||
Load this file in the browser to gain access to a `base1` global.
|
Load this file in the browser to gain access to a `base1` global.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script src="https://unpkg.com/base1" crossorigin></script>
|
<script src="https://unpkg.com/base1@1/dist/iife/base1.js" crossorigin></script>
|
||||||
<script>
|
<script>
|
||||||
console.log(base1.decode('AAAAAAAAAAAAAAAAAAAAAAAAAA'))
|
console.log(base1.decode('AAAAAAAAAAAAAAAAAAAAAAAAAA'))
|
||||||
</script>
|
</script>
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "base1",
|
"name": "base1",
|
||||||
"version": "1.0.1",
|
"version": "1.0.3",
|
||||||
"description": "Convert binary data to Base1",
|
"description": "Convert binary data to Base1",
|
||||||
"homepage": "https://github.com/qntm/base1",
|
"homepage": "https://github.com/qntm/base1",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -9,7 +9,6 @@
|
||||||
},
|
},
|
||||||
"module": "dist/es6/base1.js",
|
"module": "dist/es6/base1.js",
|
||||||
"main": "dist/cjs/base1.js",
|
"main": "dist/cjs/base1.js",
|
||||||
"browser": "dist/iife/base1.js",
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"base64",
|
"base64",
|
||||||
"base1",
|
"base1",
|
||||||
|
|
|
@ -23,11 +23,15 @@ export const encode = uint8Array => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const decodeL = l => {
|
export const decodeL = l => {
|
||||||
|
if (typeof l !== 'bigint' || l < 0n) {
|
||||||
|
throw Error('This is not a non-negative BigInt')
|
||||||
|
}
|
||||||
|
|
||||||
const bytes = []
|
const bytes = []
|
||||||
while (l > 0n) {
|
while (l > 0n) {
|
||||||
l -= 1n
|
l -= 1n
|
||||||
bytes.push(Number(l % 256n))
|
bytes.push(Number(l % 256n))
|
||||||
l /= 256n
|
l /= 256n // rounds down
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes.reverse()
|
bytes.reverse()
|
||||||
|
|
|
@ -39,6 +39,11 @@ describe('base1', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('decodeL', () => {
|
describe('decodeL', () => {
|
||||||
|
it('wants a non-negative BigInt', () => {
|
||||||
|
expect(() => decodeL(8)).toThrowError('This is not a non-negative BigInt')
|
||||||
|
expect(() => decodeL(-1n)).toThrowError('This is not a non-negative BigInt')
|
||||||
|
})
|
||||||
|
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
expect(decodeL(0n)).toEqual(Uint8Array.from([]))
|
expect(decodeL(0n)).toEqual(Uint8Array.from([]))
|
||||||
expect(decodeL(1n)).toEqual(Uint8Array.from([0]))
|
expect(decodeL(1n)).toEqual(Uint8Array.from([0]))
|
||||||
|
|
Loading…
Reference in New Issue