({ value, regex, tagName = 'span', attributes = {} }: TagWrapper)
| 1 | type TagWrapper = { value: string; regex: RegExp; tagName?: string; attributes?: object }; |
| 2 | |
| 3 | const tagWrapper = ({ value, regex, tagName = 'span', attributes = {} }: TagWrapper): string => { |
| 4 | if (typeof value !== 'string') return value; |
| 5 | |
| 6 | const setAttributes = attrs => |
| 7 | Object.entries(attrs) |
| 8 | .map(([key, val]) => `${key === 'className' ? 'class' : key}="${val}"`) |
| 9 | .join(' '); |
| 10 | |
| 11 | return value.replace(regex, `<${tagName} ${setAttributes(attributes)}>$1</${tagName}>`); |
| 12 | }; |
| 13 | |
| 14 | export default tagWrapper; |
no test coverage detected