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 <alex@achingbrain.net>
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 0 additions & 16 deletions
This file was deleted.

package.json

Lines changed: 38 additions & 27 deletions
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 <alex@achingbrain.net>",
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 <alex@achingbrain.net>",
6273
"Cayman <caymannava@gmail.com>",

compare.js renamed to src/compare.js

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 2 additions & 6 deletions
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

0 commit comments

Comments
 (0)