Skip to content

Instantly share code, notes, and snippets.

@Lagaz
Created January 1, 2019 16:45
Show Gist options
  • Select an option

  • Save Lagaz/40609dac964241917cf21801227d7dee to your computer and use it in GitHub Desktop.

Select an option

Save Lagaz/40609dac964241917cf21801227d7dee to your computer and use it in GitHub Desktop.
Test Lorem Ipsum Generator
.lorem
.controls
p.paragraphs
button.add Add
button.remove Remove
label #[input(type="text", value="1")] Paragraphs
p.sentences
button.add(title="(to the last paragraph)") Add
button.remove(title="(from the last paragraph)") Remove
label #[input(type="text", value="1")] Sentences
p.words
button.add(title="(to the last sentence in the last paragraph)") Add
button.remove(title="(from the last sentence in the last paragraph)") Remove
label #[input(type="text", value="4")] Words
(function (d) {
// IIFE parameter expectations:
// * document to be passed in as the value for d.
//
var
// get reference to the lorem ipsum display div
lorem = d.querySelector('.lorem'),
// get the reference to the paragraph controls div
// and the paragraph control's quantity input.
p = d.querySelector('.paragraphs'),
pInput = p.querySelector('input'),
// and the reference to the sentence controls div
// and the sentence control's quantity input.
s = d.querySelector('.sentences'),
sInput = s.querySelector('input'),
// and the reference to the sentence controls div
// and the sentence control's quantity input.
w = d.querySelector('.words'),
wInput = w.querySelector('input');
p.addEventListener('click', function (event) {
if (event.target.classList.contains('add')) {
var paragraphs = HolderIpsum.paragraphs(pInput.value, true);
paragraphs.forEach(function (paragraph) {
lorem.insertAdjacentHTML('beforeend', `<p>${paragraph}</p>`);
});
} else if (event.target.classList.contains('remove')) {
var i;
for (i = pInput.value; i > 0; i--) {
if (lorem.children.length > 0) {
lorem.removeChild(lorem.children[lorem.children.length - 1]);
}
}
}
});
s.addEventListener('click', function (event) {
if (event.target.classList.contains('add')) {
var lastP = lorem.querySelector('p:last-child'),
sentences = [], i;
for (i = 0; i < sInput.value; i++) {
sentences.push(HolderIpsum.sentence());
}
lastP.innerText += ' ' + sentences.join(' ');
} else if (event.target.classList.contains('remove')) {
var lastP = lorem.querySelector('p:last-child'),
sentences, sentenceKeepCount;
// Split the paragraph into an array with 2 elements
// per sentence. For each sentence, the first element is
// the sentence content. The second is the punctuation
// followed by any spaces.
// Except for the last sentence, which is not followed by
// a space and is therefore a single element in the array.
sentences = lastP.innerText.trim().split(/([.?!] +)/);
// To get the paragraph's sentence-count, we add 1 to the
// array's element count (because unlike the other sentences,
// the final sentence exists as only a single element) and halve it.
// The sentenceKeepCount is the sentence-count minus the number of
// sentences we're told to remove.
var sentenceKeepCount = (sentences.length + 1) / 2 - sInput.value;
// slice out the first N sentences (which is N x 2 array elements),
// join them together, trim, and replace the paragraph
// text with the result.
lastP.innerText = sentences.slice(0, sentenceKeepCount * 2).join('').trim();
}
});
w.addEventListener('click', function (event) {
if (event.target.classList.contains('add')) {
var lastP = lorem.querySelector('p:last-child'),
pText = lastP.innerText.trim(),
lastPunctuation = pText.substr(-1),
everythingElse = pText.slice(0, -1).trim(),
newWords = HolderIpsum.words(wInput.value, true).trim();
// insert the words before the final punctuation of
// the last sentence of the last paragraph.
lastP.innerText = everythingElse + ' ' + newWords + lastPunctuation;
} else if (event.target.classList.contains('remove')) {
var lastP = lorem.querySelector('p:last-child'),
pText = lastP.innerText.trim(),
// we assume the last character of the trimmed
// paragraph text is punctuation. No real loss if
// we're wrong, since at least we're not going to
// make the paragraph worse that it already is.
lastPunctuation = pText.substr(-1),
// everything up to, but not including
// that last punctuation character.
everythingElse = pText.slice(0, -1).trim(),
// the input value tells us how many words to remove.
// TODO - we should get out early if this value is
// not a useful integer.
// TODO - edge cases, e.g. when wInput.value is
// greater than the number of words in the
// paragraph.??
numWordsToRemove = wInput.value;
pText = everythingElse
// split the text on whitespace only, not
// other word boundaries.
.split(/\s+/)
// remove N words as requested
.slice(0, -numWordsToRemove)
// join it back together with spaces
.join(' ')
// get rid of any trailing punctuation at the end of pText
// which could be any sentence-ending or inner-sentence
// punctuation that happened to be at the boundary of
// the words we removed.
.replace(/[^a-zA-Z]*$/, '');
// replace the content of the last paragraph with
// our modified text.
lastP.innerText = pText + lastPunctuation;
}
});
}(document));
<script src="https://rawgit.com/mattymaloney/holder-ipsum/master/holder-ipsum.js"></script>
body {
}
.lorem {
background: tan;
padding: 1em;
margin: 1em;
}
.controls {
background: forestgreen;
padding: 1em;
margin: 1em;
input[type="text"] {
width: 2em;
box-sizing: content-box;
text-align: center;
}
}
<link href="https://rawgit.com/mattymaloney/css-normalizer/master/normalize.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment