This repository was archived by the owner on Sep 17, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy patharraywriter.js
More file actions
159 lines (140 loc) · 6.83 KB
/
Copy patharraywriter.js
File metadata and controls
159 lines (140 loc) · 6.83 KB
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
/**
* Class that writes the contents of an array to a webpage between specified tags.
* @author Michael Van Leeuwen <michaeljvanleeuwen@gmail.com>
*/
class ArrayWriter {
/**
* Creates an instance of ArrayPrinter.
* @constructor
* @param {Array} list The desired array to fill the ArrayPrinter with.
*/
constructor(list) {
this._list = list;
}
/**
* Getter
* @returns {Array} The current array of this instance of ArrayWriter.
*/
get list() {
return this._list;
}
/**
* Setter
* @param {Array} list The desired array to fill the ArrayPrinter with.
*/
set list(list) {
this._list = list;
}
/**
* Writes the contents of _list to the document between the entered tags.
* @param {String} startTag The desired start tag to write.
* @param {String} endTag The desired end tag to write.
*/
writeArray(startTag, endTag) {
this._list.forEach(function(element) {
document.write(startTag + element + endTag);
});
}
/** Writes the contents of _list to the document in paragraph tags. */
writeArrayP() {
this.writeArray('<p>','</p>')
}
/**
* Writes the contents of _list to the document in paragraph tags that are assigned to the specified class.
* @param {String} eClass The desired class to assign to all elements.
*/
writeArrayClassP(eClass) {
this.writeArray('<p class=' + eClass + '>','</p>')
}
/**
* Writes the contents of _list to the document in header tags at a specified level.
* @param {Number} headerLevel The desired header level to write (1-6).
*/
writeArrayH(headerLevel) {
this.writeArray('<h' + String(headerLevel) + '>','</h' + String(headerLevel) + '>')
}
/**
* Writes the contents of _list to the document in header tags at a specified level that are assigned to the specified class.
* @param {Number} headerLevel The desired header level to write (1-6).
* @param {String} eClass The desired class to assign to all elements.
*/
writeArrayClassH(headerLevel, eClass) {
this.writeArray('<h' + String(headerLevel) + ' class=' + eClass + '>','</h' + String(headerLevel) + '>')
}
/** Writes the contents of _list to the document in divider tags. */
writeArrayDiv() {
this.writeArray('<div>','</div>')
}
/**
* Writes the contents of _list to the document in divider tags that are assigned to the specified class.
* @param {String} eClass The desired class to assign to all elements.
*/
writeArrayClassDiv(eClass) {
this.writeArray('<div class=' + eClass + '>','</div>')
}
/** Writes the contents of _list to the document in span tags. */
writeArraySpan() {
this.writeArray('<span>','</span>')
}
/**
* Writes the contents of _list to the document in span tags that are assigned to the specified class.
* @param {String} eClass The desired class to assign to all elements.
*/
writeArrayClassSpan(eClass) {
this.writeArray('<span class=' + eClass + '>','</span>')
}
/** Writes the contents of _list to the document in list item tags. */
writeArrayLI() {
this.writeArray('<li>','</li>')
}
/**
* Writes the contents of _list to the document in list item tags that are assigned to the specified class.
* @param {String} eClass The desired class to assign to all elements.
*/
writeArrayClassLI(eClass) {
this.writeArray('<li class=' + eClass + '>','</li>')
}
/**
* Writes the contents of _list to the document between the entered tags in addition to adding an incrementing id based on the prefix.
* Removes last character of startTag under the assumption it's '>' in order to insert the id.
* @param {String} startTag The desired start tag to write.
* @param {String} endTag The desired end tag to write.
* @param {String} prefix The prefix for every element's id.
*/
writeArrayIDs(startTag, endTag, prefix) {
let eNum = 0;
this._list.forEach(function(element) {
eNum++;
document.write(startTag.substring(0, startTag.length - 1) + ' id=' + prefix + String(eNum) + '>' + element + endTag);
});
}
/**
* Writes the contents of _list to to the document between two sets of entered tags determined by the evaluation of the passed boolean function for each element.
* @param {String} startTagTrue The desired start tag to write if the condition is true for the element.
* @param {String} endTagTrue The desired end tag to write if the condition is true for the element.
* @param {String} startTagFalse The desired start tag to write if the condition is false for the element.
* @param {String} endTagFalse The desired end tag to write if the condition is false for the element.
* @param {Function} boolFunction The desired boolean function to be evaluated for each element.
*/
writeArrayConditional(startTagTrue, endTagTrue, startTagFalse, endTagFalse, boolFunction) {
this._list.forEach(function(element) {
document.write(boolFunction(element) ? startTagTrue + element + endTagTrue : startTagFalse + element + endTagFalse)
});
}
/**
* Writes the contents of _list to to the document between two sets of entered tags determined by the evaluation of the passed boolean function for each element.
* Modifies element based on the passed mod functions depending on the condition result for the element.
* @param {String} startTagTrue The desired start tag to write if the condition is true for the element.
* @param {String} endTagTrue The desired end tag to write if the condition is true for the element.
* @param {String} startTagFalse The desired start tag to write if the condition is false for the element.
* @param {String} endTagFalse The desired end tag to write if the condition is false for the element.
* @param {Function} boolFunction The desired boolean function to be evaluated for each element.
* @param {Function} modFuncTrue The desired function to modify element if the condition is true for the element.
* @param {Function} modFuncFalse The desired function to modify element if the condition is false for the element.
*/
writeArrayConditionalModifiable(startTagTrue, endTagTrue, startTagFalse, endTagFalse, boolFunction, modFuncTrue, modFuncFalse) {
this._list.forEach(function(element) {
document.write(boolFunction(element) ? startTagTrue + modFuncTrue(element) + endTagTrue : startTagFalse + modFuncFalse(element) + endTagFalse);
});
}
}