Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Created March 10, 2023 01:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DarrenSem/02f872823000d38c975de061fa51a7b0 to your computer and use it in GitHub Desktop.
truncate.js (string, max) => "Ellipses after 1st half of ...then the second half of it." [2000 = default for max if omitted]
// truncate.js (string, max) => "Ellipses after 1st half of ...then the second half of it." [2000 = default for max if omitted]
// 101 chars:
// T=(a,b=2e3)=>null==a?"":(a=a+"").length>(b=Math.max(b,5))?a.slice(0,b/2-1)+"..."+a.slice(-b/2+1.5):a;
var truncate = (string, max = 2000) => (
string == null ? ""
: ( string = String(string) ).length > ( max = Math.max(max, 5) )
? (
string.slice(0, max / 2 - 1) + "..." + string.slice(-max / 2 + 1.5)
)
: string
);
// 105 chars using template literal:
// T=(a,b=2e3)=>null==a?"":(a=a+"").length>(b=Math.max(b,5))?`${a.slice(0,b/2-1)}...${a.slice(-b/2+1.5)}`:a;
var truncate_using_template_literal = (string, max = 2000) => (
string == null ? ""
: ( string = String(string) ).length > ( max = Math.max(max, 5) )
? (
`${string.slice(0, max / 2 - 1)}...${string.slice(-max / 2 + 1.5)}`
)
: string
);
// 112 chars using legacy function instead of => arrow (and no default of 2000)
// T=function(a,b){return null==a?"":(a=a+"").length>(b=Math.max(b,5))?a.slice(0,b/2-1)+"..."+a.slice(-b/2+1.5):a};
var truncate_using_legacy_function = function(string, max) {
return (
string == null ? ""
: ( string = String(string) ).length > ( max = Math.max(max, 5) )
? (
string.slice(0, max / 2 - 1) + "..." + string.slice(-max / 2 + 1.5)
)
: string
);
};
var truncate_tests = function(func) {
var results = [
func(
"Ellipses after 1st half of this string that is too long,then the second half of it.", 57)
=== "Ellipses after 1st half of ...then the second half of it.",
func(
"Ellipses after 1st half of this string that is too long,then the second half of it.", 58)
=== "Ellipses after 1st half of t...then the second half of it.",
func(
"Ellipses after 1st half of this string that is too long,then the second half of it.", 59)
=== "Ellipses after 1st half of t...,then the second half of it.",
"" === func(),
"" === func(undefined),
"" === func(null),
"" === func([]),
"" === func(""),
"NaN" === func(NaN),
func(0.123456789, 10 ) === "0.12...789",
func([11, 22, 33, 44] ) === "11,22,33,44",
func([11, 22, 33, 44],11) === "11,22,33,44",
func([11, 22, 33, 44],10) === "11,2...,44",
func([11, 22, 33, 44], 9) === "11,...,44",
func([11, 22, 33, 44], 8) === "11,...44",
func([11, 22, 33, 44], 7) === "11...44",
func([11, 22, 33, 44], 6) === "11...4",
func([11, 22, 33, 44], 5) === "1...4",
func([11, 22, 33, 44], 4) === "1...4",
func([11, 22, 33, 44],-4) === "1...4"
];
console.log(results);
if(Date.now)console.assert(results.every(Boolean), `truncate_tests(${func.name})`);
};
truncate_tests(truncate);
truncate_tests(truncate_using_template_literal);
truncate_tests(truncate_using_legacy_function);
if("xyzDEFAULT OF 2000 = WHAT MOTIVATED ME TO WRITE THIS..." === "DEFAULT OF 2000 = WHAT MOTIVATED ME TO WRITE THIS...") {
prompt( "2000 exactly, prompt default is unchanged", truncate( "X" + Array(2000).join("X") ) );
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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