Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ansi-truncate

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ansi-truncate - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

dist/types.d.ts

3

dist/constants.d.ts
declare const ANSI_RE: RegExp;
declare const ELLIPSIS = "\u2026";
export { ANSI_RE, ELLIPSIS };
declare const RESET = "\u001B[0m";
export { ANSI_RE, ELLIPSIS, RESET };
/* MAIN */
const ANSI_RE = /([\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><])/g;
const ANSI_RE = /[\x1B\x9B]/;
const ELLIPSIS = '…';
const RESET = '\x1B[0m';
/* EXPORT */
export { ANSI_RE, ELLIPSIS };
export { ANSI_RE, ELLIPSIS, RESET };

@@ -1,4 +0,6 @@

declare const truncate: (str: string, width: number, options?: {
import type { Options } from './types';
declare const truncate: (input: string, width: number, options?: {
ellipsis?: string;
}) => string;
export default truncate;
export type { Options };
/* IMPORT */
import stringWidth from 'fast-string-width';
import { ANSI_RE, ELLIPSIS } from './constants.js';
import strip from './strip.js';
import fastStringTruncatedWidth from 'fast-string-truncated-width';
import { ANSI_RE, ELLIPSIS, RESET } from './constants.js';
/* MAIN */
const truncate = (str, width, options) => {
if (width <= 0)
return '';
const strNoAnsi = strip(str);
if (stringWidth(strNoAnsi) <= width)
return str;
//TODO: Maybe detect where "RESET" is necessary more precisely
const truncate = (input, width, options) => {
const limit = width;
const ellipsis = options?.ellipsis ?? ELLIPSIS;
width -= stringWidth(strip(ellipsis));
const parts = str.split(ANSI_RE);
let truncated = '';
let truncatedWidth = 0;
for (let i = 0, l = parts.length; i < l; i++) {
const part = parts[i];
if (!(i % 2)) { // Non-ANSI part
const partWidth = stringWidth(part);
if ((truncatedWidth + partWidth) >= width) { // Exceeding part, truncating and exiting
const escapesRight = parts.slice(i).filter((_, i) => (i % 2)).join('');
return `${truncated}${part.slice(0, width - partWidth)}${ellipsis}${escapesRight}`;
}
truncatedWidth += partWidth;
}
truncated += part;
}
return str;
const { index, ellipsed, truncated } = fastStringTruncatedWidth(input, { limit, ellipsis });
if (!truncated)
return input;
const slice = input.slice(0, index);
const resettable = ANSI_RE.test(slice);
return `${slice}${ellipsed ? ellipsis : ''}${resettable ? RESET : ''}`;
};
/* EXPORT */
export default truncate;

@@ -5,3 +5,3 @@ {

"description": "A tiny function for truncating a string that may contain ANSI escape sequences.",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",

@@ -24,10 +24,9 @@ "main": "dist/index.js",

"dependencies": {
"fast-string-width": "^1.0.2"
"fast-string-truncated-width": "^1.0.4"
},
"devDependencies": {
"fava": "^0.2.1",
"tiny-colors": "^2.0.2",
"tsex": "^3.0.1",
"typescript": "^5.1.6"
"fava": "^0.3.2",
"tsex": "^3.0.2",
"typescript": "^5.3.3"
}
}
/* MAIN */
const ANSI_RE = /([\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><])/g;
const ANSI_RE = /[\x1B\x9B]/;
const ELLIPSIS = '…';
const RESET = '\x1B[0m';
/* EXPORT */
export {ANSI_RE, ELLIPSIS};
export {ANSI_RE, ELLIPSIS, RESET};
/* IMPORT */
import stringWidth from 'fast-string-width';
import {ANSI_RE, ELLIPSIS} from './constants';
import strip from './strip';
import fastStringTruncatedWidth from 'fast-string-truncated-width';
import {ANSI_RE, ELLIPSIS, RESET} from './constants';
import type {Options} from './types';
/* MAIN */
const truncate = ( str: string, width: number, options?: { ellipsis?: string } ): string => {
//TODO: Maybe detect where "RESET" is necessary more precisely
if ( width <= 0 ) return '';
const truncate = ( input: string, width: number, options?: { ellipsis?: string } ): string => {
const strNoAnsi = strip ( str );
if ( stringWidth ( strNoAnsi ) <= width ) return str;
const limit = width;
const ellipsis = options?.ellipsis ?? ELLIPSIS;
const {index, ellipsed, truncated} = fastStringTruncatedWidth ( input, { limit, ellipsis } );
width -= stringWidth ( strip ( ellipsis ) );
if ( !truncated ) return input;
const parts = str.split ( ANSI_RE );
const slice = input.slice ( 0, index );
const resettable = ANSI_RE.test ( slice );
let truncated = '';
let truncatedWidth = 0;
return `${slice}${ellipsed ? ellipsis : ''}${resettable ? RESET : ''}`;
for ( let i = 0, l = parts.length; i < l; i++ ) {
const part = parts[i];
if ( !( i % 2 ) ) { // Non-ANSI part
const partWidth = stringWidth ( part );
if ( ( truncatedWidth + partWidth ) >= width ) { // Exceeding part, truncating and exiting
const escapesRight = parts.slice ( i ).filter ( ( _, i ) => ( i % 2 ) ).join ( '' );
return `${truncated}${part.slice ( 0, width - partWidth )}${ellipsis}${escapesRight}`;
}
truncatedWidth += partWidth;
}
truncated += part;
}
return str;
};

@@ -58,1 +30,2 @@

export default truncate;
export type {Options};

@@ -5,5 +5,12 @@

import {describe} from 'fava';
import colors from 'tiny-colors';
import truncate from '../dist/index.js';
/* HELPERS */
const RED_START = '\u001b[31m';
const RED_END = '\u001b[39m';
const BLUE_START = '\u001b[34m';
const BLUE_END = '\u001b[39m';
const RESET = '\u001b[0m';
/* MAIN */

@@ -15,3 +22,3 @@

t.is ( truncate ( colors.red ( 'foo' ), 100 ), colors.red ( 'foo' ) );
t.is ( truncate ( `${RED_START}foo${RED_END}`, 100 ), `${RED_START}foo${RED_END}` );

@@ -22,3 +29,3 @@ });

t.is ( truncate ( colors.red ( 'foo' ), 2 ), colors.red ( 'f…' ) );
t.is ( truncate ( `${RED_START}foo${RED_END}`, 2 ), `${RED_START}f…${RESET}` );

@@ -49,7 +56,6 @@ });

t.is ( truncate ( 'foo', 2, { ellipsis: '' } ), 'fo' );
t.is ( truncate ( colors.red ( 'foo' ), 2, { ellipsis: colors.blue ( '+' ) } ), colors.red ( `f${colors.blue ( '+' )}` ) );
t.is ( truncate ( `${RED_START}foo${RED_END}`, 2, { ellipsis: `${BLUE_START}+${BLUE_END}` } ), `${RED_START}f${BLUE_START}+${BLUE_END}${RESET}` );
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc