Skip to content

Commit

Permalink
fix: fix #17 (improve PropertiesEditor .insert() performance)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbouvrette committed Oct 11, 2023
1 parent 0409944 commit 73531d5
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export type UpsertOptions = {
* A .properties file editor.
*/
export class PropertiesEditor extends Properties {
/** Is line parsing required to re-async the object's properties? */
private needsLineParsing = false

/**
* Create `PropertiesEditor` object.
*
Expand All @@ -81,6 +84,16 @@ export class PropertiesEditor extends Properties {
super(content)
}

/**
* Parse the `.properties` content line by line only when needed.
*/
private parseLinesIfNeeded(): void {
if (this.needsLineParsing) {
this.parseLines()
this.needsLineParsing = false
}
}

/**
* Insert a new property in the existing object (by default it will be at the end).
*
Expand All @@ -100,6 +113,10 @@ export class PropertiesEditor extends Properties {
const referenceKey = options?.referenceKey
const position = options?.position || 'after'

if (referenceKey) {
this.parseLinesIfNeeded()
}

// Allow multiline keys.
const multilineKey = key
.split(/\r?\n/)
Expand All @@ -124,7 +141,7 @@ export class PropertiesEditor extends Properties {
if (referenceKey === undefined) {
// Insert the new property at the end if the reference key was not defined.
this.lines.push(...newLines)
this.parseLines()
this.needsLineParsing = true
return true
} else {
// Find the last occurrence of the reference key.
Expand All @@ -143,7 +160,7 @@ export class PropertiesEditor extends Properties {
...newLines,
...this.lines.slice(insertPosition),
]
this.parseLines()
this.needsLineParsing = true
return true
}
return false
Expand All @@ -162,6 +179,10 @@ export class PropertiesEditor extends Properties {
const referenceKey = options?.referenceKey
const position = options?.position || 'after'

if (referenceKey) {
this.parseLinesIfNeeded()
}

// Allow multiline comments.
const commentPrefix = `${options?.commentDelimiter || DEFAULT_COMMENT_DELIMITER} `
const newLines = `${commentPrefix}${comment}`
Expand All @@ -171,7 +192,7 @@ export class PropertiesEditor extends Properties {
if (referenceKey === undefined) {
// Insert the new comment at the end if the reference key was not defined.
this.lines.push(...newLines)
this.parseLines()
this.needsLineParsing = true
return true
} else {
// Find the last occurrence of the reference key.
Expand All @@ -190,7 +211,7 @@ export class PropertiesEditor extends Properties {
...newLines,
...this.lines.slice(insertPosition),
]
this.parseLines()
this.needsLineParsing = true
return true
}
return false
Expand All @@ -206,6 +227,8 @@ export class PropertiesEditor extends Properties {
* @returns True if the key was deleted, otherwise false.
*/
public delete(key: string, deleteCommentsAndWhiteSpace = true): boolean {
this.parseLinesIfNeeded()

// Find the last occurrence of the key.
const property = [...this.collection].reverse().find((property) => property.key === key)

Expand All @@ -215,7 +238,7 @@ export class PropertiesEditor extends Properties {
: property.startingLineNumber - 1
const endLine = property.endingLineNumber
this.lines = [...this.lines.slice(0, startLine), ...this.lines.slice(endLine)]
this.parseLines()
this.needsLineParsing = true
return true
}
return false
Expand Down Expand Up @@ -270,6 +293,8 @@ export class PropertiesEditor extends Properties {
* @returns True if the key was updated, otherwise false.
*/
public update(key: string, options?: UpdateOptions): boolean {
this.parseLinesIfNeeded()

// Find the last occurrence of the key to update.
const property = [...this.collection].reverse().find((property) => property.key === key)

Expand Down Expand Up @@ -316,7 +341,7 @@ export class PropertiesEditor extends Properties {
...newLines,
...this.lines.slice(property.endingLineNumber),
]
this.parseLines()
this.needsLineParsing = true
return true
}

Expand All @@ -330,6 +355,8 @@ export class PropertiesEditor extends Properties {
* @returns True if the key was updated or inserted, otherwise false.
*/
public upsert(key: string, value: string, options?: UpsertOptions): boolean {
this.parseLinesIfNeeded()

return this.keyLineNumbers[key]
? this.update(key, {
newValue: value,
Expand Down

0 comments on commit 73531d5

Please sign in to comment.