Skip to content

Commit

Permalink
chore: switch to esm (#22)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
vasco-santos and achingbrain committed Aug 16, 2021
1 parent 54be55e commit ce698bc
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 123 deletions.
File renamed without changes.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-main --bail
- run: npm run build -- --esm-tests # build tests with esm
- run: npx xvfb-maybe aegir test -t electron-main -f "dist/cjs/node-test/*js" --bail
test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
- run: npm run build -- --esm-tests # build tests with esm
- run: npx xvfb-maybe aegir test -t electron-renderer -f "dist/cjs/node-test/*js" --bail
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
types
docs
package-lock.json
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Compare two `Uint8Arrays`
#### Example

```js
const compare = require('uint8arrays/compare')
import { compare } from 'uint8arrays/compare'

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

```js
const concat = require('uint8arrays/concat')
import { concat } from 'uint8arrays/concat'

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

```js
const equals = require('uint8arrays/equals')
import { equals } from 'uint8arrays/equals'

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

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

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

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

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

```js
const xor = require('uint8arrays/xor')
import { xor } from 'uint8arrays/xor'

console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
```
16 changes: 0 additions & 16 deletions index.js

This file was deleted.

65 changes: 38 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,17 @@
"name": "uint8arrays",
"version": "2.1.10",
"description": "Utility functions to make dealing with Uint8Arrays easier",
"main": "index.js",
"main": "src/index.js",
"author": "Alex Potsides <alex@achingbrain.net>",
"homepage": "https://github.com/achingbrain/uint8arrays",
"bugs": "https://github.com/achingbrain/uint8arrays/issues",
"types": "dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"dist/*"
],
"index.js": [
"dist/index.d.ts"
]
}
},
"files": [
"compare.js",
"concat.js",
"equals.js",
"from-string.js",
"index.js",
"to-string.js",
"xor.js",
"dist/**/*.ts",
"dist/**/*.map",
"dist/**/*.js",
"util/*.js"
],
"type": "module",
"types": "types/src/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/achingbrain/uint8arrays.git"
},
"scripts": {
"prepare": "aegir build --no-bundle",
"test": "aegir test",
"lint": "aegir ts -p check && aegir lint",
"release": "aegir release",
Expand All @@ -48,15 +25,49 @@
"multiformats": "^9.4.2"
},
"devDependencies": {
"aegir": "^34.0.2",
"aegir": "^35.0.0",
"util": "^0.12.4"
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
},
"ignorePatterns": [
"!.aegir.js"
]
},
"typesVersions": {
"*": {
"*": [
"types/src",
"types/src/*"
]
}
},
"exports": {
".": {
"import": "./src/index.js"
},
"./compare": {
"import": "./src/compare.js"
},
"./concat": {
"import": "./src/concat.js"
},
"./equals": {
"import": "./src/equals.js"
},
"./from-string": {
"import": "./src/from-string.js"
},
"./to-string": {
"import": "./src/to-string.js"
},
"./xor": {
"import": "./src/xor.js"
}
},
"contributors": [
"achingbrain <alex@achingbrain.net>",
"Cayman <caymannava@gmail.com>",
Expand Down
6 changes: 1 addition & 5 deletions compare.js → src/compare.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

/**
* Can be used with Array.sort to sort and array with Uint8Array entries
*
* @param {Uint8Array} a
* @param {Uint8Array} b
*/
function compare (a, b) {
export function compare (a, b) {
for (let i = 0; i < a.byteLength; i++) {
if (a[i] < b[i]) {
return -1
Expand All @@ -27,5 +25,3 @@ function compare (a, b) {

return 0
}

module.exports = compare
6 changes: 1 addition & 5 deletions concat.js → src/concat.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

/**
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
*
* @param {Array<ArrayLike<number>>} arrays
* @param {number} [length]
*/
function concat (arrays, length) {
export function concat (arrays, length) {
if (!length) {
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
}
Expand All @@ -21,5 +19,3 @@ function concat (arrays, length) {

return output
}

module.exports = concat
6 changes: 1 addition & 5 deletions equals.js → src/equals.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

/**
* Returns true if the two passed Uint8Arrays have the same content
*
* @param {Uint8Array} a
* @param {Uint8Array} b
*/
function equals (a, b) {
export function equals (a, b) {
if (a === b) {
return true
}
Expand All @@ -23,5 +21,3 @@ function equals (a, b) {

return true
}

module.exports = equals
8 changes: 2 additions & 6 deletions from-string.js → src/from-string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const bases = require('./util/bases')
import bases from './util/bases.js'

/**
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings
Expand All @@ -17,7 +15,7 @@ const bases = require('./util/bases')
* @param {SupportedEncodings} [encoding=utf8] - utf8, base16, base64, base64urlpad, etc
* @returns {Uint8Array}
*/
function fromString (string, encoding = 'utf8') {
export function fromString (string, encoding = 'utf8') {
const base = bases[encoding]

if (!base) {
Expand All @@ -27,5 +25,3 @@ function fromString (string, encoding = 'utf8') {
// add multibase prefix
return base.decoder.decode(`${base.prefix}${string}`)
}

module.exports = fromString
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { compare } from './compare.js'
import { concat } from './concat.js'
import { equals } from './equals.js'
import { fromString } from './from-string.js'
import { toString } from './to-string.js'
import { xor } from './xor.js'

export {
compare,
concat,
equals,
fromString,
toString,
xor
}
8 changes: 2 additions & 6 deletions to-string.js → src/to-string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const bases = require('./util/bases')
import bases from './util/bases.js'

/**
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings
Expand All @@ -17,7 +15,7 @@ const bases = require('./util/bases')
* @param {SupportedEncodings} [encoding=utf8] - The encoding to use
* @returns {string}
*/
function toString (array, encoding = 'utf8') {
export function toString (array, encoding = 'utf8') {
const base = bases[encoding]

if (!base) {
Expand All @@ -27,5 +25,3 @@ function toString (array, encoding = 'utf8') {
// strip multibase prefix
return base.encoder.encode(array).substring(1)
}

module.exports = toString
16 changes: 7 additions & 9 deletions util/bases.js → src/util/bases.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const { bases } = require('multiformats/basics')
import { bases } from 'multiformats/basics'

/**
* @typedef {import('multiformats/bases/interface').MultibaseCodec<any>} MultibaseCodec
Expand Down Expand Up @@ -62,14 +60,14 @@ const ascii = createCodec('ascii', 'a', (buf) => {
* @type {Record<SupportedEncodings, MultibaseCodec>}
*/
const BASES = {
'utf8': string,
utf8: string,
'utf-8': string,
'hex': bases.base16,
'latin1': ascii,
'ascii': ascii,
'binary': ascii,
hex: bases.base16,
latin1: ascii,
ascii: ascii,
binary: ascii,

...bases
}

module.exports = BASES
export default BASES
6 changes: 1 addition & 5 deletions xor.js → src/xor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

/**
* Returns the xor distance between two arrays
*
* @param {Uint8Array} a
* @param {Uint8Array} b
*/
function xor (a, b) {
export function xor (a, b) {
if (a.length !== b.length) {
throw new Error('Inputs should have the same length')
}
Expand All @@ -19,5 +17,3 @@ function xor (a, b) {

return result
}

module.exports = xor
5 changes: 2 additions & 3 deletions test/compare.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('aegir/utils/chai')
const compare = require('../compare')
import { expect } from 'aegir/utils/chai.js'
import { compare } from '../src/compare.js'

describe('Uint8Array compare', () => {
it('is stable', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/concat.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('aegir/utils/chai')
const concat = require('../concat')
import { expect } from 'aegir/utils/chai.js'
import { concat } from '../src/concat.js'

describe('Uint8Array concat', () => {
it('concats two Uint8Arrays', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/equals.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('aegir/utils/chai')
const equals = require('../equals')
import { expect } from 'aegir/utils/chai.js'
import { equals } from '../src/equals.js'

describe('Uint8Array equals', () => {
it('finds two Uint8Arrays equal', () => {
Expand Down
Loading

0 comments on commit ce698bc

Please sign in to comment.