🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

cli-truncate

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-truncate - npm Package Compare versions

Comparing version

to
1.0.0

24

index.js
'use strict';
var sliceAnsi = require('slice-ansi');
var stringWidth = require('string-width');
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');
module.exports = function (input, columns, options) {
options = options || {};
module.exports = (input, columns, opts) => {
opts = Object.assign({
position: 'end'
}, opts);
var position = options.position || 'end';
var ellipsis = '…';
const position = opts.position;
const ellipsis = '…';
if (typeof input !== 'string') {
throw new TypeError('Expected `input` to be a string, got ' + typeof input);
throw new TypeError(`Expected \`input\` to be a string, got ${typeof input}`);
}
if (typeof columns !== 'number') {
throw new TypeError('Expected `columns` to be a number, got ' + typeof columns);
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}

@@ -27,3 +29,3 @@

var length = stringWidth(input);
const length = stringWidth(input);

@@ -37,3 +39,3 @@ if (length <= columns) {

} else if (position === 'middle') {
var half = Math.floor(columns / 2);
const half = Math.floor(columns / 2);
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);

@@ -44,3 +46,3 @@ } else if (position === 'end') {

throw new Error('Expected `options.position` to be either `start`, `middle` or `end`, got ' + position);
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
};
{
"name": "cli-truncate",
"version": "0.2.1",
"version": "1.0.0",
"description": "Truncate a string to a specific width in the terminal",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},

@@ -37,3 +37,3 @@ "scripts": {

"slice-ansi": "0.0.4",
"string-width": "^1.0.1"
"string-width": "^2.0.0"
},

@@ -40,0 +40,0 @@ "devDependencies": {

@@ -23,3 +23,3 @@ # cli-truncate [![Build Status](https://travis-ci.org/sindresorhus/cli-truncate.svg?branch=master)](https://travis-ci.org/sindresorhus/cli-truncate)

// truncate at different positions
// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});

@@ -34,3 +34,3 @@ //=> '…orn'

// truncate the paragraph to the terminal width
// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';

@@ -60,2 +60,4 @@ cliTruncate(paragraph, process.stdout.columns));

Type: `Object`
##### position

@@ -62,0 +64,0 @@