properties-file
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -34,4 +34,4 @@ import { Properties } from '../properties'; | ||
}; | ||
/** Options on the `Properties.edit` method. */ | ||
export type EditOptions = { | ||
/** Options on the `Properties.update` method. */ | ||
export type UpdateOptions = { | ||
/** Optionally replace the existing value with a new value. */ | ||
@@ -50,2 +50,13 @@ newValue?: string; | ||
}; | ||
/** Options on the `Properties.upsert` method. */ | ||
export type UpsertOptions = { | ||
/** Escape unicode characters into ISO-8859-1 compatible encoding? */ | ||
escapeUnicode?: boolean; | ||
/** The key/value separator character. */ | ||
separator?: KeyValuePairSeparator; | ||
/** A comment to insert before. */ | ||
comment?: string; | ||
/** The comment's delimiter. */ | ||
commentDelimiter?: CommentDelimiter; | ||
}; | ||
/** | ||
@@ -67,4 +78,6 @@ * A .properties file editor. | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was inserted, otherwise false. | ||
*/ | ||
insert(key: string, value: string, options?: InsertOptions): void; | ||
insert(key: string, value: string, options?: InsertOptions): boolean; | ||
/** | ||
@@ -75,11 +88,15 @@ * Insert a new comment in the existing object (by default it will be at the end). | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the comment was inserted, otherwise false. | ||
*/ | ||
insertComment(comment: string, options?: InsertCommentOptions): void; | ||
insertComment(comment: string, options?: InsertCommentOptions): boolean; | ||
/** | ||
* Remove the last occurrence of a given key from the existing object. | ||
* Delete the last occurrence of a given key from the existing object. | ||
* | ||
* @param key - The name of the key to remove. | ||
* @param removeCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be removed. | ||
* @param key - The name of the key to delete. | ||
* @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted. | ||
* | ||
* @returns True if the key was deleted, otherwise false. | ||
*/ | ||
remove(key: string, removeCommentsAndWhiteSpace?: boolean): void; | ||
delete(key: string, deleteCommentsAndWhiteSpace?: boolean): boolean; | ||
/** | ||
@@ -102,8 +119,20 @@ * Restore the original newline characters of a key. | ||
/** | ||
* Edit the last occurrence of a given key from the existing object. | ||
* Update the last occurrence of a given key from the existing object. | ||
* | ||
* @param key - The name of the key to edit. | ||
* @param key - The name of the key to update. | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was updated, otherwise false. | ||
*/ | ||
edit(key: string, options?: EditOptions): void; | ||
update(key: string, options?: UpdateOptions): boolean; | ||
/** | ||
* Update a key if it exist, otherwise add it at the end. | ||
* | ||
* @param key - A property key (unescaped). | ||
* @param value - A property value (unescaped). | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was updated or inserted, otherwise false. | ||
*/ | ||
upsert(key: string, value: string, options?: UpsertOptions): boolean; | ||
} |
@@ -69,2 +69,4 @@ "use strict"; | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was inserted, otherwise false. | ||
*/ | ||
@@ -82,3 +84,3 @@ PropertiesEditor.prototype.insert = function (key, value, options) { | ||
var position = (options === null || options === void 0 ? void 0 : options.position) || 'after'; | ||
// Allow to add multiline keys. | ||
// Allow multiline keys. | ||
var multilineKey = key | ||
@@ -88,3 +90,3 @@ .split(/\r?\n/) | ||
.join('\\\n'); | ||
// Allow to add multiline values. | ||
// Allow multiline values. | ||
var multilineValue = value | ||
@@ -94,3 +96,3 @@ .split(/\r?\n/) | ||
.join('\\\n'); | ||
// Allow to add multiline comments. | ||
// Allow multiline comments. | ||
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " "); | ||
@@ -105,2 +107,3 @@ var multilineComment = (options === null || options === void 0 ? void 0 : options.comment) === undefined | ||
this.parseLines(); | ||
return true; | ||
} | ||
@@ -118,3 +121,5 @@ else { | ||
this.parseLines(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
@@ -127,2 +132,4 @@ }; | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the comment was inserted, otherwise false. | ||
*/ | ||
@@ -134,3 +141,3 @@ PropertiesEditor.prototype.insertComment = function (comment, options) { | ||
var position = (options === null || options === void 0 ? void 0 : options.position) || 'after'; | ||
// Allow to add multiline comments. | ||
// Allow multiline comments. | ||
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " "); | ||
@@ -144,2 +151,3 @@ var newLines = "".concat(commentPrefix).concat(comment) | ||
this.parseLines(); | ||
return true; | ||
} | ||
@@ -157,18 +165,22 @@ else { | ||
this.parseLines(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
/** | ||
* Remove the last occurrence of a given key from the existing object. | ||
* Delete the last occurrence of a given key from the existing object. | ||
* | ||
* @param key - The name of the key to remove. | ||
* @param removeCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be removed. | ||
* @param key - The name of the key to delete. | ||
* @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted. | ||
* | ||
* @returns True if the key was deleted, otherwise false. | ||
*/ | ||
PropertiesEditor.prototype.remove = function (key, removeCommentsAndWhiteSpace) { | ||
PropertiesEditor.prototype.delete = function (key, deleteCommentsAndWhiteSpace) { | ||
var _a, _b; | ||
if (removeCommentsAndWhiteSpace === void 0) { removeCommentsAndWhiteSpace = true; } | ||
if (deleteCommentsAndWhiteSpace === void 0) { deleteCommentsAndWhiteSpace = true; } | ||
// Find the last occurrence of the key. | ||
var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; }); | ||
if (property) { | ||
var startLine = removeCommentsAndWhiteSpace | ||
var startLine = deleteCommentsAndWhiteSpace | ||
? (_b = (_a = property.previousProperty) === null || _a === void 0 ? void 0 : _a.endingLineNumber) !== null && _b !== void 0 ? _b : 0 | ||
@@ -179,3 +191,5 @@ : property.startingLineNumber - 1; | ||
this.parseLines(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
@@ -218,16 +232,18 @@ /** | ||
/** | ||
* Edit the last occurrence of a given key from the existing object. | ||
* Update the last occurrence of a given key from the existing object. | ||
* | ||
* @param key - The name of the key to edit. | ||
* @param key - The name of the key to update. | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was updated, otherwise false. | ||
*/ | ||
PropertiesEditor.prototype.edit = function (key, options) { | ||
PropertiesEditor.prototype.update = function (key, options) { | ||
var _a, _b, _c, _d; | ||
// Find the last occurrence of the key to edit. | ||
// Find the last occurrence of the key to update. | ||
var property = __spreadArray([], __read(this.collection), false).reverse().find(function (property) { return property.key === key; }); | ||
if (!property) { | ||
return; | ||
if (!property || !options) { | ||
return false; | ||
} | ||
var escapeUnicode = (options === null || options === void 0 ? void 0 : options.escapeUnicode) || false; | ||
var separator = (options === null || options === void 0 ? void 0 : options.separator) | ||
var escapeUnicode = options.escapeUnicode || false; | ||
var separator = options.separator | ||
? options.separator === ' ' | ||
@@ -237,15 +253,15 @@ ? ' ' | ||
: property.separator || " ".concat(exports.DEFAULT_SEPARATOR, " ").replace(' ', ' '); | ||
// Allow to edit multiline keys. | ||
var multilineKey = ((_a = options === null || options === void 0 ? void 0 : options.newKey) !== null && _a !== void 0 ? _a : this.getKeyWithNewlines(property)) | ||
// Allow multiline keys. | ||
var multilineKey = ((_a = options.newKey) !== null && _a !== void 0 ? _a : this.getKeyWithNewlines(property)) | ||
.split(/\r?\n/) | ||
.map(function (key) { return (0, escape_1.escapeKey)(key, escapeUnicode); }) | ||
.join('\\\n'); | ||
// Allow to edit multiline values. | ||
var multilineValue = ((_b = options === null || options === void 0 ? void 0 : options.newValue) !== null && _b !== void 0 ? _b : this.getValueWithNewlines(property)) | ||
// Allow multiline values. | ||
var multilineValue = ((_b = options.newValue) !== null && _b !== void 0 ? _b : this.getValueWithNewlines(property)) | ||
.split(/\r?\n/) | ||
.map(function (value) { return (0, escape_1.escapeValue)(value, escapeUnicode); }) | ||
.join('\\\n'); | ||
// Allow to edit multiline comments. | ||
var commentPrefix = "".concat((options === null || options === void 0 ? void 0 : options.commentDelimiter) || exports.DEFAULT_COMMENT_DELIMITER, " "); | ||
var multilineComment = (options === null || options === void 0 ? void 0 : options.newComment) === undefined | ||
// Allow multiline comments. | ||
var commentPrefix = "".concat(options.commentDelimiter || exports.DEFAULT_COMMENT_DELIMITER, " "); | ||
var multilineComment = options.newComment === undefined | ||
? '' | ||
@@ -255,9 +271,30 @@ : "".concat("".concat(commentPrefix).concat(options.newComment).split(/\r?\n/).join("\n".concat(commentPrefix)), "\n"); | ||
// Replace the existing property with the new one. | ||
this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, (options === null || options === void 0 ? void 0 : options.newComment) === undefined | ||
this.lines = __spreadArray(__spreadArray(__spreadArray([], __read(this.lines.slice(0, options.newComment === undefined | ||
? property.startingLineNumber - 1 | ||
: (_d = (_c = property.previousProperty) === null || _c === void 0 ? void 0 : _c.endingLineNumber) !== null && _d !== void 0 ? _d : 0)), false), __read(newLines), false), __read(this.lines.slice(property.endingLineNumber)), false); | ||
this.parseLines(); | ||
return true; | ||
}; | ||
/** | ||
* Update a key if it exist, otherwise add it at the end. | ||
* | ||
* @param key - A property key (unescaped). | ||
* @param value - A property value (unescaped). | ||
* @param options - Additional options. | ||
* | ||
* @returns True if the key was updated or inserted, otherwise false. | ||
*/ | ||
PropertiesEditor.prototype.upsert = function (key, value, options) { | ||
return this.keyLineNumbers[key] | ||
? this.update(key, { | ||
newValue: value, | ||
newComment: options === null || options === void 0 ? void 0 : options.comment, | ||
commentDelimiter: options === null || options === void 0 ? void 0 : options.commentDelimiter, | ||
separator: options === null || options === void 0 ? void 0 : options.separator, | ||
escapeUnicode: options === null || options === void 0 ? void 0 : options.escapeUnicode, | ||
}) | ||
: this.insert(key, value, options); | ||
}; | ||
return PropertiesEditor; | ||
}(properties_1.Properties)); | ||
exports.PropertiesEditor = PropertiesEditor; |
@@ -12,2 +12,10 @@ /// <reference types="node" /> | ||
/** | ||
* Get the first end of line (EOL) character from multiline content. | ||
* | ||
* @param content - The content of a `.properties` file. | ||
* | ||
* @returns The multiline content's first end of line (EOL) character. | ||
*/ | ||
export declare const getFirstEolCharacter: (content: string) => string | undefined; | ||
/** | ||
* A class representing the content of a .properties file. | ||
@@ -14,0 +22,0 @@ */ |
@@ -30,3 +30,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KeyCollisions = exports.Properties = exports.DEFAULT_END_OF_LINE_CHARACTER = exports.BOM_CODE_POINT = exports.BOM = void 0; | ||
exports.KeyCollisions = exports.Properties = exports.getFirstEolCharacter = exports.DEFAULT_END_OF_LINE_CHARACTER = exports.BOM_CODE_POINT = exports.BOM = void 0; | ||
var property_1 = require("./property"); | ||
@@ -52,2 +52,3 @@ var property_line_1 = require("./property-line"); | ||
}; | ||
exports.getFirstEolCharacter = getFirstEolCharacter; | ||
/** | ||
@@ -70,3 +71,3 @@ * A class representing the content of a .properties file. | ||
this.hasBom = stringContent.codePointAt(0) === exports.BOM_CODE_POINT; | ||
this.eolCharacter = (_a = getFirstEolCharacter(stringContent)) !== null && _a !== void 0 ? _a : exports.DEFAULT_END_OF_LINE_CHARACTER; | ||
this.eolCharacter = (_a = (0, exports.getFirstEolCharacter)(stringContent)) !== null && _a !== void 0 ? _a : exports.DEFAULT_END_OF_LINE_CHARACTER; | ||
this.lines = (this.hasBom ? stringContent.slice(1) : stringContent).split(/\r?\n/); | ||
@@ -140,3 +141,3 @@ this.parseLines(); | ||
property.keyCollisionLines = this.keyLineNumbers[property.key]; | ||
// Remove collision so that we can overwrite it with the latest object. | ||
// Remove the collision from the collection (we only keep latest value). | ||
this.collection = this.collection.filter(function (existingPropertyObject) { return existingPropertyObject.key !== property.key; }); | ||
@@ -143,0 +144,0 @@ } |
{ | ||
"name": "properties-file", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": ".properties file parser, editor, formatter and Webpack loader.", | ||
@@ -57,3 +57,3 @@ "keywords": [ | ||
"build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc && npm run add-import-type && npm run test", | ||
"ci": "npm run lint-check && rm -Rf ./lib && tsc && npm run add-import-type && npm run test", | ||
"ci": "npm run build", | ||
"lint-check": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --ext .json .", | ||
@@ -69,4 +69,4 @@ "lint-fix": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --ext .json --fix .", | ||
"@types/jest": "29.5.1", | ||
"@typescript-eslint/eslint-plugin": "5.59.0", | ||
"@typescript-eslint/parser": "5.59.0", | ||
"@typescript-eslint/eslint-plugin": "5.59.1", | ||
"@typescript-eslint/parser": "5.59.1", | ||
"dotenv-cli": "7.2.1", | ||
@@ -73,0 +73,0 @@ "eslint": "8.39.0", |
@@ -31,3 +31,3 @@ # properties-file | ||
- The library also includes a Webpack loader to import `.properties` files directly into your application. | ||
- Tiny ([under 2kB compressed](https://bundlephobia.com/package/properties-file)) with 0 dependencies. | ||
- Tiny ([under 3kB compressed](https://bundlephobia.com/package/properties-file)) with 0 dependencies. | ||
- 100% test coverage based on the output from a Java implementation. | ||
@@ -171,3 +171,3 @@ - Active maintenance (many popular `.properties` packages have been inactive for years). | ||
properties.remove('hello') | ||
properties.delete('hello') | ||
console.log(properties.format()) | ||
@@ -189,2 +189,4 @@ | ||
We were not able to show all methods in the example, but the `update` and `upsert` methods can also be useful to modify content. | ||
### Webpack File Loader | ||
@@ -191,0 +193,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69450
1315
280