Add separator symbol support (#14)

This commit is contained in:
Guanzhong Chen 2020-06-15 22:29:41 -04:00 committed by GitHub
parent 706cd51ed7
commit 3fcf6e5327
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 12 deletions

View file

@ -1,6 +1,7 @@
const words = require('./words') const words = require('./words')
const digits = '0123456789' const digits = '0123456789'
const symbols = '`~!@#$%^&*()_+-=,./<>?;:|' const symbols = '`~!@#$%^&*()_+-=,./<>?;:|[]{} '
const defaultSymbol = '-'
function getWordList (name) { function getWordList (name) {
if (['small', 'medium', 'large'].includes(name)) { if (['small', 'medium', 'large'].includes(name)) {
@ -41,30 +42,31 @@ function generate (options) {
words = words.map(capitalize) words = words.map(capitalize)
} }
if (options.symbol) {
words.push(pickChar(symbols))
}
if (options.digit) { if (options.digit) {
words.push(pickChar(digits)) words.push(pickChar(digits))
} }
return words.join('') return words.join(options.symbol ? options.separator : '')
} }
function lengthBits (list) { function lengthBits (list) {
return Math.log2(list.length) return Math.log2(list.length)
} }
function bitsForSymbol (symbol) {
return symbol === defaultSymbol ? 1 : lengthBits(symbols)
}
function computeBits (options) { function computeBits (options) {
const wordBits = lengthBits(getWordList(options.list)) const wordBits = lengthBits(getWordList(options.list))
const capsBits = options.capitalize ? 1 : 0 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 const digitBits = options.digit ? lengthBits(digits) : 0
return wordBits * options.count + capsBits + symbolBits + digitBits return wordBits * options.count + capsBits + symbolBits + digitBits
} }
module.exports = { module.exports = {
getWordList, getWords, capitalize, generate, getChar, lengthBits, computeBits getWordList, getWords, capitalize, generate, getChar, lengthBits, computeBits,
symbols, defaultSymbol,
} }

View file

@ -81,8 +81,14 @@
<label for="use-capitalize">Capitalize the first letter</label> <label for="use-capitalize">Capitalize the first letter</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" id="use-symbol" name="symbol"> <input class="form-check-input" type="checkbox" id="use-symbol" name="symbol" checked>
<label for="use-symbol">Add a symbol</label> <label for="use-symbol">Use separator symbol:</label>
<select id="separator-symbol" name="separator">
<% [...require('./generator').symbols].forEach(symbol => { %>
<option<% if (symbol === require('./generator').defaultSymbol) { %> selected<% }
else if (symbol === ' ') { %> value=" "<% } %>><%= _.escape(symbol) %></option>
<% }) %>
</select>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" id="use-digit" name="digit"> <input class="form-check-input" type="checkbox" id="use-digit" name="digit">

View file

@ -1,7 +1,7 @@
import $ from 'jquery/dist/jquery' import $ from 'jquery/dist/jquery'
import 'form-serializer/jquery.serialize-object' import 'form-serializer/jquery.serialize-object'
import { generate, computeBits } from './generator' import { generate, computeBits, defaultSymbol } from './generator'
$(() => { $(() => {
const $options = $('#options-form') const $options = $('#options-form')
@ -9,8 +9,10 @@ $(() => {
const $bits = $('#password-bits').find('div') const $bits = $('#password-bits').find('div')
const classes = 'bg-danger bg-warning bg-info bg-success' const classes = 'bg-danger bg-warning bg-info bg-success'
const defaults = { const defaults = {
list: 'small', list: 'large',
count: 4, count: 4,
symbol: true,
separator: defaultSymbol,
} }
function bitClass (bits) { function bitClass (bits) {
@ -49,6 +51,7 @@ $(() => {
$options.find(':checkbox').each(function () { $options.find(':checkbox').each(function () {
$(this).prop('checked', !!settings[this.name]) $(this).prop('checked', !!settings[this.name])
}) })
$('#separator-symbol').val(settings.separator || defaultSymbol)
updateBitMeter() updateBitMeter()
} }