[Pkg-javascript-commits] [node-acorn-jsx] 389/484: Made tokenize() compliant with ES6 iterables for easier processing.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:21:00 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-acorn-jsx.

commit ad9411d2aec4ac37bf441bc452a85e3217b6a05d
Author: Ingvar Stepanyan <me at rreverser.com>
Date:   Wed Jan 14 12:27:58 2015 +0200

    Made tokenize() compliant with ES6 iterables for easier processing.
---
 README.md | 11 +++++++++++
 acorn.js  | 13 +++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/README.md b/README.md
index 5f790a8..7714e87 100644
--- a/README.md
+++ b/README.md
@@ -162,6 +162,17 @@ token, and returns a `{start, end, type, value}` object (with added
 `loc` property when the `locations` option is enabled and `range`
 property when the `ranges` option is enabled).
 
+In ES6 environment, returned result can be used as any other protocol-compliant iterable:
+
+```javascript
+for (let token of acorn.tokenize(str)) {
+  // iterate over the tokens
+}
+
+// transform code to array of tokens:
+var tokens = [...acorn.tokenize(str)];
+```
+
 **tokTypes** holds an object mapping names to the token type objects
 that end up in the `type` properties of tokens.
 
diff --git a/acorn.js b/acorn.js
index 06134a0..f3a9d61 100644
--- a/acorn.js
+++ b/acorn.js
@@ -243,6 +243,19 @@
       skipSpace();
     };
     getToken.current = function() { return new Token(); };
+    if (typeof Symbol !== 'undefined') {
+      getToken[Symbol.iterator] = function () {
+        return {
+          next: function () {
+            var token = getToken();
+            return {
+              done: token.type === _eof,
+              value: token
+            };
+          }
+        };
+      };
+    }
     getToken.options = options;
     return getToken;
   };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git



More information about the Pkg-javascript-commits mailing list