This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (138 loc) · 3.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
import resolveImplicitTag from '@emmetio/implicit-tag';
import latin from './lang/latin.json';
import ru from './lang/russian.json';
import sp from './lang/spanish.json';
const langs = { latin, ru, sp };
const defaultOptions = {
wordCount: 30,
skipCommon: false,
lang: 'latin'
};
/**
* Replaces given parsed Emmet abbreviation node with nodes filled with
* Lorem Ipsum stub text.
* @param {Node} node
* @return {Node}
*/
export default function(node, options) {
options = Object.assign({}, defaultOptions, options);
const dict = langs[options.lang] || langs.latin;
const startWithCommon = !options.skipCommon && !isRepeating(node);
if (!node.repeat && !isRoot(node.parent)) {
// non-repeating element, insert text stub as a content of parent node
// and remove current one
node.parent.value = paragraph(dict, options.wordCount, startWithCommon);
node.remove();
} else {
// Replace named node with generated content
node.value = paragraph(dict, options.wordCount, startWithCommon);
node.name = node.parent.name ? resolveImplicitTag(node.parent.name) : null;
}
return node;
}
function isRoot(node) {
return !node.parent;
}
/**
* Returns random integer between <code>from</code> and <code>to</code> values
* @param {Number} from
* @param {Number} to
* @returns {Number}
*/
function rand(from, to) {
return Math.floor(Math.random() * (to - from) + from);
}
/**
* @param {Array} arr
* @param {Number} count
* @returns {Array}
*/
function sample(arr, count) {
const len = arr.length;
const iterations = Math.min(len, count);
const result = new Set();
while (result.size < iterations) {
result.add(arr[rand(0, len)]);
}
return Array.from(result);
}
function choice(val) {
return val[rand(0, val.length - 1)];
}
function sentence(words, end) {
if (words.length) {
words = [capitalize(words[0])].concat(words.slice(1));
}
return words.join(' ') + (end || choice('?!...')); // more dots than question marks
}
function capitalize(word) {
return word[0].toUpperCase() + word.slice(1);
}
/**
* Insert commas at randomly selected words. This function modifies values
* inside <code>words</code> array
* @param {Array} words
*/
function insertCommas(words) {
if (words.length < 2) {
return words;
}
words = words.slice();
const len = words.length;
const hasComma = /,$/;
let totalCommas = 0;
if (len > 3 && len <= 6) {
totalCommas = rand(0, 1);
} else if (len > 6 && len <= 12) {
totalCommas = rand(0, 2);
} else {
totalCommas = rand(1, 4);
}
for (let i = 0, pos, word; i < totalCommas; i++) {
pos = rand(0, len - 2);
if (!hasComma.test(words[pos])) {
words[pos] += ',';
}
}
return words;
}
/**
* Generate a paragraph of "Lorem ipsum" text
* @param {Object} dict Words dictionary (see `lang/*.json`)
* @param {Number} wordCount Words count in paragraph
* @param {Boolean} startWithCommon Should paragraph start with common
* "lorem ipsum" sentence.
* @returns {String}
*/
function paragraph(dict, wordCount, startWithCommon) {
const result = [];
let totalWords = 0;
let words;
if (startWithCommon && dict.common) {
words = dict.common.slice(0, wordCount);
totalWords += words.length;
result.push(sentence(insertCommas(words), '.'));
}
while (totalWords < wordCount) {
words = sample(dict.words, Math.min(rand(2, 30), wordCount - totalWords));
totalWords += words.length;
result.push(sentence(insertCommas(words)));
}
return result.join(' ');
}
/**
* Check if given node is in repeating context, e.g. node itself or one of its
* parent is repeated
* @param {Node} node
* @return {Boolean}
*/
function isRepeating(node) {
while (node.parent) {
if (node.repeat && node.repeat.value && node.repeat.value > 1) {
return true;
}
node = node.parent;
}
return false;
}