/
-format-base.js
58 lines (49 loc) · 1.33 KB
/
-format-base.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
/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import { getOwner } from '@ember/application';
import Helper from '@ember/component/helper';
import { isEmpty } from '@ember/utils';
/**
* @private
* @hide
*/
export default class AbstractHelper extends Helper {
intl = null;
unsubscribeLocaleChanged = null;
constructor() {
super(...arguments);
if (this.constructor === AbstractHelper) {
throw new Error(
'FormatHelper is an abstract class, can not be instantiated directly.',
);
}
this.intl = getOwner(this).lookup('service:intl');
this.unsubscribeLocaleChanged = this.intl.onLocaleChanged(
this.recompute,
this,
);
}
format() {
throw new Error('not implemented');
}
compute([value, positionalOptions], namedOptions) {
const options = positionalOptions
? Object.assign({}, positionalOptions, namedOptions)
: namedOptions;
if (isEmpty(value)) {
if (options.allowEmpty ?? this.allowEmpty) {
return;
}
if (typeof value === 'undefined') {
throw new Error(`${this} helper requires value attribute.`);
}
}
return this.format(value, options);
}
willDestroy() {
super.willDestroy();
this.unsubscribeLocaleChanged();
}
}