-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLElementFactoryCollection.js
More file actions
77 lines (70 loc) · 2.22 KB
/
HTMLElementFactoryCollection.js
File metadata and controls
77 lines (70 loc) · 2.22 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
"use strict";
/**
* Collection of HTMLElement factories.
*
* @class HTMLElementFactoryCollection
* @constructor
* @param {Object} obj An object containing HTMLElement factories.
*/
export default class HTMLElementFactoryCollection {
constructor(){
this._obj = {};
}
/**
* Add a new element factory to the collection.
*
* @param {string} tagName The tag name of the element to be created. Example: 'iot-button'.
* @param {function} elementFactory The factory function that creates the element.
* @throws {Error} If the tagName is not a string or the elementFactory is not a function.
*/
add(tagName, elementFactory){
if (!elementFactory || typeof elementFactory !== 'function') {
throw new Error('Only objects of type function are allowed in HTMLElementFactoryCollection.');
}
if (!tagName || typeof tagName !== 'string') {
throw new Error('Mandatory tagName when adding a new element to HTMLElementFactoryCollection.');
}
this._obj[tagName] = elementFactory;
}
/**
* Remove an element factory from the collection.
*
* @param {string} tagName The tag name of the element to be removed. Example: 'iot-button'.
*/
remove(tagName){
delete this._obj[tagName];
}
/**
* Get an element factory from the collection.
*
* @param {string} tagName The tag name of the element to be retrieved. Example: 'iot-button'.
* @returns {function} The element factory function.
*/
get(tagName){
return this._obj[tagName];
}
/**
* Get an iterator for the collection.
*
* @example
* for (const [tagName, elementFactory] of collection) {
* ...
* }
*
* @returns {Object} An iterator for the collection.
*/
[Symbol.iterator]() {
const keys = Object.keys(this._obj);
let index = 0;
const obj = this._obj;
return {
next() {
if (index < keys.length) {
const key = keys[index++];
return { value: [key, obj[key]], done: false };
}
return { done: true };
}
};
}
}