forked from Daniel-Hug/bible-people
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
77 lines (66 loc) · 2 KB
/
helpers.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
/*
The functions in here have no purpose specific to this app.
*/
// removes all of an element's childNodes
function removeChilds(el) {
var last;
while ((last = el.lastChild)) el.removeChild(last);
}
var verbalize = {
// Accepts a singular noun:
// getPlural('box')
// Returns the noun in plural form:
// "boxes"
// false positives: "ox", "potato", "goose", "deer", etc.
getPlural: function getPlural(noun) {
var lastChar = noun.slice(-1);
var last2;
// if ends with 's', 'x', 'z', 'ch', or 'sh', add 'es'
if ('sxz'.indexOf(lastChar) >= 0 ||
['ch', 'sh'].indexOf(last2 = noun.slice(-2)) >= 0) return noun + 'es';
// else, if ends with 'y', replace with 'ies'
if (lastChar === 'y') return noun.slice(0, -1) + 'ies';
// else, if ends with 'fe' or 'lf', replace f+ with 'ves'
if (['fe', 'lf'].indexOf(last2) >= 0) {
var fi = noun.lastIndexOf('f');
return noun.slice(0, fi) + 'ves';
}
// else, if ends with 'man' replace with 'men'
if (noun.slice(-3) === 'man') return noun.slice(0, -3) + 'men';
// else add 's'
return noun + 's';
},
givePossession: function givePossession(nounStr) {
return nounStr + (nounStr[nounStr.length - 1] === 's' ? '\'' : '\'s');
}
};
// val can be a function that returns a value or a value
function fillArray(len, val) {
var arr = [], i;
if (typeof val === 'function') {
for (i = 0; i < len; i++) {
arr.push(val(i));
}
} else {
for (i = 0; i < len; i++) {
arr.push(val);
}
}
return arr;
}
// Zip arrays into one
// Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
function zipArrays() {
var zipped = [];
var arrays = [].slice.call(arguments);
for (var valueI = 0; arrays.length > 0; valueI++) {
for (var arrayI = 0; arrayI < arrays.length; arrayI++) {
if (arrays[arrayI].length - 1 < valueI) {
arrays.splice(arrayI, 1);
continue;
}
zipped.push(arrays[arrayI][valueI]);
}
}
return zipped;
}