Skip to content

Instantly share code, notes, and snippets.

@lloss
Created April 16, 2017 21:02
Show Gist options
  • Save lloss/d8a4a040df38c966824f4c9d9346b99b to your computer and use it in GitHub Desktop.
Save lloss/d8a4a040df38c966824f4c9d9346b99b to your computer and use it in GitHub Desktop.
random
import _ from 'lodash';
const FIRST_POOL = 'firstPool';
const SECOND_POOL = 'secondPool';
const THIRD_POOL = 'thirdPool';
const FOURTH_POOL = 'fourthPool';
const FIFTH_POOL = 'readySentences';
const INSULTS_LINK = 'https://api.myjson.com/bins/pxhnd';
const isJsonLoaded = {loaded: false, json: {}};
//fetch first time, write to object then use it from there
const parseJson = () => {
return new Promise(resolve => {
if (isJsonLoaded.loaded === false) {
fetch(INSULTS_LINK)
.then(respond => {
const parsedJson = respond.json();
isJsonLoaded.loaded = true;
isJsonLoaded.json = parsedJson;
resolve(parsedJson);
})
.catch(err => console.log(err));
} else {
resolve(isJsonLoaded.json);
}
});
};
const getRandomInsultFromPool = (collection, pool) => {
const collectionPool = collection[pool];
const usedInSentence = {
[FIRST_POOL]: [],
[SECOND_POOL]: [],
[THIRD_POOL]: [],
[FOURTH_POOL]: [],
[FIFTH_POOL]: [],
};
let word;
let used = true;
while (used) {
word = _.sample(collectionPool);
if (!_.includes(usedInSentence[pool], word)) {
used = false;
usedInSentence[pool] = [...usedInSentence[pool], word];
}
}
return word;
};
const generateRandomTemplateString = collection => {
//transform long name to short abb
const _use = pool => getRandomInsultFromPool(collection, pool);
return _.sample(
[
[
`Thou ${_use(FIRST_POOL,)}, ${_use(FIRST_POOL,)}, ${_use(FIRST_POOL,)},${_use(FIRST_POOL,)}`,
],
[`Thou art a ${_use(FIRST_POOL)}`],
[`${_use(FIRST_POOL)} and ${_use(FIRST_POOL)}`],
[
`Thou${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}, thou ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}, thou ${_use(SECOND_POOL,)},${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}`,
],
[
`Thou art ${_use(SECOND_POOL,)}, ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}!`,
],
[
`${_use(FOURTH_POOL,)} you ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)},you ${_use(FIRST_POOL,)}, you ${_use(SECOND_POOL,)}, ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}!`,
`${_use(FOURTH_POOL)} ${_use(SECOND_POOL)} ${_use(FIRST_POOL)}`,
],
[`${_use(THIRD_POOL)}, how art thou ${_use(SECOND_POOL)}`],
[
`${_use(THIRD_POOL,)} thou art ${_use(FIRST_POOL,)}, thou ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}`,
],
`thou ${_use(SECOND_POOL)} one!`,
],
[`${_use(FIRST_POOL)} ${_use(SECOND_POOL)} ${_use(FIRST_POOL)}!`],
[
`This ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}, this ${_use(FIRST_POOL,)}, this ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}!`,
],
[
`A ${_use(FIRST_POOL,)}, ${_use(FIRST_POOL,)},${_use(FIRST_POOL,)} and a ${_use(SECOND_POOL,)},${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}`,
],
[
`You ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}!You ${_use(SECOND_POOL,)} ${_use(SECOND_POOL,)} ${_use(FIRST_POOL,)}!`,
],
);
};
const getAndMix = (collection, func, max) => {
let arr = [];
_.times(max, () => {
arr = [...arr, [getRandomInsultFromPool(collection, FIFTH_POOL)]];
});
_.times(max * 2, () => {
arr = [...arr, func(collection)];
});
let secondArr = [];
_.times(max, () => {
const stringFromRow = _.sample(arr)[0];
secondArr = [
...secondArr,
stringFromRow.replace(/(\r\n|\n|\r)/gm, '').trim(),
];
});
//TODO fix letter T spawn in a better way
secondArr = secondArr.filter(item => item !== 't');
return _.shuffle(_.uniq(secondArr));
};
const spawnSentence = (max = 10) => {
return new Promise((resolve, reject) => {
parseJson()
.then(parsedJson =>
getAndMix(parsedJson, generateRandomTemplateString, max))
.then(generatedArray => resolve(generatedArray))
.catch(err => reject(err));
});
};
export default spawnSentence;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment