From 14be1dff3c06f2f0cb5f632352d8ab93087a21fa Mon Sep 17 00:00:00 2001 From: Quantum Date: Mon, 15 Jun 2020 22:16:11 -0400 Subject: [PATCH] Add separator symbol support --- src/generator.js | 16 +++++++++------- src/index.ejs | 9 +++++++-- src/ui.js | 3 ++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/generator.js b/src/generator.js index 084767f..612cf7e 100644 --- a/src/generator.js +++ b/src/generator.js @@ -1,6 +1,7 @@ const words = require('./words') const digits = '0123456789' const symbols = '`~!@#$%^&*()_+-=,./<>?;:|' +const defaultSymbol = '-' function getWordList (name) { if (['small', 'medium', 'large'].includes(name)) { @@ -41,30 +42,31 @@ function generate (options) { words = words.map(capitalize) } - if (options.symbol) { - words.push(pickChar(symbols)) - } - if (options.digit) { words.push(pickChar(digits)) } - return words.join('') + return words.join(options.symbol ? options.separator : '') } function lengthBits (list) { return Math.log2(list.length) } +function bitsForSymbol (symbol) { + return symbol === defaultSymbol ? 1 : lengthBits(symbols) +} + function computeBits (options) { const wordBits = lengthBits(getWordList(options.list)) const capsBits = options.capitalize ? 1 : 0 - const symbolBits = options.symbol ? lengthBits(symbols) : 0 + const symbolBits = options.symbol ? bitsForSymbol(options.separator) : 0 const digitBits = options.digit ? lengthBits(digits) : 0 return wordBits * options.count + capsBits + symbolBits + digitBits } module.exports = { - getWordList, getWords, capitalize, generate, getChar, lengthBits, computeBits + getWordList, getWords, capitalize, generate, getChar, lengthBits, computeBits, + symbols, defaultSymbol, } diff --git a/src/index.ejs b/src/index.ejs index 7de99e5..54a6ca4 100644 --- a/src/index.ejs +++ b/src/index.ejs @@ -81,8 +81,13 @@
- - + + +
diff --git a/src/ui.js b/src/ui.js index 6e887d2..4223f07 100644 --- a/src/ui.js +++ b/src/ui.js @@ -1,7 +1,7 @@ import $ from 'jquery/dist/jquery' import 'form-serializer/jquery.serialize-object' -import { generate, computeBits } from './generator' +import { generate, computeBits, defaultSymbol } from './generator' $(() => { const $options = $('#options-form') @@ -49,6 +49,7 @@ $(() => { $options.find(':checkbox').each(function () { $(this).prop('checked', !!settings[this.name]) }) + $('#separator-symbol').val(settings.separator || defaultSymbol) updateBitMeter() }