Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Created November 10, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangpin34/cd23bc9d7c145983cd12bf300a9c4e64 to your computer and use it in GitHub Desktop.
Save wangpin34/cd23bc9d7c145983cd12bf300a9c4e64 to your computer and use it in GitHub Desktop.
Nodejs file util
'use strict'
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
/*
* Bom headers are 3 Bytes binary
*/
const removeBom = filePath => {
return fs.readFileAsync(filePath).then(buff => {
if(buff[0].toString(16).toLowerCase() === 'ef' && buff[1].toString(16).toLowerCase() === 'bb' && buff[2].toString(16).toLowerCase() === 'bf'){
console.log('%s has bom headers', filePath)
buff = buff.slice(3)
return fs.writeFileAsync(filePath, buff.toString(), 'utf8')
}
return
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment