Last active
August 4, 2023 21:30
-
-
Save librz/bd8764065196923b29441be6968a25bf to your computer and use it in GitHub Desktop.
read a file, print char & byte count using nodejs
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 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 }) | |
}) |
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
さようなら | |
你还好吗 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment