properties-file 2.0.9 → 2.1.2

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.
@@ -13,7 +13,7 @@ var property_line_1 = require("../property-line");
13
13
  */
14
14
  function getProperties(content) {
15
15
  // Remove BOM character if present and create an array from lines.
16
- var lines = (content.charCodeAt(0) === 0xfeff ? content.slice(1) : content).split(/\r?\n/);
16
+ var lines = (content.codePointAt(0) === 0xfeff ? content.slice(1) : content).split(/\r?\n/);
17
17
  /** Line number while parsing properties file content. */
18
18
  var lineNumber = 0;
19
19
  /** The current property object being parsed. */
package/lib/file/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.propertiesToJson = exports.getProperties = void 0;
4
- var fs_1 = require("fs");
4
+ var node_fs_1 = require("node:fs");
5
5
  var content_1 = require("../content/");
6
6
  /**
7
7
  * Get a `Properties` object from the content of a `.properties` file.
@@ -12,10 +12,10 @@ var content_1 = require("../content/");
12
12
  * @returns A `Properties` object representing the content of a `.properties` file.
13
13
  */
14
14
  function getProperties(filePath, encoding) {
15
- if (!(0, fs_1.existsSync)(filePath)) {
16
- throw Error("file not found at ".concat(filePath));
15
+ if (!(0, node_fs_1.existsSync)(filePath)) {
16
+ throw new Error("file not found at ".concat(filePath));
17
17
  }
18
- return (0, content_1.getProperties)((0, fs_1.readFileSync)(filePath, encoding ? encoding : 'utf-8'));
18
+ return (0, content_1.getProperties)((0, node_fs_1.readFileSync)(filePath, encoding !== null && encoding !== void 0 ? encoding : 'utf8'));
19
19
  }
20
20
  exports.getProperties = getProperties;
21
21
  /**
@@ -4,10 +4,10 @@ import { Property } from './property';
4
4
  * A class representing the content of a .properties file.
5
5
  */
6
6
  export declare class Properties {
7
- /** Object associating keys with their starting line numbers. */
8
- keyLineNumbers: KeyLineNumbers;
9
7
  /** The collection of property object. */
10
8
  collection: Property[];
9
+ /** Object associating keys with their starting line numbers. */
10
+ keyLineNumbers: KeyLineNumbers;
11
11
  /**
12
12
  * Add a property object into a properties object collection.
13
13
  *
@@ -16,16 +16,16 @@ export declare class Properties {
16
16
  * @returns Undefined so that we conveniently overwrite the property object.
17
17
  */
18
18
  add(property: Property | undefined): undefined;
19
+ /**
20
+ * Get keys that have collisions (more than one occurrence).
21
+ */
22
+ getKeyCollisions(): KeyCollisions[];
19
23
  /**
20
24
  * Get the JSON (key/value) representation of the properties.
21
25
  *
22
26
  * @returns A key/value representing the properties of the object.
23
27
  */
24
28
  toJson(): KeyValueObject;
25
- /**
26
- * Get keys that have collisions (more than one occurrence).
27
- */
28
- getKeyCollisions(): KeyCollisions[];
29
29
  }
30
30
  /**
31
31
  * Object associating keys with their line numbers.
package/lib/properties.js CHANGED
@@ -6,10 +6,10 @@ exports.KeyCollisions = exports.Properties = void 0;
6
6
  */
7
7
  var Properties = /** @class */ (function () {
8
8
  function Properties() {
9
- /** Object associating keys with their starting line numbers. */
10
- this.keyLineNumbers = {};
11
9
  /** The collection of property object. */
12
10
  this.collection = [];
11
+ /** Object associating keys with their starting line numbers. */
12
+ this.keyLineNumbers = {};
13
13
  }
14
14
  /**
15
15
  * Add a property object into a properties object collection.
@@ -38,18 +38,6 @@ var Properties = /** @class */ (function () {
38
38
  this.collection.push(property);
39
39
  return undefined;
40
40
  };
41
- /**
42
- * Get the JSON (key/value) representation of the properties.
43
- *
44
- * @returns A key/value representing the properties of the object.
45
- */
46
- Properties.prototype.toJson = function () {
47
- var keyValueObject = {};
48
- this.collection.forEach(function (property) {
49
- keyValueObject[property.key] = property.value;
50
- });
51
- return keyValueObject;
52
- };
53
41
  /**
54
42
  * Get keys that have collisions (more than one occurrence).
55
43
  */
@@ -63,6 +51,18 @@ var Properties = /** @class */ (function () {
63
51
  }
64
52
  return keyCollisions;
65
53
  };
54
+ /**
55
+ * Get the JSON (key/value) representation of the properties.
56
+ *
57
+ * @returns A key/value representing the properties of the object.
58
+ */
59
+ Properties.prototype.toJson = function () {
60
+ var keyValueObject = {};
61
+ this.collection.forEach(function (property) {
62
+ keyValueObject[property.key] = property.value;
63
+ });
64
+ return keyValueObject;
65
+ };
66
66
  return Properties;
67
67
  }());
68
68
  exports.Properties = Properties;
@@ -4,14 +4,14 @@
4
4
  export declare class PropertyLine {
5
5
  /** The line content, minus the trailing \ that identifies that the line continues. */
6
6
  content: string;
7
- /** Is the line object a continuation from a previous line? */
8
- isMultiline: boolean;
9
7
  /** True if the line continues, otherwise false. */
10
8
  continues: boolean;
11
- /** True if the line is a comment, otherwise false. */
12
- isComment: boolean;
13
9
  /** True if the line is blank, otherwise false. */
14
10
  isBlank: boolean;
11
+ /** True if the line is a comment, otherwise false. */
12
+ isComment: boolean;
13
+ /** Is the line object a continuation from a previous line? */
14
+ isMultiline: boolean;
15
15
  /**
16
16
  * Create a new line object.
17
17
  *
@@ -14,20 +14,20 @@ var PropertyLine = /** @class */ (function () {
14
14
  function PropertyLine(line, isMultiline) {
15
15
  /** True if the line continues, otherwise false. */
16
16
  this.continues = false;
17
- /** True if the line is a comment, otherwise false. */
18
- this.isComment = false;
19
17
  /** True if the line is blank, otherwise false. */
20
18
  this.isBlank = false;
19
+ /** True if the line is a comment, otherwise false. */
20
+ this.isComment = false;
21
21
  this.content = line.trimStart();
22
22
  this.isMultiline = isMultiline;
23
- if (!this.content.length) {
23
+ if (this.content.length === 0) {
24
24
  // Line is blank.
25
25
  this.isBlank = true;
26
26
  }
27
27
  else {
28
28
  if (!this.isMultiline) {
29
29
  // Line is a comment.
30
- this.isComment = !!this.content.match(/^[!#]/);
30
+ this.isComment = !!/^[!#]/.test(this.content);
31
31
  }
32
32
  if (!this.isComment) {
33
33
  // Otherwise, check if the line continues on the next line.
package/lib/property.d.ts CHANGED
@@ -3,28 +3,28 @@ import { PropertyLine } from './property-line';
3
3
  * Object representing a property (key/value).
4
4
  */
5
5
  export declare class Property {
6
- /** The line number at which the property starts. */
7
- startingLineNumber: number;
8
- /** The content of one or multiple lines when applicable. */
9
- linesContent: string;
10
- /** Positions of the newline characters if any. */
11
- newlinePositions: number[];
12
- /** Starting line numbers of property objects with the same key. */
13
- keyCollisionLines: number[];
14
- /** The starting position of the delimiter separating the key from the value. */
15
- delimiterPosition: number | undefined;
16
6
  /** The length of the delimiter, including its whitespace characters. */
17
7
  delimiterLength: number | undefined;
8
+ /** The starting position of the delimiter separating the key from the value. */
9
+ delimiterPosition: number | undefined;
18
10
  /** The property key, including its escaped characters. */
19
11
  escapedKey: string;
20
12
  /** The property value, including its escaped characters. */
21
13
  escapedValue: string;
14
+ /** Was the property's key used more than once? */
15
+ hasKeyCollisions: boolean;
22
16
  /** The property key (unescaped). */
23
17
  key: string;
18
+ /** Starting line numbers of property objects with the same key. */
19
+ 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;
24
26
  /** The property value (unescaped). */
25
27
  value: string;
26
- /** Was the property's key used more than once? */
27
- hasKeyCollisions: boolean;
28
28
  /** Does the key definition spread across multiple lines? */
29
29
  private hasMultilineKey;
30
30
  /** Is the key empty? */
package/lib/property.js CHANGED
@@ -12,20 +12,20 @@ var Property = /** @class */ (function () {
12
12
  * @param startingLineNumber - The line number at which the property starts.
13
13
  */
14
14
  function Property(propertyLine, startingLineNumber) {
15
- /** Positions of the newline characters if any. */
16
- this.newlinePositions = [];
17
- /** Starting line numbers of property objects with the same key. */
18
- this.keyCollisionLines = [];
19
15
  /** The property key, including its escaped characters. */
20
16
  this.escapedKey = '';
21
17
  /** The property value, including its escaped characters. */
22
18
  this.escapedValue = '';
19
+ /** Was the property's key used more than once? */
20
+ this.hasKeyCollisions = false;
23
21
  /** The property key (unescaped). */
24
22
  this.key = '';
23
+ /** Starting line numbers of property objects with the same key. */
24
+ this.keyCollisionLines = [];
25
+ /** Positions of the newline characters if any. */
26
+ this.newlinePositions = [];
25
27
  /** The property value (unescaped). */
26
28
  this.value = '';
27
- /** Was the property's key used more than once? */
28
- this.hasKeyCollisions = false;
29
29
  /** Does the key definition spread across multiple lines? */
30
30
  this.hasMultilineKey = false;
31
31
  /** Is the key empty? */
@@ -41,7 +41,7 @@ var Property = /** @class */ (function () {
41
41
  * @param propertyLine - A property line object.
42
42
  */
43
43
  Property.prototype.addLine = function (propertyLine) {
44
- if (this.linesContent.length) {
44
+ if (this.linesContent.length > 0) {
45
45
  this.newlinePositions.push(this.linesContent.length);
46
46
  }
47
47
  this.linesContent += propertyLine.content;
@@ -54,12 +54,12 @@ var Property = /** @class */ (function () {
54
54
  if (this.delimiterPosition !== undefined && this.delimiterLength !== undefined) {
55
55
  // Set key if present.
56
56
  if (!this.hasNoKey) {
57
- this.escapedKey = this.linesContent.substring(0, this.delimiterPosition);
57
+ this.escapedKey = this.linesContent.slice(0, this.delimiterPosition);
58
58
  this.key = this.unescape(this.escapedKey, this.startingLineNumber);
59
59
  }
60
60
  // Set value if present.
61
61
  if (!this.hasNoValue) {
62
- this.escapedValue = this.linesContent.substring(this.delimiterPosition + this.delimiterLength);
62
+ this.escapedValue = this.linesContent.slice(this.delimiterPosition + this.delimiterLength);
63
63
  this.value = this.unescape(this.escapedValue, this.startingLineNumber);
64
64
  }
65
65
  }
@@ -79,43 +79,50 @@ var Property = /** @class */ (function () {
79
79
  */
80
80
  Property.prototype.unescape = function (escapedContent, startingLineNumber) {
81
81
  var unescapedContent = '';
82
- for (var position = 0, character = escapedContent[0]; position < escapedContent.length; position++, character = escapedContent[position]) {
82
+ for (var character = escapedContent[0], position = 0; position < escapedContent.length; position++, character = escapedContent[position]) {
83
83
  if (character === '\\') {
84
84
  var nextCharacter = escapedContent[position + 1];
85
- if (nextCharacter === 'f') {
86
- // Formfeed/
87
- unescapedContent += '\f';
88
- position++;
89
- }
90
- else if (nextCharacter === 'n') {
91
- // Newline.
92
- unescapedContent += '\n';
93
- position++;
94
- }
95
- else if (nextCharacter === 'r') {
96
- // Carriage return.
97
- unescapedContent += '\r';
98
- position++;
99
- }
100
- else if (nextCharacter === 't') {
101
- // Tab.
102
- unescapedContent += '\t';
103
- position++;
104
- }
105
- else if (nextCharacter === 'u') {
106
- // Unicode character.
107
- var codePoint = escapedContent.substring(position + 2, position + 6);
108
- if (!/[0-9a-f]{4}/i.test(codePoint)) {
109
- // Code point can only be within Unicode's Multilingual Plane (BMP).
110
- throw new Error("malformed escaped unicode characters '\\u".concat(codePoint, "' in property starting at line ").concat(startingLineNumber));
85
+ switch (nextCharacter) {
86
+ case 'f': {
87
+ // Formfeed/
88
+ unescapedContent += '\f';
89
+ position++;
90
+ break;
91
+ }
92
+ case 'n': {
93
+ // Newline.
94
+ unescapedContent += '\n';
95
+ position++;
96
+ break;
97
+ }
98
+ case 'r': {
99
+ // Carriage return.
100
+ unescapedContent += '\r';
101
+ position++;
102
+ break;
103
+ }
104
+ case 't': {
105
+ // Tab.
106
+ unescapedContent += '\t';
107
+ position++;
108
+ break;
109
+ }
110
+ case 'u': {
111
+ // Unicode character.
112
+ var codePoint = escapedContent.slice(position + 2, position + 6);
113
+ if (!/[\da-f]{4}/i.test(codePoint)) {
114
+ // Code point can only be within Unicode's Multilingual Plane (BMP).
115
+ throw new Error("malformed escaped unicode characters '\\u".concat(codePoint, "' in property starting at line ").concat(startingLineNumber));
116
+ }
117
+ unescapedContent += String.fromCodePoint(Number.parseInt(codePoint, 16));
118
+ position += 5;
119
+ break;
120
+ }
121
+ default: {
122
+ // Otherwise the escape character is not required.
123
+ unescapedContent += nextCharacter;
124
+ position++;
111
125
  }
112
- unescapedContent += String.fromCharCode(parseInt(codePoint, 16));
113
- position += 5;
114
- }
115
- else {
116
- // Otherwise the escape character is not required.
117
- unescapedContent += nextCharacter;
118
- position++;
119
126
  }
120
127
  }
121
128
  else {
@@ -134,14 +141,14 @@ var Property = /** @class */ (function () {
134
141
  if (this.hasNoKey || this.hasNoValue || this.delimiterPosition) {
135
142
  return;
136
143
  }
137
- for (var position = 0, character = this.linesContent[0]; position < this.linesContent.length; position++, character = this.linesContent[position]) {
144
+ for (var character = this.linesContent[0], position = 0; position < this.linesContent.length; position++, character = this.linesContent[position]) {
138
145
  // If the character is not a delimiter, check the next one.
139
- if (!/[ \t\f=:]/.test(character)) {
146
+ if (!/[\t\f :=]/.test(character)) {
140
147
  continue;
141
148
  }
142
149
  // Check if the delimiter might be escaped.
143
- var prefix = !position ? '' : this.linesContent.substring(0, position);
144
- if (prefix.length) {
150
+ var prefix = !position ? '' : this.linesContent.slice(0, position);
151
+ if (prefix.length > 0) {
145
152
  var backslashMatch = prefix.match(/(?<backslashes>\\+)$/);
146
153
  if (backslashMatch === null || backslashMatch === void 0 ? void 0 : backslashMatch.groups) {
147
154
  var delimiterIsEscaped = !!(backslashMatch.groups.backslashes.length % 2);
@@ -153,20 +160,20 @@ var Property = /** @class */ (function () {
153
160
  }
154
161
  var delimiter = '';
155
162
  this.delimiterPosition = position;
156
- this.hasMultilineKey = !!(this.newlinePositions.length && this.newlinePositions[0] > position);
163
+ this.hasMultilineKey = !!(this.newlinePositions.length > 0 && this.newlinePositions[0] > position);
157
164
  // Check if the delimiter starts with a whitespace.
158
- var nextContent = this.linesContent.substring(position);
165
+ var nextContent = this.linesContent.slice(position);
159
166
  var leadingWhitespaceMatch = nextContent.match(/^(?<whitespace>\s+)/);
160
167
  var leadingWhitespace = ((_a = leadingWhitespaceMatch === null || leadingWhitespaceMatch === void 0 ? void 0 : leadingWhitespaceMatch.groups) === null || _a === void 0 ? void 0 : _a.whitespace) || '';
161
168
  // If there is a whitespace, move to the next character.
162
- if (leadingWhitespace.length) {
169
+ if (leadingWhitespace.length > 0) {
163
170
  delimiter += leadingWhitespace;
164
- nextContent = nextContent.substring(leadingWhitespace.length);
171
+ nextContent = nextContent.slice(leadingWhitespace.length);
165
172
  }
166
173
  // Check if there is an equal or colon character.
167
- if (/[=:]/.test(nextContent[0])) {
174
+ if (/[:=]/.test(nextContent[0])) {
168
175
  delimiter += nextContent[0];
169
- nextContent = nextContent.substring(1);
176
+ nextContent = nextContent.slice(1);
170
177
  // If an equal or colon character was found, try to get trailing whitespace.
171
178
  var trailingWhitespaceMatch = nextContent.match(/^(?<whitespace>\s+)/);
172
179
  var trailingWhitespace = ((_b = trailingWhitespaceMatch === null || trailingWhitespaceMatch === void 0 ? void 0 : trailingWhitespaceMatch.groups) === null || _b === void 0 ? void 0 : _b.whitespace) || '';
@@ -185,7 +192,7 @@ var Property = /** @class */ (function () {
185
192
  }
186
193
  else {
187
194
  // If the delimiter is after the first newline, mark the key as multiline.
188
- if (this.newlinePositions.length) {
195
+ if (this.newlinePositions.length > 0) {
189
196
  var firstLinePosition = this.newlinePositions[0];
190
197
  if (firstLinePosition > this.delimiterPosition) {
191
198
  this.hasMultilineKey = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "properties-file",
3
- "version": "2.0.9",
3
+ "version": "2.1.2",
4
4
  "description": ".properties file parser, JSON converter and Webpack loader.",
5
5
  "author": "Avansai (https://avansai.com)",
6
6
  "repository": {
@@ -42,33 +42,39 @@
42
42
  "internationalization"
43
43
  ],
44
44
  "scripts": {
45
- "build": "rm -Rf ./lib && tsc && npm run add-import-type && npm run lint && npm test",
46
- "lint": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --fix .",
45
+ "build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc && npm run add-import-type && npm run test",
46
+ "lint-fix": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --fix .",
47
+ "lint-check": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx .",
47
48
  "lint-print-config": "eslint --print-config ./eslintrc.yaml",
48
49
  "prettier": "prettier --write .",
49
- "add-import-type": "node ./src/add-import-type",
50
+ "add-import-type": "node ./src/add-import-type.mjs",
50
51
  "test": "jest --coverage",
51
52
  "release": "dotenv -- release-it --only-version"
52
53
  },
53
54
  "devDependencies": {
54
- "@release-it/conventional-changelog": "^5.0.0",
55
- "@types/jest": "^28.1.6",
56
- "@typescript-eslint/eslint-plugin": "^5.31.0",
57
- "@typescript-eslint/parser": "^5.31.0",
55
+ "@release-it/conventional-changelog": "^5.1.0",
56
+ "@types/jest": "^28.1.7",
57
+ "@typescript-eslint/eslint-plugin": "^5.34.0",
58
+ "@typescript-eslint/parser": "^5.34.0",
58
59
  "dotenv-cli": "^6.0.0",
59
- "eslint": "^8.21.0",
60
+ "eslint": "^8.22.0",
60
61
  "eslint-config-prettier": "^8.5.0",
61
62
  "eslint-import-resolver-node": "^0.3.6",
62
- "eslint-import-resolver-typescript": "^3.3.0",
63
+ "eslint-import-resolver-typescript": "^3.5.0",
63
64
  "eslint-plugin-import": "^2.26.0",
64
- "eslint-plugin-jest": "^26.7.0",
65
+ "eslint-plugin-jest": "^26.8.7",
65
66
  "eslint-plugin-prettier": "^4.2.1",
67
+ "eslint-plugin-unicorn": "^43.0.2",
66
68
  "jest": "^28.1.3",
67
69
  "prettier": "^2.7.1",
68
- "prettier-plugin-organize-imports": "^3.0.0",
69
- "release-it": "^15.2.0",
70
- "ts-jest": "^28.0.7",
70
+ "prettier-plugin-organize-imports": "^3.1.0",
71
+ "prettier-plugin-sh": "^0.12.8",
72
+ "release-it": "^15.3.0",
73
+ "ts-jest": "^28.0.8",
71
74
  "ts-node": "^10.9.1",
72
75
  "typescript": "^4.7.4"
76
+ },
77
+ "engines": {
78
+ "node": "^14.18.1 || ^16.0.0"
73
79
  }
74
80
  }