Skip to content

Instantly share code, notes, and snippets.

@librz
Last active August 4, 2023 21:30
Show Gist options
  • Save librz/bd8764065196923b29441be6968a25bf to your computer and use it in GitHub Desktop.
Save librz/bd8764065196923b29441be6968a25bf to your computer and use it in GitHub Desktop.
read a file, print char & byte count using nodejs
const fs = require('fs');
fs.readFile('content.txt', 'utf-8', (err, data) => {
if (err) {
console.log(err)
return;
}
let charsCount = 0;
let totalBytes = 0
Array.from(data).forEach(char => {
const bytes = Buffer.byteLength(char)
const specialChars = new Map([
[" ", "space"],
["\n", "newline"]
])
console.log(specialChars.has(char) ? specialChars.get(char) : char, char.codePointAt(0), bytes)
totalBytes += bytes
charsCount++
})
console.log({ totalBytes, charsCount })
})
さようなら
你还好吗
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment