@zokugun/text-line-utils
v0.1.0
Published
Utilities for working with text lines: EOL, indent, ...
Downloads
152
Readme
@zokugun/text-line-utils
Small helpers for analyzing text lines (detect EOL or indentation) and more (iterate lines). Works in both ESM and CommonJS environments.
Install
With npm:
npm add @zokugun/text-line-utilsQuick Start
import { detectEOL, detectIndent, toLines } from '@zokugun/text-line-utils'
const text = ' a\n b\n'
console.log(detectEOL(text))
console.log(detectIndent(text))
console.log(toLines(text))API reference
detectEOL
Analyze and detect the predominant End-Of-Line character used in the input text
type EOLStyle = 'cr' | 'crlf' | 'lf' | 'mixed' | 'none';
type EOLCounts = {
style: EOLStyle,
cr: number;
crlf: number;
lf: number;
};
function detectEOL(text: string, options?: { mode?: 'majority' | 'first' | 'last'; sampleLines?: number; }): EOLStyle;
function detectEOL(text: string, options: { mode: 'counts'; sampleLines?: number; }): EOLCounts;detectIndent
Analyze and detect the predominant indentation used in the input text
type Indent = {
style: 'mixed' | 'none' | 'space' | 'tab';
size: number;
};
type IndentCounts = {
histogram: Record<number, number>;
mixeds: number;
spaces: number;
tabs: number;
totals: number;
};
function detectIndent(text: string, options: { mode: 'counts'; sampleLines?: number; }): IndentCounts;
function detectIndent(text: string, options?: { mode?: 'first' | 'gcd' | 'last'; sampleLines?: number; }): Indent;more utilities
hasFinalEOL(text: string): boolean:truewhentextends with any EOL sequence.toEOL(eol?: EOLStyle): '\n' | '\r\n' | '\r': convert theEOLStyleto a EOL character.toLines(text: string): string[]: splittextinto an array of lines (EOL removed).toLineIterator(text: string): IterableIterator<string>: iterate over lines lazily (useful for large inputs).toSpaces(indent?: Indent): number | '\t': convert theIndentas thespacesparameter of theJSON.stringifyfunction.
Donations
Support this project by becoming a financial contributor.
License
Copyright © 2026-present Baptiste Augrain
Licensed under the MIT license.
