1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | You should have received a copy of the GNU General Public License |
13 | along with this program; if not, write to the Free Software |
14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
15 | */ |
16 | /* |
17 | Changes: |
18 | - v4.0.0: |
19 | - created from v3.6.0 |
20 | Doc reviewed 202208 |
21 | */ |
22 | |
23 | import { NOT_FOUND } from '../main/Constants.js'; |
24 | import { theDataVersion } from '../data/Version.js'; |
25 | |
26 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
27 | /** |
28 | This class represent a ObjType |
29 | */ |
30 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
31 | |
32 | class ObjType { |
33 | |
34 | /** |
35 | The name of the object |
36 | @type {String} |
37 | */ |
38 | |
39 | #objTypeName = ''; |
40 | |
41 | /** |
42 | An array with all the valid objects names for the ObjType objects |
43 | @type {Array.<String>} |
44 | */ |
45 | |
46 | #validObjTypeNames = [ 'Itinerary', 'ItineraryPoint', 'Maneuver', 'Note', 'Route', 'Travel', 'WayPoint' ]; |
47 | |
48 | /** |
49 | An array with all the valid properties for the ObjType |
50 | @type {Array.<String>} |
51 | */ |
52 | |
53 | #properties = []; |
54 | |
55 | /** |
56 | constructor |
57 | @param {String} objTypeName The name of the ObjType |
58 | @param {Array.<String>} properties The properties names of the ObjType |
59 | */ |
60 | |
61 | constructor ( objTypeName, properties ) { |
62 | Object.freeze ( this ); |
63 | if ( NOT_FOUND === this.#validObjTypeNames.indexOf ( objTypeName ) ) { |
64 | throw new Error ( 'Invalid ObjType name : ' + objTypeName ); |
65 | } |
66 | this.#objTypeName = objTypeName; |
67 | this.#properties = properties; |
68 | Object.freeze ( this.#properties ); |
69 | } |
70 | |
71 | /** |
72 | the name of the ObjType |
73 | @type {String} |
74 | */ |
75 | |
76 | get name ( ) { return this.#objTypeName; } |
77 | |
78 | /** |
79 | the version of the ObjType |
80 | @type {String} |
81 | */ |
82 | |
83 | get version ( ) { return theDataVersion; } |
84 | |
85 | /** |
86 | An array with all the valid properties for the ObjType |
87 | @type {Array.<String>} |
88 | */ |
89 | |
90 | get properties ( ) { return this.#properties; } |
91 | |
92 | /** |
93 | An object literal with the ObjType properties and without any methods. |
94 | This object can be used with the JSON object |
95 | @type {JsonObject} |
96 | */ |
97 | |
98 | get jsonObject ( ) { |
99 | return { |
100 | name : this.#objTypeName, |
101 | version : theDataVersion |
102 | }; |
103 | } |
104 | |
105 | /** |
106 | Verify that the given object is an ObjType and is valid |
107 | @param {JsonObject} something An object to validate |
108 | */ |
109 | |
110 | validate ( something ) { |
111 | if ( ! Object.getOwnPropertyNames ( something ).includes ( 'name' ) ) { |
112 | throw new Error ( 'No name for ' + this.#objTypeName ); |
113 | } |
114 | if ( this.#objTypeName !== something.name ) { |
115 | throw new Error ( 'Invalid name for ' + this.#objTypeName ); |
116 | } |
117 | if ( ! Object.getOwnPropertyNames ( something ).includes ( 'version' ) ) { |
118 | throw new Error ( 'No version for ' + this.#objTypeName ); |
119 | } |
120 | } |
121 | } |
122 | |
123 | export default ObjType; |
124 | |
125 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
126 |