[Pkg-javascript-commits] [node-deps-sort] 24/79: passing dedupe test

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:53 UTC 2017


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

rouca pushed a commit to branch master
in repository node-deps-sort.

commit 5dc0c3dca17be63c3f310ad22b997db628744858
Author: James Halliday <mail at substack.net>
Date:   Mon Jul 21 07:00:12 2014 -0700

    passing dedupe test
---
 index.js       | 19 ++++++++++++++++++-
 package.json   |  1 +
 test/dedupe.js |  7 +++++--
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index f5fa01e..cf26e77 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
 var through = require('through2');
+var shasum = require('shasum');
 var isarray = require('isarray');
 
 module.exports = function (opts) {
@@ -45,7 +46,23 @@ module.exports = function (opts) {
             });
         }
         else {
-            rows.forEach(function (row) {
+            var dedupeIndex = 0, hashes = {}, hmap = {};
+            rows.forEach(function (row, ix) {
+                if (opts.dedupe) {
+                    var h = shasum(row.source);
+                    if (hashes[h] === true) {
+                        hashes[h] = ++ dedupeIndex;
+                        rows[hmap[h]].dedupe = hashes[h];
+                        row.dedupe = hashes[h];
+                    }
+                    else if (hashes[h]) {
+                        row.dedupe = hashes[h];
+                    }
+                    else {
+                        hashes[h] = true;
+                        hmap[h] = ix;
+                    }
+                }
                 tr.push(row);
             });
         }
diff --git a/package.json b/package.json
index a4ff4f8..94504df 100644
--- a/package.json
+++ b/package.json
@@ -10,6 +10,7 @@
     "JSONStream": "~0.8.4",
     "isarray": "0.0.1",
     "minimist": "~0.2.0",
+    "shasum": "^1.0.0",
     "through2": "~0.5.1"
   },
   "devDependencies": {
diff --git a/test/dedupe.js b/test/dedupe.js
index 5e02f82..7d68906 100644
--- a/test/dedupe.js
+++ b/test/dedupe.js
@@ -11,14 +11,17 @@ test('dedupe', function (t) {
         t.deepEqual(rows, [
             { id: '/bar.js', deps: {}, source: 'TWO', dedupe: 1 },
             { id: '/foo.js', deps: {}, source: 'TWO', dedupe: 1 },
-            { id: '/main.js', deps: { './foo': '/foo.js' }, source: 'ONE' }
+            { id: '/main.js', deps: {
+                './foo': '/foo.js',
+                './bar': '/bar.js'
+            }, source: 'ONE' }
         ]);
     }
     s.pipe(through.obj(write, end));
     
     s.write({
         id: '/main.js',
-        deps: { './foo': '/foo.js', './bar.js': '/bar.js' },
+        deps: { './foo': '/foo.js', './bar': '/bar.js' },
         source: 'ONE'
     });
     s.write({

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



More information about the Pkg-javascript-commits mailing list