From d8b232db67ed5434ff7b72d6ae4cd43cf5f9fb80 Mon Sep 17 00:00:00 2001 From: "Feng.YJ" <32027253+huiyifyj@users.noreply.github.com> Date: Fri, 5 Jan 2024 21:45:24 +0800 Subject: [PATCH] Move to ESM and require minimum node 16 (#32) * Refactor: move to [ESM](https://nodejs.org/api/esm.html), refer to https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c * Update doc and comments. * Require minimum node version 16. --- README.md | 3 +-- md5.d.ts | 5 ++--- md5.js | 5 ++--- package.json | 5 +++++ test/md5.test.js | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b80e6d5..6d42894 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ npm i @xn-02f/md5 ## Usage ```javascript -const md5 = require('@xn-02f/md5'); -// import md5 from '@xn-02f/md5' +import md5 from '@xn-02f/md5' md5('xn-02f'); // => '54d30fa674d13e3598970bc9c5e2388e' ``` diff --git a/md5.d.ts b/md5.d.ts index c8fd37d..aa3cfde 100644 --- a/md5.d.ts +++ b/md5.d.ts @@ -5,11 +5,10 @@ * @return {string} The hexadecimal hash string that is handled and converted. * @example * ``` - * import md5 = require('@xn-02f/md5'); - * // import md5 from '@xn-02f/md5'; + * import md5 from '@xn-02f/md5'; * md5('xn-02f'); //=> '54d30fa674d13e3598970bc9c5e2388e' * ``` */ declare const md5: (data: string) => string; -export = md5; +export default md5; diff --git a/md5.js b/md5.js index ef33aac..77657ef 100644 --- a/md5.js +++ b/md5.js @@ -11,12 +11,11 @@ * @return {string} The hexadecimal hash string that is handled and converted. * @example * ``` - * const md5 = require('@xn-02f/md5'); - * // import md5 from '@xn-02f/md5'; + * import md5 from '@xn-02f/md5'; * md5('xn-02f'); //=> '54d30fa674d13e3598970bc9c5e2388e' * ``` */ -module.exports = data => { +export default data => { const stringUTF8 = unescape(encodeURIComponent(data)); const stringNumArr = rstr2binl(stringUTF8); diff --git a/package.json b/package.json index 981a581..a376c19 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,8 @@ "name": "@xn-02f/md5", "version": "2.4.4", "description": "A tiny MD5 library without dependencies for node, convert string to MD5 hash.", + "type": "module", + "exports": "./md5.js", "main": "md5.js", "types": "md5.d.ts", "scripts": { @@ -24,6 +26,9 @@ "ava": "5.x", "tsd": "^0.28.1" }, + "engines": { + "node": ">=16" + }, "files": [ "md5.js", "md5.d.ts" diff --git a/test/md5.test.js b/test/md5.test.js index 34b0458..7550fb7 100644 --- a/test/md5.test.js +++ b/test/md5.test.js @@ -1,9 +1,9 @@ /* * This file is used to test. */ -const test = require('ava'); +import test from 'ava'; -const md5 = require('../md5'); +import md5 from '../md5.js'; // Instance object for testing const example = {