mirror of
https://github.com/quantum5/correcthorsebatterystaple.git
synced 2025-04-24 10:11:57 -04:00
Add separator symbol support
This commit is contained in:
parent
706cd51ed7
commit
14be1dff3c
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,8 +81,13 @@
|
||||||
<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<% } %>><%= 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">
|
||||||
|
|
|
@ -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')
|
||||||
|
@ -49,6 +49,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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue