mirror of
https://github.com/qntm/base1
synced 2025-09-23 14:24:42 +02:00
Compare commits
4 Commits
8055f6203e
...
f7dbe70888
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f7dbe70888 | ||
![]() |
e9fb76dbb6 | ||
![]() |
20b477d5f9 | ||
![]() |
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.
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/base1" crossorigin></script>
|
||||
<script src="https://unpkg.com/base1@1/dist/iife/base1.js" crossorigin></script>
|
||||
<script>
|
||||
console.log(base1.decode('AAAAAAAAAAAAAAAAAAAAAAAAAA'))
|
||||
</script>
|
||||
|
2675
package-lock.json
generated
2675
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "base1",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.3",
|
||||
"description": "Convert binary data to Base1",
|
||||
"homepage": "https://github.com/qntm/base1",
|
||||
"repository": {
|
||||
@ -9,7 +9,6 @@
|
||||
},
|
||||
"module": "dist/es6/base1.js",
|
||||
"main": "dist/cjs/base1.js",
|
||||
"browser": "dist/iife/base1.js",
|
||||
"keywords": [
|
||||
"base64",
|
||||
"base1",
|
||||
|
@ -23,11 +23,15 @@ export const encode = uint8Array => {
|
||||
}
|
||||
|
||||
export const decodeL = l => {
|
||||
if (typeof l !== 'bigint' || l < 0n) {
|
||||
throw Error('This is not a non-negative BigInt')
|
||||
}
|
||||
|
||||
const bytes = []
|
||||
while (l > 0n) {
|
||||
l -= 1n
|
||||
bytes.push(Number(l % 256n))
|
||||
l /= 256n
|
||||
l /= 256n // rounds down
|
||||
}
|
||||
|
||||
bytes.reverse()
|
||||
|
@ -39,6 +39,11 @@ describe('base1', () => {
|
||||
})
|
||||
|
||||
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', () => {
|
||||
expect(decodeL(0n)).toEqual(Uint8Array.from([]))
|
||||
expect(decodeL(1n)).toEqual(Uint8Array.from([0]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user