@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
69 lines • 2.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduceToText = void 0;
const react_1 = __importDefault(require("react"));
const server_1 = require("react-dom/server");
const ReactIs = __importStar(require("react-is"));
const reduceToText = (input, options) => {
const { maxNodes, maxLength } = options || {};
const content = input;
let nodeCount = 0;
const onlyText = (nodes) => {
if (typeof maxNodes !== "undefined" && nodeCount >= maxNodes)
return "";
return react_1.default.Children.toArray(nodes)
.slice(0, maxNodes)
.map((child) => {
var _a;
if (typeof maxNodes !== "undefined" && nodeCount >= maxNodes)
return "";
if (ReactIs.isFragment(child))
return onlyText((_a = child.props) === null || _a === void 0 ? void 0 : _a.children);
if (typeof child === "string" || typeof child === "number") {
nodeCount++;
return child.toString();
}
if (ReactIs.isElement(child)) {
nodeCount++;
return (0, server_1.renderToString)(react_1.default.createElement("span", null, child));
}
return "";
})
.join(" ");
};
let text = typeof content === "string" ? content : onlyText(content);
// Basic HTML cleanup
text = text.replace(/<[^\s][^>]*>/g, "").replace(/\n/g, " ");
if (typeof maxLength === "number") {
text = text.slice(0, maxLength);
}
return text.trim();
};
exports.reduceToText = reduceToText;
//# sourceMappingURL=reduceToText.js.map