diff --git a/.aegir.js b/.aegir.cjs similarity index 100% rename from .aegir.js rename to .aegir.cjs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ef126da..0ca9e95 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/.gitignore b/.gitignore index 8c93a42..5aa0b00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules dist +types docs package-lock.json diff --git a/README.md b/README.md index b329b9b..a922ff0 100644 --- a/README.md +++ b/README.md @@ -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]), @@ -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]), @@ -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]) @@ -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 ... @@ -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' @@ -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] ``` diff --git a/index.js b/index.js deleted file mode 100644 index f8ffa90..0000000 --- a/index.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' -const compare = require('./compare') -const concat = require('./concat') -const equals = require('./equals') -const fromString = require('./from-string') -const toString = require('./to-string') -const xor = require('./xor') - -module.exports = { - compare, - concat, - equals, - fromString, - toString, - xor -} diff --git a/package.json b/package.json index e7cb377..402e34d 100644 --- a/package.json +++ b/package.json @@ -2,40 +2,17 @@ "name": "uint8arrays", "version": "2.1.8", "description": "Utility functions to make dealing with Uint8Arrays easier", - "main": "index.js", + "main": "src/index.js", "author": "Alex Potsides ", "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", @@ -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 ", "Cayman ", diff --git a/compare.js b/src/compare.js similarity index 85% rename from compare.js rename to src/compare.js index 951d4cb..d71d1f2 100644 --- a/compare.js +++ b/src/compare.js @@ -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 @@ -27,5 +25,3 @@ function compare (a, b) { return 0 } - -module.exports = compare diff --git a/concat.js b/src/concat.js similarity index 84% rename from concat.js rename to src/concat.js index c130089..e03970d 100644 --- a/concat.js +++ b/src/concat.js @@ -1,12 +1,10 @@ -'use strict' - /** * Returns a new Uint8Array created by concatenating the passed ArrayLikes * * @param {Array>} arrays * @param {number} [length] */ -function concat (arrays, length) { +export function concat (arrays, length) { if (!length) { length = arrays.reduce((acc, curr) => acc + curr.length, 0) } @@ -21,5 +19,3 @@ function concat (arrays, length) { return output } - -module.exports = concat diff --git a/equals.js b/src/equals.js similarity index 84% rename from equals.js rename to src/equals.js index af149f6..3d770b8 100644 --- a/equals.js +++ b/src/equals.js @@ -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 } @@ -23,5 +21,3 @@ function equals (a, b) { return true } - -module.exports = equals diff --git a/from-string.js b/src/from-string.js similarity index 82% rename from from-string.js rename to src/from-string.js index e869c7e..8f936d3 100644 --- a/from-string.js +++ b/src/from-string.js @@ -1,6 +1,4 @@ -'use strict' - -const bases = require('./util/bases') +import bases from './util/bases.js' /** * @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings @@ -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) { @@ -27,5 +25,3 @@ function fromString (string, encoding = 'utf8') { // add multibase prefix return base.decoder.decode(`${base.prefix}${string}`) } - -module.exports = fromString diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..411c9e0 --- /dev/null +++ b/src/index.js @@ -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 +} diff --git a/to-string.js b/src/to-string.js similarity index 83% rename from to-string.js rename to src/to-string.js index 670881c..c827584 100644 --- a/to-string.js +++ b/src/to-string.js @@ -1,6 +1,4 @@ -'use strict' - -const bases = require('./util/bases') +import bases from './util/bases.js' /** * @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings @@ -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) { @@ -27,5 +25,3 @@ function toString (array, encoding = 'utf8') { // strip multibase prefix return base.encoder.encode(array).substring(1) } - -module.exports = toString diff --git a/util/bases.js b/src/util/bases.js similarity index 87% rename from util/bases.js rename to src/util/bases.js index bc31fe1..79a0c18 100644 --- a/util/bases.js +++ b/src/util/bases.js @@ -1,6 +1,4 @@ - 'use strict' - -const { bases } = require('multiformats/basics') +import { bases } from 'multiformats/basics' /** * @typedef {import('multiformats/bases/interface').MultibaseCodec} MultibaseCodec @@ -62,14 +60,14 @@ const ascii = createCodec('ascii', 'a', (buf) => { * @type {Record} */ 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 diff --git a/xor.js b/src/xor.js similarity index 85% rename from xor.js rename to src/xor.js index dabbba9..35c014c 100644 --- a/xor.js +++ b/src/xor.js @@ -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') } @@ -19,5 +17,3 @@ function xor (a, b) { return result } - -module.exports = xor diff --git a/test/compare.spec.js b/test/compare.spec.js index f0c144c..7176fd7 100644 --- a/test/compare.spec.js +++ b/test/compare.spec.js @@ -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', () => { diff --git a/test/concat.spec.js b/test/concat.spec.js index ecb6354..8156ac7 100644 --- a/test/concat.spec.js +++ b/test/concat.spec.js @@ -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', () => { diff --git a/test/equals.spec.js b/test/equals.spec.js index 9129cf0..87efd87 100644 --- a/test/equals.spec.js +++ b/test/equals.spec.js @@ -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', () => { diff --git a/test/from-string.spec.js b/test/from-string.spec.js index abc64ca..50bbab8 100644 --- a/test/from-string.spec.js +++ b/test/from-string.spec.js @@ -1,12 +1,11 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const fromString = require('../from-string') -const toString = require('../to-string') -const bases = require('../util/bases') +import { expect } from 'aegir/utils/chai.js' +import { fromString } from '../src/from-string.js' +import { toString } from '../src/to-string.js' +import bases from '../src/util/bases.js' -/** @type {import('../util/bases').SupportedEncodings[]} */ +/** @type {import('../src/util/bases').SupportedEncodings[]} */ // @ts-ignore Object.keys returns a string[] const supportedBases = Object.keys(bases) diff --git a/test/to-string.spec.js b/test/to-string.spec.js index ca5a9c7..c610447 100644 --- a/test/to-string.spec.js +++ b/test/to-string.spec.js @@ -1,8 +1,7 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const toString = require('../to-string') +import { expect } from 'aegir/utils/chai.js' +import { toString } from '../src/to-string.js' describe('Uint8Array toString', () => { it('creates a String from a Uint8Array', () => { diff --git a/test/xor.spec.js b/test/xor.spec.js index 3ceb306..81059ed 100644 --- a/test/xor.spec.js +++ b/test/xor.spec.js @@ -1,8 +1,7 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const xor = require('../xor') +import { expect } from 'aegir/utils/chai.js' +import { xor } from '../src/xor.js' describe('Uint8Array xor', () => { it('xors 1,0 and 0,1', () => { diff --git a/tsconfig.json b/tsconfig.json index b0af813..ecde80b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,10 @@ { - "extends": "aegir/src/config/tsconfig.aegir.json", - "compilerOptions": { - "outDir": "dist" - }, - "include": [ - "test", - "util", - "*.js", - ".aegir.js" - ] + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "./types" + }, + "include": [ + "src", + "test" + ] }