UNPKG

4.75 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.of = exports.PropertiesFile = void 0;
7
8var _fs = _interopRequireDefault(require("fs"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12/*
13 * properties
14 *
15 * Copyright (c) 2013 Matt Steele
16 * Licensed under the MIT license.
17 */
18class PropertiesFile {
19 constructor(...args) {
20 this.objs = {};
21
22 if (args.length) {
23 this.of.apply(this, args);
24 }
25 }
26
27 makeKeys(line) {
28 if (line && line.indexOf('#') !== 0) {
29 //let splitIndex = line.indexOf('=');
30 let separatorPositions = ['=', ':'].map(sep => {
31 return line.indexOf(sep);
32 }).filter(index => {
33 return index > -1;
34 });
35 let splitIndex = Math.min(...separatorPositions);
36 let key = line.substring(0, splitIndex).trim();
37 let value = line.substring(splitIndex + 1).trim(); // if keys already exists ...
38
39 if (this.objs.hasOwnProperty(key)) {
40 // if it is already an Array
41 if (Array.isArray(this.objs[key])) {
42 // just push the new value
43 this.objs[key].push(value);
44 } else {
45 // transform the value into Array
46 let oldValue = this.objs[key];
47 this.objs[key] = [oldValue, value];
48 }
49 } else {
50 // the key does not exists
51 const escapedValue = value.replace(/"/g, '\\"') // escape "
52 .replace(/\\:/g, ':') // remove \ before :
53 .replace(/\\=/g, '='); // remove \ before =
54
55 this.objs[key] = unescape(JSON.parse('"' + escapedValue + '"'));
56 }
57 }
58 }
59
60 addFile(file) {
61 let data = _fs.default.readFileSync(file, 'utf-8');
62
63 let items = data.split(/\r?\n/);
64 let me = this;
65
66 for (let i = 0; i < items.length; i++) {
67 let line = items[i];
68
69 while (line.substring(line.length - 1) === '\\') {
70 line = line.slice(0, -1);
71 let nextLine = items[i + 1];
72 line = line + nextLine.trim();
73 i++;
74 }
75
76 me.makeKeys(line);
77 }
78 }
79
80 of(...args) {
81 for (let i = 0; i < args.length; i++) {
82 this.addFile(args[i]);
83 }
84 }
85
86 get(key, defaultValue) {
87 if (this.objs.hasOwnProperty(key)) {
88 if (Array.isArray(this.objs[key])) {
89 let ret = [];
90
91 for (let i = 0; i < this.objs[key].length; i++) {
92 ret[i] = this.interpolate(this.objs[key][i]);
93 }
94
95 return ret;
96 } else {
97 return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
98 }
99 }
100
101 return defaultValue;
102 }
103
104 getLast(key, defaultValue) {
105 if (this.objs.hasOwnProperty(key)) {
106 if (Array.isArray(this.objs[key])) {
107 var lg = this.objs[key].length;
108 return this.interpolate(this.objs[key][lg - 1]);
109 } else {
110 return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
111 }
112 }
113
114 return defaultValue;
115 }
116
117 getFirst(key, defaultValue) {
118 if (this.objs.hasOwnProperty(key)) {
119 if (Array.isArray(this.objs[key])) {
120 return this.interpolate(this.objs[key][0]);
121 } else {
122 return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
123 }
124 }
125
126 return defaultValue;
127 }
128
129 getInt(key, defaultIntValue) {
130 let val = this.getLast(key);
131
132 if (!val) {
133 return defaultIntValue;
134 } else {
135 return parseInt(val, 10);
136 }
137 }
138
139 getFloat(key, defaultFloatValue) {
140 let val = this.getLast(key);
141
142 if (!val) {
143 return defaultFloatValue;
144 } else {
145 return parseFloat(val);
146 }
147 }
148
149 getBoolean(key, defaultBooleanValue) {
150 function parseBool(b) {
151 return !/^(false|0)$/i.test(b) && !!b;
152 }
153
154 let val = this.getLast(key);
155
156 if (!val) {
157 return defaultBooleanValue || false;
158 } else {
159 return parseBool(val);
160 }
161 }
162
163 set(key, value) {
164 this.objs[key] = value;
165 }
166
167 interpolate(s) {
168 let me = this;
169 return s.replace(/\\\\/g, '\\').replace(/\$\{([A-Za-z0-9\.\-\_]*)\}/g, function (match) {
170 return me.getLast(match.substring(2, match.length - 1));
171 });
172 }
173
174 getKeys() {
175 let keys = [];
176
177 for (let key in this.objs) {
178 keys.push(key);
179 }
180
181 return keys;
182 }
183
184 getMatchingKeys(matchstr) {
185 let keys = [];
186
187 for (let key in this.objs) {
188 if (key.search(matchstr) !== -1) {
189 keys.push(key);
190 }
191 }
192
193 return keys;
194 }
195
196 reset() {
197 this.objs = {};
198 }
199
200} // Retain 'of' from v1 for backward compatibility
201
202
203exports.PropertiesFile = PropertiesFile;
204
205let of = function of(...args) {
206 let globalFile = new PropertiesFile();
207 globalFile.of.apply(globalFile, args);
208 return globalFile;
209};
210
211exports.of = of;
\No newline at end of file