Lodash Truncate Omission Parameter Calculation

This article explains how the Lodash utility library processes the omission variable inside its _.truncate method to compute text truncation boundaries. It covers the internal normalization of strings, how unicode-aware character budgets are established, how the final slice offset is derived by subtracting the omission length from the target length, and how separator matching interacts with the calculated omission window.

Configuration Defaults and Input Normalization

In Lodash, _.truncate accepts an input string and an options object. The two core parameters governing length calculations are:

Before any math takes place, Lodash normalizes these values. If string is nullish, it is cast to an empty string. The omission argument is coerced to a string, ensuring properties like .length reflect standard string representations.

Unicode-Aware Length Measurement

JavaScript represents strings using UTF-16 code units, meaning multi-byte characters like emojis or certain accented symbols can yield unexpected lengths. Lodash accounts for this by checking if strings contain complex unicode symbols using an internal hasUnicode check.

When unicode characters are present, Lodash decomposes the string using an internal stringToArray helper:

const strSymbols = hasUnicode(string) ? stringToArray(string) : undefined;
const strLength = strSymbols ? strSymbols.length : string.length;

If strLength <= length, the input string is already within the budget, and Lodash returns the string unmodified without invoking the omission logic.

Calculating the Truncation Boundary (end)

When truncation is required, the final output length must not exceed the target length. Because the omission string is appended to the truncated result, the characters allocated to the omission directly reduce the available characters for the primary text.

Lodash computes the target slice end index:

let end = length - omission.length;

If unicode characters are present in the omission string itself, its length is similarly evaluated by symbol count rather than code units.

Boundary Edge Cases

  1. end < 0: If the length of the omission exceeds or equals the requested length, Lodash clamps the slice index to 0. As a result, no characters from the original string are retained, and only the omission (or an empty slice plus omission) is returned.
  2. end >= strLength: This case is eliminated early by the initial strLength <= length guard clause.

Separator-Aware Bound Adjustments

When an optional separator (string or regular expression) is supplied, Lodash does not merely slice the text at end. Instead, it treats end as an upper limit:

  1. A candidate substring is sliced from index 0 to end.
  2. If separator is a RegExp, Lodash searches for the last match within this candidate substring.
  3. If separator is a string, Lodash searches backward using lastIndexOf.
  4. If a match is found before end, the end index is updated to the boundary determined by the separator match.

This ensures that word boundaries or custom delimiters are respected without ever exceeding the ceiling set by length - omission.length.

Assembly and Return Value

Once the final end offset is determined, the text is sliced and assembled:

let result;
if (strSymbols) {
  result = castSlice(strSymbols, 0, end).join('');
} else {
  result = string.slice(0, end);
}

return result + omission;

By strictly calculating end as a function of the total allowed capacity minus the explicit length of the omission, Lodash guarantees that the concatenated output string matches the configured length parameter.