Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed May 23, 2021
1 parent 5686695 commit 56de66d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/metaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ export default class MetaController {
const fileContent = await this.app.vault.read(file);

const newFileContent = fileContent.split("\n").map(line => {
const propertyRegex = new RegExp(`^\s*${property.key}:`);
const tagRegex = new RegExp(`^\s*#${property.key}`);

if (line.match(propertyRegex) || line.match(tagRegex)) {
if (this.lineMatch(property, line)) {
return this.updatePropertyLine(property, newValue);
}

Expand All @@ -315,6 +312,13 @@ export default class MetaController {
await this.app.vault.modify(file, newFileContent);
}

private lineMatch(property: Partial<Property>, line: string) {
const propertyRegex = new RegExp(`^\s*${property.key}:`);
const tagRegex = new RegExp(`^\s*#${property.key}`);

return line.match(propertyRegex) || line.match(tagRegex);
}

private updatePropertyLine(property: Partial<Property>, newValue: string) {
let newLine: string;

Expand Down Expand Up @@ -350,12 +354,9 @@ export default class MetaController {
let fileContent = (await this.app.vault.read(file)).split("\n");

for (const prop of properties) {
const regexp = new RegExp(`^\s*${prop.key}`);
const tagRegex = new RegExp(`^\s*#${prop.key}`);

fileContent = fileContent.map(line => {

if (line.match(regexp) || line.match(tagRegex)) {
if (this.lineMatch(prop, line)) {
return this.updatePropertyLine(prop, prop.content)
}

Expand Down

0 comments on commit 56de66d

Please sign in to comment.