Upgrade Ava to v4
This commit is contained in:
parent
9a40cc5274
commit
ce89f1b611
1153 changed files with 27264 additions and 95308 deletions
60
node_modules/cli-truncate/index.js
generated
vendored
60
node_modules/cli-truncate/index.js
generated
vendored
|
|
@ -1,35 +1,34 @@
|
|||
'use strict';
|
||||
const sliceAnsi = require('slice-ansi');
|
||||
const stringWidth = require('string-width');
|
||||
import sliceAnsi from 'slice-ansi';
|
||||
import stringWidth from 'string-width';
|
||||
|
||||
function getIndexOfNearestSpace(string, index, shouldSearchRight) {
|
||||
if (string.charAt(index) === ' ') {
|
||||
return index;
|
||||
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
||||
if (string.charAt(wantedIndex) === ' ') {
|
||||
return wantedIndex;
|
||||
}
|
||||
|
||||
for (let i = 1; i <= 3; i++) {
|
||||
for (let index = 1; index <= 3; index++) {
|
||||
if (shouldSearchRight) {
|
||||
if (string.charAt(index + i) === ' ') {
|
||||
return index + i;
|
||||
if (string.charAt(wantedIndex + index) === ' ') {
|
||||
return wantedIndex + index;
|
||||
}
|
||||
} else if (string.charAt(index - i) === ' ') {
|
||||
return index - i;
|
||||
} else if (string.charAt(wantedIndex - index) === ' ') {
|
||||
return wantedIndex - index;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
return wantedIndex;
|
||||
}
|
||||
|
||||
module.exports = (text, columns, options) => {
|
||||
export default function cliTruncate(text, columns, options) {
|
||||
options = {
|
||||
position: 'end',
|
||||
preferTruncationOnSpace: false,
|
||||
...options
|
||||
truncationCharacter: '…',
|
||||
...options,
|
||||
};
|
||||
|
||||
const {position, space, preferTruncationOnSpace} = options;
|
||||
let ellipsis = '…';
|
||||
let ellipsisWidth = 1;
|
||||
let {truncationCharacter} = options;
|
||||
|
||||
if (typeof text !== 'string') {
|
||||
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
||||
|
|
@ -44,7 +43,7 @@ module.exports = (text, columns, options) => {
|
|||
}
|
||||
|
||||
if (columns === 1) {
|
||||
return ellipsis;
|
||||
return truncationCharacter;
|
||||
}
|
||||
|
||||
const length = stringWidth(text);
|
||||
|
|
@ -56,21 +55,19 @@ module.exports = (text, columns, options) => {
|
|||
if (position === 'start') {
|
||||
if (preferTruncationOnSpace) {
|
||||
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
|
||||
return ellipsis + sliceAnsi(text, nearestSpace, length).trim();
|
||||
return truncationCharacter + sliceAnsi(text, nearestSpace, length).trim();
|
||||
}
|
||||
|
||||
if (space === true) {
|
||||
ellipsis += ' ';
|
||||
ellipsisWidth = 2;
|
||||
truncationCharacter += ' ';
|
||||
}
|
||||
|
||||
return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length);
|
||||
return truncationCharacter + sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
|
||||
}
|
||||
|
||||
if (position === 'middle') {
|
||||
if (space === true) {
|
||||
ellipsis = ' ' + ellipsis + ' ';
|
||||
ellipsisWidth = 3;
|
||||
truncationCharacter = ` ${truncationCharacter} `;
|
||||
}
|
||||
|
||||
const half = Math.floor(columns / 2);
|
||||
|
|
@ -78,29 +75,28 @@ module.exports = (text, columns, options) => {
|
|||
if (preferTruncationOnSpace) {
|
||||
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
||||
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
|
||||
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + ellipsis + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
||||
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
||||
}
|
||||
|
||||
return (
|
||||
sliceAnsi(text, 0, half) +
|
||||
ellipsis +
|
||||
sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
|
||||
sliceAnsi(text, 0, half)
|
||||
+ truncationCharacter
|
||||
+ sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
|
||||
);
|
||||
}
|
||||
|
||||
if (position === 'end') {
|
||||
if (preferTruncationOnSpace) {
|
||||
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
|
||||
return sliceAnsi(text, 0, nearestSpace) + ellipsis;
|
||||
return sliceAnsi(text, 0, nearestSpace) + truncationCharacter;
|
||||
}
|
||||
|
||||
if (space === true) {
|
||||
ellipsis = ' ' + ellipsis;
|
||||
ellipsisWidth = 2;
|
||||
truncationCharacter = ` ${truncationCharacter}`;
|
||||
}
|
||||
|
||||
return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
|
||||
return sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)) + truncationCharacter;
|
||||
}
|
||||
|
||||
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue