From 4446c521f084a587556a83ad7daee542d5ced15f Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Tue, 21 Dec 2010 15:32:58 -0800 Subject: [PATCH] Bug 620593: add normalizeAccount, use in addon UI. r=philiKON --- services/sync/modules/util.js | 14 +++++++++++++- services/sync/tests/unit/test_utils_passphrase.js | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/services/sync/modules/util.js b/services/sync/modules/util.js index daf16927e168..799d06a8549b 100644 --- a/services/sync/modules/util.js +++ b/services/sync/modules/util.js @@ -1249,6 +1249,12 @@ let Utils = { return Utils.encodeKeyBase32(Utils.generateRandomBytes(16)); }, + trim: function trim(s) { + if (s) + return s.replace(/^\s+/, "").replace(/\s+$/, ""); + return s; + }, + /** * The following are the methods supported for UI use: * @@ -1264,6 +1270,8 @@ let Utils = { * take a presentable passphrase and reduce it to a normalized * representation for storage. normalizePassphrase can safely be called * on normalized input. + * * normalizeAccount: + * take user input for account/username, cleaning up appropriately. */ isPassphrase: function(s) { @@ -1310,7 +1318,7 @@ let Utils = { normalizePassphrase: function normalizePassphrase(pp) { // Short var name... have you seen the lines below?! // Allow leading and trailing whitespace. - pp = pp.toLowerCase().replace(/^\s+/, "").replace(/\s+$/, ""); + pp = Utils.trim(pp.toLowerCase()); // 20-char sync key. if (pp.length == 23 && @@ -1332,6 +1340,10 @@ let Utils = { // Something else -- just return. return pp; }, + + normalizeAccount: function normalizeAccount(acc) { + return Utils.trim(acc); + }, // WeaveCrypto returns bad base64 strings. Truncate excess padding // and decode. diff --git a/services/sync/tests/unit/test_utils_passphrase.js b/services/sync/tests/unit/test_utils_passphrase.js index a914b90bfa42..028900b7d798 100644 --- a/services/sync/tests/unit/test_utils_passphrase.js +++ b/services/sync/tests/unit/test_utils_passphrase.js @@ -82,4 +82,8 @@ function run_test() { do_check_eq(Utils.passphraseStrength("1"), 10); do_check_eq(Utils.passphraseStrength("12"), 12); do_check_eq(Utils.passphraseStrength("a1"), 12); + + _("Normalizing username."); + do_check_eq(Utils.normalizeAccount(" QA1234+boo@mozilla.com "), "QA1234+boo@mozilla.com"); + do_check_eq(Utils.normalizeAccount("QA1234+boo@mozilla.com"), "QA1234+boo@mozilla.com"); }