Last active
May 26, 2022 13:09
-
-
Save okovpashko/a1afdf34061f1a5685436c9d9ad60d5b to your computer and use it in GitHub Desktop.
Detect UTF-8 BOM symbol with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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