mirror of
https://github.com/dmuth/diceware.git
synced 2024-12-03 11:55:21 -05:00
Merge pull request #43 from dmuth/issue-42-quality-of-life-improvements
Added convertBigNumberToString(). Tweaked delays so that rolling 8 dice is less painful.
This commit is contained in:
commit
cf3cb0c37f
5 changed files with 211 additions and 2998 deletions
35
dist/bundle.js.LICENSE.txt
vendored
35
dist/bundle.js.LICENSE.txt
vendored
|
@ -1,35 +0,0 @@
|
|||
/* @preserve
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2018 Petka Antonov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*!
|
||||
* The buffer module from node.js, for the browser.
|
||||
*
|
||||
* @author Feross Aboukhadijeh <https://feross.org>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
||||
|
||||
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
3074
package-lock.json
generated
3074
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -57,7 +57,7 @@ Diceware.get_word = function(wordlist, index) {
|
|||
Diceware.display_row = function(rows, cb, in_fadein_duration, in_fadeout_delay) {
|
||||
|
||||
var fadein_duration = in_fadein_duration || 250;
|
||||
var fadeout_delay = in_fadeout_delay || 750;
|
||||
var fadeout_delay = in_fadeout_delay || 400;
|
||||
|
||||
if (rows.length) {
|
||||
//
|
||||
|
@ -89,7 +89,7 @@ Diceware.display_row = function(rows, cb, in_fadein_duration, in_fadeout_delay)
|
|||
// (I know I did when rolling 8 dice!)
|
||||
//
|
||||
fadein_duration -= 25;
|
||||
fadeout_delay -= 50;
|
||||
//fadeout_delay -= 50;
|
||||
|
||||
//
|
||||
// Now fade out the entire row, and call ourselves again
|
||||
|
@ -249,6 +249,7 @@ Diceware.rollDiceHandler = function(e) {
|
|||
|
||||
var roll = {};
|
||||
roll.dice = row;
|
||||
//console.log("Debug Dice Roll", JSON.stringify(roll.dice)); // Debugging
|
||||
roll.word = Diceware.get_word(wordlist, roll.dice.value);
|
||||
rolls.push(roll);
|
||||
passphrase.push(roll.word);
|
||||
|
@ -288,8 +289,10 @@ Diceware.rollDiceHandlerPost = function(rolls, passphrase, num_passwords) {
|
|||
// Convert the number of passwords to something based on the
|
||||
// locale and then add in <wbr> tags so they too will wrap.
|
||||
//
|
||||
num_passwords_html = num_passwords.toLocaleString("en");
|
||||
num_passwords = convertBigNumberToString(num_passwords);
|
||||
num_passwords_html = num_passwords.toLocaleString("fullwide");
|
||||
num_passwords_html = num_passwords_html.replace(/,/g, ",<wbr>");
|
||||
|
||||
jQuery(".results_num_possible_value").html(num_passwords_html);
|
||||
|
||||
var rows = new Array();
|
||||
|
|
57
src/lib.js
57
src/lib.js
|
@ -213,3 +213,60 @@ module.exports.rollDice = rollDice = function(num_dice) {
|
|||
} // End of rollDice()
|
||||
|
||||
|
||||
/**
|
||||
* Convert a big number to a string for readability.
|
||||
*/
|
||||
module.exports.convertBigNumberToString = convertBigNumberToString = function(bignum) {
|
||||
|
||||
//
|
||||
// Default to what we passed in, in case we don't get a match.
|
||||
//
|
||||
let retval = bignum
|
||||
|
||||
let bigstring = Number(bignum).toLocaleString("fullwide", {useGrouping: false});
|
||||
|
||||
let len = bigstring.length;
|
||||
|
||||
if (len >= 31) {
|
||||
let remainder = bigstring.slice(0, -30);
|
||||
retval = `${remainder} nonillion`
|
||||
|
||||
} else if (len >= 28) {
|
||||
let remainder = bigstring.slice(0, -27);
|
||||
retval = `${remainder} octillion`
|
||||
|
||||
} else if (len >= 25) {
|
||||
let remainder = bigstring.slice(0, -24);
|
||||
retval = `${remainder} septillion`
|
||||
|
||||
} else if (len >= 22) {
|
||||
let remainder = bigstring.slice(0, -21);
|
||||
retval = `${remainder} sextillion`
|
||||
|
||||
} else if (len >= 19) {
|
||||
let remainder = bigstring.slice(0, -18);
|
||||
retval = `${remainder} quintillion`
|
||||
|
||||
} else if (len >= 16) {
|
||||
let remainder = bigstring.slice(0, -15);
|
||||
retval = `${remainder} quadrillion`
|
||||
|
||||
} else if (len >= 13) {
|
||||
let remainder = bigstring.slice(0, -12);
|
||||
retval = `${remainder} trillion`
|
||||
|
||||
} else if (len >= 10) {
|
||||
let remainder = bigstring.slice(0, -9);
|
||||
retval = `${remainder} billion`
|
||||
|
||||
} else if (len >= 7) {
|
||||
let remainder = bigstring.slice(0, -6);
|
||||
retval = `${remainder} million`
|
||||
|
||||
}
|
||||
|
||||
return(retval);
|
||||
|
||||
} // End of convertBigNumberToString()
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
var assert = require('assert');
|
||||
var should = require('should');
|
||||
|
||||
var Promise = require("bluebird");
|
||||
|
@ -170,16 +169,31 @@ describe("Diceware", function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("convertBigNumberToString()", function() {
|
||||
it("Please pass", function() {
|
||||
|
||||
diceware.convertBigNumberToString(6 * Math.pow(10, 6)).should.equal("6 million");
|
||||
diceware.convertBigNumberToString(60 * Math.pow(10, 9)).should.equal("60 billion");
|
||||
diceware.convertBigNumberToString(600 * Math.pow(10, 12)).should.equal("600 trillion");
|
||||
diceware.convertBigNumberToString(1 * Math.pow(10, 15)).should.equal("1 quadrillion");
|
||||
diceware.convertBigNumberToString(123 * Math.pow(10, 18)).should.equal("123 quintillion");
|
||||
|
||||
diceware.convertBigNumberToString(6e+6).should.equal("6 million");
|
||||
diceware.convertBigNumberToString(50E+9).should.equal("50 billion");
|
||||
|
||||
diceware.convertBigNumberToString("7e+6").should.equal("7 million");
|
||||
diceware.convertBigNumberToString("51E+9").should.equal("51 billion");
|
||||
diceware.convertBigNumberToString("512E+18").should.equal("512 quintillion");
|
||||
diceware.convertBigNumberToString("513E+21").should.equal("513 sextillion");
|
||||
diceware.convertBigNumberToString("514E+24").should.equal("514 septillion");
|
||||
diceware.convertBigNumberToString("515E+27").should.equal("515 octillion");
|
||||
diceware.convertBigNumberToString("516E+30").should.equal("516 nonillion");
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
TEST/TODO: Things to refactor:
|
||||
X Diceware.getRandomValue
|
||||
X Diceware.getBase6
|
||||
X Diceware.convertBase6ToDice
|
||||
- Diceware.getNumValuesFromNumDice
|
||||
- Diceware.rollDice(1).roll.length
|
||||
*/
|
||||
|
||||
|
|
Loading…
Reference in a new issue