-
Notifications
You must be signed in to change notification settings - Fork 10
/
Component.js
281 lines (243 loc) · 6.99 KB
/
Component.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import difference from 'lodash.difference'
import isNode from 'detect-node'
import { MIME_TYPE } from './constants'
import * as properties from './properties'
import {
IncompatiblePlatformError,
InvalidComponentError,
InvalidProvidedComponentError,
InvalidProvidedPropError
} from './errors'
/**
* Base class for components.
*/
export default class Component {
/**
* Name of the component
*
* @abstract
* @type {string}
*/
static componentName = 'Component'
/**
* String used to separate lines when returning the component as a string.
*
* @type {string}
*/
static separator = '\r\n'
/**
* Array of required properties.
*
* @abstract
* @type {string[]}
*/
static requiredProps = []
/**
* Valid properties with appropriate validators.
*
* @abstract
* @type {Object}
*/
static validProps = {}
/**
* Valid components with appropriate validators.
*
* @abstract
* @type {Object}
*/
static validComponents = {}
/**
* Create a new component.
*/
constructor () {
/**
* Prefix used when returning the component as a string.
*
* @private
* @type {string}
*/
this.prefix = 'BEGIN:' + this.constructor.componentName
/**
* Suffix used when returning the component as a string.
*
* @private
* @type {string}
*/
this.suffix = 'END:' + this.constructor.componentName
/**
* Array to store the component's properties.
*
* @private
* @type {Property[]}
*/
this.internalProps = []
/**
* Array to store the component's components.
*
* @private
* @type {Component[]}
*/
this.internalComponents = []
// IE10 Fix: bind `this` in the consturctor.
this.addProp = this.addProp.bind(this)
this.props = this.props.bind(this)
this.propNames = this.propNames.bind(this)
this.components = this.components.bind(this)
this.componentNames = this.componentNames.bind(this)
this.addComponent = this.addComponent.bind(this)
this.reset = this.reset.bind(this)
this.validateRequired = this.validateRequired.bind(this)
this.toString = this.toString.bind(this)
this.toBlob = this.toBlob.bind(this)
this.toBase64 = this.toBase64.bind(this)
}
/**
* Get a frozen array of the component's properties.
*
* @returns {Property[]} Frozen array of the component's properties.
*/
props () {
return Object.freeze(this.internalProps.slice(0))
}
/**
* Get a frozen array of the component's properties' names.
*
* @returns {string[]} Frozen array of the component's properties' names.
*/
propNames () {
return Object.freeze(this.internalProps.map((prop) => {
return prop.constructor.propName
}))
}
/**
* Add a property to the component.
*
* @param {string} name - Name of the property (e.g. DTSTAMP).
* @param {*} [value] - Value of the property.
* @param {Object} [props={}] - Object of properties for the property. Object keys and values are directly injected.
* @param {boolean} [skipTransformer=false] - Explicitly determine if the property's value is transformed.
* @throws {InvalidProvidedPropError} Provided property type is not in the component's list of valid property types.
* @returns {Property} Configured property instance.
*/
addProp (name, value, props = {}, skipTransformer = false) {
const { validProps } = this.constructor
if (!validProps[name]) {
throw new InvalidProvidedPropError()
}
const PropClass = properties[name] || properties.base(name)
const prop = new PropClass(value, props, skipTransformer)
validProps[name].forEach((validator) => {
validator(this, prop)
})
this.internalProps.push(prop)
return prop
}
/**
* Get a frozen array of the component's components.
*
* @returns {Component[]} Frozen array of the component's components.
*/
components () {
return Object.freeze(this.internalComponents.slice(0))
}
/**
* Get a frozen array of the component's components' names.
*
* @returns {string[]} Frozen array of the component's components' names.
*/
componentNames () {
return Object.freeze(this.internalComponents.map((component) => {
return component.constructor.componentName
}))
}
/**
* Add a component to the component.
*
* @param {Component} component - Configured component to add to the component.
* @throws {InvalidProvidedComponentError} Provided component type is not in the component's list of valid component types.
* @returns {Component} Provided component.
*/
addComponent (component) {
const { validComponents } = this.constructor
const { componentName } = component.constructor
if (!(component instanceof Component)) {
throw new TypeError('Expected component to be an instance of Component.')
}
if (!validComponents[componentName]) {
throw new InvalidProvidedComponentError()
}
validComponents[componentName].forEach((validator) => {
validator(this, component)
})
this.internalComponents.push(component)
return component
}
/**
* Reset the components components and properties to the default empty state.
*/
reset () {
this.internalProps = []
this.internalComponents = []
}
/**
* Validate if all required properties have been added.
*
* @throws {InvalidComponentError} Required properties are missing.
* @returns {boolean} All required properties are present.
*/
validateRequired () {
const { requiredProps } = this.constructor
if (difference(requiredProps, this.propNames()).length > 0) {
throw new InvalidComponentError()
}
return true
}
/**
* Get a string representation of the component.
*
* @returns {string} String representation of the component.
*/
toString () {
this.validateRequired()
const props = this.internalProps.map((prop) => {
return prop.toString()
})
const components = this.internalComponents.map((component) => {
return component.toString()
})
return [
this.prefix,
...props,
...components,
this.suffix
].join(this.constructor.separator)
}
/**
* Get a Blob representation of the component. Uses MIME_TYPE as the MIME
* type.
*
* Note: This is a browser-only function unless a Blob polyfill is provided.
*
* @throws {IncompatiblePlatformError} Blob is undefined. Usually due to running in Node without a polyfill.
* @returns {Blob} Blob representation of the component.
*/
toBlob () {
if (typeof Blob === 'undefined') {
throw new IncompatiblePlatformError()
}
return new Blob([this.toString()], { type: MIME_TYPE })
}
/**
* Get a Base64 encoded string representation of the component.
*
* @returns {String} Base64 encoded string representation of the component.
*/
toBase64 () {
const string = this.toString()
if (isNode) {
return new Buffer(string).toString('base64')
} else {
return window.btoa(string)
}
}
}