Skip to content

Commit

Permalink
chore: fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored and rvagg committed Sep 16, 2024
1 parent a0fdb76 commit 9d24b62
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# multiformats

[![multiformats.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://multiformats.io)
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiformats.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiformats)
[![CI](https://img.shields.io/github/actions/workflow/status/multiformats/js-multiformats/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/multiformats/js-multiformats/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
Expand Down Expand Up @@ -212,7 +214,7 @@ $ npm i multiformats

## Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `Multiformats` in the global namespace.
Loading this module through a script tag will make its exports available as `Multiformats` in the global namespace.

```html
<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>
Expand All @@ -226,8 +228,8 @@ Loading this module through a script tag will make it's exports available as `Mu

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
- Apache 2.0, ([LICENSE-APACHE](https://github.com/multiformats/js-multiformats/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/multiformats/js-multiformats/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"bugs": {
"url": "https://github.com/multiformats/js-multiformats/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"ipfs",
"ipld",
Expand Down
5 changes: 3 additions & 2 deletions src/bases/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ class Decoder<Base extends string, Prefix extends string> implements MultibaseDe
constructor (name: Base, prefix: Prefix, baseDecode: DecodeFn) {
this.name = name
this.prefix = prefix
const prefixCodePoint = prefix.codePointAt(0)
/* c8 ignore next 3 */
if (prefix.codePointAt(0) === undefined) {
if (prefixCodePoint === undefined) {
throw new Error('Invalid prefix character')
}
this.prefixCodePoint = prefix.codePointAt(0) as number
this.prefixCodePoint = prefixCodePoint
this.baseDecode = baseDecode
}

Expand Down
17 changes: 14 additions & 3 deletions src/bases/base256emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { from } from './base.js'

const alphabet = Array.from('🚀🪐☄🛰🌌🌑🌒🌓🌔🌕🌖🌗🌘🌍🌏🌎🐉☀💻🖥💾💿😂❤😍🤣😊🙏💕😭😘👍😅👏😁🔥🥰💔💖💙😢🤔😆🙄💪😉☺👌🤗💜😔😎😇🌹🤦🎉💞✌✨🤷😱😌🌸🙌😋💗💚😏💛🙂💓🤩😄😀🖤😃💯🙈👇🎶😒🤭❣😜💋👀😪😑💥🙋😞😩😡🤪👊🥳😥🤤👉💃😳✋😚😝😴🌟😬🙃🍀🌷😻😓⭐✅🥺🌈😈🤘💦✔😣🏃💐☹🎊💘😠☝😕🌺🎂🌻😐🖕💝🙊😹🗣💫💀👑🎵🤞😛🔴😤🌼😫⚽🤙☕🏆🤫👈😮🙆🍻🍃🐶💁😲🌿🧡🎁⚡🌞🎈❌✊👋😰🤨😶🤝🚶💰🍓💢🤟🙁🚨💨🤬✈🎀🍺🤓😙💟🌱😖👶🥴▶➡❓💎💸⬇😨🌚🦋😷🕺⚠🙅😟😵👎🤲🤠🤧📌🔵💅🧐🐾🍒😗🤑🌊🤯🐷☎💧😯💆👆🎤🙇🍑❄🌴💣🐸💌📍🥀🤢👅💡💩👐📸👻🤐🤮🎼🥵🚩🍎🍊👼💍📣🥂')
const alphabetBytesToChars: string[] = (alphabet.reduce<string[]>((p, c, i) => { p[i] = c; return p }, ([])))
const alphabetCharsToBytes: number[] = (alphabet.reduce<number[]>((p, c, i) => { p[c.codePointAt(0) as number] = i; return p }, ([])))
const alphabetCharsToBytes: number[] = (alphabet.reduce<number[]>((p, c, i) => {
const codePoint = c.codePointAt(0)
if (codePoint == null) {
throw new Error(`Invalid character: ${c}`)
}
p[codePoint] = i
return p
}, ([])))

function encode (data: Uint8Array): string {
return data.reduce((p, c) => {
Expand All @@ -14,8 +21,12 @@ function encode (data: Uint8Array): string {
function decode (str: string): Uint8Array {
const byts = []
for (const char of str) {
const byt = alphabetCharsToBytes[char.codePointAt(0) as number]
if (byt === undefined) {
const codePoint = char.codePointAt(0)
if (codePoint == null) {
throw new Error(`Invalid character: ${char}`)
}
const byt = alphabetCharsToBytes[codePoint]
if (byt == null) {
throw new Error(`Non-base256emoji character: ${char}`)
}
byts.push(byt)
Expand Down
1 change: 1 addition & 0 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ type CreateUnsafeInput <T, Code extends number, Alg extends number, V extends AP
* @template V - CID version
*/
export function createUnsafe <T, Code extends number, Alg extends number, V extends API.Version> ({ bytes, cid, value: maybeValue, codec }: CreateUnsafeInput<T, Code, Alg, V>): API.BlockView<T, Code, Alg, V> {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const value = maybeValue !== undefined
? maybeValue
: (codec?.decode(bytes))
Expand Down
25 changes: 14 additions & 11 deletions test/test-cid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* globals describe, it */
/* eslint-disable @typescript-eslint/strict-boolean-expressions */

import { assert } from 'aegir/chai'
import OLDCID from 'cids'
Expand Down Expand Up @@ -105,8 +106,8 @@ describe('CID', () => {
it('should construct from an old CID', () => {
const cidStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const oldCid = CID.parse(cidStr)
const newCid = (CID.asCID(oldCid) as CID)
assert.deepStrictEqual(newCid.toString(), cidStr)
const newCid = CID.asCID(oldCid)
assert.deepStrictEqual(newCid?.toString(), cidStr)
})

it('inspect bytes', () => {
Expand Down Expand Up @@ -226,8 +227,8 @@ describe('CID', () => {
const cidStr =
'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
const oldCid = CID.parse(cidStr)
const newCid = (CID.asCID(oldCid) as CID)
assert.deepStrictEqual(newCid.toString(), cidStr)
const newCid = CID.asCID(oldCid)
assert.deepStrictEqual(newCid?.toString(), cidStr)
})

it('.link() should return this CID', () => {
Expand Down Expand Up @@ -516,7 +517,7 @@ describe('CID', () => {
// @ts-expect-error - no such method
assert.strictEqual(typeof incompatibleCID.toV0, 'undefined')

const cid1 = (CID.asCID(incompatibleCID) as CID)
const cid1 = CID.asCID(incompatibleCID)
assert.ok(cid1 instanceof CID)
assert.strictEqual(cid1.code, code)
assert.strictEqual(cid1.version, version)
Expand All @@ -538,7 +539,7 @@ describe('CID', () => {
assert.strictEqual(cid3, cid4)

const cid5 = (
CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) as CID
CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes)))
)
assert.ok(cid5 instanceof CID)
assert.strictEqual(cid5.version, 1)
Expand All @@ -557,7 +558,9 @@ describe('CID', () => {
assert.deepStrictEqual({ ...x, ...empty }, { ...y, ...empty })
}

const equalDigest = (x: MultihashDigest, y: MultihashDigest): void => {
const equalDigest = (x?: MultihashDigest, y?: MultihashDigest): void => {
assert.ok(x)
assert.ok(y)
assert.deepStrictEqual(x.digest, y.digest)
assert.deepStrictEqual(x.code, y.code)
assert.deepStrictEqual(x.digest, y.digest)
Expand Down Expand Up @@ -662,12 +665,12 @@ describe('CID', () => {
it('new CID from old CID', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = (
CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) as CID
CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes)))
)
assert.deepStrictEqual(cid.version, 1)
assert.deepStrictEqual(cid?.version, 1)

equalDigest(cid.multihash, hash)
assert.deepStrictEqual(cid.code, 85)
equalDigest(cid?.multihash, hash)
assert.deepStrictEqual(cid?.code, 85)
})

it('util.inspect', async () => {
Expand Down
15 changes: 8 additions & 7 deletions test/test-link.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* globals describe, it */
/* eslint-disable @typescript-eslint/strict-boolean-expressions */

import { assert } from 'aegir/chai'
import { sha256 } from '../src/hashes/sha2.js'
Expand All @@ -25,10 +26,10 @@ describe('Link', () => {
it('create v1', async () => {
const hash = await sha256.digest(utf8.encode('abc'))
const link = Link.create(0x71, hash)
const code = link.code as 0x71
const code = link.code
assert.deepStrictEqual(code, 0x71)

const version = link.version as 1
const version = link.version
assert.deepEqual(version, 1)

const multihash = link.multihash
Expand All @@ -39,10 +40,10 @@ describe('Link', () => {
const hash = await sha256.digest(utf8.encode('abc'))
const link = Link.createLegacy(hash)

const code = link.code as 0x70
const code = link.code
assert.deepStrictEqual(code, 0x70)

const version = link.version as 0
const version = link.version
assert.deepEqual(version, 0)

const multihash = link.multihash
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('Link', () => {
const t1 = link
assert.ok(t1)

// ensurate that you can't cast incorrectly
// ensure that you can't cast incorrectly
const t2 =
// @ts-expect-error - version is 1 not 0
(link as Link.Link<unknown, typeof CBOR, typeof SHA256, 0>)
Expand Down Expand Up @@ -124,10 +125,10 @@ describe('decode', () => {

const link = Link.decode(bytes)

const code = link.code as 0x71
const code = link.code
assert.deepStrictEqual(code, 0x71)

const version = link.version as 1
const version = link.version
assert.deepEqual(version, 1)

const multihash = link.multihash
Expand Down
4 changes: 2 additions & 2 deletions test/test-multibase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ describe('multibase', () => {
})

it('infers prefix and name corretly', () => {
const name = base32.name as 'base32'
const name = base32.name

// @ts-expect-error - TS catches mismatch
const name2: 'base16' = base32.name

const prefix = base32.prefix as 'b'
const prefix = base32.prefix
assert.equal(prefix, 'b')
assert.equal(name, 'base32')
assert.equal(name2, name)
Expand Down

0 comments on commit 9d24b62

Please sign in to comment.