properties-file 2.2.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -90
- package/lib/editor/index.d.ts +104 -0
- package/lib/editor/index.js +249 -0
- package/lib/index.d.ts +12 -6
- package/lib/index.js +14 -9
- package/lib/loader/webpack.js +2 -2
- package/lib/properties.d.ts +37 -5
- package/lib/properties.js +139 -14
- package/lib/property.d.ts +39 -23
- package/lib/property.js +55 -46
- package/package.json +13 -11
- package/lib/content/index.d.ts +0 -18
- package/lib/content/index.js +0 -59
- package/lib/file/index.d.ts +0 -21
- package/lib/file/index.js +0 -30
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
+
if (ar || !(i in from)) {
|
|
36
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
+
ar[i] = from[i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
+
};
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.PropertiesEditor = exports.DEFAULT_COMMENT_DELIMITER = exports.DEFAULT_SEPARATOR = void 0;
|
|
44
|
+
var escape_1 = require("../escape");
|
|
45
|
+
var properties_1 = require("../properties");
|
|
46
|
+
/** The default separator between keys and values. */
|
|
47
|
+
exports.DEFAULT_SEPARATOR = '=';
|
|
48
|
+
/** The default character used as comment delimiter. */
|
|
49
|
+
exports.DEFAULT_COMMENT_DELIMITER = '#';
|
|
50
|
+
/**
|
|
51
|
+
* A .properties file editor.
|
|
52
|
+
*/
|
|
53
|
+
var PropertiesEditor = /** @class */ (function (_super) {
|
|
54
|
+
__extends(PropertiesEditor, _super);
|
|
55
|
+
/**
|
|
56
|
+
* Create `PropertiesEditor` object.
|
|
57
|
+
*
|
|
58
|
+
* @param content - The content of a `.properties` file.
|
|
59
|
+
*/
|
|
60
|
+
function PropertiesEditor(content) {
|
|
61
|
+
return _super.call(this, content) || this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Insert a new property in the existing object (by default it will be at the end).
|
|
65
|
+
*
|
|
66
|
+
* @param key - A property key (unescaped).
|
|
67
|
+
* @param value - A property value (unescaped).
|
|
68
|
+
* @param options - Additional options.
|
|
69
|
+
*/
|
|
70
|
+
PropertiesEditor.prototype.insert = function (key, value, options) {
|
|
71
|
+
var _a;
|
|
72
|
+
var _b, _c;
|
|
73
|
+
var escapeUnicode = (options === null || options === void 0 ? void 0 : options.escapeUnicode) || false;
|
|
74
|
+
var separator = (options === null || options === void 0 ? void 0 : options.separator)
|
|
75
|
+
? options.separator === ' '
|
|
76
|
+
? ' '
|
|
77
|
+
: " ".concat(options.separator, " ")
|
|
78
|
+
: " ".concat(exports.DEFAULT_SEPARATOR, " ").replace(' ', ' ');
|
|
79
|
+
var referenceKey = options === null || options === void 0 ? void 0 : options.referenceKey;
|
|
80
|
+
var position = (options === null || options === void 0 ? void 0 : options.position) || 'after';
|
|
81
|
+
// Allow to add multiline keys.
|
|
82
|
+
var multilineKey = key
|
|
83
|
+
.split(/\r?\n/)
|
|
84
|
+
.map(function (key) { return (0, escape_1.escapeKey)(key, escapeUnicode); })
|
|
85
|
+
.join('\\\n');
|
|
86
|
+
// Allow to add multiline values.
|
|
87
|
+
var multilineValue = value
|
|
88
|
+
.split(/\r?\n/)
|
|
89
|
+
.map(function (value) { return (0, escape_1.escapeValue)(value, escapeUnicode); })
|
|
90
|
+
.join('\\\n');
|
|
91
|
+
// Allow to add multiline comments.
|
|
92
|
+
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " ");
|
|
93
|
+
var multilineComment = (options === null || options === void 0 ? void 0 : options.comment) === undefined
|
|
94
|
+
? ''
|
|
95
|
+
: "".concat("".concat(commentPrefix).concat(options.comment).split(/\r?\n/).join("\n".concat(commentPrefix)), "\n");
|
|
96
|
+
var newLines = "".concat(multilineComment).concat(multilineKey).concat(separator).concat(multilineValue).split(/\n/);
|
|
97
|
+
if (referenceKey === undefined) {
|
|
98
|
+
// Insert the new property at the end if the reference key was not defined.
|
|
99
|
+
(_a = this.lines).push.apply(_a, __spreadArray([], __read(newLines), false));
|
|
100
|
+
this.parseLines();
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// Find the last occurrence of the reference key.
|
|
104
|
+
var property = __spreadArray([], __read(this.collection), false).reverse()
|
|
105
|
+
.find(function (property) { return property.key === referenceKey; });
|
|
106
|
+
// Insert the new property when a reference key defined only when found.
|
|
107
|
+
if (property) {
|
|
108
|
+
var insertPosition = position === 'after'
|
|
109
|
+
? property.endingLineNumber
|
|
110
|
+
: (_c = (_b = property.previousProperty) === null || _b === void 0 ? void 0 : _b.endingLineNumber) !== null && _c !== void 0 ? _c : 0;
|
|
111
|
+
this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, insertPosition)), false), __read(newLines), false), __read(this.lines.slice(insertPosition)), false);
|
|
112
|
+
this.parseLines();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Insert a new comment in the existing object (by default it will be at the end).
|
|
118
|
+
*
|
|
119
|
+
* @param comment - The comment to add.
|
|
120
|
+
* @param options - Additional options.
|
|
121
|
+
*/
|
|
122
|
+
PropertiesEditor.prototype.insertComment = function (comment, options) {
|
|
123
|
+
var _a;
|
|
124
|
+
var _b, _c;
|
|
125
|
+
var referenceKey = options === null || options === void 0 ? void 0 : options.referenceKey;
|
|
126
|
+
var position = (options === null || options === void 0 ? void 0 : options.position) || 'after';
|
|
127
|
+
// Allow to add multiline comments.
|
|
128
|
+
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " ");
|
|
129
|
+
var newLines = "".concat(commentPrefix).concat(comment)
|
|
130
|
+
.replace(/\r?\n/g, "\n".concat(commentPrefix))
|
|
131
|
+
.split(/\n/);
|
|
132
|
+
if (referenceKey === undefined) {
|
|
133
|
+
// Insert the new comment at the end if the reference key was not defined.
|
|
134
|
+
(_a = this.lines).push.apply(_a, __spreadArray([], __read(newLines), false));
|
|
135
|
+
this.parseLines();
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// Find the last occurrence of the reference key.
|
|
139
|
+
var property = __spreadArray([], __read(this.collection), false).reverse()
|
|
140
|
+
.find(function (property) { return property.key === referenceKey; });
|
|
141
|
+
// Insert the new comment when a reference key defined only when found.
|
|
142
|
+
if (property) {
|
|
143
|
+
var insertPosition = position === 'after'
|
|
144
|
+
? property.endingLineNumber
|
|
145
|
+
: (_c = (_b = property.previousProperty) === null || _b === void 0 ? void 0 : _b.endingLineNumber) !== null && _c !== void 0 ? _c : 0;
|
|
146
|
+
this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, insertPosition)), false), __read(newLines), false), __read(this.lines.slice(insertPosition)), false);
|
|
147
|
+
this.parseLines();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Remove the last occurrence of a given key from the existing object.
|
|
153
|
+
*
|
|
154
|
+
* @param key - The name of the key to remove.
|
|
155
|
+
* @param removeCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be removed.
|
|
156
|
+
*/
|
|
157
|
+
PropertiesEditor.prototype.remove = function (key, removeCommentsAndWhiteSpace) {
|
|
158
|
+
var _a, _b;
|
|
159
|
+
if (removeCommentsAndWhiteSpace === void 0) { removeCommentsAndWhiteSpace = true; }
|
|
160
|
+
// Find the last occurrence of the key.
|
|
161
|
+
var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; });
|
|
162
|
+
if (property) {
|
|
163
|
+
var startLine = removeCommentsAndWhiteSpace
|
|
164
|
+
? (_b = (_a = property.previousProperty) === null || _a === void 0 ? void 0 : _a.endingLineNumber) !== null && _b !== void 0 ? _b : 0
|
|
165
|
+
: property.startingLineNumber - 1;
|
|
166
|
+
var endLine = property.endingLineNumber;
|
|
167
|
+
this.lines = __spreadArray(__spreadArray([], __read(this.lines.slice(0, startLine)), false), __read(this.lines.slice(endLine)), false);
|
|
168
|
+
this.parseLines();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Restore the original newline characters of a key.
|
|
173
|
+
*
|
|
174
|
+
* @param property - A property object.
|
|
175
|
+
*
|
|
176
|
+
* @returns The key with its original newlines characters restored.
|
|
177
|
+
*/
|
|
178
|
+
PropertiesEditor.prototype.getKeyWithNewlines = function (property) {
|
|
179
|
+
return property.newlinePositions.length === 0
|
|
180
|
+
? property.key
|
|
181
|
+
: // eslint-disable-next-line unicorn/no-array-reduce
|
|
182
|
+
__spreadArray([], __read(property.key), false).reduce(function (accumulator, character, index) {
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
184
|
+
return "".concat(accumulator).concat(property.newlinePositions.includes(index) ? '\n' : '').concat(character);
|
|
185
|
+
}, '');
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Restore the original newline characters of a value.
|
|
189
|
+
*
|
|
190
|
+
* @param property - A property object.
|
|
191
|
+
*
|
|
192
|
+
* @returns The value with its original newlines characters restored.
|
|
193
|
+
*/
|
|
194
|
+
PropertiesEditor.prototype.getValueWithNewlines = function (property) {
|
|
195
|
+
return property.newlinePositions.length === 0 || property.valuePosition === undefined
|
|
196
|
+
? property.value
|
|
197
|
+
: // eslint-disable-next-line unicorn/no-array-reduce
|
|
198
|
+
__spreadArray([], __read(property.value), false).reduce(function (accumulator, character, index) {
|
|
199
|
+
return "".concat(accumulator).concat(property.newlinePositions.includes(index + property.valuePosition)
|
|
200
|
+
? '\n'
|
|
201
|
+
: ''
|
|
202
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
203
|
+
).concat(character);
|
|
204
|
+
}, '');
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Edit the last occurrence of a given key from the existing object.
|
|
208
|
+
*
|
|
209
|
+
* @param key - The name of the key to edit.
|
|
210
|
+
* @param options - Additional options.
|
|
211
|
+
*/
|
|
212
|
+
PropertiesEditor.prototype.edit = function (key, options) {
|
|
213
|
+
var _a, _b, _c, _d;
|
|
214
|
+
// Find the last occurrence of the key to edit.
|
|
215
|
+
var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; });
|
|
216
|
+
if (!property) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
var escapeUnicode = (options === null || options === void 0 ? void 0 : options.escapeUnicode) || false;
|
|
220
|
+
var separator = (options === null || options === void 0 ? void 0 : options.separator)
|
|
221
|
+
? options.separator === ' '
|
|
222
|
+
? ' '
|
|
223
|
+
: " ".concat(options.separator, " ")
|
|
224
|
+
: property.separator || " ".concat(exports.DEFAULT_SEPARATOR, " ").replace(' ', ' ');
|
|
225
|
+
// Allow to edit multiline keys.
|
|
226
|
+
var multilineKey = ((_a = options === null || options === void 0 ? void 0 : options.newKey) !== null && _a !== void 0 ? _a : this.getKeyWithNewlines(property))
|
|
227
|
+
.split(/\r?\n/)
|
|
228
|
+
.map(function (key) { return (0, escape_1.escapeKey)(key, escapeUnicode); })
|
|
229
|
+
.join('\\\n');
|
|
230
|
+
// Allow to edit multiline values.
|
|
231
|
+
var multilineValue = ((_b = options === null || options === void 0 ? void 0 : options.newValue) !== null && _b !== void 0 ? _b : this.getValueWithNewlines(property))
|
|
232
|
+
.split(/\r?\n/)
|
|
233
|
+
.map(function (value) { return (0, escape_1.escapeValue)(value, escapeUnicode); })
|
|
234
|
+
.join('\\\n');
|
|
235
|
+
// Allow to edit multiline comments.
|
|
236
|
+
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " ");
|
|
237
|
+
var multilineComment = (options === null || options === void 0 ? void 0 : options.newComment) === undefined
|
|
238
|
+
? ''
|
|
239
|
+
: "".concat("".concat(commentPrefix).concat(options.newComment).split(/\r?\n/).join("\n".concat(commentPrefix)), "\n");
|
|
240
|
+
var newLines = "".concat(multilineComment).concat(multilineKey).concat(separator).concat(multilineValue).split(/\n/);
|
|
241
|
+
// Replace the existing property with the new one.
|
|
242
|
+
this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, (options === null || options === void 0 ? void 0 : options.newComment) === undefined
|
|
243
|
+
? property.startingLineNumber - 1
|
|
244
|
+
: (_d = (_c = property.previousProperty) === null || _c === void 0 ? void 0 : _c.endingLineNumber) !== null && _d !== void 0 ? _d : 0)), false), __read(newLines), false), __read(this.lines.slice(property.endingLineNumber)), false);
|
|
245
|
+
this.parseLines();
|
|
246
|
+
};
|
|
247
|
+
return PropertiesEditor;
|
|
248
|
+
}(properties_1.Properties));
|
|
249
|
+
exports.PropertiesEditor = PropertiesEditor;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
/// <reference types="./properties-file" />
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
export {
|
|
5
|
-
export { Property } from './property';
|
|
6
|
-
export { PropertyLine } from './property-line';
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
export { Properties } from './properties';
|
|
7
5
|
/**
|
|
8
|
-
* A
|
|
6
|
+
* A key-value pair object.
|
|
9
7
|
*/
|
|
10
|
-
export type
|
|
8
|
+
export type KeyValuePairObject = {
|
|
11
9
|
[key: string]: string;
|
|
12
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Converts the content of a `.properties` file to a key-value pair object.
|
|
13
|
+
*
|
|
14
|
+
* @param content - The content of a `.properties` file.
|
|
15
|
+
*
|
|
16
|
+
* @returns A key/value object representing the content of a `.properties` file.
|
|
17
|
+
*/
|
|
18
|
+
export declare const getProperties: (content: string | Buffer) => KeyValuePairObject;
|
package/lib/index.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var file_1 = require("./file");
|
|
5
|
-
Object.defineProperty(exports, "getProperties", { enumerable: true, get: function () { return file_1.getProperties; } });
|
|
6
|
-
Object.defineProperty(exports, "propertiesToJson", { enumerable: true, get: function () { return file_1.propertiesToJson; } });
|
|
3
|
+
exports.getProperties = exports.Properties = void 0;
|
|
7
4
|
var properties_1 = require("./properties");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
var properties_2 = require("./properties");
|
|
6
|
+
Object.defineProperty(exports, "Properties", { enumerable: true, get: function () { return properties_2.Properties; } });
|
|
7
|
+
/**
|
|
8
|
+
* Converts the content of a `.properties` file to a key-value pair object.
|
|
9
|
+
*
|
|
10
|
+
* @param content - The content of a `.properties` file.
|
|
11
|
+
*
|
|
12
|
+
* @returns A key/value object representing the content of a `.properties` file.
|
|
13
|
+
*/
|
|
14
|
+
var getProperties = function (content) {
|
|
15
|
+
return new properties_1.Properties(content).toObject();
|
|
16
|
+
};
|
|
17
|
+
exports.getProperties = getProperties;
|
package/lib/loader/webpack.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var
|
|
3
|
+
var __1 = require("..");
|
|
4
4
|
/**
|
|
5
5
|
* Webpack file loader for `.properties` files.
|
|
6
6
|
*
|
|
@@ -9,6 +9,6 @@ var content_1 = require("../content");
|
|
|
9
9
|
* @returns A Webpack file loader string containing the content of a `.properties` file.
|
|
10
10
|
*/
|
|
11
11
|
var webpackLoader = function (content) {
|
|
12
|
-
return "module.exports = ".concat(JSON.stringify((0,
|
|
12
|
+
return "module.exports = ".concat(JSON.stringify((0, __1.getProperties)(content)), ";");
|
|
13
13
|
};
|
|
14
14
|
exports.default = webpackLoader;
|
package/lib/properties.d.ts
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { KeyValuePairObject } from '.';
|
|
2
3
|
import { Property } from './property';
|
|
4
|
+
/**
|
|
5
|
+
* Byte-order mark.
|
|
6
|
+
*/
|
|
7
|
+
export declare const BOM = "\uFEFF";
|
|
8
|
+
export declare const BOM_CODE_POINT: number | undefined;
|
|
9
|
+
/** The default end of line character. */
|
|
10
|
+
export declare const DEFAULT_END_OF_LINE_CHARACTER = "\n";
|
|
3
11
|
/**
|
|
4
12
|
* A class representing the content of a .properties file.
|
|
5
13
|
*/
|
|
6
14
|
export declare class Properties {
|
|
15
|
+
/** Does the .properties content starts with a BOM character? */
|
|
16
|
+
readonly hasBom: boolean;
|
|
17
|
+
/** The end of line character. */
|
|
18
|
+
readonly eolCharacter: string;
|
|
19
|
+
/** `.properties` content split by line. */
|
|
20
|
+
protected lines: string[];
|
|
7
21
|
/** The collection of property object. */
|
|
8
22
|
collection: Property[];
|
|
9
23
|
/** Object associating keys with their starting line numbers. */
|
|
10
24
|
keyLineNumbers: KeyLineNumbers;
|
|
25
|
+
/**
|
|
26
|
+
* Create `Properties` object.
|
|
27
|
+
*
|
|
28
|
+
* @param content - The content of a `.properties` file.
|
|
29
|
+
*/
|
|
30
|
+
constructor(content: string | Buffer);
|
|
31
|
+
/**
|
|
32
|
+
* Parse the `.properties` content line by line.
|
|
33
|
+
*/
|
|
34
|
+
protected parseLines(): void;
|
|
11
35
|
/**
|
|
12
36
|
* Add a property object into a properties object collection.
|
|
13
37
|
*
|
|
@@ -15,17 +39,25 @@ export declare class Properties {
|
|
|
15
39
|
*
|
|
16
40
|
* @returns Undefined so that we conveniently overwrite the property object.
|
|
17
41
|
*/
|
|
18
|
-
|
|
42
|
+
private addToCollection;
|
|
19
43
|
/**
|
|
20
44
|
* Get keys that have collisions (more than one occurrence).
|
|
21
45
|
*/
|
|
22
46
|
getKeyCollisions(): KeyCollisions[];
|
|
23
47
|
/**
|
|
24
|
-
* Get the
|
|
48
|
+
* Get the key/value object representing the properties.
|
|
49
|
+
*
|
|
50
|
+
* @returns A key/value object representing the properties.
|
|
51
|
+
*/
|
|
52
|
+
toObject(): KeyValuePairObject;
|
|
53
|
+
/**
|
|
54
|
+
* Format the object in `.properties`.
|
|
55
|
+
*
|
|
56
|
+
* @param endOfLineCharacter - The character used for end of lines.
|
|
25
57
|
*
|
|
26
|
-
* @returns
|
|
58
|
+
* @returns The object in `.properties` format.
|
|
27
59
|
*/
|
|
28
|
-
|
|
60
|
+
format(endOfLineCharacter?: '\n' | '\r\n'): string;
|
|
29
61
|
}
|
|
30
62
|
/**
|
|
31
63
|
* Object associating keys with their line numbers.
|
package/lib/properties.js
CHANGED
|
@@ -1,16 +1,125 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
2
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KeyCollisions = exports.Properties = void 0;
|
|
30
|
+
exports.KeyCollisions = exports.Properties = exports.DEFAULT_END_OF_LINE_CHARACTER = exports.BOM_CODE_POINT = exports.BOM = void 0;
|
|
31
|
+
var property_1 = require("./property");
|
|
32
|
+
var property_line_1 = require("./property-line");
|
|
33
|
+
/**
|
|
34
|
+
* Byte-order mark.
|
|
35
|
+
*/
|
|
36
|
+
exports.BOM = '\uFEFF';
|
|
37
|
+
exports.BOM_CODE_POINT = exports.BOM.codePointAt(0);
|
|
38
|
+
/** The default end of line character. */
|
|
39
|
+
exports.DEFAULT_END_OF_LINE_CHARACTER = '\n';
|
|
40
|
+
/**
|
|
41
|
+
* Get the first end of line (EOL) character from multiline content.
|
|
42
|
+
*
|
|
43
|
+
* @param content - The content of a `.properties` file.
|
|
44
|
+
*
|
|
45
|
+
* @returns The multiline content's first end of line (EOL) character.
|
|
46
|
+
*/
|
|
47
|
+
var getFirstEolCharacter = function (content) {
|
|
48
|
+
var newlineIndex = content.indexOf('\n');
|
|
49
|
+
return newlineIndex < 0 ? undefined : "".concat(content[newlineIndex - 1] === '\r' ? '\r' : '', "\n");
|
|
50
|
+
};
|
|
4
51
|
/**
|
|
5
52
|
* A class representing the content of a .properties file.
|
|
6
53
|
*/
|
|
7
54
|
var Properties = /** @class */ (function () {
|
|
8
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Create `Properties` object.
|
|
57
|
+
*
|
|
58
|
+
* @param content - The content of a `.properties` file.
|
|
59
|
+
*/
|
|
60
|
+
function Properties(content) {
|
|
61
|
+
var _a;
|
|
9
62
|
/** The collection of property object. */
|
|
10
63
|
this.collection = [];
|
|
11
64
|
/** Object associating keys with their starting line numbers. */
|
|
12
65
|
this.keyLineNumbers = {};
|
|
66
|
+
var stringContent = Buffer.isBuffer(content) ? content.toString() : content;
|
|
67
|
+
this.hasBom = stringContent.codePointAt(0) === exports.BOM_CODE_POINT;
|
|
68
|
+
this.eolCharacter = (_a = getFirstEolCharacter(stringContent)) !== null && _a !== void 0 ? _a : exports.DEFAULT_END_OF_LINE_CHARACTER;
|
|
69
|
+
this.lines = (this.hasBom ? stringContent.slice(1) : stringContent).split(/\r?\n/);
|
|
70
|
+
this.parseLines();
|
|
13
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Parse the `.properties` content line by line.
|
|
74
|
+
*/
|
|
75
|
+
Properties.prototype.parseLines = function () {
|
|
76
|
+
var e_1, _a;
|
|
77
|
+
/** reset existing object properties to their initial values. */
|
|
78
|
+
this.collection = [];
|
|
79
|
+
this.keyLineNumbers = {};
|
|
80
|
+
/** Line number while parsing properties file content. */
|
|
81
|
+
var lineNumber = 0;
|
|
82
|
+
/** The current property object being parsed. */
|
|
83
|
+
var property;
|
|
84
|
+
/** The previous property object that was parsed. */
|
|
85
|
+
var previousProperty;
|
|
86
|
+
try {
|
|
87
|
+
for (var _b = __values(this.lines), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
88
|
+
var line = _c.value;
|
|
89
|
+
lineNumber++;
|
|
90
|
+
var propertyLine = new property_line_1.PropertyLine(line, !!property);
|
|
91
|
+
if (property) {
|
|
92
|
+
// Continue parsing an existing property.
|
|
93
|
+
property.addLine(propertyLine);
|
|
94
|
+
if (propertyLine.isContinuing) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// Check if the line is a new property.
|
|
100
|
+
if (propertyLine.isComment || propertyLine.isBlank) {
|
|
101
|
+
continue; // Skip line if its a comment or blank.
|
|
102
|
+
}
|
|
103
|
+
// The line is a new property.
|
|
104
|
+
property = new property_1.Property(propertyLine, lineNumber, previousProperty);
|
|
105
|
+
if (propertyLine.isContinuing) {
|
|
106
|
+
continue; // Continue parsing the next line.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// If the line does not continue, add the property to the collection.
|
|
110
|
+
this.addToCollection(property);
|
|
111
|
+
previousProperty = property;
|
|
112
|
+
property = undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
116
|
+
finally {
|
|
117
|
+
try {
|
|
118
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
119
|
+
}
|
|
120
|
+
finally { if (e_1) throw e_1.error; }
|
|
121
|
+
}
|
|
122
|
+
};
|
|
14
123
|
/**
|
|
15
124
|
* Add a property object into a properties object collection.
|
|
16
125
|
*
|
|
@@ -18,11 +127,8 @@ var Properties = /** @class */ (function () {
|
|
|
18
127
|
*
|
|
19
128
|
* @returns Undefined so that we conveniently overwrite the property object.
|
|
20
129
|
*/
|
|
21
|
-
Properties.prototype.
|
|
130
|
+
Properties.prototype.addToCollection = function (property) {
|
|
22
131
|
var _a;
|
|
23
|
-
if (property === undefined) {
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
132
|
property.setKeyAndValue();
|
|
27
133
|
if ((_a = this.keyLineNumbers[property.key]) === null || _a === void 0 ? void 0 : _a.length) {
|
|
28
134
|
this.keyLineNumbers[property.key].push(property.startingLineNumber);
|
|
@@ -37,33 +143,52 @@ var Properties = /** @class */ (function () {
|
|
|
37
143
|
}
|
|
38
144
|
// Add the property to the collection.
|
|
39
145
|
this.collection.push(property);
|
|
40
|
-
return undefined;
|
|
41
146
|
};
|
|
42
147
|
/**
|
|
43
148
|
* Get keys that have collisions (more than one occurrence).
|
|
44
149
|
*/
|
|
45
150
|
Properties.prototype.getKeyCollisions = function () {
|
|
151
|
+
var e_2, _a;
|
|
46
152
|
var keyCollisions = [];
|
|
47
|
-
|
|
48
|
-
var _b =
|
|
49
|
-
|
|
50
|
-
|
|
153
|
+
try {
|
|
154
|
+
for (var _b = __values(Object.entries(this.keyLineNumbers)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
155
|
+
var _d = __read(_c.value, 2), key = _d[0], startingLineNumbers = _d[1];
|
|
156
|
+
if (startingLineNumbers.length > 1) {
|
|
157
|
+
keyCollisions.push(new KeyCollisions(key, startingLineNumbers));
|
|
158
|
+
}
|
|
51
159
|
}
|
|
52
160
|
}
|
|
161
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
162
|
+
finally {
|
|
163
|
+
try {
|
|
164
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
165
|
+
}
|
|
166
|
+
finally { if (e_2) throw e_2.error; }
|
|
167
|
+
}
|
|
53
168
|
return keyCollisions;
|
|
54
169
|
};
|
|
55
170
|
/**
|
|
56
|
-
* Get the
|
|
171
|
+
* Get the key/value object representing the properties.
|
|
57
172
|
*
|
|
58
|
-
* @returns A key/value representing the properties
|
|
173
|
+
* @returns A key/value object representing the properties.
|
|
59
174
|
*/
|
|
60
|
-
Properties.prototype.
|
|
175
|
+
Properties.prototype.toObject = function () {
|
|
61
176
|
var keyValueObject = {};
|
|
62
177
|
this.collection.forEach(function (property) {
|
|
63
178
|
keyValueObject[property.key] = property.value;
|
|
64
179
|
});
|
|
65
180
|
return keyValueObject;
|
|
66
181
|
};
|
|
182
|
+
/**
|
|
183
|
+
* Format the object in `.properties`.
|
|
184
|
+
*
|
|
185
|
+
* @param endOfLineCharacter - The character used for end of lines.
|
|
186
|
+
*
|
|
187
|
+
* @returns The object in `.properties` format.
|
|
188
|
+
*/
|
|
189
|
+
Properties.prototype.format = function (endOfLineCharacter) {
|
|
190
|
+
return "".concat(this.hasBom ? exports.BOM : '').concat(this.lines.join(endOfLineCharacter || this.eolCharacter));
|
|
191
|
+
};
|
|
67
192
|
return Properties;
|
|
68
193
|
}());
|
|
69
194
|
exports.Properties = Properties;
|