Skip to content

Commit ce698bc

Browse files
chore: switch to esm (#22)
BREAKING CHANGE: built content includes ESM and CJS and has switched to named exports for the external API Co-authored-by: Alex Potsides <[email protected]>
1 parent 54be55e commit ce698bc

21 files changed

+102
-123
lines changed

.aegir.js renamed to .aegir.cjs

File renamed without changes.

.github/workflows/main.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ jobs:
6666
steps:
6767
- uses: actions/checkout@v2
6868
- run: npm install
69-
- run: npx xvfb-maybe aegir test -t electron-main --bail
69+
- run: npm run build -- --esm-tests # build tests with esm
70+
- run: npx xvfb-maybe aegir test -t electron-main -f "dist/cjs/node-test/*js" --bail
7071
test-electron-renderer:
7172
needs: check
7273
runs-on: ubuntu-latest
7374
steps:
7475
- uses: actions/checkout@v2
7576
- run: npm install
76-
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
77+
- run: npm run build -- --esm-tests # build tests with esm
78+
- run: npx xvfb-maybe aegir test -t electron-renderer -f "dist/cjs/node-test/*js" --bail

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
dist
3+
types
34
docs
45
package-lock.json

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Compare two `Uint8Arrays`
2525
#### Example
2626

2727
```js
28-
const compare = require('uint8arrays/compare')
28+
import { compare } from 'uint8arrays/compare'
2929

3030
const arrays = [
3131
Uint8Array.from([3, 4, 5]),
@@ -50,7 +50,7 @@ If you know the length of the arrays, pass it as a second parameter, otherwise i
5050
#### Example
5151

5252
```js
53-
const concat = require('uint8arrays/concat')
53+
import { concat } from 'uint8arrays/concat'
5454

5555
const arrays = [
5656
Uint8Array.from([0, 1, 2]),
@@ -70,7 +70,7 @@ Returns true if the two arrays are the same array or if they have the same lengt
7070
#### Example
7171

7272
```js
73-
const equals = require('uint8arrays/equals')
73+
import { equals } from 'uint8arrays/equals'
7474

7575
const a = Uint8Array.from([0, 1, 2])
7676
const b = Uint8Array.from([3, 4, 5])
@@ -90,7 +90,7 @@ Supports `utf8` and any of the [multibase encodings](https://github.com/multifor
9090
#### Example
9191

9292
```js
93-
const fromString = require('uint8arrays/from-string')
93+
import { fromString } from 'uint8arrays/from-string'
9494

9595
console.info(fromString('hello world')) // Uint8Array[104, 101 ...
9696
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
@@ -107,7 +107,7 @@ Supports `utf8` and any of the [multibase encodings](https://github.com/multifor
107107
#### Example
108108

109109
```js
110-
const toString = require('uint8arrays/to-string')
110+
import { toString } from 'uint8arrays/to-string'
111111

112112
console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
113113
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
@@ -122,7 +122,7 @@ Returns a `Uint8Array` containing `a` and `b` xored together.
122122
#### Example
123123

124124
```js
125-
const xor = require('uint8arrays/xor')
125+
import { xor } from 'uint8arrays/xor'
126126

127127
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
128128
```

index.js

-16
This file was deleted.

package.json

+38-27
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,17 @@
22
"name": "uint8arrays",
33
"version": "2.1.10",
44
"description": "Utility functions to make dealing with Uint8Arrays easier",
5-
"main": "index.js",
5+
"main": "src/index.js",
66
"author": "Alex Potsides <[email protected]>",
77
"homepage": "https://github.com/achingbrain/uint8arrays",
88
"bugs": "https://github.com/achingbrain/uint8arrays/issues",
9-
"types": "dist/index.d.ts",
10-
"typesVersions": {
11-
"*": {
12-
"*": [
13-
"dist/*"
14-
],
15-
"index.js": [
16-
"dist/index.d.ts"
17-
]
18-
}
19-
},
20-
"files": [
21-
"compare.js",
22-
"concat.js",
23-
"equals.js",
24-
"from-string.js",
25-
"index.js",
26-
"to-string.js",
27-
"xor.js",
28-
"dist/**/*.ts",
29-
"dist/**/*.map",
30-
"dist/**/*.js",
31-
"util/*.js"
32-
],
9+
"type": "module",
10+
"types": "types/src/index.d.ts",
3311
"repository": {
3412
"type": "git",
3513
"url": "https://github.com/achingbrain/uint8arrays.git"
3614
},
3715
"scripts": {
38-
"prepare": "aegir build --no-bundle",
3916
"test": "aegir test",
4017
"lint": "aegir ts -p check && aegir lint",
4118
"release": "aegir release",
@@ -48,15 +25,49 @@
4825
"multiformats": "^9.4.2"
4926
},
5027
"devDependencies": {
51-
"aegir": "^34.0.2",
28+
"aegir": "^35.0.0",
5229
"util": "^0.12.4"
5330
},
5431
"eslintConfig": {
5532
"extends": "ipfs",
33+
"parserOptions": {
34+
"sourceType": "module"
35+
},
5636
"ignorePatterns": [
5737
"!.aegir.js"
5838
]
5939
},
40+
"typesVersions": {
41+
"*": {
42+
"*": [
43+
"types/src",
44+
"types/src/*"
45+
]
46+
}
47+
},
48+
"exports": {
49+
".": {
50+
"import": "./src/index.js"
51+
},
52+
"./compare": {
53+
"import": "./src/compare.js"
54+
},
55+
"./concat": {
56+
"import": "./src/concat.js"
57+
},
58+
"./equals": {
59+
"import": "./src/equals.js"
60+
},
61+
"./from-string": {
62+
"import": "./src/from-string.js"
63+
},
64+
"./to-string": {
65+
"import": "./src/to-string.js"
66+
},
67+
"./xor": {
68+
"import": "./src/xor.js"
69+
}
70+
},
6071
"contributors": [
6172
"achingbrain <[email protected]>",
6273
"Cayman <[email protected]>",

compare.js renamed to src/compare.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
31
/**
42
* Can be used with Array.sort to sort and array with Uint8Array entries
53
*
64
* @param {Uint8Array} a
75
* @param {Uint8Array} b
86
*/
9-
function compare (a, b) {
7+
export function compare (a, b) {
108
for (let i = 0; i < a.byteLength; i++) {
119
if (a[i] < b[i]) {
1210
return -1
@@ -27,5 +25,3 @@ function compare (a, b) {
2725

2826
return 0
2927
}
30-
31-
module.exports = compare

concat.js renamed to src/concat.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
31
/**
42
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
53
*
64
* @param {Array<ArrayLike<number>>} arrays
75
* @param {number} [length]
86
*/
9-
function concat (arrays, length) {
7+
export function concat (arrays, length) {
108
if (!length) {
119
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
1210
}
@@ -21,5 +19,3 @@ function concat (arrays, length) {
2119

2220
return output
2321
}
24-
25-
module.exports = concat

equals.js renamed to src/equals.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
31
/**
42
* Returns true if the two passed Uint8Arrays have the same content
53
*
64
* @param {Uint8Array} a
75
* @param {Uint8Array} b
86
*/
9-
function equals (a, b) {
7+
export function equals (a, b) {
108
if (a === b) {
119
return true
1210
}
@@ -23,5 +21,3 @@ function equals (a, b) {
2321

2422
return true
2523
}
26-
27-
module.exports = equals

from-string.js renamed to src/from-string.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const bases = require('./util/bases')
1+
import bases from './util/bases.js'
42

53
/**
64
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings
@@ -17,7 +15,7 @@ const bases = require('./util/bases')
1715
* @param {SupportedEncodings} [encoding=utf8] - utf8, base16, base64, base64urlpad, etc
1816
* @returns {Uint8Array}
1917
*/
20-
function fromString (string, encoding = 'utf8') {
18+
export function fromString (string, encoding = 'utf8') {
2119
const base = bases[encoding]
2220

2321
if (!base) {
@@ -27,5 +25,3 @@ function fromString (string, encoding = 'utf8') {
2725
// add multibase prefix
2826
return base.decoder.decode(`${base.prefix}${string}`)
2927
}
30-
31-
module.exports = fromString

src/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { compare } from './compare.js'
2+
import { concat } from './concat.js'
3+
import { equals } from './equals.js'
4+
import { fromString } from './from-string.js'
5+
import { toString } from './to-string.js'
6+
import { xor } from './xor.js'
7+
8+
export {
9+
compare,
10+
concat,
11+
equals,
12+
fromString,
13+
toString,
14+
xor
15+
}

to-string.js renamed to src/to-string.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const bases = require('./util/bases')
1+
import bases from './util/bases.js'
42

53
/**
64
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings
@@ -17,7 +15,7 @@ const bases = require('./util/bases')
1715
* @param {SupportedEncodings} [encoding=utf8] - The encoding to use
1816
* @returns {string}
1917
*/
20-
function toString (array, encoding = 'utf8') {
18+
export function toString (array, encoding = 'utf8') {
2119
const base = bases[encoding]
2220

2321
if (!base) {
@@ -27,5 +25,3 @@ function toString (array, encoding = 'utf8') {
2725
// strip multibase prefix
2826
return base.encoder.encode(array).substring(1)
2927
}
30-
31-
module.exports = toString

util/bases.js renamed to src/util/bases.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const { bases } = require('multiformats/basics')
1+
import { bases } from 'multiformats/basics'
42

53
/**
64
* @typedef {import('multiformats/bases/interface').MultibaseCodec<any>} MultibaseCodec
@@ -62,14 +60,14 @@ const ascii = createCodec('ascii', 'a', (buf) => {
6260
* @type {Record<SupportedEncodings, MultibaseCodec>}
6361
*/
6462
const BASES = {
65-
'utf8': string,
63+
utf8: string,
6664
'utf-8': string,
67-
'hex': bases.base16,
68-
'latin1': ascii,
69-
'ascii': ascii,
70-
'binary': ascii,
65+
hex: bases.base16,
66+
latin1: ascii,
67+
ascii: ascii,
68+
binary: ascii,
7169

7270
...bases
7371
}
7472

75-
module.exports = BASES
73+
export default BASES

xor.js renamed to src/xor.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
31
/**
42
* Returns the xor distance between two arrays
53
*
64
* @param {Uint8Array} a
75
* @param {Uint8Array} b
86
*/
9-
function xor (a, b) {
7+
export function xor (a, b) {
108
if (a.length !== b.length) {
119
throw new Error('Inputs should have the same length')
1210
}
@@ -19,5 +17,3 @@ function xor (a, b) {
1917

2018
return result
2119
}
22-
23-
module.exports = xor

test/compare.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
2-
'use strict'
32

4-
const { expect } = require('aegir/utils/chai')
5-
const compare = require('../compare')
3+
import { expect } from 'aegir/utils/chai.js'
4+
import { compare } from '../src/compare.js'
65

76
describe('Uint8Array compare', () => {
87
it('is stable', () => {

test/concat.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
2-
'use strict'
32

4-
const { expect } = require('aegir/utils/chai')
5-
const concat = require('../concat')
3+
import { expect } from 'aegir/utils/chai.js'
4+
import { concat } from '../src/concat.js'
65

76
describe('Uint8Array concat', () => {
87
it('concats two Uint8Arrays', () => {

test/equals.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
2-
'use strict'
32

4-
const { expect } = require('aegir/utils/chai')
5-
const equals = require('../equals')
3+
import { expect } from 'aegir/utils/chai.js'
4+
import { equals } from '../src/equals.js'
65

76
describe('Uint8Array equals', () => {
87
it('finds two Uint8Arrays equal', () => {

0 commit comments

Comments
 (0)