properties-file 2.2.4 → 3.1.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.
@@ -0,0 +1,286 @@
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
+ * @returns True if the key was inserted, otherwise false.
71
+ */
72
+ PropertiesEditor.prototype.insert = function (key, value, options) {
73
+ var _a;
74
+ var _b, _c;
75
+ var escapeUnicode = (options === null || options === void 0 ? void 0 : options.escapeUnicode) || false;
76
+ var separator = (options === null || options === void 0 ? void 0 : options.separator)
77
+ ? options.separator === ' '
78
+ ? ' '
79
+ : " ".concat(options.separator, " ")
80
+ : " ".concat(exports.DEFAULT_SEPARATOR, " ").replace(' ', ' ');
81
+ var referenceKey = options === null || options === void 0 ? void 0 : options.referenceKey;
82
+ var position = (options === null || options === void 0 ? void 0 : options.position) || 'after';
83
+ // Allow multiline keys.
84
+ var multilineKey = key
85
+ .split(/\r?\n/)
86
+ .map(function (key) { return (0, escape_1.escapeKey)(key, escapeUnicode); })
87
+ .join('\\\n');
88
+ // Allow multiline values.
89
+ var multilineValue = value
90
+ .split(/\r?\n/)
91
+ .map(function (value) { return (0, escape_1.escapeValue)(value, escapeUnicode); })
92
+ .join('\\\n');
93
+ // Allow multiline comments.
94
+ var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " ");
95
+ var multilineComment = (options === null || options === void 0 ? void 0 : options.comment) === undefined
96
+ ? ''
97
+ : "".concat("".concat(commentPrefix).concat(options.comment).split(/\r?\n/).join("\n".concat(commentPrefix)), "\n");
98
+ var newLines = "".concat(multilineComment).concat(multilineKey).concat(separator).concat(multilineValue).split(/\n/);
99
+ if (referenceKey === undefined) {
100
+ // Insert the new property at the end if the reference key was not defined.
101
+ (_a = this.lines).push.apply(_a, __spreadArray([], __read(newLines), false));
102
+ this.parseLines();
103
+ return true;
104
+ }
105
+ else {
106
+ // Find the last occurrence of the reference key.
107
+ var property = __spreadArray([], __read(this.collection), false).reverse()
108
+ .find(function (property) { return property.key === referenceKey; });
109
+ // Insert the new property when a reference key defined only when found.
110
+ if (property) {
111
+ var insertPosition = position === 'after'
112
+ ? property.endingLineNumber
113
+ : (_c = (_b = property.previousProperty) === null || _b === void 0 ? void 0 : _b.endingLineNumber) !== null && _c !== void 0 ? _c : 0;
114
+ this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, insertPosition)), false), __read(newLines), false), __read(this.lines.slice(insertPosition)), false);
115
+ this.parseLines();
116
+ return true;
117
+ }
118
+ return false;
119
+ }
120
+ };
121
+ /**
122
+ * Insert a new comment in the existing object (by default it will be at the end).
123
+ *
124
+ * @param comment - The comment to add.
125
+ * @param options - Additional options.
126
+ *
127
+ * @returns True if the comment was inserted, otherwise false.
128
+ */
129
+ PropertiesEditor.prototype.insertComment = function (comment, options) {
130
+ var _a;
131
+ var _b, _c;
132
+ var referenceKey = options === null || options === void 0 ? void 0 : options.referenceKey;
133
+ var position = (options === null || options === void 0 ? void 0 : options.position) || 'after';
134
+ // Allow multiline comments.
135
+ var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " ");
136
+ var newLines = "".concat(commentPrefix).concat(comment)
137
+ .replace(/\r?\n/g, "\n".concat(commentPrefix))
138
+ .split(/\n/);
139
+ if (referenceKey === undefined) {
140
+ // Insert the new comment at the end if the reference key was not defined.
141
+ (_a = this.lines).push.apply(_a, __spreadArray([], __read(newLines), false));
142
+ this.parseLines();
143
+ return true;
144
+ }
145
+ else {
146
+ // Find the last occurrence of the reference key.
147
+ var property = __spreadArray([], __read(this.collection), false).reverse()
148
+ .find(function (property) { return property.key === referenceKey; });
149
+ // Insert the new comment when a reference key defined only when found.
150
+ if (property) {
151
+ var insertPosition = position === 'after'
152
+ ? property.endingLineNumber
153
+ : (_c = (_b = property.previousProperty) === null || _b === void 0 ? void 0 : _b.endingLineNumber) !== null && _c !== void 0 ? _c : 0;
154
+ this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, insertPosition)), false), __read(newLines), false), __read(this.lines.slice(insertPosition)), false);
155
+ this.parseLines();
156
+ return true;
157
+ }
158
+ return false;
159
+ }
160
+ };
161
+ /**
162
+ * Delete the last occurrence of a given key from the existing object.
163
+ *
164
+ * @param key - The name of the key to delete.
165
+ * @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted.
166
+ *
167
+ * @returns True if the key was deleted, otherwise false.
168
+ */
169
+ PropertiesEditor.prototype.delete = function (key, deleteCommentsAndWhiteSpace) {
170
+ var _a, _b;
171
+ if (deleteCommentsAndWhiteSpace === void 0) { deleteCommentsAndWhiteSpace = true; }
172
+ // Find the last occurrence of the key.
173
+ var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; });
174
+ if (property) {
175
+ var startLine = deleteCommentsAndWhiteSpace
176
+ ? (_b = (_a = property.previousProperty) === null || _a === void 0 ? void 0 : _a.endingLineNumber) !== null && _b !== void 0 ? _b : 0
177
+ : property.startingLineNumber - 1;
178
+ var endLine = property.endingLineNumber;
179
+ this.lines = __spreadArray(__spreadArray([], __read(this.lines.slice(0, startLine)), false), __read(this.lines.slice(endLine)), false);
180
+ this.parseLines();
181
+ return true;
182
+ }
183
+ return false;
184
+ };
185
+ /**
186
+ * Restore the original newline characters of a key.
187
+ *
188
+ * @param property - A property object.
189
+ *
190
+ * @returns The key with its original newlines characters restored.
191
+ */
192
+ PropertiesEditor.prototype.getKeyWithNewlines = function (property) {
193
+ return property.newlinePositions.length === 0
194
+ ? property.key
195
+ : // eslint-disable-next-line unicorn/no-array-reduce
196
+ __spreadArray([], __read(property.key), false).reduce(function (accumulator, character, index) {
197
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
198
+ return "".concat(accumulator).concat(property.newlinePositions.includes(index) ? '\n' : '').concat(character);
199
+ }, '');
200
+ };
201
+ /**
202
+ * Restore the original newline characters of a value.
203
+ *
204
+ * @param property - A property object.
205
+ *
206
+ * @returns The value with its original newlines characters restored.
207
+ */
208
+ PropertiesEditor.prototype.getValueWithNewlines = function (property) {
209
+ return property.newlinePositions.length === 0 || property.valuePosition === undefined
210
+ ? property.value
211
+ : // eslint-disable-next-line unicorn/no-array-reduce
212
+ __spreadArray([], __read(property.value), false).reduce(function (accumulator, character, index) {
213
+ return "".concat(accumulator).concat(property.newlinePositions.includes(index + property.valuePosition)
214
+ ? '\n'
215
+ : ''
216
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
217
+ ).concat(character);
218
+ }, '');
219
+ };
220
+ /**
221
+ * Update the last occurrence of a given key from the existing object.
222
+ *
223
+ * @param key - The name of the key to update.
224
+ * @param options - Additional options.
225
+ *
226
+ * @returns True if the key was updated, otherwise false.
227
+ */
228
+ PropertiesEditor.prototype.update = function (key, options) {
229
+ var _a, _b, _c, _d;
230
+ // Find the last occurrence of the key to update.
231
+ var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; });
232
+ if (!property || !options) {
233
+ return false;
234
+ }
235
+ var escapeUnicode = options.escapeUnicode || false;
236
+ var separator = options.separator
237
+ ? options.separator === ' '
238
+ ? ' '
239
+ : " ".concat(options.separator, " ")
240
+ : property.separator || " ".concat(exports.DEFAULT_SEPARATOR, " ").replace(' ', ' ');
241
+ // Allow multiline keys.
242
+ var multilineKey = ((_a = options.newKey) !== null && _a !== void 0 ? _a : this.getKeyWithNewlines(property))
243
+ .split(/\r?\n/)
244
+ .map(function (key) { return (0, escape_1.escapeKey)(key, escapeUnicode); })
245
+ .join('\\\n');
246
+ // Allow multiline values.
247
+ var multilineValue = ((_b = options.newValue) !== null && _b !== void 0 ? _b : this.getValueWithNewlines(property))
248
+ .split(/\r?\n/)
249
+ .map(function (value) { return (0, escape_1.escapeValue)(value, escapeUnicode); })
250
+ .join('\\\n');
251
+ // Allow multiline comments.
252
+ var commentPrefix = "".concat(options.commentDelimiter || exports.DEFAULT_COMMENT_DELIMITER, " ");
253
+ var multilineComment = options.newComment === undefined
254
+ ? ''
255
+ : "".concat("".concat(commentPrefix).concat(options.newComment).split(/\r?\n/).join("\n".concat(commentPrefix)), "\n");
256
+ var newLines = "".concat(multilineComment).concat(multilineKey).concat(separator).concat(multilineValue).split(/\n/);
257
+ // Replace the existing property with the new one.
258
+ this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, options.newComment === undefined
259
+ ? property.startingLineNumber - 1
260
+ : (_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);
261
+ this.parseLines();
262
+ return true;
263
+ };
264
+ /**
265
+ * Update a key if it exist, otherwise add it at the end.
266
+ *
267
+ * @param key - A property key (unescaped).
268
+ * @param value - A property value (unescaped).
269
+ * @param options - Additional options.
270
+ *
271
+ * @returns True if the key was updated or inserted, otherwise false.
272
+ */
273
+ PropertiesEditor.prototype.upsert = function (key, value, options) {
274
+ return this.keyLineNumbers[key]
275
+ ? this.update(key, {
276
+ newValue: value,
277
+ newComment: options === null || options === void 0 ? void 0 : options.comment,
278
+ commentDelimiter: options === null || options === void 0 ? void 0 : options.commentDelimiter,
279
+ separator: options === null || options === void 0 ? void 0 : options.separator,
280
+ escapeUnicode: options === null || options === void 0 ? void 0 : options.escapeUnicode,
281
+ })
282
+ : this.insert(key, value, options);
283
+ };
284
+ return PropertiesEditor;
285
+ }(properties_1.Properties));
286
+ exports.PropertiesEditor = PropertiesEditor;
package/lib/index.d.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  /// <reference types="./properties-file" />
2
2
 
3
- export { getProperties, propertiesToJson } from './file';
4
- export { KeyLineNumbers, Properties } from './properties';
5
- export { Property } from './property';
6
- export { PropertyLine } from './property-line';
3
+ /// <reference types="node" />
4
+ export { Properties } from './properties';
7
5
  /**
8
- * A simple "key/value" object.
6
+ * A key-value pair object.
9
7
  */
10
- export type KeyValueObject = {
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.PropertyLine = exports.Property = exports.Properties = exports.propertiesToJson = exports.getProperties = void 0;
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
- Object.defineProperty(exports, "Properties", { enumerable: true, get: function () { return properties_1.Properties; } });
9
- var property_1 = require("./property");
10
- Object.defineProperty(exports, "Property", { enumerable: true, get: function () { return property_1.Property; } });
11
- var property_line_1 = require("./property-line");
12
- Object.defineProperty(exports, "PropertyLine", { enumerable: true, get: function () { return property_line_1.PropertyLine; } });
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;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var content_1 = require("../content");
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, content_1.propertiesToJson)(content)), ";");
12
+ return "module.exports = ".concat(JSON.stringify((0, __1.getProperties)(content)), ";");
13
13
  };
14
14
  exports.default = webpackLoader;
@@ -1,13 +1,45 @@
1
- import { KeyValueObject } from './';
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";
11
+ /**
12
+ * Get the first end of line (EOL) character from multiline content.
13
+ *
14
+ * @param content - The content of a `.properties` file.
15
+ *
16
+ * @returns The multiline content's first end of line (EOL) character.
17
+ */
18
+ export declare const getFirstEolCharacter: (content: string) => string | undefined;
3
19
  /**
4
20
  * A class representing the content of a .properties file.
5
21
  */
6
22
  export declare class Properties {
23
+ /** Does the .properties content starts with a BOM character? */
24
+ readonly hasBom: boolean;
25
+ /** The end of line character. */
26
+ readonly eolCharacter: string;
27
+ /** `.properties` content split by line. */
28
+ protected lines: string[];
7
29
  /** The collection of property object. */
8
30
  collection: Property[];
9
31
  /** Object associating keys with their starting line numbers. */
10
32
  keyLineNumbers: KeyLineNumbers;
33
+ /**
34
+ * Create `Properties` object.
35
+ *
36
+ * @param content - The content of a `.properties` file.
37
+ */
38
+ constructor(content: string | Buffer);
39
+ /**
40
+ * Parse the `.properties` content line by line.
41
+ */
42
+ protected parseLines(): void;
11
43
  /**
12
44
  * Add a property object into a properties object collection.
13
45
  *
@@ -15,17 +47,25 @@ export declare class Properties {
15
47
  *
16
48
  * @returns Undefined so that we conveniently overwrite the property object.
17
49
  */
18
- add(property: Property | undefined): undefined;
50
+ private addToCollection;
19
51
  /**
20
52
  * Get keys that have collisions (more than one occurrence).
21
53
  */
22
54
  getKeyCollisions(): KeyCollisions[];
23
55
  /**
24
- * Get the JSON (key/value) representation of the properties.
56
+ * Get the key/value object representing the properties.
57
+ *
58
+ * @returns A key/value object representing the properties.
59
+ */
60
+ toObject(): KeyValuePairObject;
61
+ /**
62
+ * Format the object in `.properties`.
63
+ *
64
+ * @param endOfLineCharacter - The character used for end of lines.
25
65
  *
26
- * @returns A key/value representing the properties of the object.
66
+ * @returns The object in `.properties` format.
27
67
  */
28
- toJson(): KeyValueObject;
68
+ format(endOfLineCharacter?: '\n' | '\r\n'): string;
29
69
  }
30
70
  /**
31
71
  * Object associating keys with their line numbers.
package/lib/properties.js CHANGED
@@ -1,16 +1,126 @@
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.getFirstEolCharacter = 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
+ };
51
+ exports.getFirstEolCharacter = getFirstEolCharacter;
4
52
  /**
5
53
  * A class representing the content of a .properties file.
6
54
  */
7
55
  var Properties = /** @class */ (function () {
8
- function Properties() {
56
+ /**
57
+ * Create `Properties` object.
58
+ *
59
+ * @param content - The content of a `.properties` file.
60
+ */
61
+ function Properties(content) {
62
+ var _a;
9
63
  /** The collection of property object. */
10
64
  this.collection = [];
11
65
  /** Object associating keys with their starting line numbers. */
12
66
  this.keyLineNumbers = {};
67
+ var stringContent = Buffer.isBuffer(content) ? content.toString() : content;
68
+ this.hasBom = stringContent.codePointAt(0) === exports.BOM_CODE_POINT;
69
+ this.eolCharacter = (_a = (0, exports.getFirstEolCharacter)(stringContent)) !== null && _a !== void 0 ? _a : exports.DEFAULT_END_OF_LINE_CHARACTER;
70
+ this.lines = (this.hasBom ? stringContent.slice(1) : stringContent).split(/\r?\n/);
71
+ this.parseLines();
13
72
  }
73
+ /**
74
+ * Parse the `.properties` content line by line.
75
+ */
76
+ Properties.prototype.parseLines = function () {
77
+ var e_1, _a;
78
+ /** reset existing object properties to their initial values. */
79
+ this.collection = [];
80
+ this.keyLineNumbers = {};
81
+ /** Line number while parsing properties file content. */
82
+ var lineNumber = 0;
83
+ /** The current property object being parsed. */
84
+ var property;
85
+ /** The previous property object that was parsed. */
86
+ var previousProperty;
87
+ try {
88
+ for (var _b = __values(this.lines), _c = _b.next(); !_c.done; _c = _b.next()) {
89
+ var line = _c.value;
90
+ lineNumber++;
91
+ var propertyLine = new property_line_1.PropertyLine(line, !!property);
92
+ if (property) {
93
+ // Continue parsing an existing property.
94
+ property.addLine(propertyLine);
95
+ if (propertyLine.isContinuing) {
96
+ continue;
97
+ }
98
+ }
99
+ else {
100
+ // Check if the line is a new property.
101
+ if (propertyLine.isComment || propertyLine.isBlank) {
102
+ continue; // Skip line if its a comment or blank.
103
+ }
104
+ // The line is a new property.
105
+ property = new property_1.Property(propertyLine, lineNumber, previousProperty);
106
+ if (propertyLine.isContinuing) {
107
+ continue; // Continue parsing the next line.
108
+ }
109
+ }
110
+ // If the line does not continue, add the property to the collection.
111
+ this.addToCollection(property);
112
+ previousProperty = property;
113
+ property = undefined;
114
+ }
115
+ }
116
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
117
+ finally {
118
+ try {
119
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
120
+ }
121
+ finally { if (e_1) throw e_1.error; }
122
+ }
123
+ };
14
124
  /**
15
125
  * Add a property object into a properties object collection.
16
126
  *
@@ -18,17 +128,14 @@ var Properties = /** @class */ (function () {
18
128
  *
19
129
  * @returns Undefined so that we conveniently overwrite the property object.
20
130
  */
21
- Properties.prototype.add = function (property) {
131
+ Properties.prototype.addToCollection = function (property) {
22
132
  var _a;
23
- if (property === undefined) {
24
- return undefined;
25
- }
26
133
  property.setKeyAndValue();
27
134
  if ((_a = this.keyLineNumbers[property.key]) === null || _a === void 0 ? void 0 : _a.length) {
28
135
  this.keyLineNumbers[property.key].push(property.startingLineNumber);
29
136
  property.hasKeyCollisions = true;
30
137
  property.keyCollisionLines = this.keyLineNumbers[property.key];
31
- // Remove collision so that we can overwrite it with the latest object.
138
+ // Remove the collision from the collection (we only keep latest value).
32
139
  this.collection = this.collection.filter(function (existingPropertyObject) { return existingPropertyObject.key !== property.key; });
33
140
  }
34
141
  else {
@@ -37,33 +144,52 @@ var Properties = /** @class */ (function () {
37
144
  }
38
145
  // Add the property to the collection.
39
146
  this.collection.push(property);
40
- return undefined;
41
147
  };
42
148
  /**
43
149
  * Get keys that have collisions (more than one occurrence).
44
150
  */
45
151
  Properties.prototype.getKeyCollisions = function () {
152
+ var e_2, _a;
46
153
  var keyCollisions = [];
47
- for (var _i = 0, _a = Object.entries(this.keyLineNumbers); _i < _a.length; _i++) {
48
- var _b = _a[_i], key = _b[0], startingLineNumbers = _b[1];
49
- if (startingLineNumbers.length > 1) {
50
- keyCollisions.push(new KeyCollisions(key, startingLineNumbers));
154
+ try {
155
+ for (var _b = __values(Object.entries(this.keyLineNumbers)), _c = _b.next(); !_c.done; _c = _b.next()) {
156
+ var _d = __read(_c.value, 2), key = _d[0], startingLineNumbers = _d[1];
157
+ if (startingLineNumbers.length > 1) {
158
+ keyCollisions.push(new KeyCollisions(key, startingLineNumbers));
159
+ }
51
160
  }
52
161
  }
162
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
163
+ finally {
164
+ try {
165
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
166
+ }
167
+ finally { if (e_2) throw e_2.error; }
168
+ }
53
169
  return keyCollisions;
54
170
  };
55
171
  /**
56
- * Get the JSON (key/value) representation of the properties.
172
+ * Get the key/value object representing the properties.
57
173
  *
58
- * @returns A key/value representing the properties of the object.
174
+ * @returns A key/value object representing the properties.
59
175
  */
60
- Properties.prototype.toJson = function () {
176
+ Properties.prototype.toObject = function () {
61
177
  var keyValueObject = {};
62
178
  this.collection.forEach(function (property) {
63
179
  keyValueObject[property.key] = property.value;
64
180
  });
65
181
  return keyValueObject;
66
182
  };
183
+ /**
184
+ * Format the object in `.properties`.
185
+ *
186
+ * @param endOfLineCharacter - The character used for end of lines.
187
+ *
188
+ * @returns The object in `.properties` format.
189
+ */
190
+ Properties.prototype.format = function (endOfLineCharacter) {
191
+ return "".concat(this.hasBom ? exports.BOM : '').concat(this.lines.join(endOfLineCharacter || this.eolCharacter));
192
+ };
67
193
  return Properties;
68
194
  }());
69
195
  exports.Properties = Properties;