Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
fix(bug): #59 - issue with periods in filenames (#68)
Browse files Browse the repository at this point in the history
Closes #59

Resolves issue with disallowed nedb characters. Noticed that this was
broken on update as well, so created a method in `converter` to convert
a file into a nedb-compatible key.

This ballooned a bit more than I expected. Hopefully everything fits
convention - please let me know if there's something I need to change.
  • Loading branch information
aspencer authored and lauthieb committed Mar 14, 2019
1 parent 07ec7ba commit a9091f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,18 @@ export default {
...mapActions(['addNote']),
createNote() {
if (!this.containsDupFiles()) {
let separator = '-';
let prefix = 'note';
const noteType = this.getNoteType();
if (this.gistsSelected) {
separator = '.';
prefix = 'gist';
}
let name;
this.files.forEach((file, i) => {
name = file.name || `${prefix}file${i + 1}`;
this.note.files[
`${name}${separator}${converter.languageToExtension(file.language)}`
] = file;
const filename = file.name || `${noteType}file${i + 1}`;
const key = converter.filenameToKey(filename, file.language, noteType);
this.note.files[key] = file;
});
this.note.createdAt = new Date();
this.note.updatedAt = new Date();
if (!this.note.name || this.note.name.trim() === '') {
this.note.name = `${prefix}:${generateNoteName()}`;
this.note.name = `${noteType}:${generateNoteName()}`;
}
this.addNote(this.note).then(() => {
Expand All @@ -95,9 +87,7 @@ export default {
let dupFiles = false;
this.files.forEach(file => {
const key = `${file.name}.${converter.languageToExtension(
file.language
)}`;
const key = converter.filenameToKey(file.name, file.language, this.getNoteType());
if (map.has(key)) {
dupFiles = true;
Expand All @@ -108,6 +98,9 @@ export default {
return dupFiles;
},
getNoteType() {
return (this.gistsSelected) ? 'gist' : 'note';
},
},
computed: {
...mapGetters(['gistsSelected']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export default {
});
this.gistFiles.forEach((file, index) => {
const key = `${
this.gistFiles[index].name
}.${converter.languageToExtension(this.gistFiles[index].language)}`;
const key = converter.filenameToKey(file.name, file.language, this.getNoteType());
if (file.deleted) {
this.noteUpdated.files[key] = null;
Expand All @@ -82,7 +80,8 @@ export default {
});
} else {
this.files.forEach(file => {
this.noteUpdated.files[file.name] = file;
const key = converter.filenameToKey(file.name, file.language, this.getNoteType());
this.noteUpdated.files[key] = file;
});
this.noteUpdated.updatedAt = new Date();
}
Expand Down Expand Up @@ -119,10 +118,7 @@ export default {
let dupFiles = false;
this.files.forEach(file => {
const key = `${file.name}.${converter.languageToExtension(
file.language
)}`;
const key = converter.filenameToKey(file.name, file.language, this.getNoteType());
if (map.has(key)) {
dupFiles = true;
}
Expand All @@ -132,6 +128,9 @@ export default {
return dupFiles;
},
getNoteType() {
return (this.gistsSelected) ? 'gist' : 'note';
},
},
computed: {
...mapGetters(['gistsSelected']),
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import languages from './assets/data/languages';

const converter = {
filenameToKey(filename, language, type = 'note') {
// Build separator by type
const separator = (type === 'gist') ? '.' : '-';

// Sanitize name for disallowed nedb characters
// list of disallowed characters taken from https://github.com/louischatriot/nedb/issues/477
let name = filename;
if (type === 'note' && typeof name === 'string') {
name = name.replace(/[.$]/, '_');
}

return `${name}${separator}${this.languageToExtension(language)}`;
},
languageToExtension(language) {
if (languages.filter(l => l.name === language).length > 0) {
return languages.filter(l => l.name === language)[0].extension;
Expand Down

0 comments on commit a9091f5

Please sign in to comment.