Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2016 20:00
Show Gist options
  • Save anonymous/fbbfc976f4eea1c10db6f0bfa1dd9fab to your computer and use it in GitHub Desktop.
Save anonymous/fbbfc976f4eea1c10db6f0bfa1dd9fab to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/tinevo
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
html, body {
margin: 15px;
font-family: 'Helvetica Neue';
font-weight: 200;
}
* {
box-sizing: border-box;
}
h1 {
font-size: 4em;
width: 100%;
max-width: 500px;
font-weight: 200;
margin: 0 auto 2rem auto;
text-align: center;
}
input {
width: 100%;
max-width: 500px;
margin-left: auto;
margin-right: auto;
display: block;
font-size: 1.5em;
padding: 5px 20px;
margin-bottom: 10px;
}
#result {
border: 0;
font-size: 2em;
text-align: center;
color: #0059FF;
}
</style>
</head>
<body>
<h1>speakCrypt</h1>
<input id="int" type="number" tabindex="0" placeholder="Integer to encrypt" />
<input id="salt" type="text" tabindex="0" placeholder="Salt" />
<input id="result" type="text" tabindex="0" disabled="disabled" />
<script id="jsbin-javascript">
$(function(){
$('#int, #salt').on('input', function () {
if ( $('#int').val() == '' ) return $('#result').val('');
var res = speakCrypt($('#int').val(), $('#salt').val());
$('#result').val(res);
});
$('#int').val(Math.ceil(Math.random() * 10000)).trigger('input');
});
function speakCrypt (int, salt, dict) {
dict = dict || [
'a,e,i,o,u,oi,ou,ai,au,on,in',
'b,c,d,f,g,j,k,l,m,n,p,r,s,t,v,w,x,y,z,bl,ch,cl,cr,dr,fl,tr,br'
];
if (!isFinite(int) || int < 0)
throw new TypeError('First argument must be a positive finite number.');
// Alternate vowel-first and consonant-first syllables
// depending on whether the int is pair or not
var mod = (int % 2 === 0) ? 0 : 1;
// Generate all syllables
var syllables = [];
dict[ mod ].split(',').forEach(function (first) {
dict[ (mod+1) % 2 ].split(',').forEach(function (second) {
syllables.push(first + second);
});
});
// If there's a salt, use it to consistently shuffle the syllables
if (salt) {
var charCode, j, i, v, p; salt += '';
for (i = syllables.length - 1, v = 0, p = 0; i > 0; i--, v++) {
v %= salt.length;
p += charCode = salt.charAt(v).charCodeAt(0);
j = (charCode + v + p) % i;
syllables.splice(j, 0, syllables.splice(i, 1)[0]);
}
}
// Find how many steps to look for
var steps = Math.ceil(Math.log(int) / Math.log(syllables.length));
if (steps < 2) steps = 1;
// Deduce syllables from steps
var result = '', steppedIndex;
for (var i = steps; i > 0; i--) {
steppedIndex = Math.floor(int / Math.pow(syllables.length, i - 1));
result += syllables[ Math.floor((steppedIndex % syllables.length) / 2) ];
}
return result;
}
</script>
<script id="jsbin-source-css" type="text/css">html, body {
margin: 15px;
font-family: 'Helvetica Neue';
font-weight: 200;
}
* {
box-sizing: border-box;
}
h1 {
font-size: 4em;
width: 100%;
max-width: 500px;
font-weight: 200;
margin: 0 auto 2rem auto;
text-align: center;
}
input {
width: 100%;
max-width: 500px;
margin-left: auto;
margin-right: auto;
display: block;
font-size: 1.5em;
padding: 5px 20px;
margin-bottom: 10px;
}
#result {
border: 0;
font-size: 2em;
text-align: center;
color: #0059FF;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">$(function(){
$('#int, #salt').on('input', function () {
if ( $('#int').val() == '' ) return $('#result').val('');
var res = speakCrypt($('#int').val(), $('#salt').val());
$('#result').val(res);
});
$('#int').val(Math.ceil(Math.random() * 10000)).trigger('input');
});
function speakCrypt (int, salt, dict) {
dict = dict || [
'a,e,i,o,u,oi,ou,ai,au,on,in',
'b,c,d,f,g,j,k,l,m,n,p,r,s,t,v,w,x,y,z,bl,ch,cl,cr,dr,fl,tr,br'
];
if (!isFinite(int) || int < 0)
throw new TypeError('First argument must be a positive finite number.');
// Alternate vowel-first and consonant-first syllables
// depending on whether the int is pair or not
var mod = (int % 2 === 0) ? 0 : 1;
// Generate all syllables
var syllables = [];
dict[ mod ].split(',').forEach(function (first) {
dict[ (mod+1) % 2 ].split(',').forEach(function (second) {
syllables.push(first + second);
});
});
// If there's a salt, use it to consistently shuffle the syllables
if (salt) {
var charCode, j, i, v, p; salt += '';
for (i = syllables.length - 1, v = 0, p = 0; i > 0; i--, v++) {
v %= salt.length;
p += charCode = salt.charAt(v).charCodeAt(0);
j = (charCode + v + p) % i;
syllables.splice(j, 0, syllables.splice(i, 1)[0]);
}
}
// Find how many steps to look for
var steps = Math.ceil(Math.log(int) / Math.log(syllables.length));
if (steps < 2) steps = 1;
// Deduce syllables from steps
var result = '', steppedIndex;
for (var i = steps; i > 0; i--) {
steppedIndex = Math.floor(int / Math.pow(syllables.length, i - 1));
result += syllables[ Math.floor((steppedIndex % syllables.length) / 2) ];
}
return result;
}</script></body>
</html>
html, body {
margin: 15px;
font-family: 'Helvetica Neue';
font-weight: 200;
}
* {
box-sizing: border-box;
}
h1 {
font-size: 4em;
width: 100%;
max-width: 500px;
font-weight: 200;
margin: 0 auto 2rem auto;
text-align: center;
}
input {
width: 100%;
max-width: 500px;
margin-left: auto;
margin-right: auto;
display: block;
font-size: 1.5em;
padding: 5px 20px;
margin-bottom: 10px;
}
#result {
border: 0;
font-size: 2em;
text-align: center;
color: #0059FF;
}
$(function(){
$('#int, #salt').on('input', function () {
if ( $('#int').val() == '' ) return $('#result').val('');
var res = speakCrypt($('#int').val(), $('#salt').val());
$('#result').val(res);
});
$('#int').val(Math.ceil(Math.random() * 10000)).trigger('input');
});
function speakCrypt (int, salt, dict) {
dict = dict || [
'a,e,i,o,u,oi,ou,ai,au,on,in',
'b,c,d,f,g,j,k,l,m,n,p,r,s,t,v,w,x,y,z,bl,ch,cl,cr,dr,fl,tr,br'
];
if (!isFinite(int) || int < 0)
throw new TypeError('First argument must be a positive finite number.');
// Alternate vowel-first and consonant-first syllables
// depending on whether the int is pair or not
var mod = (int % 2 === 0) ? 0 : 1;
// Generate all syllables
var syllables = [];
dict[ mod ].split(',').forEach(function (first) {
dict[ (mod+1) % 2 ].split(',').forEach(function (second) {
syllables.push(first + second);
});
});
// If there's a salt, use it to consistently shuffle the syllables
if (salt) {
var charCode, j, i, v, p; salt += '';
for (i = syllables.length - 1, v = 0, p = 0; i > 0; i--, v++) {
v %= salt.length;
p += charCode = salt.charAt(v).charCodeAt(0);
j = (charCode + v + p) % i;
syllables.splice(j, 0, syllables.splice(i, 1)[0]);
}
}
// Find how many steps to look for
var steps = Math.ceil(Math.log(int) / Math.log(syllables.length));
if (steps < 2) steps = 1;
// Deduce syllables from steps
var result = '', steppedIndex;
for (var i = steps; i > 0; i--) {
steppedIndex = Math.floor(int / Math.pow(syllables.length, i - 1));
result += syllables[ Math.floor((steppedIndex % syllables.length) / 2) ];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment