Compare commits

..

4 Commits

Author SHA1 Message Date
qntm f7dbe70888 Bump 2020-05-08 16:43:06 +01:00
qntm e9fb76dbb6 Bump 2020-05-08 16:41:57 +01:00
qntm 20b477d5f9 Merge branch 'master' of https://github.com/qntm/base1 2020-05-08 16:41:35 +01:00
qntm d02e93dcf2 In this part of the country? 2020-05-08 16:41:28 +01:00
5 changed files with 1444 additions and 1247 deletions

View File

@ -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>

2691
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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",

View File

@ -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()

View File

@ -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]))