diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..0f10977 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +**/*.d.ts \ No newline at end of file diff --git a/.github/workflows/main_ci.yml b/.github/workflows/main_ci.yml index 4f7d04b..d1a97ec 100644 --- a/.github/workflows/main_ci.yml +++ b/.github/workflows/main_ci.yml @@ -10,20 +10,30 @@ jobs: unit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v4 with: - node-version: 12 + node-version: 18 registry-url: https://registry.npmjs.org/ - - run: npm i + - run: npm ci - run: npm run unit format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v4 with: - node-version: 12 + node-version: 18 registry-url: https://registry.npmjs.org/ - - run: npm i - - run: npm run standard \ No newline at end of file + - run: npm ci + - run: npm run standard + gitdiff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v4 + with: + node-version: 18 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm run gitdiff \ No newline at end of file diff --git a/README.md b/README.md index ca0aa19..76a4ff0 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ API **example**: ```js -const bs58 = require('bs58') +import bs58 from 'bs58' const bytes = Uint8Array.from([ 0, 60, 23, 110, 101, 155, 234, @@ -45,7 +45,7 @@ console.log(address) **example**: ```js -const bs58 = require('bs58') +import bs58 from 'bs58' const address = '16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS' const bytes = bs58.decode(address) @@ -65,7 +65,7 @@ npm install -g browserify then run: ```bash -browserify node_modules/bs58/index.js -o bs58.bundle.js --standalone bs58 +browserify node_modules/bs58/cjs/index.cjs -o bs58.bundle.js --standalone bs58 ``` Hack / Test diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 72d0ab8..0000000 --- a/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { BaseConverter } from 'base-x'; - -declare const base58: BaseConverter; - -export = base58; diff --git a/package.json b/package.json index 6e53086..7688dc5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "bs58", "version": "5.0.0", + "type": "module", "description": "Base 58 encoding / decoding", "keywords": [ "base58", @@ -15,25 +16,40 @@ ], "license": "MIT", "devDependencies": { - "standard": "^16.0.4", - "tape": "^4.6.3" + "@types/node": "^20.14.8", + "rimraf": "^5.0.7", + "tape": "^5.3.0", + "ts-standard": "^12.0.2", + "typescript": "^5.5.2" }, "repository": { "url": "https://github.com/cryptocoinjs/bs58", "type": "git" }, "files": [ - "index.js", - "index.d.ts" + "src/cjs", + "src/esm" ], - "main": "index.js", - "types": "index.d.ts", + "main": "src/cjs/index.cjs", + "module": "src/esm/index.js", + "types": "src/cjs/index.d.ts", + "exports": { + ".": { + "import": "./src/esm/index.js", + "require": "./src/cjs/index.cjs", + "types": "./src/cjs/index.d.ts" + } + }, "scripts": { - "standard": "standard", + "build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json", + "clean": "rimraf src", + "gitdiff": "npm run build && git diff --exit-code", + "postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;", + "standard": "ts-standard --ignore src --ignore test", "test": "npm run standard && npm run unit", "unit": "tape test/index.js" }, "dependencies": { - "base-x": "^4.0.0" + "base-x": "^5.0.0" } } diff --git a/src/cjs/index.cjs b/src/cjs/index.cjs new file mode 100644 index 0000000..0a5d975 --- /dev/null +++ b/src/cjs/index.cjs @@ -0,0 +1,8 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var base_x_1 = __importDefault(require("base-x")); +var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; +exports.default = (0, base_x_1.default)(ALPHABET); diff --git a/src/cjs/index.d.ts b/src/cjs/index.d.ts new file mode 100644 index 0000000..6aeee88 --- /dev/null +++ b/src/cjs/index.d.ts @@ -0,0 +1,3 @@ +import basex from 'base-x'; +declare const _default: basex.BaseConverter; +export default _default; diff --git a/src/esm/index.js b/src/esm/index.js new file mode 100644 index 0000000..84a71b1 --- /dev/null +++ b/src/esm/index.js @@ -0,0 +1,3 @@ +import basex from 'base-x'; +var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; +export default basex(ALPHABET); diff --git a/test/index.js b/test/index.js index f4500cb..dddb729 100644 --- a/test/index.js +++ b/test/index.js @@ -1,13 +1,15 @@ -const test = require('tape') -const base58 = require('../') +import tape from 'tape' +import base58 from '../src/esm/index.js' +import fixtures from './fixtures.json' assert { type: "json" } -const fixtures = require('./fixtures.json') +const { encode, decode } = base58 +const { valid, invalid } = fixtures -test('base58', function (t) { - t.test('encode', function (t) { - fixtures.valid.forEach(function (f) { - t.test('can encode ' + f.hex, function (t) { - const actual = base58.encode(Buffer.from(f.hex, 'hex')) +tape('base58', function (t) { + tape('encode', function (t) { + valid.forEach(function (f) { + tape('can encode ' + f.hex, function (t) { + const actual = encode(Buffer.from(f.hex, 'hex')) t.equal(actual, f.string) t.end() }) @@ -16,19 +18,19 @@ test('base58', function (t) { t.end() }) - t.test('decode', function (t) { - fixtures.valid.forEach(function (f) { - t.test('can decode ' + f.string, function (t) { - const actual = Buffer.from(base58.decode(f.string)).toString('hex') + tape('decode', function (t) { + valid.forEach(function (f) { + tape('can decode ' + f.string, function (t) { + const actual = Buffer.from(decode(f.string)).toString('hex') t.same(actual, f.hex) t.end() }) }) - fixtures.invalid.forEach(function (f) { - t.test('throws on ' + f.description, function (t) { + invalid.forEach(function (f) { + tape('throws on ' + f.description, function (t) { t.throws(function () { - base58.decode(f.string) + decode(f.string) }, /^Error: Non-base58 character$/) t.end() }) diff --git a/index.js b/ts_src/index.ts similarity index 54% rename from index.js rename to ts_src/index.ts index 3d02b0c..6b56def 100644 --- a/index.js +++ b/ts_src/index.ts @@ -1,4 +1,4 @@ -const basex = require('base-x') +import basex from 'base-x' const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' -module.exports = basex(ALPHABET) +export default basex(ALPHABET) diff --git a/tsconfig.base.json b/tsconfig.base.json new file mode 100644 index 0000000..d10d8ce --- /dev/null +++ b/tsconfig.base.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "commonjs", + "outDir": "./src", + "rootDir": "./ts_src", + "types": ["node"], + "allowJs": false, + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "esModuleInterop": true, + "noUnusedLocals": true, + "noUnusedParameters": true + }, + "include": ["ts_src/**/*.ts"], + "exclude": ["node_modules/**/*"] + } + \ No newline at end of file diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..1417240 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "declaration": true, + "emitDeclarationOnly": false, + "outDir": "src/cjs", + "module": "commonjs" + } + } + \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0d8c4ff --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "outDir": "src/esm", + "esModuleInterop": true, + "resolveJsonModule": true, + "module": "ESNext", + "moduleResolution": "Node" + }, +} + \ No newline at end of file