Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
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 ourterms of service andprivacy 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 merge1 commit intounshiftio:master
base:master
Choose a base branch
Loading
fromdarrachequesne:feat/esm

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 commentedOct 3, 2021
edited
Loading

This is a breaking change because:

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

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

diff --git a/README.md b/README.mdindex 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 idsdiff --git a/index.js b/index.jsindex 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.jsonindex 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.jsindex 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.mjsnew file mode 100644index 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

By the way, I'm not even sure it is worth the effort.yeast() depends onencode() anddecode() is 157 bytes unminified.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@darrachequesne@lpinca

[8]ページ先頭

©2009-2025 Movatter.jp