Comparing version 12.0.0 to 12.0.1
export type EllipsisLocation = 'beginning' | 'middle' | 'end'; | ||
/** | ||
* truncates the passed in string if its length exceeds the length specified by | ||
* `finalLength` and adds an `ellipsis` at the point of truncation | ||
* | ||
* ex: | ||
* ```typescript | ||
* const original = 'the quick brown fox jumped over the lazy dogs'; | ||
* ellide(original, 10); // 'the qui...' | ||
* ellide(original, 10, 'beginning'); // '...zy dogs' | ||
* ellide(original, 10, 'middle'); // 'the...dogs' | ||
* ellide(original, 10, 'end', '_'); // 'the quick_' | ||
* ``` | ||
* @param original the original string to be ellided if over the specified `finalLength` | ||
* @param finalLength the maximum length the output string can be (including ellipsis) | ||
* @param ellipsisLocation a value of `beginning`, `middle`, or `end` indicating where | ||
* the ellipsis will be added and what part of the input string will be truncated | ||
* @default end | ||
* @param ellipsis the value to use as the ellipsis @default '...' | ||
* @returns if the `original` string is over the length specified by `finalLength` then | ||
* a truncated string will be returned with the `ellipsis` character(s) at the location | ||
* of the truncation as specified by the `ellipsisLocation` | ||
*/ | ||
export declare const ellide: (original: string, finalLength: number, ellipsisLocation?: EllipsisLocation, ellipsis?: string) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ellide = void 0; | ||
/** | ||
* truncates the passed in string if its length exceeds the length specified by | ||
* `finalLength` and adds an `ellipsis` at the point of truncation | ||
* | ||
* ex: | ||
* ```typescript | ||
* const original = 'the quick brown fox jumped over the lazy dogs'; | ||
* ellide(original, 10); // 'the qui...' | ||
* ellide(original, 10, 'beginning'); // '...zy dogs' | ||
* ellide(original, 10, 'middle'); // 'the...dogs' | ||
* ellide(original, 10, 'end', '_'); // 'the quick_' | ||
* ``` | ||
* @param original the original string to be ellided if over the specified `finalLength` | ||
* @param finalLength the maximum length the output string can be (including ellipsis) | ||
* @param ellipsisLocation a value of `beginning`, `middle`, or `end` indicating where | ||
* the ellipsis will be added and what part of the input string will be truncated | ||
* @default end | ||
* @param ellipsis the value to use as the ellipsis @default '...' | ||
* @returns if the `original` string is over the length specified by `finalLength` then | ||
* a truncated string will be returned with the `ellipsis` character(s) at the location | ||
* of the truncation as specified by the `ellipsisLocation` | ||
*/ | ||
const ellide = function (original, finalLength, ellipsisLocation = 'end', ellipsis = '...') { | ||
if (finalLength >= 5 && original.length > finalLength) { | ||
const length = Math.round(finalLength); | ||
if (length >= 5 && original.length > length) { | ||
switch (ellipsisLocation) { | ||
case 'beginning': | ||
const shortenedStr = original.substring((original.length - finalLength) + ellipsis.length); | ||
const shortenedStr = original.substring((original.length - length) + ellipsis.length); | ||
return `${ellipsis}${shortenedStr}`; | ||
case 'middle': | ||
const beginningStr = original.substring(0, original.length / 2); | ||
const endStr = original.substring(original.length / 2); | ||
let shortenedBeginningStr = (0, exports.ellide)(beginningStr, (finalLength / 2) - (ellipsis.length / 2), 'end', ''); | ||
let shortenedEndStr = (0, exports.ellide)(endStr, (finalLength / 2) - (ellipsis.length / 2), 'beginning', ''); | ||
let beginning = original.substring(0, Math.ceil(length / 2)).split(''); | ||
let end = original.substring(original.length - Math.ceil(length / 2)).split(''); | ||
let removeFromBeginning = true; | ||
while (shortenedBeginningStr.length + ellipsis.length + shortenedEndStr.length > finalLength) { | ||
while (beginning.length + ellipsis.length + end.length > length) { | ||
if (removeFromBeginning) { | ||
shortenedBeginningStr = shortenedBeginningStr.substring(0, shortenedBeginningStr.length - 2); | ||
beginning.pop(); | ||
removeFromBeginning = false; | ||
} | ||
else { | ||
shortenedEndStr = shortenedEndStr.substring(1, shortenedEndStr.length - 1); | ||
end.shift(); | ||
removeFromBeginning = true; | ||
} | ||
} | ||
const finalStr = `${shortenedBeginningStr}${ellipsis}${shortenedEndStr}`; | ||
const finalStr = `${beginning.join('')}${ellipsis}${end.join('')}`; | ||
return finalStr; | ||
case 'end': | ||
default: | ||
const shortStr = original.substring(0, (finalLength - ellipsis.length)); | ||
const shortStr = original.substring(0, (length - ellipsis.length)); | ||
return `${shortStr}${ellipsis}`; | ||
@@ -32,0 +53,0 @@ } |
@@ -40,3 +40,14 @@ "use strict"; | ||
}); | ||
const data = [ | ||
{ input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: undefined, ellipsis: undefined, expected: 'the qui...' }, | ||
{ input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'beginning', ellipsis: undefined, expected: '...zy dogs' }, | ||
{ input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'middle', ellipsis: undefined, expected: 'the...dogs' }, | ||
{ input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'end', ellipsis: '_', expected: 'the quick_' }, | ||
]; | ||
for (const d of data) { | ||
it(`can process as expected: ${JSON.stringify(d)}`, () => { | ||
expect((0, src_1.ellide)(d.input, d.length, d.location, d.ellipsis)).toEqual(d.expected); | ||
}); | ||
} | ||
}); | ||
//# sourceMappingURL=ellide-spec.js.map |
{ | ||
"name": "aft-core", | ||
"version": "12.0.0", | ||
"version": "12.0.1", | ||
"description": "Automation Framework for Testing (AFT) package supporting JavaScript unit, integration and functional testing", | ||
@@ -52,3 +52,3 @@ "repository": { | ||
}, | ||
"gitHead": "7e6c8c80a3c0d5c79588fd11ca7c657ab11f1705" | ||
"gitHead": "9b31f395e0cc61a15b2ef4d44b3d63d548cbbf72" | ||
} |
export type EllipsisLocation = 'beginning' | 'middle' | 'end'; | ||
/** | ||
* truncates the passed in string if its length exceeds the length specified by | ||
* `finalLength` and adds an `ellipsis` at the point of truncation | ||
* | ||
* ex: | ||
* ```typescript | ||
* const original = 'the quick brown fox jumped over the lazy dogs'; | ||
* ellide(original, 10); // 'the qui...' | ||
* ellide(original, 10, 'beginning'); // '...zy dogs' | ||
* ellide(original, 10, 'middle'); // 'the...dogs' | ||
* ellide(original, 10, 'end', '_'); // 'the quick_' | ||
* ``` | ||
* @param original the original string to be ellided if over the specified `finalLength` | ||
* @param finalLength the maximum length the output string can be (including ellipsis) | ||
* @param ellipsisLocation a value of `beginning`, `middle`, or `end` indicating where | ||
* the ellipsis will be added and what part of the input string will be truncated | ||
* @default end | ||
* @param ellipsis the value to use as the ellipsis @default '...' | ||
* @returns if the `original` string is over the length specified by `finalLength` then | ||
* a truncated string will be returned with the `ellipsis` character(s) at the location | ||
* of the truncation as specified by the `ellipsisLocation` | ||
*/ | ||
export const ellide = function(original: string, finalLength: number, ellipsisLocation: EllipsisLocation = 'end', ellipsis: string = '...'): string { | ||
if (finalLength >= 5 && original.length > finalLength) { | ||
const length = Math.round(finalLength); | ||
if (length >= 5 && original.length > length) { | ||
switch (ellipsisLocation) { | ||
case 'beginning': | ||
const shortenedStr: string = original.substring((original.length - finalLength) + ellipsis.length); | ||
const shortenedStr: string = original.substring((original.length - length) + ellipsis.length); | ||
return `${ellipsis}${shortenedStr}`; | ||
case 'middle': | ||
const beginningStr: string = original.substring(0, original.length / 2); | ||
const endStr: string = original.substring(original.length / 2); | ||
let shortenedBeginningStr: string = ellide(beginningStr, (finalLength / 2) - (ellipsis.length / 2), 'end', ''); | ||
let shortenedEndStr: string = ellide(endStr, (finalLength / 2) - (ellipsis.length / 2), 'beginning', ''); | ||
let beginning = original.substring(0, Math.ceil(length / 2)).split(''); | ||
let end = original.substring(original.length - Math.ceil(length / 2)).split(''); | ||
let removeFromBeginning = true; | ||
while (shortenedBeginningStr.length + ellipsis.length + shortenedEndStr.length > finalLength) { | ||
while (beginning.length + ellipsis.length + end.length > length) { | ||
if (removeFromBeginning) { | ||
shortenedBeginningStr = shortenedBeginningStr.substring(0, shortenedBeginningStr.length - 2); | ||
beginning.pop(); | ||
removeFromBeginning = false; | ||
} else { | ||
shortenedEndStr = shortenedEndStr.substring(1, shortenedEndStr.length - 1); | ||
end.shift(); | ||
removeFromBeginning = true; | ||
} | ||
} | ||
const finalStr = `${shortenedBeginningStr}${ellipsis}${shortenedEndStr}`; | ||
const finalStr = `${beginning.join('')}${ellipsis}${end.join('')}`; | ||
return finalStr; | ||
case 'end': | ||
default: | ||
const shortStr = original.substring(0, (finalLength - ellipsis.length)); | ||
const shortStr = original.substring(0, (length - ellipsis.length)); | ||
return `${shortStr}${ellipsis}`; | ||
@@ -30,0 +51,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { ellide, EllipsisLocation, rand } from "../../src"; | ||
import { EllipsisLocation, ellide, rand } from "../../src"; | ||
@@ -53,2 +53,14 @@ describe('ellide', () => { | ||
}); | ||
}) | ||
const data = [ | ||
{input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: undefined, ellipsis: undefined, expected: 'the qui...'}, | ||
{input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'beginning', ellipsis: undefined, expected: '...zy dogs'}, | ||
{input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'middle', ellipsis: undefined, expected: 'the...dogs'}, | ||
{input: 'the quick brown fox jumped over the lazy dogs', length: 10, location: 'end', ellipsis: '_', expected: 'the quick_'}, | ||
]; | ||
for (const d of data) { | ||
it(`can process as expected: ${JSON.stringify(d)}`, () => { | ||
expect(ellide(d.input, d.length, d.location as EllipsisLocation, d.ellipsis)).toEqual(d.expected); | ||
}); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
991707
15524