properties-file 3.7.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +253 -285
- package/dist/cjs/bundler/bun.d.ts +10 -0
- package/dist/cjs/bundler/bun.js +1 -0
- package/dist/cjs/bundler/esbuild.d.ts +11 -0
- package/dist/cjs/bundler/esbuild.js +1 -0
- package/dist/cjs/bundler/rollup.d.ts +11 -0
- package/dist/cjs/bundler/rollup.js +1 -0
- package/dist/cjs/bundler/webpack.d.ts +12 -0
- package/dist/cjs/bundler/webpack.js +1 -0
- package/dist/cjs/editor/index.d.ts +76 -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 +113 -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/bundler/bun.d.ts +10 -0
- package/dist/esm/bundler/bun.js +1 -0
- package/dist/esm/bundler/esbuild.d.ts +11 -0
- package/dist/esm/bundler/esbuild.js +1 -0
- package/dist/esm/bundler/rollup.d.ts +11 -0
- package/dist/esm/bundler/rollup.js +1 -0
- package/dist/esm/bundler/webpack.d.ts +12 -0
- package/dist/esm/editor/index.d.ts +76 -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 +113 -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 +81 -30
- package/dist/cjs/loader/webpack.d.ts +0 -12
- package/dist/cjs/loader/webpack.js +0 -1
- 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/loader/webpack.d.ts +0 -12
- 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
- /package/dist/esm/{loader → bundler}/webpack.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"Properties",{enumerable:!0,get:function(){return Properties}});var _normalize=require("./normalize.js"),_parse=require("./parse.js");function _instanceof(e,r){return null!=r&&"undefined"!=typeof Symbol&&r[Symbol.hasInstance]?!!r[Symbol.hasInstance](e):e instanceof r}function _class_call_check(e,r){if(!_instanceof(e,r))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _create_class(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),e}var BOM="\ufeff",Properties=function(){function e(r){_class_call_check(this,e);var t=(0,_parse.parseDocument)(r);this.hasBom=t.hasBom,this.eolCharacter=t.eolCharacter,this.nodes=t.nodes}return _create_class(e,[{key:"getProperties",value:function(){return this.nodes.filter(function(e){return"property"===e.type})}},{key:"getComments",value:function(){return this.nodes.filter(function(e){return"comment"===e.type})}},{key:"getBlankLines",value:function(){return this.nodes.filter(function(e){return"blank"===e.type})}},{key:"toObject",value:function(){var e={},r=!0,t=!1,n=void 0;try{for(var o,a=this.nodes[Symbol.iterator]();!(r=(o=a.next()).done);r=!0){var i=o.value;"property"===i.type&&(e[i.key]=i.value)}}catch(e){t=!0,n=e}finally{try{r||null==a.return||a.return()}finally{if(t)throw n}}return e}},{key:"getPropertyNodes",value:function(e){return this.nodes.filter(function(r){return"property"===r.type&&r.key===e})}},{key:"getEffectiveProperty",value:function(e){for(var r=this.nodes.length-1;r>=0;r--){var t=this.nodes[r];if("property"===t.type&&t.key===e)return t}}},{key:"getKeyCollisions",value:function(){var e={},r=!0,t=!1,n=void 0;try{for(var o,a=this.nodes[Symbol.iterator]();!(r=(o=a.next()).done);r=!0){var i=o.value;"property"===i.type&&(e[i.key]||(e[i.key]=[]),e[i.key].push(i))}}catch(e){t=!0,n=e}finally{try{r||null==a.return||a.return()}finally{if(t)throw n}}var s=[],l=!0,u=!1,f=void 0;try{for(var y,c=Object.keys(e)[Symbol.iterator]();!(l=(y=c.next()).done);l=!0){var p=y.value;e[p].length>1&&s.push({key:p,nodes:e[p]})}}catch(e){u=!0,f=e}finally{try{l||null==c.return||c.return()}finally{if(u)throw f}}return s}},{key:"getLeadingNodes",value:function(e){for(var r=-1,t=this.nodes.length-1;t>=0;t--){var n=this.nodes[t];if("property"===n.type&&n.key===e){r=t;break}}if(r<=0)return[];for(var o=[],a=r-1;a>=0;a--){var i=this.nodes[a];if("property"===i.type)break;o.unshift(i)}return o}},{key:"format",value:function(e){if(!e){var r=this.eolCharacter,t=[],n=!0,o=!1,a=void 0;try{for(var i,s=this.nodes[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var l=i.value;"property"===l.type?t.push(l.rawLines.join(r)):t.push(l.rawLine)}}catch(e){o=!0,a=e}finally{try{n||null==s.return||s.return()}finally{if(o)throw a}}return(this.hasBom?BOM:"")+t.join(r)}return(0,_normalize.formatNormalized)(this.nodes,this.hasBom,this.eolCharacter,e)}}]),e}();
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tries to unescape the content from either key or value of a property.
|
|
3
3
|
*
|
|
4
|
+
* Uses a single-pass, segment-based approach with `charCodeAt()` instead of
|
|
5
|
+
* regex. Scans for backslashes, flushes literal text in bulk via `slice()`,
|
|
6
|
+
* and only builds new characters for actual escape sequences.
|
|
7
|
+
*
|
|
4
8
|
* @param content - The content to unescape.
|
|
5
9
|
*
|
|
6
10
|
* @returns The unescaped content.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"unescapeContent",{enumerable:!0,get:function(){return unescapeContent}});var CH_BACKSLASH=92,CH_LOWER_F=102,CH_LOWER_N=110,CH_LOWER_R=114,CH_LOWER_T=116,CH_LOWER_U=117,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){if(-1===e.indexOf("\\"))return e;for(var r=e.length,t="",a=0,c=0;c<r;)if(e.charCodeAt(c)===CH_BACKSLASH){if(c>a&&(t+=e.slice(a,c)),++c>=r){t+="\\",a=c;break}switch(e.charCodeAt(c)){case CH_LOWER_N:t+="\n",c++;break;case CH_LOWER_T:t+="\t",c++;break;case CH_LOWER_R:t+="\r",c++;break;case CH_LOWER_F:t+="\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,"'"))}var n=hexValue(e.charCodeAt(c+1))<<12|hexValue(e.charCodeAt(c+2))<<8|hexValue(e.charCodeAt(c+3))<<4|hexValue(e.charCodeAt(c+4));t+=String.fromCharCode(n),c+=5;break;default:t+=e.charAt(c),c++}a=c}else c++;return a<r&&(t+=e.slice(a,r)),t};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BunPlugin } from 'bun';
|
|
2
|
+
/**
|
|
3
|
+
* Bun plugin for `.properties` files. Works with both `Bun.plugin` (runtime) and `Bun.build`
|
|
4
|
+
* (build-time).
|
|
5
|
+
*/
|
|
6
|
+
declare const bunPlugin: BunPlugin;
|
|
7
|
+
export default bunPlugin;
|
|
8
|
+
|
|
9
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
10
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readFileSync}from"node:fs";import{getProperties}from"../index.js";var bunPlugin={name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var r=e.path;return{exports:{properties:getProperties(readFileSync(r,"utf8"))},loader:"object"}})}};export default bunPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild';
|
|
2
|
+
/**
|
|
3
|
+
* esbuild plugin for `.properties` files.
|
|
4
|
+
*
|
|
5
|
+
* @returns An esbuild plugin that transforms `.properties` imports into JavaScript modules.
|
|
6
|
+
*/
|
|
7
|
+
declare const esbuildPlugin: () => Plugin;
|
|
8
|
+
export default esbuildPlugin;
|
|
9
|
+
|
|
10
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
11
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readFileSync}from"node:fs";import{getProperties}from"../index.js";var esbuildPlugin=function(){return{name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var r=e.path;return{contents:"export const properties = ".concat(JSON.stringify(getProperties(readFileSync(r,"utf8"))),";"),loader:"js"}})}}};export default esbuildPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'rollup';
|
|
2
|
+
/**
|
|
3
|
+
* Rollup plugin for `.properties` files. Also compatible with Vite and Rolldown.
|
|
4
|
+
*
|
|
5
|
+
* @returns A Rollup plugin that transforms `.properties` imports into JavaScript modules.
|
|
6
|
+
*/
|
|
7
|
+
declare const rollupPlugin: () => Plugin;
|
|
8
|
+
export default rollupPlugin;
|
|
9
|
+
|
|
10
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
11
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getProperties}from"../index.js";var PROPERTIES_EXTENSION=".properties",rollupPlugin=function(){return{name:"properties-file",transform:function(e,r){return-1===r.indexOf(PROPERTIES_EXTENSION,r.length-PROPERTIES_EXTENSION.length)?null:{code:"export const properties = ".concat(JSON.stringify(getProperties(e)),";"),map:null}}}};export default rollupPlugin;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webpack file loader for `.properties` files. Also compatible with Rspack.
|
|
3
|
+
*
|
|
4
|
+
* @param content - The content of a `.properties` file.
|
|
5
|
+
*
|
|
6
|
+
* @returns A CommonJS module string exporting the parsed key-value pairs.
|
|
7
|
+
*/
|
|
8
|
+
declare const webpackLoader: (content: string) => string;
|
|
9
|
+
export default webpackLoader;
|
|
10
|
+
|
|
11
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
12
|
+
import '../properties-file.d.ts'
|
|
@@ -1,154 +1,137 @@
|
|
|
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
|
+
/** Key to insert before or after. */
|
|
14
10
|
referenceKey?: string;
|
|
15
|
-
/**
|
|
11
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
16
12
|
position?: 'before' | 'after';
|
|
17
|
-
/** Escape
|
|
13
|
+
/** Escape non-ASCII characters as `\\uXXXX`. Default: `false`. */
|
|
18
14
|
escapeUnicode?: boolean;
|
|
19
|
-
/**
|
|
15
|
+
/** Separator character to use. Default: `'='`. */
|
|
20
16
|
separator?: KeyValuePairSeparator;
|
|
21
|
-
/**
|
|
17
|
+
/** Comment text to prepend (without delimiter). */
|
|
22
18
|
comment?: string;
|
|
23
|
-
/**
|
|
19
|
+
/** Comment delimiter. Default: `'#'`. */
|
|
24
20
|
commentDelimiter?: CommentDelimiter;
|
|
25
21
|
};
|
|
26
|
-
/** Options
|
|
22
|
+
/** Options for {@link PropertiesEditor.insertComment}. */
|
|
27
23
|
export type InsertCommentOptions = {
|
|
28
|
-
/**
|
|
24
|
+
/** Key to insert before or after. */
|
|
29
25
|
referenceKey?: string;
|
|
30
|
-
/**
|
|
26
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
31
27
|
position?: 'before' | 'after';
|
|
32
|
-
/**
|
|
28
|
+
/** Comment delimiter. Default: `'#'`. */
|
|
33
29
|
commentDelimiter?: CommentDelimiter;
|
|
34
30
|
};
|
|
35
|
-
/** Options
|
|
31
|
+
/** Options for {@link PropertiesEditor.insertBlankLine}. */
|
|
32
|
+
export type InsertBlankLineOptions = {
|
|
33
|
+
/** Key to insert before or after. */
|
|
34
|
+
referenceKey?: string;
|
|
35
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
36
|
+
position?: 'before' | 'after';
|
|
37
|
+
};
|
|
38
|
+
/** Options for {@link PropertiesEditor.update}. */
|
|
36
39
|
export type UpdateOptions = {
|
|
37
|
-
/**
|
|
40
|
+
/** New value. */
|
|
38
41
|
newValue?: string;
|
|
39
|
-
/**
|
|
42
|
+
/** New key (rename). */
|
|
40
43
|
newKey?: string;
|
|
41
|
-
/** Escape
|
|
44
|
+
/** Escape non-ASCII characters as `\\uXXXX`. Default: `false`. */
|
|
42
45
|
escapeUnicode?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
separator?:
|
|
45
|
-
/**
|
|
46
|
+
/** New separator character. */
|
|
47
|
+
separator?: KeyValuePairSeparator;
|
|
48
|
+
/** New comment text (replaces all leading comment nodes). */
|
|
46
49
|
newComment?: string;
|
|
47
|
-
/**
|
|
50
|
+
/** Comment delimiter. Default: `'#'`. */
|
|
48
51
|
commentDelimiter?: CommentDelimiter;
|
|
49
52
|
};
|
|
50
|
-
/** Options
|
|
53
|
+
/** Options for {@link PropertiesEditor.upsert}. */
|
|
51
54
|
export type UpsertOptions = {
|
|
52
|
-
/** Escape
|
|
55
|
+
/** Escape non-ASCII characters as `\\uXXXX`. Default: `false`. */
|
|
53
56
|
escapeUnicode?: boolean;
|
|
54
|
-
/**
|
|
57
|
+
/** Separator character. Default: `'='`. */
|
|
55
58
|
separator?: KeyValuePairSeparator;
|
|
56
|
-
/**
|
|
59
|
+
/** Comment text for new properties. */
|
|
57
60
|
comment?: string;
|
|
58
|
-
/**
|
|
61
|
+
/** Comment delimiter. Default: `'#'`. */
|
|
59
62
|
commentDelimiter?: CommentDelimiter;
|
|
60
63
|
};
|
|
64
|
+
/** Options for {@link PropertiesEditor.delete}. */
|
|
65
|
+
export type DeleteOptions = {
|
|
66
|
+
/** If `true`, also delete preceding comment and blank line nodes. Default: `true`. */
|
|
67
|
+
deleteLeadingNodes?: boolean;
|
|
68
|
+
};
|
|
61
69
|
/**
|
|
62
|
-
*
|
|
70
|
+
* An editor for `.properties` files that extends the lossless {@link Properties}
|
|
71
|
+
* parser with insert, update, delete, and upsert operations.
|
|
63
72
|
*/
|
|
64
73
|
export declare class PropertiesEditor extends Properties {
|
|
65
|
-
/** Is line parsing required to re-async the object's properties? */
|
|
66
|
-
private needsLineParsing;
|
|
67
74
|
/**
|
|
68
|
-
*
|
|
75
|
+
* Find the index of the last property node with the given key.
|
|
69
76
|
*
|
|
70
|
-
* @param
|
|
71
|
-
*/
|
|
72
|
-
constructor(content: string);
|
|
73
|
-
/**
|
|
74
|
-
* Find the last occurrence of a property by key (iterates backward for performance).
|
|
77
|
+
* @param key - The unescaped key to search for.
|
|
75
78
|
*
|
|
76
|
-
* @
|
|
77
|
-
*
|
|
78
|
-
* @returns The last matching property, or undefined if not found.
|
|
79
|
+
* @returns The index in `this.nodes`, or `-1` if not found.
|
|
79
80
|
*/
|
|
80
|
-
private
|
|
81
|
+
private findLastPropertyIndex;
|
|
81
82
|
/**
|
|
82
|
-
*
|
|
83
|
-
*/
|
|
84
|
-
private parseLinesIfNeeded;
|
|
85
|
-
/**
|
|
86
|
-
* Insert a new property in the existing object (by default it will be at the end).
|
|
87
|
-
*
|
|
88
|
-
* @param key - A property key (unescaped).
|
|
89
|
-
* @param value - A property value (unescaped).
|
|
90
|
-
* @param options - Additional options.
|
|
83
|
+
* Insert a new property.
|
|
91
84
|
*
|
|
92
|
-
* @
|
|
85
|
+
* @param key - The unescaped key.
|
|
86
|
+
* @param value - The unescaped value.
|
|
87
|
+
* @param options - Insert options.
|
|
93
88
|
*/
|
|
94
|
-
insert(key: string, value: string, options?: InsertOptions):
|
|
89
|
+
insert(key: string, value: string, options?: InsertOptions): void;
|
|
95
90
|
/**
|
|
96
|
-
* Insert a
|
|
91
|
+
* Insert a comment.
|
|
97
92
|
*
|
|
98
|
-
* @param comment - The comment
|
|
99
|
-
* @param options -
|
|
100
|
-
*
|
|
101
|
-
* @returns True if the comment was inserted, otherwise false.
|
|
93
|
+
* @param comment - The comment text (may contain newlines).
|
|
94
|
+
* @param options - Insert comment options.
|
|
102
95
|
*/
|
|
103
|
-
insertComment(comment: string, options?: InsertCommentOptions):
|
|
96
|
+
insertComment(comment: string, options?: InsertCommentOptions): void;
|
|
104
97
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* @param key - The name of the key to delete.
|
|
108
|
-
* @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted.
|
|
98
|
+
* Insert a blank line.
|
|
109
99
|
*
|
|
110
|
-
* @
|
|
100
|
+
* @param options - Insert blank line options.
|
|
111
101
|
*/
|
|
112
|
-
|
|
102
|
+
insertBlankLine(options?: InsertBlankLineOptions): void;
|
|
113
103
|
/**
|
|
114
|
-
*
|
|
104
|
+
* Update an existing property.
|
|
115
105
|
*
|
|
116
|
-
* @param
|
|
106
|
+
* @param key - The unescaped key to update (uses last occurrence).
|
|
107
|
+
* @param options - Update options.
|
|
117
108
|
*
|
|
118
|
-
* @returns
|
|
109
|
+
* @returns `true` if the property was found and updated, `false` otherwise.
|
|
119
110
|
*/
|
|
120
|
-
|
|
111
|
+
update(key: string, options: UpdateOptions): boolean;
|
|
121
112
|
/**
|
|
122
|
-
*
|
|
113
|
+
* Update a property if it exists, or insert it if it doesn't.
|
|
123
114
|
*
|
|
124
|
-
* @param
|
|
125
|
-
*
|
|
126
|
-
* @
|
|
115
|
+
* @param key - The unescaped key.
|
|
116
|
+
* @param value - The unescaped value.
|
|
117
|
+
* @param options - Upsert options.
|
|
127
118
|
*/
|
|
128
|
-
|
|
119
|
+
upsert(key: string, value: string, options?: UpsertOptions): void;
|
|
129
120
|
/**
|
|
130
|
-
*
|
|
121
|
+
* Delete the last occurrence of a property (the effective value in last-wins semantics).
|
|
131
122
|
*
|
|
132
|
-
* @param key - The
|
|
133
|
-
* @param options -
|
|
123
|
+
* @param key - The unescaped key to delete.
|
|
124
|
+
* @param options - Delete options.
|
|
134
125
|
*
|
|
135
|
-
* @returns
|
|
126
|
+
* @returns The deleted {@link PropertyNode}, or `undefined` if the key was not found.
|
|
136
127
|
*/
|
|
137
|
-
|
|
128
|
+
delete(key: string, options?: DeleteOptions): PropertyNode | undefined;
|
|
138
129
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* @param key - A property key (unescaped).
|
|
142
|
-
* @param value - A property value (unescaped).
|
|
143
|
-
* @param options - Additional options.
|
|
130
|
+
* Delete all occurrences of a key.
|
|
144
131
|
*
|
|
145
|
-
* @
|
|
146
|
-
*/
|
|
147
|
-
upsert(key: string, value: string, options?: UpsertOptions): boolean;
|
|
148
|
-
/**
|
|
149
|
-
* Get the key/value object representing the properties.
|
|
132
|
+
* @param key - The unescaped key to delete.
|
|
150
133
|
*
|
|
151
|
-
* @returns
|
|
134
|
+
* @returns An array of the deleted {@link PropertyNode} instances.
|
|
152
135
|
*/
|
|
153
|
-
|
|
136
|
+
deleteAll(key: string): PropertyNode[];
|
|
154
137
|
}
|
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,113 @@
|
|
|
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
|
+
/** Options for {@link Properties.format} when producing normalized output. */
|
|
88
|
+
export type NormalizeOptions = {
|
|
89
|
+
/** End-of-line character to use. Defaults to the detected EOL from the source. */
|
|
90
|
+
endOfLineCharacter?: '\n' | '\r\n';
|
|
91
|
+
/** If `true`, remove all comment lines. Default: `false`. */
|
|
92
|
+
removeComments?: boolean;
|
|
93
|
+
/** If `true`, remove all blank lines. Default: `false`. */
|
|
94
|
+
removeBlankLines?: boolean;
|
|
95
|
+
/** If `true`, remove leading whitespace from all lines. Default: `false`. */
|
|
96
|
+
removeLeadingWhitespace?: boolean;
|
|
97
|
+
/** If `true`, keep only the last occurrence of each key. Default: `false`. */
|
|
98
|
+
deduplicateKeys?: boolean;
|
|
99
|
+
/** Standardize the separator character. Original separators are preserved if not set. */
|
|
100
|
+
separatorChar?: '=' | ':' | ' ';
|
|
101
|
+
/** Whitespace to place before the separator character. */
|
|
102
|
+
separatorLeading?: string;
|
|
103
|
+
/** Whitespace to place after the separator character. */
|
|
104
|
+
separatorTrailing?: string;
|
|
105
|
+
/** If `true`, escape all non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
106
|
+
escapeUnicode?: boolean;
|
|
107
|
+
/** If `true`, collapse multiline keys and values to single lines. Default: `false`. */
|
|
108
|
+
collapseMultiline?: boolean;
|
|
109
|
+
/** Wrap keys at this character width using line continuations. `undefined` = no wrapping. */
|
|
110
|
+
wrapKeysAt?: number;
|
|
111
|
+
/** Wrap values at this character width using line continuations. `undefined` = no wrapping. */
|
|
112
|
+
wrapValuesAt?: number;
|
|
113
|
+
};
|
|
@@ -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(a,e){if(a.length<=e)return a;for(var r=[],i=0,t=a.length;i<t;){var o=Math.min(i+e,t);if(o<t)for(var n=0;n<6&&o-n>i;n++){var p=o-n;if(p+5<=t&&"\\"===a[p]&&"u"===a[p+1]){o=p;break}}r.push(a.slice(i,o)),i=o}return r.join("\\\n ")},rebuildPropertyLine=function(a,e){var r,i,t,o,n=!0===e.escapeUnicode,p=e.removeLeadingWhitespace?"":a.leadingWhitespace,s=n?escapeKey(a.key,!0):a.escapedKey;void 0!==e.separatorChar?(i=void 0!==e.separatorLeading?e.separatorLeading:a.separatorLeading,t=" "===e.separatorChar?"":e.separatorChar,o=void 0!==e.separatorTrailing?e.separatorTrailing:a.separatorTrailing," "===e.separatorChar&&(i=void 0!==e.separatorLeading?e.separatorLeading:" ")):(i=void 0!==e.separatorLeading?e.separatorLeading:a.separatorLeading,t=null!==(r=a.separatorChar)&&void 0!==r?r:"",o=void 0!==e.separatorTrailing?e.separatorTrailing:a.separatorTrailing);var c=n?escapeValue(a.value,!0):a.escapedValue;return void 0!==e.wrapKeysAt&&e.wrapKeysAt>0&&(s=wrapAtWidth(s,e.wrapKeysAt)),void 0!==e.wrapValuesAt&&e.wrapValuesAt>0&&(c=wrapAtWidth(c,e.wrapValuesAt)),"".concat(p).concat(s).concat(i).concat(t).concat(o).concat(c)};export var formatNormalized=function(a,e,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){var s={};o={};for(var c=a.length-1;c>=0;c--){var d=a[c];"property"!==d.type||s[d.key]||(s[d.key]=!0,o[c]=!0)}}for(var l=[],v=0;v<a.length;v++){var u=a[v];switch(u.type){case"comment":if(i.removeComments)continue;l.push(i.removeLeadingWhitespace?"".concat(u.delimiter).concat(u.body):u.rawLine);break;case"blank":if(i.removeBlankLines)continue;l.push(u.rawLine);break;case"property":if(o&&!o[v])continue;p?l.push(rebuildPropertyLine(u,i)):l.push(u.rawLines.join(n))}}return(e?BOM:"")+l.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}};
|