Add distribution to browser test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent b506b39 · Browse files · 2 files·+111 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+111 added−3 removed
- tests:+111 added−3 removed
Provenance
- Commit
a95fe43- Tree
4424bd0- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
b506b39
2 files changed+111 added−3 removed
modified:test/demo/index.html+23 added-2 removed
5 unmodified lines678910111011121314151617181920212223242526272829303132333435363738395 unmodified lines <style> body { font-family: monospace; text-align: center; margin: 3em 2em; font-size: 200%; margin: 3rem 2rem; } main { font-size: 120%; line-height: 1.5; padding-bottom: 3rem; } section { width: 320px; margin: 0 auto; } section + section { padding-top: 0.7rem; } h2 { font-size: 90%; font-weight: normal; } .dot { height: 1.5rem; line-height: 1.5rem; color: rgba(0, 0, 0, 0.5); display: inline-block; font-size: 70%; } </style> </head> <body> </body></html>modified:test/demo/index.js+88 added-1 removed
123456787910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394var generate = require('../../generate')var random = require('../../random')var nanoid = require('../../')
var html = ''for (var i = 0; i < 10; i++) { html += '<div>' + nanoid() + '</div>'}document.body.innerHTML = htmldocument.body.innerHTML = '<main>' + html + '</main>'
var COUNT = 50 * 1000var STEP = 5000var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'var LENGTH = ALPHABET.length
function print (title, chars) { var length = Object.keys(chars).length var width = 100 / length
var dots = '' for (var l in chars) { var distribution = (chars[l] * length) / (COUNT * LENGTH) dots += '<div class="dot" style="' + 'background: hsl(' + (200 * distribution) + ', 100%, 50%); ' + 'width: ' + width + '%; ' + '">' + l + '</div>' }
document.body.innerHTML += '<section>' + '<h2>' + title + '</h2>' + dots + '</section>'}
function printDistribution (title, fn) { var steps = COUNT var chars = { }
return new Promise(function (resolve) { function step () { if (steps === 0) { print(title, chars) resolve() return }
setTimeout(function () { for (var j = 0; j < STEP; j++) { var id = fn() for (var k = 0; k < id.length; k++) { var char = id[k] if (!chars[char]) chars[char] = 0 chars[char] += 1 } } steps -= STEP step() }) } step() })}
var tasks = [ function () { return printDistribution('ideal', function () { return Array(LENGTH).fill().map(function (_, index) { return ALPHABET[index] }) }) }, function () { return printDistribution('nanoid', function () { return generate(ALPHABET, LENGTH) }) }, function () { return printDistribution('random % alphabet', function () { return [].slice.call(random(LENGTH)).map(function (num) { return ALPHABET[num % ALPHABET.length] }) }) }]
function run () { if (tasks.length === 0) return var task = tasks.shift() task().then(function () { run() })}
run()