cli-truncate
Advanced tools
Comparing version 0.2.1 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 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4664
35
0
78
+ Addedansi-regex@3.0.1(transitive)
+ Addedis-fullwidth-code-point@2.0.0(transitive)
+ Addedstring-width@2.1.1(transitive)
+ Addedstrip-ansi@4.0.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstrip-ansi@3.0.1(transitive)
Updatedstring-width@^2.0.0