|
1 |
| -import fetch from 'fetch'; |
2 | 1 | import Service from '@ember/service';
|
| 2 | +import rfcMappings from 'ember-rfc176-data'; |
| 3 | +import { computed } from '@ember/object'; |
3 | 4 |
|
4 |
| -const LOCALNAME_CONVERSIONS = { |
| 5 | +const LOCAL_NAME_CONVERSIONS = { |
5 | 6 | Object: 'EmberObject',
|
6 | 7 | Array: 'EmberArray',
|
7 | 8 | Error: 'EmberError'
|
8 | 9 | };
|
9 | 10 |
|
10 | 11 | export default Service.extend({
|
11 |
| - |
12 |
| - async initMappings() { |
| 12 | + mappings: computed(function() { |
13 | 13 | try {
|
14 |
| - let response = await this.fetch(); |
15 |
| - let mappings = await response.json(); |
16 |
| - let newMappings = this.buildMappings(mappings); |
17 |
| - this.set('mappings', newMappings); |
| 14 | + return rfcMappings.map(item => { |
| 15 | + let newItem = Object.assign({}, item); |
| 16 | + if (LOCAL_NAME_CONVERSIONS[newItem.localName]) { |
| 17 | + newItem.localName = LOCAL_NAME_CONVERSIONS[newItem.localName]; |
| 18 | + } |
| 19 | + return newItem; |
| 20 | + }); |
18 | 21 | } catch (e) {
|
19 |
| - this.set('mappings', []); |
| 22 | + return []; |
20 | 23 | }
|
21 |
| - }, |
22 |
| - |
23 |
| - buildMappings(mappings) { |
24 |
| - return mappings.map(item => { |
25 |
| - let newItem = Object.assign({}, item); |
26 |
| - if (LOCALNAME_CONVERSIONS[newItem.localName]) { |
27 |
| - newItem.localName = LOCALNAME_CONVERSIONS[newItem.localName]; |
28 |
| - } |
29 |
| - return newItem; |
30 |
| - }); |
31 |
| - }, |
32 |
| - |
33 |
| - fetch() { |
34 |
| - return fetch('/assets/mappings.json'); |
35 |
| - }, |
| 24 | + }), |
36 | 25 |
|
37 | 26 | getModule(name, documentedModule) {
|
38 | 27 | if (!this.mappings) {
|
39 | 28 | return '';
|
40 | 29 | }
|
41 |
| - let matches = this.mappings.filter(element => element.localName === name); |
42 |
| - return matches.length > 0 ? matches[0].module : documentedModule; |
| 30 | + |
| 31 | + let match = this.mappings.find(element => element.localName === name); |
| 32 | + return match ? match.module : documentedModule; |
43 | 33 | },
|
44 | 34 |
|
45 | 35 | getNewClassFromOld(oldClassName, mappings) {
|
46 |
| - let matches = mappings.filter(element => element.global === oldClassName); |
47 |
| - if (matches.length > 0) { |
48 |
| - if (matches[0].localName) { |
| 36 | + let match = mappings.find(element => element.global === oldClassName); |
| 37 | + |
| 38 | + if (match) { |
| 39 | + if (match.localName) { |
49 | 40 | return {
|
50 | 41 | itemType: 'class',
|
51 |
| - newModule: matches[0].module, |
52 |
| - newName: matches[0].localName |
53 |
| - } |
| 42 | + newModule: match.module, |
| 43 | + newName: match.localName |
| 44 | + }; |
54 | 45 | } else {
|
55 | 46 | return {
|
56 | 47 | itemType: 'function',
|
57 |
| - newModule: matches[0].module, |
58 |
| - newName: matches[0].export |
59 |
| - } |
| 48 | + newModule: match.module, |
| 49 | + newName: match.export |
| 50 | + }; |
60 | 51 | }
|
61 |
| - |
62 | 52 | } else {
|
63 | 53 | return {
|
64 | 54 | itemType: 'class',
|
65 | 55 | newName: oldClassName
|
66 |
| - } |
| 56 | + }; |
67 | 57 | }
|
68 | 58 | },
|
69 | 59 |
|
70 | 60 | getNewModuleFromOld(oldModuleName, mappings) {
|
71 |
| - let matches = mappings.filter(element => element.module === oldModuleName); |
72 |
| - if (matches.length > 0) { |
73 |
| - return { |
74 |
| - module: matches[0].replacement.module |
75 |
| - }; |
76 |
| - } else { |
77 |
| - return { |
78 |
| - module: oldModuleName |
79 |
| - }; |
80 |
| - } |
| 61 | + let match = mappings.find(element => element.module === oldModuleName); |
| 62 | + return { module: match ? match.replacement.module : oldModuleName }; |
81 | 63 | },
|
82 | 64 |
|
83 |
| - |
84 |
| - hasFunctionMapping(name, module) { |
| 65 | + hasFunctionMapping(name, moduleName) { |
85 | 66 | if (!this.mappings) {
|
86 | 67 | return false;
|
87 | 68 | }
|
88 |
| - let filtered = this.mappings.filter(element => element.export === name && element.module === module); |
89 |
| - return filtered.length > 0; |
| 69 | + |
| 70 | + return Boolean( |
| 71 | + this.mappings.find(element => element.export === name && element.module === moduleName) |
| 72 | + ); |
90 | 73 | },
|
91 | 74 |
|
92 | 75 | hasClassMapping(name) {
|
93 | 76 | if (!this.mappings) {
|
94 | 77 | return false;
|
95 | 78 | }
|
96 |
| - return this.mappings.filter(element => element.localName === name).length > 0; |
97 |
| - } |
98 | 79 |
|
| 80 | + return Boolean(this.mappings.find(element => element.localName === name)); |
| 81 | + } |
99 | 82 | });
|
0 commit comments