Skip to content

Commit f925195

Browse files
committed
wip: migrate to esm
1 parent 6ac7278 commit f925195

18 files changed

+56
-58
lines changed

bases/_base64-browser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/* globals btoa, atob */
2-
exports.encode = b => btoa([].reduce.call(b, (p, c) => p + String.fromCharCode(c), ''))
3-
exports.decode = str => Uint8Array.from(atob(str), c => c.charCodeAt(0))
2+
const encode = b => btoa([].reduce.call(b, (p, c) => p + String.fromCharCode(c), ''))
3+
const decode = str => Uint8Array.from(atob(str), c => c.charCodeAt(0))
4+
export { encode, decode }

bases/_base64.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
const { coerce } = require('../bytes')
2-
exports.encode = o => Buffer.from(o).toString('base64')
3-
exports.decode = s => coerce(Buffer.from(s, 'base64'))
1+
import { coerce } from '../bytes.js'
2+
const encode = o => Buffer.from(o).toString('base64')
3+
const decode = s => coerce(Buffer.from(s, 'base64'))
4+
export { encode, decode }

bases/base16.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
2-
const { fromHex } = require('../bytes')
3-
const bytes = require('../bytes')
2+
import { fromHex, toHex } from '../bytes.js'
43

54
const create = function base16 (alphabet) {
65
return {
7-
encode: input => bytes.toHex(input),
6+
encode: input => toHex(input),
87
decode (input) {
98
for (const char of input) {
109
if (alphabet.indexOf(char) < 0) {
@@ -16,4 +15,4 @@ const create = function base16 (alphabet) {
1615
}
1716
}
1817

19-
module.exports = { prefix: 'f', name: 'base16', ...create('0123456789abcdef') }
18+
export default { prefix: 'f', name: 'base16', ...create('0123456789abcdef') }

bases/base32.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const create = alphabet => {
7474
}
7575
}
7676

77-
module.exports = [
77+
export default [
7878
{ prefix: 'b', name: 'base32', ...create('abcdefghijklmnopqrstuvwxyz234567') },
7979
{ prefix: 'c', name: 'base32pad', ...create('abcdefghijklmnopqrstuvwxyz234567=') },
8080
{ prefix: 'v', name: 'base32hex', ...create('0123456789abcdefghijklmnopqrstuv') },

bases/base58.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict'
2-
const baseX = require('base-x')
3-
const bytes = require('../bytes')
4-
const { Buffer } = require('buffer')
2+
import baseX from 'base-x'
3+
import { coerce } from '../bytes.js'
4+
import { Buffer } from 'buffer'
55

66
const wrap = obj => ({
77
encode: b => obj.encode(Buffer.from(b)),
8-
decode: s => bytes.coerce(obj.decode(s))
8+
decode: s => coerce(obj.decode(s))
99
})
1010

1111
const btc = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
1212
const flickr = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
1313

14-
module.exports = [
14+
export default [
1515
{ name: 'base58btc', prefix: 'z', ...wrap(baseX(btc)) },
1616
{ name: 'base58flickr', prefix: 'Z', ...wrap(baseX(flickr)) }
1717
]

bases/base64.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const b64 = require('./_base64')
2+
import * as b64 from './_base64.js'
33

44
const create = alphabet => {
55
// The alphabet is only used to know:
@@ -42,7 +42,7 @@ const base64pad = create('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01
4242
const base64url = create('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
4343
const base64urlpad = create('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=')
4444

45-
module.exports = [
45+
export default [
4646
{ prefix: 'm', name: 'base64', ...base64 },
4747
{ prefix: 'M', name: 'base64pad', ...base64pad },
4848
{ prefix: 'u', name: 'base64url', ...base64url },

basics.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const multiformats = require('./')()
2-
const raw = require('./codecs/raw')
3-
const json = require('./codecs/json')
4-
const base32 = require('./bases/base32')
5-
const base64 = require('./bases/base64')
6-
const sha2 = require('./hashes/sha2')
1+
import { create } from './index.js'
2+
import raw from './codecs/raw.js'
3+
import json from './codecs/json.js'
4+
import base32 from './bases/base32.js'
5+
import base64 from './bases/base64.js'
6+
import sha2 from './hashes/sha2.js'
77

8+
const multiformats = create()
89
multiformats.multihash.add(sha2)
910
multiformats.multicodec.add([raw, json])
1011
multiformats.multibase.add([base32, base64])
1112

12-
module.exports = multiformats
13+
export default multiformats

bytes.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,4 @@ const isBinary = o => {
4141
const fromString = str => (new TextEncoder()).encode(str)
4242
const toString = b => (new TextDecoder()).decode(b)
4343

44-
exports.equals = equals
45-
exports.coerce = coerce
46-
exports.isBinary = isBinary
47-
exports.fromHex = fromHex
48-
exports.toHex = toHex
49-
exports.fromString = fromString
50-
exports.toString = toString
44+
export { equals, coerce, isBinary, fromHex, toHex, fromString, toString }

cid.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
2-
const bytes = require('./bytes')
3-
const withIs = require('class-is')
2+
import * as bytes from './bytes.js'
3+
import withIs from 'class-is'
44

55
const readonly = (object, key, value) => {
66
Object.defineProperty(object, key, {
@@ -10,7 +10,7 @@ const readonly = (object, key, value) => {
1010
})
1111
}
1212

13-
module.exports = multiformats => {
13+
export default multiformats => {
1414
const { multibase, varint, multihash } = multiformats
1515
const parse = buff => {
1616
const [code, length] = varint.decode(buff)

codecs/json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
encode: obj => new TextEncoder().encode(JSON.stringify(obj)),
33
decode: buff => JSON.parse(new TextDecoder().decode(buff)),
44
name: 'json',

0 commit comments

Comments
 (0)