Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add an ESM version #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

darrachequesne
Copy link

This change allows bundlers to apply tree shaking to the library.

This change allows bundlers to apply tree shaking to the library.
@lpinca
Copy link
Member

lpinca commented Oct 3, 2021

This is a breaking change because:

  1. It exposes a different API. There is no yeast.encode() and yeast.decode().
  2. It uses conditional exports. require('package.json') no longer works.

Considering that, wouldn't it be better to use an ES module wrapper instead of duplicating everything?

diff --git a/README.md b/README.md
index 0b0af0d..c066e8d 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ All the examples assume that this library is initialized as follow:
 ```js
 'use strict';
 
-var yeast = require('yeast');
+const { encode, decode, yeast } = require('yeast');
 ```
 
 To generate an id just call the `yeast` function.
@@ -54,16 +54,16 @@ setTimeout(function () {
 });
 ```
 
-### yeast.encode(num)
+### encode(num)
 
 An helper function that returns a string representing the specified number. The
 returned string contains only URL safe characters.
 
 ```js
-yeast.encode(+new Date()); // outputs: Kyxjuo1
+encode(+new Date()); // outputs: Kyxjuo1
 ```
 
-### yeast.decode(str)
+### decode(str)
 
 An helper function that returns the integer value specified by the given string.
 This function can be used to retrieve the timestamp from a `yeast` id.
@@ -71,7 +71,7 @@ This function can be used to retrieve the timestamp from a `yeast` id.
 ```js
 var id = yeast(); // holds the value: Kyxl1OU
 
-yeast.decode(id); // outputs: 1439816226334
+decode(id); // outputs: 1439816226334
 ```
 
 That's all folks. If you have ideas on how we can further compress the ids
diff --git a/index.js b/index.js
index 7299762..1806ad0 100644
--- a/index.js
+++ b/index.js
@@ -63,6 +63,8 @@ for (; i < length; i++) map[alphabet[i]] = i;
 //
 // Expose the `yeast`, `encode` and `decode` functions.
 //
-yeast.encode = encode;
-yeast.decode = decode;
-module.exports = yeast;
+module.exports = {
+  encode: encode,
+  decode: decode,
+  yeast: yeast
+};
diff --git a/package.json b/package.json
index e17cb1e..26a75b0 100644
--- a/package.json
+++ b/package.json
@@ -3,6 +3,10 @@
   "version": "0.1.2",
   "description": "Tiny but linear growing unique id generator",
   "main": "index.js",
+  "exports": {
+    "import": "./wrapper.mjs",
+    "require": "./index.js"
+  },
   "scripts": {
     "100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100",
     "test-node": "istanbul cover _mocha --report lcovonly -- test.js",
diff --git a/test.js b/test.js
index f5e11f1..7da7ada 100644
--- a/test.js
+++ b/test.js
@@ -2,8 +2,13 @@
 describe('yeast', function () {
   'use strict';
 
-  var assume = require('assume')
-    , yeast = require('./');
+  var assume = require('assume');
+  var yeast = require('./');
+
+  var encode = yeast.encode;
+  var decode = yeast.decode;
+
+  yeast = yeast.yeast;
 
   function waitUntilNextMillisecond() {
     var now = +new Date();
@@ -15,8 +20,8 @@ describe('yeast', function () {
   });
 
   it('exposes the helper functions', function () {
-    assume(yeast.encode).is.a('function');
-    assume(yeast.decode).is.a('function');
+    assume(encode).is.a('function');
+    assume(decode).is.a('function');
   });
 
   it('returns strings', function () {
@@ -71,7 +76,7 @@ describe('yeast', function () {
     var now = +new Date()
       , id = yeast();
 
-    assume(yeast.encode(now)).equals(id);
-    assume(yeast.decode(id)).equals(now);
+    assume(encode(now)).equals(id);
+    assume(decode(id)).equals(now);
   });
 });
diff --git a/wrapper.mjs b/wrapper.mjs
new file mode 100644
index 0000000..eaae47a
--- /dev/null
+++ b/wrapper.mjs
@@ -0,0 +1,4 @@
+import { encode, decode, yeast } from 'index.js';
+
+export { encode, decode, yeast };
+export default yeast;

@lpinca
Copy link
Member

lpinca commented Oct 3, 2021

By the way, I'm not even sure it is worth the effort. yeast() depends on encode() and decode() is 157 bytes unminified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants