Passed
Push — feature/azure-webapp-pipeline-... ( ed5482...50ec4b )
by Grant
08:00 queued 16s
created

resources/assets/js/helpers/textToParagraphs.tsx   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 35
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 22
mnd 2
bc 2
fnc 0
dl 0
loc 35
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from "react";
2
3
/**
4
 * Splits a string on newlines and creates a list of <p> elements with the results.
5
 * @param text A string that may or may not contain newlines.
6
 * @param props An object with attributes to add to each <p> element.
7
 * @param indexedProps Maps objects of attributes to the paragraph index they apply to, allowing different attributes for specific paragraphs.
8
 * @returns One or more <p> elements wrapped in a fragment.
9
 */
10
export const textToParagraphs = (
11
  text: string,
12
  props?: { [attribute: string]: any },
13
  indexedProps?: { [index: number]: any },
14
): React.ReactFragment => {
15
  const items = text.split("\n");
16
  return (
17
    <>
18
      {items.map(
19
        (item, index): React.ReactElement => {
20
          const getIndexedProps =
21
            indexedProps && indexedProps[index] ? indexedProps[index] : "";
22
          return (
23
            // eslint-disable-next-line react/no-array-index-key
24
            <p key={index} {...props} {...getIndexedProps}>
25
              {item.trim().length > 0 ? item : <br />}
26
            </p>
27
          );
28
        },
29
      )}
30
    </>
31
  );
32
};
33
34
export default textToParagraphs;
35