Passed
Push — feature/settings-2fa ( 31e075...1e717f )
by Grant
19:37 queued 06:19
created

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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 29
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 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
 * @returns One or more <p> elements wrapped in a fragment.
8
 */
9
export const textToParagraphs = (
10
  text: string,
11
  props?: any,
12
): React.ReactFragment => {
13
  const items = text.split("\n");
14
  return (
15
    <>
16
      {items.map(
17
        (item, index): React.ReactElement => (
18
          // eslint-disable-next-line react/no-array-index-key
19
          <p key={index} {...props}>
20
            {item.trim().length > 0 ? item : <br />}
21
          </p>
22
        ),
23
      )}
24
    </>
25
  );
26
};
27
28
export default textToParagraphs;
29