@wordpress/components
Version:
UI components for WordPress.
100 lines (98 loc) • 3.17 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/components/src/truncate/utils.ts
var utils_exports = {};
__export(utils_exports, {
TRUNCATE_DEFAULT_PROPS: () => TRUNCATE_DEFAULT_PROPS,
TRUNCATE_ELLIPSIS: () => TRUNCATE_ELLIPSIS,
TRUNCATE_TYPE: () => TRUNCATE_TYPE,
truncateContent: () => truncateContent,
truncateMiddle: () => truncateMiddle
});
module.exports = __toCommonJS(utils_exports);
var import_values = require("../utils/values");
var TRUNCATE_ELLIPSIS = "\u2026";
var TRUNCATE_TYPE = {
auto: "auto",
head: "head",
middle: "middle",
tail: "tail",
none: "none"
};
var TRUNCATE_DEFAULT_PROPS = {
ellipsis: TRUNCATE_ELLIPSIS,
ellipsizeMode: TRUNCATE_TYPE.auto,
limit: 0,
numberOfLines: 0
};
function truncateMiddle(word, headLength, tailLength, ellipsis) {
if (typeof word !== "string") {
return "";
}
const wordLength = word.length;
const frontLength = ~~headLength;
const backLength = ~~tailLength;
const truncateStr = (0, import_values.isValueDefined)(ellipsis) ? ellipsis : TRUNCATE_ELLIPSIS;
if (frontLength === 0 && backLength === 0 || frontLength >= wordLength || backLength >= wordLength || frontLength + backLength >= wordLength) {
return word;
} else if (backLength === 0) {
return word.slice(0, frontLength) + truncateStr;
}
return word.slice(0, frontLength) + truncateStr + word.slice(wordLength - backLength);
}
function truncateContent(words = "", props) {
const mergedProps = {
...TRUNCATE_DEFAULT_PROPS,
...props
};
const {
ellipsis,
ellipsizeMode,
limit
} = mergedProps;
if (ellipsizeMode === TRUNCATE_TYPE.none) {
return words;
}
let truncateHead;
let truncateTail;
switch (ellipsizeMode) {
case TRUNCATE_TYPE.head:
truncateHead = 0;
truncateTail = limit;
break;
case TRUNCATE_TYPE.middle:
truncateHead = Math.floor(limit / 2);
truncateTail = Math.floor(limit / 2);
break;
default:
truncateHead = limit;
truncateTail = 0;
}
const truncatedContent = ellipsizeMode !== TRUNCATE_TYPE.auto ? truncateMiddle(words, truncateHead, truncateTail, ellipsis) : words;
return truncatedContent;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TRUNCATE_DEFAULT_PROPS,
TRUNCATE_ELLIPSIS,
TRUNCATE_TYPE,
truncateContent,
truncateMiddle
});
//# sourceMappingURL=utils.js.map