uppy/packages/@uppy/utils/src/truncateString.js
Kevin van Zonneveld 764c2ccada
Update Linter (#2796)
* relocate .vscode

* Switch to transloadit linter

* Update .eslintrc.json

* autofix code

* unlink and install eslint-config-transloadit@1.1.1

* Change 0 to "off"

* Don't change 'use strict'

* Do not vertically align

* disable key-spacing

* add import/no-extraneous-dependencies per package

* add more react/a11y warnings

* Revert "autofix code"

This reverts commit 14c8a8cde8.

* add import/no-extraneous-dependencies per example and main package

* autofix code (2)

* Allow devDependencies in ./bin

* Change import/no-extraneous-dependencies to warn again

* upgrade linter

* Set import/no-extraneous-dependencies to warn
2021-03-15 16:25:17 +00:00

25 lines
1,012 B
JavaScript

/**
* Truncates a string to the given number of chars (maxLength) by inserting '...' in the middle of that string.
* Partially taken from https://stackoverflow.com/a/5723274/3192470.
*
* @param {string} string - string to be truncated
* @param {number} maxLength - maximum size of the resulting string
* @returns {string}
*/
module.exports = function truncateString (string, maxLength) {
const separator = '...'
// Return original string if it's already shorter than maxLength
if (string.length <= maxLength) {
return string
// Return truncated substring without '...' if string can't be meaningfully truncated
} if (maxLength <= separator.length) {
return string.substr(0, maxLength)
// Return truncated string divided in half by '...'
}
const charsToShow = maxLength - separator.length
const frontChars = Math.ceil(charsToShow / 2)
const backChars = Math.floor(charsToShow / 2)
return string.substr(0, frontChars) + separator + string.substr(string.length - backChars)
}