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.
package/lib/property.d.ts CHANGED
@@ -3,41 +3,57 @@ import { PropertyLine } from './property-line';
3
3
  * Object representing a property (key/value).
4
4
  */
5
5
  export declare class Property {
6
- /** The length of the delimiter, including its whitespace characters. */
7
- delimiterLength: number | undefined;
8
- /** The starting position of the delimiter separating the key from the value. */
9
- delimiterPosition: number | undefined;
10
- /** The property key, including its escaped characters. */
11
- escapedKey: string;
12
- /** The property value, including its escaped characters. */
13
- escapedValue: string;
14
- /** Was the property's key used more than once? */
15
- hasKeyCollisions: boolean;
6
+ /** The content of one or multiple lines when applicable. */
7
+ linesContent: string;
16
8
  /** The property key (unescaped). */
17
9
  key: string;
10
+ /** The property key, including its escaped characters. */
11
+ escapedKey: string;
12
+ /** Is the key empty? */
13
+ private hasNoKey;
14
+ /** Does the key definition spread across multiple lines? */
15
+ private hasMultilineKey;
18
16
  /** Starting line numbers of property objects with the same key. */
19
17
  keyCollisionLines: number[];
20
- /** The content of one or multiple lines when applicable. */
21
- linesContent: string;
22
- /** Positions of the newline characters if any. */
23
- newlinePositions: number[];
24
- /** The line number at which the property starts. */
25
- startingLineNumber: number;
18
+ /** Was the property's key used more than once? */
19
+ hasKeyCollisions: boolean;
20
+ /** The key/value pair separator */
21
+ separator: string | undefined;
22
+ /** The length of the key/value pair separator, including its whitespace characters. */
23
+ separatorLength: number | undefined;
24
+ /** The starting position of the key/value pair separator. */
25
+ separatorPosition: number | undefined;
26
26
  /** The property value (unescaped). */
27
27
  value: string;
28
- /** Does the key definition spread across multiple lines? */
29
- private hasMultilineKey;
30
- /** Is the key empty? */
31
- private hasNoKey;
28
+ /** The starting position of the value. */
29
+ valuePosition: number | undefined;
30
+ /** The property value, including its escaped characters. */
31
+ escapedValue: string;
32
32
  /** Is the value empty? */
33
33
  private hasNoValue;
34
+ /** Positions of the newline characters if any. */
35
+ newlinePositions: number[];
36
+ /** The line number at which the property starts. */
37
+ readonly startingLineNumber: number;
38
+ /** The line number at which the property ends. */
39
+ endingLineNumber: number;
40
+ /** The previous property object if it exists. */
41
+ readonly previousProperty?: Property;
42
+ /** The next property object if it exists. */
43
+ nextProperty?: Property;
34
44
  /**
35
45
  * Create a new property object.
36
46
  *
37
47
  * @param propertyLine - A property line object.
38
48
  * @param startingLineNumber - The line number at which the property starts.
39
49
  */
40
- constructor(propertyLine: PropertyLine, startingLineNumber: number);
50
+ constructor(propertyLine: PropertyLine, startingLineNumber: number, previousProperty?: Property);
51
+ /**
52
+ * Set the next property object.
53
+ *
54
+ * @param property - The next property object
55
+ */
56
+ setNextProperty(property: Property): void;
41
57
  /**
42
58
  * Add the a line to a multiline property object.
43
59
  *
@@ -61,7 +77,7 @@ export declare class Property {
61
77
  */
62
78
  private unescapeLine;
63
79
  /**
64
- * Find the delimiting characters separating the key from the value.
80
+ * Find the character separating the key from the value.
65
81
  */
66
- private findDelimiter;
82
+ private findSeparator;
67
83
  }
package/lib/property.js CHANGED
@@ -12,30 +12,41 @@ var Property = /** @class */ (function () {
12
12
  * @param propertyLine - A property line object.
13
13
  * @param startingLineNumber - The line number at which the property starts.
14
14
  */
15
- function Property(propertyLine, startingLineNumber) {
16
- /** The property key, including its escaped characters. */
17
- this.escapedKey = '';
18
- /** The property value, including its escaped characters. */
19
- this.escapedValue = '';
20
- /** Was the property's key used more than once? */
21
- this.hasKeyCollisions = false;
15
+ function Property(propertyLine, startingLineNumber, previousProperty) {
22
16
  /** The property key (unescaped). */
23
17
  this.key = '';
18
+ /** The property key, including its escaped characters. */
19
+ this.escapedKey = '';
20
+ /** Is the key empty? */
21
+ this.hasNoKey = false;
22
+ /** Does the key definition spread across multiple lines? */
23
+ this.hasMultilineKey = false;
24
24
  /** Starting line numbers of property objects with the same key. */
25
25
  this.keyCollisionLines = [];
26
- /** Positions of the newline characters if any. */
27
- this.newlinePositions = [];
26
+ /** Was the property's key used more than once? */
27
+ this.hasKeyCollisions = false;
28
28
  /** The property value (unescaped). */
29
29
  this.value = '';
30
- /** Does the key definition spread across multiple lines? */
31
- this.hasMultilineKey = false;
32
- /** Is the key empty? */
33
- this.hasNoKey = false;
30
+ /** The property value, including its escaped characters. */
31
+ this.escapedValue = '';
34
32
  /** Is the value empty? */
35
33
  this.hasNoValue = false;
34
+ /** Positions of the newline characters if any. */
35
+ this.newlinePositions = [];
36
36
  this.linesContent = propertyLine.content;
37
37
  this.startingLineNumber = startingLineNumber;
38
+ this.endingLineNumber = startingLineNumber;
39
+ this.previousProperty = previousProperty;
40
+ previousProperty === null || previousProperty === void 0 ? void 0 : previousProperty.setNextProperty(this);
38
41
  }
42
+ /**
43
+ * Set the next property object.
44
+ *
45
+ * @param property - The next property object
46
+ */
47
+ Property.prototype.setNextProperty = function (property) {
48
+ this.nextProperty = property;
49
+ };
39
50
  /**
40
51
  * Add the a line to a multiline property object.
41
52
  *
@@ -44,6 +55,7 @@ var Property = /** @class */ (function () {
44
55
  Property.prototype.addLine = function (propertyLine) {
45
56
  if (this.linesContent.length > 0) {
46
57
  this.newlinePositions.push(this.linesContent.length);
58
+ this.endingLineNumber++;
47
59
  }
48
60
  this.linesContent += propertyLine.content;
49
61
  };
@@ -51,21 +63,21 @@ var Property = /** @class */ (function () {
51
63
  * Set the property's key and value.
52
64
  */
53
65
  Property.prototype.setKeyAndValue = function () {
54
- this.findDelimiter();
55
- if (this.delimiterPosition !== undefined && this.delimiterLength !== undefined) {
66
+ this.findSeparator();
67
+ if (this.separatorPosition !== undefined && this.separatorLength !== undefined) {
56
68
  // Set key if present.
57
69
  if (!this.hasNoKey) {
58
- this.escapedKey = this.linesContent.slice(0, this.delimiterPosition);
70
+ this.escapedKey = this.linesContent.slice(0, this.separatorPosition);
59
71
  this.key = this.unescapeLine(this.escapedKey, this.startingLineNumber);
60
72
  }
61
73
  // Set value if present.
62
74
  if (!this.hasNoValue) {
63
- this.escapedValue = this.linesContent.slice(this.delimiterPosition + this.delimiterLength);
75
+ this.escapedValue = this.linesContent.slice(this.separatorPosition + this.separatorLength);
64
76
  this.value = this.unescapeLine(this.escapedValue, this.startingLineNumber);
65
77
  }
66
78
  }
67
79
  else if (this.hasNoValue) {
68
- // Set key if present (no delimiter).
80
+ // Set key if present (no separator).
69
81
  this.escapedKey = this.linesContent;
70
82
  this.key = this.unescapeLine(this.escapedKey, this.startingLineNumber);
71
83
  }
@@ -90,71 +102,68 @@ var Property = /** @class */ (function () {
90
102
  }
91
103
  };
92
104
  /**
93
- * Find the delimiting characters separating the key from the value.
105
+ * Find the character separating the key from the value.
94
106
  */
95
- Property.prototype.findDelimiter = function () {
107
+ Property.prototype.findSeparator = function () {
96
108
  var _a, _b;
97
- // If the delimiter was already found, skip.
98
- if (this.hasNoKey || this.hasNoValue || this.delimiterPosition) {
109
+ // If the separator was already found, skip.
110
+ if (this.hasNoKey || this.hasNoValue || this.separatorPosition) {
99
111
  return;
100
112
  }
101
113
  for (var character = this.linesContent[0], position = 0; position < this.linesContent.length; position++, character = this.linesContent[position]) {
102
- // If the character is not a delimiter, check the next one.
114
+ // If the character is not a separator, check the next one.
103
115
  if (!/[\t\f :=]/.test(character)) {
104
116
  continue;
105
117
  }
106
- // Check if the delimiter might be escaped.
118
+ // Check if the separator might be escaped.
107
119
  var prefix = position ? this.linesContent.slice(0, position) : '';
108
120
  if (prefix.length > 0) {
109
121
  var backslashMatch = prefix.match(/(?<backslashes>\\+)$/);
110
122
  if (backslashMatch === null || backslashMatch === void 0 ? void 0 : backslashMatch.groups) {
111
- var delimiterIsEscaped = !!(backslashMatch.groups.backslashes.length % 2);
112
- if (delimiterIsEscaped) {
113
- // If the delimiter is escaped, check the next character.
123
+ var separatorIsEscaped = !!(backslashMatch.groups.backslashes.length % 2);
124
+ if (separatorIsEscaped) {
125
+ // If the separator is escaped, check the next character.
114
126
  continue;
115
127
  }
116
128
  }
117
129
  }
118
- var delimiter = '';
119
- this.delimiterPosition = position;
120
- this.hasMultilineKey = !!(this.newlinePositions.length > 0 && this.newlinePositions[0] > position);
121
- // Check if the delimiter starts with a whitespace.
130
+ var separator = '';
131
+ this.separatorPosition = position;
132
+ // Check if the separator starts with a whitespace.
122
133
  var nextContent = this.linesContent.slice(position);
123
134
  var leadingWhitespaceMatch = nextContent.match(/^(?<whitespace>\s+)/);
124
135
  var leadingWhitespace = ((_a = leadingWhitespaceMatch === null || leadingWhitespaceMatch === void 0 ? void 0 : leadingWhitespaceMatch.groups) === null || _a === void 0 ? void 0 : _a.whitespace) || '';
125
136
  // If there is a whitespace, move to the next character.
126
137
  if (leadingWhitespace.length > 0) {
127
- delimiter += leadingWhitespace;
138
+ separator += leadingWhitespace;
128
139
  nextContent = nextContent.slice(leadingWhitespace.length);
129
140
  }
130
141
  // Check if there is an equal or colon character.
131
142
  if (/[:=]/.test(nextContent[0])) {
132
- delimiter += nextContent[0];
143
+ separator += nextContent[0];
133
144
  nextContent = nextContent.slice(1);
134
145
  // If an equal or colon character was found, try to get trailing whitespace.
135
146
  var trailingWhitespaceMatch = nextContent.match(/^(?<whitespace>\s+)/);
136
147
  var trailingWhitespace = ((_b = trailingWhitespaceMatch === null || trailingWhitespaceMatch === void 0 ? void 0 : trailingWhitespaceMatch.groups) === null || _b === void 0 ? void 0 : _b.whitespace) || '';
137
- delimiter += trailingWhitespace;
148
+ separator += trailingWhitespace;
138
149
  }
139
- this.delimiterLength = delimiter.length;
140
- // If the line starts with a delimiter, the property has no key.
150
+ this.separatorLength = separator.length;
151
+ this.valuePosition = this.separatorPosition + this.separatorLength;
152
+ this.separator = this.linesContent.slice(this.separatorPosition, this.separatorPosition + this.separatorLength);
153
+ // If the line starts with a separator, the property has no key.
141
154
  if (!position) {
142
155
  this.hasNoKey = true;
143
156
  }
144
157
  break;
145
158
  }
146
- // If there was no delimiter found, the property has no value.
147
- if (this.delimiterPosition === undefined) {
159
+ if (this.separatorPosition === undefined) {
160
+ // If there was no separator found, the property has no value.
148
161
  this.hasNoValue = true;
149
162
  }
150
- else {
151
- // If the delimiter is after the first newline, mark the key as multiline.
152
- if (this.newlinePositions.length > 0) {
153
- var firstLinePosition = this.newlinePositions[0];
154
- if (firstLinePosition > this.delimiterPosition) {
155
- this.hasMultilineKey = true;
156
- }
157
- }
163
+ else if (this.newlinePositions.length > 0 &&
164
+ this.newlinePositions[0] < this.separatorPosition) {
165
+ // If the separator is after the first newline, the key is on multiple lines.
166
+ this.hasMultilineKey = true;
158
167
  }
159
168
  };
160
169
  return Property;
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "properties-file",
3
- "version": "2.2.4",
4
- "description": ".properties file parser, JSON converter and Webpack loader.",
3
+ "version": "3.1.0",
4
+ "description": ".properties file parser, editor, formatter and Webpack loader.",
5
5
  "keywords": [
6
6
  ".properties",
7
7
  "properties",
8
8
  ".properties file",
9
9
  "properties file",
10
10
  "parser",
11
+ "editor",
12
+ "formatter",
11
13
  "Java",
12
14
  "intl",
13
15
  "i18n",
@@ -23,7 +25,7 @@
23
25
  "author": "Avansai (https://avansai.com)",
24
26
  "exports": {
25
27
  ".": "./lib/index.js",
26
- "./content": "./lib/content/index.js",
28
+ "./editor": "./lib/editor/index.js",
27
29
  "./escape": "./lib/escape/index.js",
28
30
  "./unescape": "./lib/unescape/index.js",
29
31
  "./webpack-loader": "./lib/loader/webpack.js"
@@ -32,8 +34,8 @@
32
34
  "types": "lib/index.d.ts",
33
35
  "typesVersions": {
34
36
  "*": {
35
- "content": [
36
- "lib/content/index.d.ts"
37
+ "editor": [
38
+ "lib/editor/index.d.ts"
37
39
  ],
38
40
  "escape": [
39
41
  "lib/escape/index.d.ts"
@@ -50,9 +52,9 @@
50
52
  "lib"
51
53
  ],
52
54
  "scripts": {
53
- "add-import-type": "node ./src/add-import-type.mjs",
55
+ "add-import-type": "ts-node ./src/add-import-type.ts && rm -f ./lib/add-import-type.*",
54
56
  "build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc && npm run add-import-type && npm run test",
55
- "ci": "npm run lint-check && rm -Rf ./lib && tsc && npm run add-import-type && npm run test",
57
+ "ci": "npm run build",
56
58
  "lint-check": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --ext .json .",
57
59
  "lint-fix": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --ext .json --fix .",
58
60
  "lint-print-config": "eslint --print-config ./eslintrc.yaml",
@@ -62,11 +64,11 @@
62
64
  },
63
65
  "devDependencies": {
64
66
  "@release-it/conventional-changelog": "5.1.1",
65
- "@types/jest": "29.5.0",
66
- "@typescript-eslint/eslint-plugin": "5.58.0",
67
- "@typescript-eslint/parser": "5.58.0",
67
+ "@types/jest": "29.5.1",
68
+ "@typescript-eslint/eslint-plugin": "5.59.1",
69
+ "@typescript-eslint/parser": "5.59.1",
68
70
  "dotenv-cli": "7.2.1",
69
- "eslint": "8.38.0",
71
+ "eslint": "8.39.0",
70
72
  "eslint-config-prettier": "8.8.0",
71
73
  "eslint-import-resolver-node": "0.3.7",
72
74
  "eslint-import-resolver-typescript": "3.5.5",
@@ -78,7 +80,7 @@
78
80
  "eslint-plugin-tsdoc": "0.2.17",
79
81
  "eslint-plugin-unicorn": "46.0.0",
80
82
  "jest": "29.5.0",
81
- "prettier": "2.8.7",
83
+ "prettier": "2.8.8",
82
84
  "prettier-plugin-organize-imports": "3.2.2",
83
85
  "prettier-plugin-sh": "0.12.8",
84
86
  "release-it": "15.10.1",
@@ -1,18 +0,0 @@
1
- import { KeyValueObject } from '../';
2
- import { Properties } from '../properties';
3
- /**
4
- * Get a `Properties` object from the content of a `.properties` file.
5
- *
6
- * @param content - the content of a `.properties` file.
7
- *
8
- * @returns A `Properties` object representing the content of a `.properties` file.
9
- */
10
- export declare const getProperties: (content: string) => Properties;
11
- /**
12
- * Converts the content of a `.properties` file to JSON.
13
- *
14
- * @param content - the content of a `.properties` file.
15
- *
16
- * @returns A (JSON) key/value object representing the content of a `.properties` file.
17
- */
18
- export declare const propertiesToJson: (content: string) => KeyValueObject;
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.propertiesToJson = exports.getProperties = void 0;
4
- var properties_1 = require("../properties");
5
- var property_1 = require("../property");
6
- var property_line_1 = require("../property-line");
7
- /**
8
- * Get a `Properties` object from the content of a `.properties` file.
9
- *
10
- * @param content - the content of a `.properties` file.
11
- *
12
- * @returns A `Properties` object representing the content of a `.properties` file.
13
- */
14
- var getProperties = function (content) {
15
- // Remove BOM character if present and create an array from lines.
16
- var lines = (content.codePointAt(0) === 0xfeff ? content.slice(1) : content).split(/\r?\n/);
17
- /** Line number while parsing properties file content. */
18
- var lineNumber = 0;
19
- /** The current property object being parsed. */
20
- var property;
21
- /** The collection of property objects. */
22
- var properties = new properties_1.Properties();
23
- for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
24
- var line = lines_1[_i];
25
- lineNumber++;
26
- var propertyLine = new property_line_1.PropertyLine(line, !!property);
27
- if (property) {
28
- // Continue parsing an existing property.
29
- property.addLine(propertyLine);
30
- if (propertyLine.isContinuing) {
31
- continue;
32
- }
33
- }
34
- else {
35
- // Check if the line is a new property.
36
- if (propertyLine.isComment || propertyLine.isBlank) {
37
- continue; // Skip line if its a comment or blank.
38
- }
39
- // The line is a new property.
40
- property = new property_1.Property(propertyLine, lineNumber);
41
- if (propertyLine.isContinuing) {
42
- continue; // Continue parsing the next line.
43
- }
44
- }
45
- // If the line does not continue, add the property to the collection.
46
- property = properties.add(property);
47
- }
48
- return properties;
49
- };
50
- exports.getProperties = getProperties;
51
- /**
52
- * Converts the content of a `.properties` file to JSON.
53
- *
54
- * @param content - the content of a `.properties` file.
55
- *
56
- * @returns A (JSON) key/value object representing the content of a `.properties` file.
57
- */
58
- var propertiesToJson = function (content) { return (0, exports.getProperties)(content).toJson(); };
59
- exports.propertiesToJson = propertiesToJson;
@@ -1,21 +0,0 @@
1
- /// <reference types="node" />
2
- import { KeyValueObject, Properties } from '../';
3
- export { KeyValueObject } from '../';
4
- /**
5
- * Get a `Properties` object from the content of a `.properties` file.
6
- *
7
- * @param filePath - The file path of the `.properties` file.
8
- * @param encoding - The encoding of the file to parse (default is UTF-8).
9
- *
10
- * @returns A `Properties` object representing the content of a `.properties` file.
11
- */
12
- export declare const getProperties: (filePath: string, encoding?: BufferEncoding) => Properties;
13
- /**
14
- * Converts the content of a `.properties` file to JSON.
15
- *
16
- * @param filePath - The file path of the `.properties` file.
17
- * @param encoding - The encoding of the file to parse (default is UTF-8).
18
- *
19
- * @returns A (JSON) key/value object representing the content of a `.properties` file.
20
- */
21
- export declare const propertiesToJson: (filePath: string, encoding?: BufferEncoding) => KeyValueObject;
package/lib/file/index.js DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.propertiesToJson = exports.getProperties = void 0;
4
- var node_fs_1 = require("node:fs");
5
- var content_1 = require("../content/");
6
- /**
7
- * Get a `Properties` object from the content of a `.properties` file.
8
- *
9
- * @param filePath - The file path of the `.properties` file.
10
- * @param encoding - The encoding of the file to parse (default is UTF-8).
11
- *
12
- * @returns A `Properties` object representing the content of a `.properties` file.
13
- */
14
- var getProperties = function (filePath, encoding) {
15
- // No need to check if the file exists first since this will already throw an error.
16
- return (0, content_1.getProperties)((0, node_fs_1.readFileSync)(filePath, encoding !== null && encoding !== void 0 ? encoding : 'utf8'));
17
- };
18
- exports.getProperties = getProperties;
19
- /**
20
- * Converts the content of a `.properties` file to JSON.
21
- *
22
- * @param filePath - The file path of the `.properties` file.
23
- * @param encoding - The encoding of the file to parse (default is UTF-8).
24
- *
25
- * @returns A (JSON) key/value object representing the content of a `.properties` file.
26
- */
27
- var propertiesToJson = function (filePath, encoding) {
28
- return (0, exports.getProperties)(filePath, encoding).toJson();
29
- };
30
- exports.propertiesToJson = propertiesToJson;