Skip to content

Instantly share code, notes, and snippets.

@YodaLightsabr
Last active Sep 24, 2022
Embed
What would you like to do?
Calculate how true something is
const crypto = require('crypto');
const truths = [
'so true',
'undeniably true',
'This response is undeniably true;',
'So true!',
'This is true;',
'true',
'false',
'Untrue'
];
function sentenceHash (sentence) {
const symbols = sentence
.trim()
.toLowerCase()
.split('')
.sort()
.filter(char => 'abcdefghijklmnopqrstuvwxyz123456789'.split('').includes(char))
.join('');
return crypto.createHash('md5').update(symbols + process.env.HOWTRUE_SALT).digest('hex');
}
function howTrue (text) {
return truths[parseInt(sentenceHash(text).substring(0, 5), 16) % truths.length];
}
module.exports = async function (req, res, [ text ]) {
res.header('Hash-Output', sentenceHash(text));
res.send(howTrue(text));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment