Skip to content

Instantly share code, notes, and snippets.

@SanSan-
Created March 10, 2024 14:46
Show Gist options
  • Save SanSan-/064324900f4882ef01a107fbddac8acb to your computer and use it in GitHub Desktop.
Save SanSan-/064324900f4882ef01a107fbddac8acb to your computer and use it in GitHub Desktop.
collect all tags without repeat from current dir wd14 files in output file
var fs = require('fs');
const path = require('path');
const UTF8 = 'utf8';
const EXTENSION = '.wd14';
const OUTPUT_FILE = 'words.txt';
const EMPTY_STRING = '';
const NEW_LINE_SEPARATOR = '\n';
const COMA_SEPARATOR = ',';
const COMA_SPACE_SEPARATOR = ', ';
async function readFile(filePath) {
try {
return await fs.promises.readFile(filePath, UTF8);
} catch (err) {
return false;
}
}
async function writeFile(filename, writedata) {
try {
await fs.writeFileSync(filename, writedata, UTF8);
return true;
} catch (err) {
return false;
}
}
const words = new Set();
fs.readdir(process.cwd(), (err, files) => {
files.filter((file) => path.extname(file) === EXTENSION).forEach(file => {
readFile(file).then(data => {
if (data) {
data.split(NEW_LINE_SEPARATOR).forEach(line => {
line.split(COMA_SEPARATOR).forEach(word => {
if (word.trim() !== EMPTY_STRING) {
words.add(word.trim());
}
})
});
}
writeFile(OUTPUT_FILE, Array.from(words).join(COMA_SPACE_SEPARATOR)).then(result => {
console.log(OUTPUT_FILE, result);
});
}).catch(err => {
console.error(err);
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment