diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 075facf..8e62f3d 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,4 +12,6 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '16, 18, 20, 22' + version: '16, 18, 20, 22, 23' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml new file mode 100644 index 0000000..bac3fac --- /dev/null +++ b/.github/workflows/pkg.pr.new.yml @@ -0,0 +1,23 @@ +name: Publish Any Commit +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm install + + - name: Build + run: npm run prepublishOnly --if-present + + - run: npx pkg-pr-new publish diff --git a/README.md b/README.md index ef6d8cc..7869776 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![CI](https://github.com/node-modules/utility/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/utility/actions/workflows/nodejs.yml) [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url] +[![Node.js Version](https://img.shields.io/node/v/utility.svg?style=flat)](https://nodejs.org/en/download/) [npm-image]: https://img.shields.io/npm/v/utility.svg?style=flat-square [npm-url]: https://npmjs.org/package/utility @@ -29,68 +30,97 @@ const utils = require('utility'); Also you can use it within typescript, like this ↓ ```ts -import * as utility from 'utility'; +import * as utils from 'utility'; ``` ### md5 -```js -utils.md5('苏千').should.equal('5f733c47c58a077d61257102b2d44481'); -utils.md5(Buffer.from('苏千')).should.equal('5f733c47c58a077d61257102b2d44481'); +```ts +import { md5 } from 'utility'; + +md5('苏千'); +// '5f733c47c58a077d61257102b2d44481' + +md5(Buffer.from('苏千')); +// '5f733c47c58a077d61257102b2d44481' + // md5 base64 format -utils.md5('苏千', 'base64'); // 'X3M8R8WKB31hJXECstREgQ==' +md5('苏千', 'base64'); +// 'X3M8R8WKB31hJXECstREgQ==' // Object md5 hash. Sorted by key, and JSON.stringify. See source code for detail -utils.md5({foo: 'bar', bar: 'foo'}).should.equal(utils.md5({bar: 'foo', foo: 'bar'})); +md5({foo: 'bar', bar: 'foo'}).should.equal(md5({bar: 'foo', foo: 'bar'})); ``` ### sha1 -```js -utils.sha1('苏千').should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5'); -utils.sha1(Buffer.from('苏千')).should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5'); +```ts +import { sha1 } from 'utility'; + +sha1('苏千'); +// '0a4aff6bab634b9c2f99b71f25e976921fcde5a5' + +sha1(Buffer.from('苏千')); +// '0a4aff6bab634b9c2f99b71f25e976921fcde5a5' + // sha1 base64 format -utils.sha1('苏千', 'base64'); // 'Ckr/a6tjS5wvmbcfJel2kh/N5aU=' +sha1('苏千', 'base64'); +// 'Ckr/a6tjS5wvmbcfJel2kh/N5aU=' // Object sha1 hash. Sorted by key, and JSON.stringify. See source code for detail -utils.sha1({foo: 'bar', bar: 'foo'}).should.equal(utils.sha1({bar: 'foo', foo: 'bar'})); +sha1({foo: 'bar', bar: 'foo'}).should.equal(sha1({bar: 'foo', foo: 'bar'})); ``` ### sha256 -```js -utils.sha256(Buffer.from('苏千')).should.equal('75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635'); +```ts +import { sha256 } from 'utility'; + +sha256(Buffer.from('苏千')); +// '75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635' ``` ### hmac -```js +```ts +import { hmac } from 'utility'; + // hmac-sha1 with base64 output encoding -utils.hmac('sha1', 'I am a key', 'hello world'); // 'pO6J0LKDxRRkvSECSEdxwKx84L0=' +hmac('sha1', 'I am a key', 'hello world'); +// 'pO6J0LKDxRRkvSECSEdxwKx84L0=' ``` ### decode and encode -```js +```ts +import { base64encode, base64decode, escape, unescape, encodeURIComponent, decodeURIComponent } from 'utility'; + // base64 encode -utils.base64encode('你好¥'); // '5L2g5aW977+l' -utils.base64decode('5L2g5aW977+l') // '你好¥' +base64encode('你好¥'); +// '5L2g5aW977+l' +base64decode('5L2g5aW977+l'); +// '你好¥' // urlsafe base64 encode -utils.base64encode('你好¥', true); // '5L2g5aW977-l' -utils.base64decode('5L2g5aW977-l', true); // '你好¥' +base64encode('你好¥', true); +// '5L2g5aW977-l' +base64decode('5L2g5aW977-l', true); +// '你好¥' // html escape and unescape -utils.escape('