@hyperfrontend/project-scope/core/encodingencoding
File encoding detection and conversion utilities for safely handling text files with mixed BOM markers and binary content.
Detects UTF-8, UTF-16 LE/BE byte-order marks, identifies binary signatures (PNG, ZIP, PDF, etc.) for isTextFile checks, and converts buffers to UTF-8 strings with optional BOM stripping or addition.
API Reference
ƒ Functions
Add UTF-8 BOM to string if not present.
Parameters
| Name | Type | Description |
|---|---|---|
§content | string | String to add BOM to |
Returns
stringString with BOM
Example
Adding UTF-8 BOM to content
const content = 'Hello World'
const withBom = addBom(content)
// => '\ufeffHello World'Convert buffer to string with encoding detection.
Parameters
Returns
stringConverted string
Example
Converting file buffer to string with auto-detection
const fileBuffer = readFileSync('./config.json')
const content = bufferToString(fileBuffer)
// => '{"key": "value"}'Detect file encoding from BOM or content analysis.
Parameters
| Name | Type | Description |
|---|---|---|
§buffer | Buffer | Buffer to analyze |
Returns
BufferEncodingDetected encoding, defaults to 'utf-8'
Example
Detecting encoding from buffer
const buffer = readFileSync('./data.txt')
const encoding = detectEncoding(buffer)
const content = buffer.toString(encoding)Detect if content is likely text or binary with encoding information.
Parameters
| Name | Type | Description |
|---|---|---|
§buffer | Buffer | Buffer to analyze |
Returns
EncodingInfoEncoding information
Example
Detecting encoding and BOM info
const buffer = readFileSync('./document.txt')
const info = detectEncodingInfo(buffer)
if (info.type === 'text') {
console.log(`Encoding: ${info.encoding}, BOM: ${info.hasBom}`)
}Check if buffer starts with a BOM.
Parameters
| Name | Type | Description |
|---|---|---|
§buffer | Buffer | Buffer to check |
Returns
booleanTrue if buffer has a BOM
Example
Checking if buffer has BOM
const buffer = readFileSync('./file.txt')
if (hasBom(buffer)) {
// Strip BOM before processing
}Check if buffer represents text content.
Parameters
| Name | Type | Description |
|---|---|---|
§buffer | Buffer | Buffer to check |
Returns
booleanTrue if the buffer appears to be text
Example
Checking if content is text
const buffer = readFileSync('./unknown-file')
if (isTextFile(buffer)) {
const content = buffer.toString('utf-8')
}Strip BOM from start of string if present.
Parameters
| Name | Type | Description |
|---|---|---|
§content | string | String that may have BOM |
Returns
stringString without BOM
Example
Stripping BOM from a string
const withBom = '\ufeffHello World'
const clean = stripBom(withBom)
// => 'Hello World'Convert content to UTF-8 string.
Parameters
Returns
stringUTF-8 string
Example
Converting a buffer to UTF-8 string
const buffer = Buffer.from('Hello', 'utf-8')
const text = toUtf8(buffer)
// => 'Hello'◆ Types
Encoding detection result.
type EncodingInfo = { encoding: BufferEncoding; hasBom: boolean; type: "text" } | { format: string; type: "binary" }