properties-file 4.0.0 → 5.0.1
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 +94 -97
- package/dist/cjs/bundler/bun.js +1 -1
- package/dist/cjs/bundler/esbuild.js +1 -1
- package/dist/cjs/bundler/rollup.js +1 -1
- package/dist/cjs/bundler/webpack.js +1 -1
- package/dist/cjs/editor/index.d.ts +100 -93
- package/dist/cjs/editor/index.js +1 -1
- package/dist/cjs/escape/index.js +1 -1
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/parse-properties.d.ts +22 -0
- package/dist/cjs/parse-properties.js +1 -0
- package/dist/cjs/parser/index.d.ts +3 -0
- package/dist/cjs/parser/index.js +1 -0
- package/dist/cjs/parser/nodes.d.ts +156 -0
- package/dist/cjs/parser/nodes.js +1 -0
- package/dist/cjs/parser/normalize.d.ts +12 -0
- package/dist/cjs/parser/normalize.js +1 -0
- package/dist/cjs/parser/parse.d.ts +23 -0
- package/dist/cjs/parser/parse.js +1 -0
- package/dist/cjs/parser/properties.d.ts +93 -0
- package/dist/cjs/parser/properties.js +1 -0
- package/dist/cjs/unescape/index.d.ts +4 -0
- package/dist/cjs/unescape/index.js +1 -1
- package/dist/esm/editor/index.d.ts +100 -93
- package/dist/esm/editor/index.js +1 -1
- package/dist/esm/escape/index.js +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/parse-properties.d.ts +22 -0
- package/dist/esm/parse-properties.js +1 -0
- package/dist/esm/parser/index.d.ts +3 -0
- package/dist/esm/parser/index.js +1 -0
- package/dist/esm/parser/nodes.d.ts +156 -0
- package/dist/esm/parser/nodes.js +1 -0
- package/dist/esm/parser/normalize.d.ts +12 -0
- package/dist/esm/parser/normalize.js +1 -0
- package/dist/esm/parser/parse.d.ts +23 -0
- package/dist/esm/parser/parse.js +1 -0
- package/dist/esm/parser/properties.d.ts +93 -0
- package/dist/esm/parser/properties.js +1 -0
- package/dist/esm/unescape/index.d.ts +4 -0
- package/dist/esm/unescape/index.js +1 -1
- package/package.json +33 -21
- package/dist/cjs/properties.d.ts +0 -96
- package/dist/cjs/properties.js +0 -1
- package/dist/cjs/property-line.d.ts +0 -22
- package/dist/cjs/property-line.js +0 -1
- package/dist/cjs/property.d.ts +0 -81
- package/dist/cjs/property.js +0 -1
- package/dist/esm/properties.d.ts +0 -96
- package/dist/esm/properties.js +0 -1
- package/dist/esm/property-line.d.ts +0 -22
- package/dist/esm/property-line.js +0 -1
- package/dist/esm/property.d.ts +0 -81
- package/dist/esm/property.js +0 -1
|
@@ -1,154 +1,161 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/** The default separator between keys and values. */
|
|
4
|
-
export declare const DEFAULT_SEPARATOR = "=";
|
|
5
|
-
/** The default character used as comment delimiter. */
|
|
6
|
-
export declare const DEFAULT_COMMENT_DELIMITER = "#";
|
|
1
|
+
import { Properties } from '../parser/properties.js';
|
|
2
|
+
import type { PropertyNode } from '../parser/nodes.js';
|
|
7
3
|
/** Characters that can be used as key-value pair separators. */
|
|
8
|
-
export type KeyValuePairSeparator = '
|
|
4
|
+
export type KeyValuePairSeparator = '=' | ':' | ' ';
|
|
9
5
|
/** Characters that can be used as comment delimiters. */
|
|
10
6
|
export type CommentDelimiter = '#' | '!';
|
|
11
|
-
/** Options
|
|
7
|
+
/** Options for {@link PropertiesEditor.insert}. */
|
|
12
8
|
export type InsertOptions = {
|
|
13
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Insert relative to this key (last occurrence). If the key is not found,
|
|
11
|
+
* the property is appended at the end.
|
|
12
|
+
*/
|
|
14
13
|
referenceKey?: string;
|
|
15
|
-
/**
|
|
14
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
16
15
|
position?: 'before' | 'after';
|
|
17
|
-
/**
|
|
16
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
18
17
|
escapeUnicode?: boolean;
|
|
19
|
-
/**
|
|
18
|
+
/** Separator character to use between key and value. Default: `'='`. */
|
|
20
19
|
separator?: KeyValuePairSeparator;
|
|
21
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Comment text to prepend before the property. Supports multi-line: newlines
|
|
22
|
+
* in the string create separate comment nodes. Empty lines within the text
|
|
23
|
+
* become blank line nodes.
|
|
24
|
+
*/
|
|
22
25
|
comment?: string;
|
|
23
|
-
/**
|
|
26
|
+
/** Delimiter character for the comment. Default: `'#'`. */
|
|
24
27
|
commentDelimiter?: CommentDelimiter;
|
|
25
28
|
};
|
|
26
|
-
/** Options
|
|
29
|
+
/** Options for {@link PropertiesEditor.insertComment}. */
|
|
27
30
|
export type InsertCommentOptions = {
|
|
28
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Insert relative to this key (last occurrence). If the key is not found,
|
|
33
|
+
* the comment is appended at the end.
|
|
34
|
+
*/
|
|
29
35
|
referenceKey?: string;
|
|
30
|
-
/**
|
|
36
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
31
37
|
position?: 'before' | 'after';
|
|
32
|
-
/**
|
|
38
|
+
/** Delimiter character for the comment. Default: `'#'`. */
|
|
33
39
|
commentDelimiter?: CommentDelimiter;
|
|
34
40
|
};
|
|
35
|
-
/** Options
|
|
41
|
+
/** Options for {@link PropertiesEditor.insertBlankLine}. */
|
|
42
|
+
export type InsertBlankLineOptions = {
|
|
43
|
+
/**
|
|
44
|
+
* Insert relative to this key (last occurrence). If the key is not found,
|
|
45
|
+
* the blank line is appended at the end.
|
|
46
|
+
*/
|
|
47
|
+
referenceKey?: string;
|
|
48
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
49
|
+
position?: 'before' | 'after';
|
|
50
|
+
};
|
|
51
|
+
/** Options for {@link PropertiesEditor.update}. */
|
|
36
52
|
export type UpdateOptions = {
|
|
37
|
-
/**
|
|
53
|
+
/** Replacement value. When not set, the original value is preserved. */
|
|
38
54
|
newValue?: string;
|
|
39
|
-
/**
|
|
55
|
+
/** Replacement key (rename). When not set, the original key is preserved. */
|
|
40
56
|
newKey?: string;
|
|
41
|
-
/**
|
|
57
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
42
58
|
escapeUnicode?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
separator?:
|
|
45
|
-
/**
|
|
59
|
+
/** New separator character. When not set, the original separator is preserved. */
|
|
60
|
+
separator?: KeyValuePairSeparator;
|
|
61
|
+
/**
|
|
62
|
+
* Replacement comment text. When set, all comment and blank line nodes immediately
|
|
63
|
+
* preceding the property (up to the previous property) are removed and replaced
|
|
64
|
+
* with the new comment. Supports multi-line via newlines in the string.
|
|
65
|
+
*/
|
|
46
66
|
newComment?: string;
|
|
47
|
-
/**
|
|
67
|
+
/** Delimiter character for the new comment. Default: `'#'`. */
|
|
48
68
|
commentDelimiter?: CommentDelimiter;
|
|
49
69
|
};
|
|
50
|
-
/** Options
|
|
70
|
+
/** Options for {@link PropertiesEditor.upsert}. */
|
|
51
71
|
export type UpsertOptions = {
|
|
52
|
-
/**
|
|
72
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
53
73
|
escapeUnicode?: boolean;
|
|
54
|
-
/**
|
|
74
|
+
/** Separator character. Default: `'='`. */
|
|
55
75
|
separator?: KeyValuePairSeparator;
|
|
56
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* Comment text. When inserting a new property, this is prepended as a comment.
|
|
78
|
+
* When updating an existing property, this replaces the leading comment nodes.
|
|
79
|
+
*/
|
|
57
80
|
comment?: string;
|
|
58
|
-
/**
|
|
81
|
+
/** Delimiter character for the comment. Default: `'#'`. */
|
|
59
82
|
commentDelimiter?: CommentDelimiter;
|
|
60
83
|
};
|
|
84
|
+
/** Options for {@link PropertiesEditor.delete}. */
|
|
85
|
+
export type DeleteOptions = {
|
|
86
|
+
/**
|
|
87
|
+
* If `false`, only the property node itself is removed. If `true` (default),
|
|
88
|
+
* all comment and blank line nodes immediately preceding the property (up to
|
|
89
|
+
* the previous property) are also removed.
|
|
90
|
+
*/
|
|
91
|
+
deleteLeadingNodes?: boolean;
|
|
92
|
+
};
|
|
61
93
|
/**
|
|
62
|
-
*
|
|
94
|
+
* An editor for `.properties` files that extends the lossless {@link Properties}
|
|
95
|
+
* parser with insert, update, delete, and upsert operations.
|
|
63
96
|
*/
|
|
64
97
|
export declare class PropertiesEditor extends Properties {
|
|
65
|
-
/** Is line parsing required to re-async the object's properties? */
|
|
66
|
-
private needsLineParsing;
|
|
67
|
-
/**
|
|
68
|
-
* Create `PropertiesEditor` object.
|
|
69
|
-
*
|
|
70
|
-
* @param content - The content of a `.properties` file.
|
|
71
|
-
*/
|
|
72
|
-
constructor(content: string);
|
|
73
98
|
/**
|
|
74
|
-
* Find the
|
|
99
|
+
* Find the index of the last property node with the given key.
|
|
75
100
|
*
|
|
76
|
-
* @param key - The
|
|
101
|
+
* @param key - The unescaped key to search for.
|
|
77
102
|
*
|
|
78
|
-
* @returns The
|
|
79
|
-
*/
|
|
80
|
-
private findLastPropertyByKey;
|
|
81
|
-
/**
|
|
82
|
-
* Parse the `.properties` content line by line only when needed.
|
|
103
|
+
* @returns The index in `this.nodes`, or `-1` if not found.
|
|
83
104
|
*/
|
|
84
|
-
private
|
|
105
|
+
private findLastPropertyIndex;
|
|
85
106
|
/**
|
|
86
|
-
* Insert a new property
|
|
107
|
+
* Insert a new property.
|
|
87
108
|
*
|
|
88
|
-
* @param key -
|
|
89
|
-
* @param value -
|
|
90
|
-
* @param options -
|
|
91
|
-
*
|
|
92
|
-
* @returns True if the key was inserted, otherwise false.
|
|
109
|
+
* @param key - The unescaped key.
|
|
110
|
+
* @param value - The unescaped value.
|
|
111
|
+
* @param options - Insert options.
|
|
93
112
|
*/
|
|
94
|
-
insert(key: string, value: string, options?: InsertOptions):
|
|
113
|
+
insert(key: string, value: string, options?: InsertOptions): void;
|
|
95
114
|
/**
|
|
96
|
-
* Insert a
|
|
97
|
-
*
|
|
98
|
-
* @param comment - The comment to add.
|
|
99
|
-
* @param options - Additional options.
|
|
115
|
+
* Insert a comment.
|
|
100
116
|
*
|
|
101
|
-
* @
|
|
117
|
+
* @param comment - The comment text (may contain newlines).
|
|
118
|
+
* @param options - Insert comment options.
|
|
102
119
|
*/
|
|
103
|
-
insertComment(comment: string, options?: InsertCommentOptions):
|
|
120
|
+
insertComment(comment: string, options?: InsertCommentOptions): void;
|
|
104
121
|
/**
|
|
105
|
-
*
|
|
122
|
+
* Insert a blank line.
|
|
106
123
|
*
|
|
107
|
-
* @param
|
|
108
|
-
* @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted.
|
|
109
|
-
*
|
|
110
|
-
* @returns True if the key was deleted, otherwise false.
|
|
124
|
+
* @param options - Insert blank line options.
|
|
111
125
|
*/
|
|
112
|
-
|
|
126
|
+
insertBlankLine(options?: InsertBlankLineOptions): void;
|
|
113
127
|
/**
|
|
114
|
-
*
|
|
128
|
+
* Update an existing property.
|
|
115
129
|
*
|
|
116
|
-
* @param
|
|
130
|
+
* @param key - The unescaped key to update (uses last occurrence).
|
|
131
|
+
* @param options - Update options.
|
|
117
132
|
*
|
|
118
|
-
* @returns
|
|
133
|
+
* @returns `true` if the property was found and updated, `false` otherwise.
|
|
119
134
|
*/
|
|
120
|
-
|
|
135
|
+
update(key: string, options: UpdateOptions): boolean;
|
|
121
136
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @param property - A property object.
|
|
137
|
+
* Update a property if it exists, or insert it if it doesn't.
|
|
125
138
|
*
|
|
126
|
-
* @
|
|
139
|
+
* @param key - The unescaped key.
|
|
140
|
+
* @param value - The unescaped value.
|
|
141
|
+
* @param options - Upsert options.
|
|
127
142
|
*/
|
|
128
|
-
|
|
143
|
+
upsert(key: string, value: string, options?: UpsertOptions): void;
|
|
129
144
|
/**
|
|
130
|
-
*
|
|
145
|
+
* Delete the last occurrence of a property (the effective value in last-wins semantics).
|
|
131
146
|
*
|
|
132
|
-
* @param key - The
|
|
133
|
-
* @param options -
|
|
147
|
+
* @param key - The unescaped key to delete.
|
|
148
|
+
* @param options - Delete options.
|
|
134
149
|
*
|
|
135
|
-
* @returns
|
|
150
|
+
* @returns The deleted {@link PropertyNode}, or `undefined` if the key was not found.
|
|
136
151
|
*/
|
|
137
|
-
|
|
152
|
+
delete(key: string, options?: DeleteOptions): PropertyNode | undefined;
|
|
138
153
|
/**
|
|
139
|
-
*
|
|
154
|
+
* Delete all occurrences of a key.
|
|
140
155
|
*
|
|
141
|
-
* @param key -
|
|
142
|
-
* @param value - A property value (unescaped).
|
|
143
|
-
* @param options - Additional options.
|
|
144
|
-
*
|
|
145
|
-
* @returns True if the key was updated or inserted, otherwise false.
|
|
146
|
-
*/
|
|
147
|
-
upsert(key: string, value: string, options?: UpsertOptions): boolean;
|
|
148
|
-
/**
|
|
149
|
-
* Get the key/value object representing the properties.
|
|
156
|
+
* @param key - The unescaped key to delete.
|
|
150
157
|
*
|
|
151
|
-
* @returns
|
|
158
|
+
* @returns An array of the deleted {@link PropertyNode} instances.
|
|
152
159
|
*/
|
|
153
|
-
|
|
160
|
+
deleteAll(key: string): PropertyNode[];
|
|
154
161
|
}
|
package/dist/esm/editor/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
function _array_like_to_array(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _assert_this_initialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _call_super(e,t,r){return t=_get_prototype_of(t),_possible_constructor_return(e,_is_native_reflect_construct()?Reflect.construct(t,r||[],_get_prototype_of(e).constructor):t.apply(e,r))}function _class_call_check(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _create_class(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}function _get_prototype_of(e){return _get_prototype_of=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},_get_prototype_of(e)}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_set_prototype_of(e,t)}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _possible_constructor_return(e,t){return!t||"object"!==_type_of(t)&&"function"!=typeof t?_assert_this_initialized(e):t}function _set_prototype_of(e,t){return _set_prototype_of=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},_set_prototype_of(e,t)}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _type_of(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}function _unsupported_iterable_to_array(e,t){if(e){if("string"==typeof e)return _array_like_to_array(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_array_like_to_array(e,t):void 0}}function _is_native_reflect_construct(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(_is_native_reflect_construct=function(){return!!e})()}import{escapeKey,escapeValue}from"../escape/index.js";import{Properties}from"../parser/properties.js";var DEFAULT_SEPARATOR="=",DEFAULT_COMMENT_DELIMITER="#",REGEX_NEWLINE=/\r\n|\r|\n/,buildPropertyNode=function(e,t,r,n){var o,a=!0===r.escapeUnicode,i=escapeKey(e,a),s=escapeValue(t,a),l=" "===r.separator?void 0:null!==(o=r.separator)&&void 0!==o?o:DEFAULT_SEPARATOR,c=l?" ":"",u=l?"".concat(" ").concat(l).concat(c):" ",p="".concat(i).concat(u).concat(s).split(REGEX_NEWLINE);return{type:"property",rawLines:p,leadingWhitespace:"",key:e,escapedKey:i,separatorLeading:" ",separatorChar:l,separatorTrailing:c,value:t,escapedValue:s,startingLineNumber:n,endingLineNumber:n+p.length-1}},buildCommentNodes=function(e,t,r){return e.split(REGEX_NEWLINE).map(function(e,n){return""===e?{type:"blank",rawLine:"",lineNumber:r+n}:{type:"comment",rawLine:"".concat(t," ").concat(e),leadingWhitespace:"",delimiter:t,body:" ".concat(e),lineNumber:r+n}})},recalculateLineNumbers=function(e){var t=1,r=!0,n=!1,o=void 0;try{for(var a,i=e[Symbol.iterator]();!(r=(a=i.next()).done);r=!0){var s=a.value;if("property"===s.type){var l=s.rawLines.length;s.startingLineNumber=t,s.endingLineNumber=t+l-1,t+=l}else s.lineNumber=t,t++}}catch(e){n=!0,o=e}finally{try{r||null==i.return||i.return()}finally{if(n)throw o}}};export var PropertiesEditor=function(e){"use strict";function t(){return _class_call_check(this,t),_call_super(this,t,arguments)}return _inherits(t,e),_create_class(t,[{key:"findLastPropertyIndex",value:function(e){for(var t=this.nodes.length-1;t>=0;t--){var r=this.nodes[t];if("property"===r.type&&r.key===e)return t}return-1}},{key:"insert",value:function(e,t,r){var n,o,a=[];if(void 0!==(null==r?void 0:r.comment)){var i,s=null!==(o=r.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER;(i=a).push.apply(i,_to_consumable_array(buildCommentNodes(r.comment,s,0)))}if(a.push(buildPropertyNode(e,t,{escapeUnicode:null==r?void 0:r.escapeUnicode,separator:null==r?void 0:r.separator},0)),null==r?void 0:r.referenceKey){var l=this.findLastPropertyIndex(r.referenceKey);if(-1!==l){var c,u="before"===r.position?l:l+1;return(c=this.nodes).splice.apply(c,[u,0].concat(_to_consumable_array(a))),void recalculateLineNumbers(this.nodes)}}(n=this.nodes).push.apply(n,_to_consumable_array(a)),recalculateLineNumbers(this.nodes)}},{key:"insertComment",value:function(e,t){var r,n,o=null!==(n=null==t?void 0:t.commentDelimiter)&&void 0!==n?n:DEFAULT_COMMENT_DELIMITER,a=buildCommentNodes(e,o,0);if(null==t?void 0:t.referenceKey){var i=this.findLastPropertyIndex(t.referenceKey);if(-1!==i){var s,l="before"===t.position?i:i+1;return(s=this.nodes).splice.apply(s,[l,0].concat(_to_consumable_array(a))),void recalculateLineNumbers(this.nodes)}}(r=this.nodes).push.apply(r,_to_consumable_array(a)),recalculateLineNumbers(this.nodes)}},{key:"insertBlankLine",value:function(e){var t={type:"blank",rawLine:"",lineNumber:0};if(null==e?void 0:e.referenceKey){var r=this.findLastPropertyIndex(e.referenceKey);if(-1!==r){var n="before"===e.position?r:r+1;return this.nodes.splice(n,0,t),void recalculateLineNumbers(this.nodes)}}this.nodes.push(t),recalculateLineNumbers(this.nodes)}},{key:"update",value:function(e,t){var r,n,o,a=this.findLastPropertyIndex(e);if(-1===a)return!1;var i=this.nodes[a],s=null!==(r=t.newKey)&&void 0!==r?r:i.key,l=null!==(n=t.newValue)&&void 0!==n?n:i.value,c=!0===t.escapeUnicode,u=c?escapeKey(s,!0):void 0!==t.newKey?escapeKey(s):i.escapedKey,p=c?escapeValue(l,!0):void 0!==t.newValue?escapeValue(l):i.escapedValue,_=t.separator?" "===t.separator?void 0:t.separator:i.separatorChar,d=t.separator?" ":i.separatorLeading,y=t.separator?_?" ":"":i.separatorTrailing,f=_?"".concat(d).concat(_).concat(y):d,m="".concat(u).concat(f).concat(p).split(REGEX_NEWLINE),v={type:"property",rawLines:m,leadingWhitespace:i.leadingWhitespace,key:s,escapedKey:u,separatorLeading:d,separatorChar:_,separatorTrailing:y,value:l,escapedValue:p,startingLineNumber:i.startingLineNumber,endingLineNumber:i.startingLineNumber+m.length-1};if(void 0!==t.newComment){for(var h,b=a,L=a-1;L>=0&&"property"!==this.nodes[L].type;L--)b=L;var E=null!==(o=t.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER,N=buildCommentNodes(t.newComment,E,0);(h=this.nodes).splice.apply(h,[b,a-b+1].concat(_to_consumable_array(N),[v]))}else this.nodes[a]=v;return recalculateLineNumbers(this.nodes),!0}},{key:"upsert",value:function(e,t,r){-1!==this.findLastPropertyIndex(e)?this.update(e,{newValue:t,escapeUnicode:null==r?void 0:r.escapeUnicode,separator:null==r?void 0:r.separator,newComment:null==r?void 0:r.comment,commentDelimiter:null==r?void 0:r.commentDelimiter}):this.insert(e,t,r)}},{key:"delete",value:function(e,t){var r=this.findLastPropertyIndex(e);if(-1!==r){var n=this.nodes[r];if(!1!==(null==t?void 0:t.deleteLeadingNodes)){for(var o=r,a=r-1;a>=0&&"property"!==this.nodes[a].type;a--)o=a;this.nodes.splice(o,r-o+1)}else this.nodes.splice(r,1);return recalculateLineNumbers(this.nodes),n}}},{key:"deleteAll",value:function(e){for(var t=[],r=this.nodes.length-1;r>=0;r--){var n=this.nodes[r];"property"===n.type&&n.key===e&&(this.nodes.splice(r,1),t.push(n))}return t.length>0&&recalculateLineNumbers(this.nodes),t.reverse()}}]),t}(Properties);
|
package/dist/esm/escape/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export var escapeKey=function(e
|
|
1
|
+
export var escapeKey=function(e){return escapeContent(e,!0,arguments.length>1&&void 0!==arguments[1]&&arguments[1])};export var escapeValue=function(e){return escapeContent(e,!1,arguments.length>1&&void 0!==arguments[1]&&arguments[1])};var REGEX_ESCAPE_NO_UNICODE=/[\s!#:=\\]/g,REGEX_ESCAPE_UNICODE=/[\s!#:=\\\u0000-\u001F\u007F-\uFFFF]/g,escapeContent=function(e,r,t){var n=t?REGEX_ESCAPE_UNICODE:REGEX_ESCAPE_NO_UNICODE;return n.lastIndex=0,e.replace(n,function(e,t){var n;switch(e){case" ":return r||0===t?"\\ ":" ";case"\\":return"\\\\";case"\f":return"\\f";case"\n":return"\\n";case"\r":return"\\r";case"\t":return"\\t";case"=":case":":case"#":case"!":return"\\".concat(e);default:return"\\u"+("0000"+(null!==(n=e.charCodeAt(0))&&void 0!==n?n:0).toString(16)).slice(-4)}})};
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{parseProperties}from"./parse-properties.js";export var getProperties=function(r){return parseProperties(r)};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { KeyValuePairObject } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a `.properties` file content string into a key-value pair object.
|
|
4
|
+
*
|
|
5
|
+
* This is a functional, single-pass parser that implements the Java
|
|
6
|
+
* `java.util.Properties` specification:
|
|
7
|
+
*
|
|
8
|
+
* - BOM detection and skipping
|
|
9
|
+
* - Comment lines (`#` or `!`)
|
|
10
|
+
* - Line continuations (trailing odd backslash)
|
|
11
|
+
* - Key-value separator detection (`=`, `:`, or whitespace)
|
|
12
|
+
* - Escape sequence processing (`\\n`, `\\t`, `\\r`, `\\f`, `\\\\`, `\\uXXXX`)
|
|
13
|
+
* - Last-value-wins semantics for duplicate keys
|
|
14
|
+
*
|
|
15
|
+
* All character inspection uses `charCodeAt()` (ES5). No regex, no
|
|
16
|
+
* intermediate object allocations.
|
|
17
|
+
*
|
|
18
|
+
* @param content - The content of a `.properties` file (string or Buffer).
|
|
19
|
+
*
|
|
20
|
+
* @returns A key-value pair object where every value is a string.
|
|
21
|
+
*/
|
|
22
|
+
export declare const parseProperties: (content: string | Buffer) => KeyValuePairObject;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var CH_TAB=9,CH_LF=10,CH_FF=12,CH_CR=13,CH_SPACE=32,CH_BANG=33,CH_HASH=35,CH_COLON=58,CH_EQUALS=61,CH_BACKSLASH=92,CH_LOWER_F=102,CH_LOWER_N=110,CH_LOWER_R=114,CH_LOWER_T=116,CH_LOWER_U=117,CH_BOM=65279,skipWhitespace=function(e,C,r){for(;C<r;){var a=e.charCodeAt(C);if(a!==CH_SPACE&&a!==CH_TAB&&a!==CH_FF)break;C++}return C},isHexDigit=function(e){return e>=48&&e<=57||e>=65&&e<=70||e>=97&&e<=102},hexValue=function(e){return e>=48&&e<=57?e-48:e>=65&&e<=70?e-55:e-87},unescapeContent=function(e,C,r,a,t){if(!a)return e.slice(C,r);for(var H="",_=C,c=C;c<r;)if(e.charCodeAt(c)===CH_BACKSLASH){switch(c>_&&(H+=e.slice(_,c)),c++,e.charCodeAt(c)){case CH_LOWER_N:H+="\n",c++;break;case CH_LOWER_T:H+="\t",c++;break;case CH_LOWER_R:H+="\r",c++;break;case CH_LOWER_F:H+="\f",c++;break;case CH_LOWER_U:if(c+4>=r||!isHexDigit(e.charCodeAt(c+1))||!isHexDigit(e.charCodeAt(c+2))||!isHexDigit(e.charCodeAt(c+3))||!isHexDigit(e.charCodeAt(c+4))){var i=e.slice(c-1,c+5);throw new Error("malformed escaped unicode characters '".concat(i,"' in property starting at line ").concat(t))}var A=hexValue(e.charCodeAt(c+1))<<12|hexValue(e.charCodeAt(c+2))<<8|hexValue(e.charCodeAt(c+3))<<4|hexValue(e.charCodeAt(c+4));H+=String.fromCharCode(A),c+=5;break;default:H+=e.charAt(c),c++}_=c}else c++;return _<r&&(H+=e.slice(_,r)),H};export var parseProperties=function(e){var C="string"==typeof e?e:e.toString(),r=C.length,a={},t=0,H=1;for(r>0&&C.charCodeAt(0)===CH_BOM&&(t=1);t<r&&!((t=skipWhitespace(C,t,r))>=r);){var _=C.charCodeAt(t);if(_!==CH_LF)if(_!==CH_CR)if(_!==CH_HASH&&_!==CH_BANG){for(var c=H,i=t,A=0,o=!1;t<r&&(_=C.charCodeAt(t))!==CH_LF&&_!==CH_CR;)_===CH_BACKSLASH?(A++,o=!0):A=0,t++;if(A%2==1){var s=[C.slice(i,t-1)],h=o;for(t<r&&(C.charCodeAt(t)===CH_CR?++t<r&&C.charCodeAt(t)===CH_LF&&t++:t++,H++),t=skipWhitespace(C,t,r);;){var n=t;A=0;for(var L=!1;t<r&&(_=C.charCodeAt(t))!==CH_LF&&_!==CH_CR;)_===CH_BACKSLASH?(A++,L=!0):A=0,t++;var d=A%2==1,f=d?t-1:t;if(s.push(C.slice(n,f)),L&&(h=!0),t<r&&(C.charCodeAt(t)===CH_CR?++t<r&&C.charCodeAt(t)===CH_LF&&t++:t++,H++),!d)break;t=skipWhitespace(C,t,r)}for(var p=s.join(""),S=p.length,u=0,F=!1;u<S;)if((_=p.charCodeAt(u))!==CH_BACKSLASH){if(!F&&(_===CH_EQUALS||_===CH_COLON||_===CH_SPACE||_===CH_TAB||_===CH_FF))break;F=!1,u++}else F=!F,u++;var l=u;l<S&&((_=p.charCodeAt(l))!==CH_SPACE&&_!==CH_TAB&&_!==CH_FF||(l=skipWhitespace(p,l,S))<S&&(_=p.charCodeAt(l)),l<S&&(_===CH_EQUALS||_===CH_COLON)&&(l++,l=skipWhitespace(p,l,S))),a[unescapeContent(p,0,u,h,c)]=unescapeContent(p,l,S,h,c)}else{var E=t;t<r&&(C.charCodeAt(t)===CH_CR?++t<r&&C.charCodeAt(t)===CH_LF&&t++:t++,H++);for(var O=i,R=!1;O<E;)if((_=C.charCodeAt(O))!==CH_BACKSLASH){if(!R&&(_===CH_EQUALS||_===CH_COLON||_===CH_SPACE||_===CH_TAB||_===CH_FF))break;R=!1,O++}else R=!R,O++;var k=O;k<E&&((_=C.charCodeAt(k))!==CH_SPACE&&_!==CH_TAB&&_!==CH_FF||(k=skipWhitespace(C,k,E))<E&&(_=C.charCodeAt(k)),k<E&&(_===CH_EQUALS||_===CH_COLON)&&(k++,k=skipWhitespace(C,k,E))),a[unescapeContent(C,i,O,o,c)]=unescapeContent(C,k,E,o,c)}}else for(;t<r;){if((_=C.charCodeAt(t))===CH_LF){t++,H++;break}if(_===CH_CR){++t<r&&C.charCodeAt(t)===CH_LF&&t++,H++;break}t++}else++t<r&&C.charCodeAt(t)===CH_LF&&t++,H++;else t++,H++}return a};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{Properties}from"./properties.js";export{PropertiesNodeType}from"./nodes.js";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/** Constants for the `type` discriminator on each node. */
|
|
2
|
+
export declare const PropertiesNodeType: {
|
|
3
|
+
/** A property (key-value pair). */
|
|
4
|
+
readonly PROPERTY: "property";
|
|
5
|
+
/** A comment line (`#` or `!`). */
|
|
6
|
+
readonly COMMENT: "comment";
|
|
7
|
+
/** A blank line. */
|
|
8
|
+
readonly BLANK: "blank";
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A property (key-value pair), potentially spanning multiple physical lines
|
|
12
|
+
* via backslash continuation.
|
|
13
|
+
*/
|
|
14
|
+
export type PropertyNode = {
|
|
15
|
+
/** Discriminator. */
|
|
16
|
+
readonly type: 'property';
|
|
17
|
+
/** Physical lines as-is (without EOL characters), for lossless reconstruction. */
|
|
18
|
+
readonly rawLines: string[];
|
|
19
|
+
/** Leading whitespace before the key on the first physical line. */
|
|
20
|
+
readonly leadingWhitespace: string;
|
|
21
|
+
/** The unescaped key. */
|
|
22
|
+
readonly key: string;
|
|
23
|
+
/** The key as written in the source (after continuation joining, before unescaping). */
|
|
24
|
+
readonly escapedKey: string;
|
|
25
|
+
/** Whitespace between the key and the separator character. */
|
|
26
|
+
readonly separatorLeading: string;
|
|
27
|
+
/**
|
|
28
|
+
* The core separator character.
|
|
29
|
+
*
|
|
30
|
+
* - `'='` or `':'` when an explicit delimiter is used.
|
|
31
|
+
* - `undefined` when whitespace alone separates key from value, or when
|
|
32
|
+
* the property is key-only with no separator.
|
|
33
|
+
*/
|
|
34
|
+
readonly separatorChar: '=' | ':' | undefined;
|
|
35
|
+
/** Whitespace between the separator character and the value. */
|
|
36
|
+
readonly separatorTrailing: string;
|
|
37
|
+
/** The unescaped value. */
|
|
38
|
+
readonly value: string;
|
|
39
|
+
/** The value as written in the source (after continuation joining, before unescaping). */
|
|
40
|
+
readonly escapedValue: string;
|
|
41
|
+
/** 1-based line number where this property starts. */
|
|
42
|
+
readonly startingLineNumber: number;
|
|
43
|
+
/** 1-based line number where this property ends. */
|
|
44
|
+
readonly endingLineNumber: number;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A comment line starting with `#` or `!`.
|
|
48
|
+
*/
|
|
49
|
+
export type CommentNode = {
|
|
50
|
+
/** Discriminator. */
|
|
51
|
+
readonly type: 'comment';
|
|
52
|
+
/** The full line as-is (without the EOL character). */
|
|
53
|
+
readonly rawLine: string;
|
|
54
|
+
/** Leading whitespace before the delimiter. */
|
|
55
|
+
readonly leadingWhitespace: string;
|
|
56
|
+
/** The comment delimiter character. */
|
|
57
|
+
readonly delimiter: '#' | '!';
|
|
58
|
+
/** The text after the delimiter (including any space immediately after it). */
|
|
59
|
+
readonly body: string;
|
|
60
|
+
/** 1-based line number. */
|
|
61
|
+
readonly lineNumber: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* A blank line (may contain only whitespace characters).
|
|
65
|
+
*/
|
|
66
|
+
export type BlankLineNode = {
|
|
67
|
+
/** Discriminator. */
|
|
68
|
+
readonly type: 'blank';
|
|
69
|
+
/** The raw whitespace content of the line (without the EOL character). */
|
|
70
|
+
readonly rawLine: string;
|
|
71
|
+
/** 1-based line number. */
|
|
72
|
+
readonly lineNumber: number;
|
|
73
|
+
};
|
|
74
|
+
/** Discriminated union of all node types in a `.properties` file. */
|
|
75
|
+
export type PropertiesNode = PropertyNode | CommentNode | BlankLineNode;
|
|
76
|
+
/** A plain key-value pair object where every value is a string. */
|
|
77
|
+
export type KeyValuePairObject = {
|
|
78
|
+
[key: string]: string;
|
|
79
|
+
};
|
|
80
|
+
/** Information about a key that appears more than once. */
|
|
81
|
+
export type KeyCollisions = {
|
|
82
|
+
/** The duplicate key. */
|
|
83
|
+
key: string;
|
|
84
|
+
/** All property nodes with this key, in file order. */
|
|
85
|
+
nodes: PropertyNode[];
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Options for {@link Properties.format} when producing normalized output.
|
|
89
|
+
*
|
|
90
|
+
* When no options are passed to `format()`, the output is a lossless reconstruction
|
|
91
|
+
* of the original content. Passing any option triggers normalization, rebuilding
|
|
92
|
+
* property lines from their parsed fields.
|
|
93
|
+
*/
|
|
94
|
+
export type NormalizeOptions = {
|
|
95
|
+
/** Override the end-of-line character. Defaults to the EOL detected from the source. */
|
|
96
|
+
endOfLineCharacter?: '\n' | '\r\n';
|
|
97
|
+
/** If `true`, remove all comment lines from the output. Default: `false`. */
|
|
98
|
+
removeComments?: boolean;
|
|
99
|
+
/** If `true`, remove all blank lines from the output. Default: `false`. */
|
|
100
|
+
removeBlankLines?: boolean;
|
|
101
|
+
/** If `true`, strip leading whitespace from all property and comment lines. Default: `false`. */
|
|
102
|
+
removeLeadingWhitespace?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* If `true`, keep only the last occurrence of each duplicate key (Java's last-wins
|
|
105
|
+
* semantics). Earlier occurrences and their leading comment/blank line nodes are
|
|
106
|
+
* removed from the output. Set `deduplicateKeysKeepLeadingNodes` to `true` to
|
|
107
|
+
* preserve the leading nodes. Default: `false`.
|
|
108
|
+
*/
|
|
109
|
+
deduplicateKeys?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* When `deduplicateKeys` is `true`, controls whether comment and blank line nodes
|
|
112
|
+
* preceding removed duplicates are preserved (`true`) or also removed (`false`).
|
|
113
|
+
* Default: `false` (leading nodes are removed along with the duplicate).
|
|
114
|
+
*/
|
|
115
|
+
deduplicateKeysKeepLeadingNodes?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Standardize the separator character across all properties. When set, every
|
|
118
|
+
* property is rewritten to use this separator. Original separators are preserved
|
|
119
|
+
* if not set. Use `' '` for whitespace-only separators (no `=` or `:`).
|
|
120
|
+
*/
|
|
121
|
+
separatorChar?: '=' | ':' | ' ';
|
|
122
|
+
/**
|
|
123
|
+
* Whitespace to place before the separator character (e.g. `' '` for `key = value`,
|
|
124
|
+
* `''` for `key=value`). When not set, each property's original leading whitespace
|
|
125
|
+
* is preserved.
|
|
126
|
+
*/
|
|
127
|
+
separatorLeading?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Whitespace to place after the separator character (e.g. `' '` for `key = value`,
|
|
130
|
+
* `''` for `key=value`). When not set, each property's original trailing whitespace
|
|
131
|
+
* is preserved.
|
|
132
|
+
*/
|
|
133
|
+
separatorTrailing?: string;
|
|
134
|
+
/**
|
|
135
|
+
* If `true`, re-escape all non-ASCII characters as `\\uXXXX` sequences. Useful for
|
|
136
|
+
* producing ISO-8859-1 compatible output. Default: `false` (preserves original encoding).
|
|
137
|
+
*/
|
|
138
|
+
escapeUnicode?: boolean;
|
|
139
|
+
/**
|
|
140
|
+
* If `true`, collapse multiline keys and values (joined via `\\` line continuations)
|
|
141
|
+
* into single lines. Default: `false` (preserves original line structure).
|
|
142
|
+
*/
|
|
143
|
+
collapseMultiline?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Wrap keys at this character width using `\\` line continuations. Only applies to
|
|
146
|
+
* keys longer than the specified width. `\\uXXXX` sequences are never split across
|
|
147
|
+
* lines. `undefined` = no wrapping.
|
|
148
|
+
*/
|
|
149
|
+
wrapKeysAt?: number;
|
|
150
|
+
/**
|
|
151
|
+
* Wrap values at this character width using `\\` line continuations. Only applies to
|
|
152
|
+
* values longer than the specified width. `\\uXXXX` sequences are never split across
|
|
153
|
+
* lines. `undefined` = no wrapping.
|
|
154
|
+
*/
|
|
155
|
+
wrapValuesAt?: number;
|
|
156
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export var PropertiesNodeType={PROPERTY:"property",COMMENT:"comment",BLANK:"blank"};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NormalizeOptions, PropertiesNode } from './nodes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Format a set of property nodes into normalized output.
|
|
4
|
+
*
|
|
5
|
+
* @param nodes - All nodes from the parsed document.
|
|
6
|
+
* @param hasBom - Whether the original content had a BOM.
|
|
7
|
+
* @param eolCharacter - The detected EOL character.
|
|
8
|
+
* @param options - Normalization options.
|
|
9
|
+
*
|
|
10
|
+
* @returns The normalized `.properties` file content.
|
|
11
|
+
*/
|
|
12
|
+
export declare const formatNormalized: (nodes: PropertiesNode[], hasBom: boolean, eolCharacter: string, options: NormalizeOptions) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{escapeKey,escapeValue}from"../escape/index.js";var BOM="\ufeff",wrapAtWidth=function(e,a){if(e.length<=a)return e;for(var r=[],i=0,t=e.length;i<t;){var o=Math.min(i+a,t);if(o<t)for(var n=0;n<6&&o-n>i;n++){var p=o-n;if(p+5<=t&&"\\"===e[p]&&"u"===e[p+1]){o=p;break}}r.push(e.slice(i,o)),i=o}return r.join("\\\n ")},rebuildPropertyLine=function(e,a){var r,i,t,o,n=!0===a.escapeUnicode,p=a.removeLeadingWhitespace?"":e.leadingWhitespace,s=n?escapeKey(e.key,!0):e.escapedKey;void 0!==a.separatorChar?(i=void 0!==a.separatorLeading?a.separatorLeading:e.separatorLeading,t=" "===a.separatorChar?"":a.separatorChar,o=void 0!==a.separatorTrailing?a.separatorTrailing:e.separatorTrailing," "===a.separatorChar&&(i=void 0!==a.separatorLeading?a.separatorLeading:" ")):(i=void 0!==a.separatorLeading?a.separatorLeading:e.separatorLeading,t=null!==(r=e.separatorChar)&&void 0!==r?r:"",o=void 0!==a.separatorTrailing?a.separatorTrailing:e.separatorTrailing);var d=n?escapeValue(e.value,!0):e.escapedValue;return void 0!==a.wrapKeysAt&&a.wrapKeysAt>0&&(s=wrapAtWidth(s,a.wrapKeysAt)),void 0!==a.wrapValuesAt&&a.wrapValuesAt>0&&(d=wrapAtWidth(d,a.wrapValuesAt)),"".concat(p).concat(s).concat(i).concat(t).concat(o).concat(d)};export var formatNormalized=function(e,a,r,i){var t,o,n=null!==(t=i.endOfLineCharacter)&&void 0!==t?t:r,p=void 0!==i.separatorChar||void 0!==i.separatorLeading||void 0!==i.separatorTrailing||!0===i.escapeUnicode||!0===i.collapseMultiline||void 0!==i.wrapKeysAt||void 0!==i.wrapValuesAt||!0===i.removeLeadingWhitespace;if(i.deduplicateKeys){for(var s={},d=[],c=e.length-1;c>=0;c--){var l=e[c];"property"===l.type&&(s[l.key]?d.push(c):s[l.key]=!0)}o={};var v=!0,u=!1,h=void 0;try{for(var f,y=d[Symbol.iterator]();!(v=(f=y.next()).done);v=!0){var g=f.value;if(o[g]=!0,!i.deduplicateKeysKeepLeadingNodes)for(var L=g-1;L>=0&&"property"!==e[L].type;L--)o[L]=!0}}catch(e){u=!0,h=e}finally{try{v||null==y.return||y.return()}finally{if(u)throw h}}}for(var m=[],w=0;w<e.length;w++)if(!o||!o[w]){var A=e[w];switch(A.type){case"comment":if(i.removeComments)continue;m.push(i.removeLeadingWhitespace?"".concat(A.delimiter).concat(A.body):A.rawLine);break;case"blank":if(i.removeBlankLines)continue;m.push(A.rawLine);break;case"property":p?m.push(rebuildPropertyLine(A,i)):m.push(A.rawLines.join(n))}}return(a?BOM:"")+m.join(n)};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PropertiesNode } from './nodes.js';
|
|
2
|
+
/** Result of parsing a `.properties` file. */
|
|
3
|
+
export type ParseResult = {
|
|
4
|
+
/** Whether the content starts with a BOM character. */
|
|
5
|
+
hasBom: boolean;
|
|
6
|
+
/** The detected end-of-line character sequence. */
|
|
7
|
+
eolCharacter: string;
|
|
8
|
+
/** All parsed nodes in file order. */
|
|
9
|
+
nodes: PropertiesNode[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Parse a `.properties` file into a lossless sequence of nodes.
|
|
13
|
+
*
|
|
14
|
+
* Every element in the file — properties, comments, and blank lines — is
|
|
15
|
+
* preserved in order. Duplicate keys are all retained. The original content
|
|
16
|
+
* can be reconstructed exactly by joining node raw lines with the EOL
|
|
17
|
+
* character.
|
|
18
|
+
*
|
|
19
|
+
* @param content - The content of a `.properties` file.
|
|
20
|
+
*
|
|
21
|
+
* @returns The parse result with BOM info, detected EOL, and ordered nodes.
|
|
22
|
+
*/
|
|
23
|
+
export declare const parseDocument: (content: string | Buffer) => ParseResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{unescapeContent}from"../unescape/index.js";var CH_TAB=9,CH_LF=10,CH_FF=12,CH_CR=13,CH_SPACE=32,CH_BANG=33,CH_HASH=35,CH_COLON=58,CH_EQUALS=61,CH_BACKSLASH=92,CH_BOM=65279,isWhitespace=function(e){return e===CH_SPACE||e===CH_TAB||e===CH_FF},countTrailingBackslashes=function(e,r){for(var a=0,i=r-1;i>=0&&e.charCodeAt(i)===CH_BACKSLASH;)a++,i--;return a};export var parseDocument=function(e){var r="string"==typeof e?e:e.toString(),a=r.length,i=0,s=a>0&&r.charCodeAt(0)===CH_BOM;s&&(i=1);for(var n="\n",t=i;t<a;t++){var C=r.charCodeAt(t);if(C===CH_CR){n=t+1<a&&r.charCodeAt(t+1)===CH_LF?"\r\n":"\r";break}if(C===CH_LF){n="\n";break}}for(var c=(i>0?r.slice(i):r).split(/\r\n|\r|\n/),o=[],l=0,h=c.length;l<h;){for(var H=c[l],p=l+1,v=0,A=H.length;v<A&&isWhitespace(H.charCodeAt(v));)v++;if(v>=A){var f={type:"blank",rawLine:H,lineNumber:p};o.push(f),l++}else{var _=H.charCodeAt(v);if(_!==CH_HASH&&_!==CH_BANG){for(var d=[H],u=p,g=v>0?H.slice(0,v):"",L=countTrailingBackslashes(H,A),S=L%2==1;S&&l+1<h;){var B=c[++l];d.push(B);var m=B.length;S=(L=countTrailingBackslashes(B,m))%2==1}var O=l+1,b=S,N=void 0;if(1===d.length){var k=v>0?H.slice(v):H;N=b?k.slice(0,-1):k}else{for(var y=[],W=0;W<d.length;W++){var F=d[W],E=void 0,T=void 0;if(0===W)E=v,T=F.length-1;else{E=0;for(var x=F.length;E<x&&isWhitespace(F.charCodeAt(E));)E++;T=W===d.length-1&&!b?x:x-1}y.push(F.slice(E,T))}N=y.join("")}for(var K=N.length,Q=0,U=!1;Q<K;){var w=N.charCodeAt(Q);if(w!==CH_BACKSLASH){if(!U&&(w===CH_EQUALS||w===CH_COLON||isWhitespace(w)))break;U=!1,Q++}else U=!U,Q++}var j=N.slice(0,Q),G="",M=void 0,P="",R=Q;if(R<K){for(var D=R;R<K&&isWhitespace(N.charCodeAt(R));)R++;if(R<K){var V=N.charCodeAt(R);if(V===CH_EQUALS||V===CH_COLON){G=N.slice(D,R),M=V===CH_EQUALS?"=":":";for(var q=++R;R<K&&isWhitespace(N.charCodeAt(R));)R++;P=N.slice(q,R)}else G=N.slice(D,R)}else G=N.slice(D,R)}var z=N.slice(R),I={type:"property",rawLines:d,leadingWhitespace:g,key:-1!==j.indexOf("\\")?unescapeContent(j):j,escapedKey:j,separatorLeading:G,separatorChar:M,separatorTrailing:P,value:-1!==z.indexOf("\\")?unescapeContent(z):z,escapedValue:z,startingLineNumber:u,endingLineNumber:O};o.push(I),l++}else{var J={type:"comment",rawLine:H,leadingWhitespace:v>0?H.slice(0,v):"",delimiter:_===CH_HASH?"#":"!",body:H.slice(v+1),lineNumber:p};o.push(J),l++}}}return{hasBom:s,eolCharacter:n,nodes:o}};
|