[Pkg-javascript-commits] [node-minimalistic-crypto-utils] 03/08: lib: be more lenient

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:24:48 UTC 2017


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

rouca pushed a commit to branch master
in repository node-minimalistic-crypto-utils.

commit db75a10f50940d599f094b8c74dcd15b5635d2e4
Author: Fedor Indutny <fedor at indutny.com>
Date:   Wed Feb 22 16:20:32 2017 -0500

    lib: be more lenient
---
 lib/utils.js       | 14 +++++++-------
 test/utils-test.js |  1 +
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/utils.js b/lib/utils.js
index f87a82e..cd48daf 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -13,7 +13,13 @@ function toArray(msg, enc) {
       res[i] = msg[i] | 0;
     return res;
   }
-  if (!enc) {
+  if (enc === 'hex') {
+    msg = msg.replace(/[^a-z0-9]+/ig, '');
+    if (msg.length % 2 !== 0)
+      msg = '0' + msg;
+    for (var i = 0; i < msg.length; i += 2)
+      res.push(parseInt(msg[i] + msg[i + 1], 16));
+  } else {
     for (var i = 0; i < msg.length; i++) {
       var c = msg.charCodeAt(i);
       var hi = c >> 8;
@@ -23,12 +29,6 @@ function toArray(msg, enc) {
       else
         res.push(lo);
     }
-  } else if (enc === 'hex') {
-    msg = msg.replace(/[^a-z0-9]+/ig, '');
-    if (msg.length % 2 !== 0)
-      msg = '0' + msg;
-    for (var i = 0; i < msg.length; i += 2)
-      res.push(parseInt(msg[i] + msg[i + 1], 16));
   }
   return res;
 }
diff --git a/test/utils-test.js b/test/utils-test.js
index bbf01ee..3da812d 100644
--- a/test/utils-test.js
+++ b/test/utils-test.js
@@ -7,6 +7,7 @@ describe('utils', () => {
   it('should convert to array', () => {
     assert.deepEqual(utils.toArray('1234', 'hex'), [ 0x12, 0x34 ]);
     assert.deepEqual(utils.toArray('1234'), [ 49, 50, 51, 52 ]);
+    assert.deepEqual(utils.toArray('1234', 'utf8'), [ 49, 50, 51, 52 ]);
     assert.deepEqual(utils.toArray('\u1234234'), [ 18, 52, 50, 51, 52 ]);
     assert.deepEqual(utils.toArray([ 1, 2, 3, 4 ]), [ 1, 2, 3, 4 ]);
   });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-minimalistic-crypto-utils.git



More information about the Pkg-javascript-commits mailing list