Skip to content

Instantly share code, notes, and snippets.

@okovpashko
Last active May 26, 2022 13:09
Show Gist options
  • Save okovpashko/a1afdf34061f1a5685436c9d9ad60d5b to your computer and use it in GitHub Desktop.
Save okovpashko/a1afdf34061f1a5685436c9d9ad60d5b to your computer and use it in GitHub Desktop.
Detect UTF-8 BOM symbol with Node.js
const {open, read} = require('fs/promises');
const {resolve} = require('path');
async function main([filePath]) {
const resolvedPath = resolve(filePath);
let file;
try {
file = await open(resolvedPath);
} catch(e) {
console.error('Couldn\'t opend provided file');
return;
}
const buffer = Buffer.alloc(3);
const bytes = await file.read(buffer, 0, 3);
const hasBOM = buffer.toString('utf8')[0] === '\uFEFF';
if (hasBOM) {
console.log(`The file at "${resolvedPath}" contains BOM symbol`);
} else {
console.log(`The file at "${resolvedPath}" does not contain BOM symbol`);
}
}
main(process.argv.slice(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment