-
Notifications
You must be signed in to change notification settings - Fork 10
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
darrachequesne
wants to merge
1
commit into
unshiftio:master
Choose a base branch
from
darrachequesne:feat/esm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This change allows bundlers to apply tree shaking to the library.
This is a breaking change because:
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; |
By the way, I'm not even sure it is worth the effort. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change allows bundlers to apply tree shaking to the library.