UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

56 lines (45 loc) 1.53 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import React, { isValidElement } from 'react'; import format from 'string-template'; import isString from 'lodash/isString'; import isNil from 'lodash/isNil'; import flatMap from 'lodash/flatMap'; export function formatReactTemplate(template, map) { var result = [template]; Object.keys(map).forEach(function (key) { var node = map[key]; if (!isNil(node)) { result = flatMap(result, function (text) { if (isString(text)) { var stringText = text; if (isValidElement(node)) { var placeholder = "{".concat(key, "}"); var length = placeholder.length; var textArr = []; var index = stringText.indexOf(placeholder); while (index > -1) { if (index > 0) { textArr.push(stringText.slice(0, index)); } textArr.push(node); stringText = stringText.slice(index + length); index = stringText.indexOf(placeholder); } if (stringText) { textArr.push(stringText); } return textArr; } return format(text, _defineProperty({}, key, node)); } return text; }); } }); if (result.every(isString)) { return result.join(''); } return React.createElement(React.Fragment, null, result); } export default formatReactTemplate; //# sourceMappingURL=formatReactTemplate.js.map