-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathhelper.js
52 lines (44 loc) · 1.2 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from 'react';
const getStringChild = child => {
return {
type: 'string',
content: child,
child,
};
};
const getTextChild = child => {
return {
type: child?.type?.displayName,
content: child.props.children,
child: React.cloneElement(child, child.props, child.props.children),
};
};
export const getTextByChildren = (children, TextComponent) => {
if (typeof children === 'string') {
return [getStringChild(children)];
}
if (typeof children === 'object' && children.props?.children) {
return getTextByChildren(React.Children.toArray(children.props.children));
}
if (Array.isArray(children)) {
return children
.filter(_child => {
return (
typeof _child === 'string' ||
_child?.type?.displayName === TextComponent?.displayName
);
})
.map(_child => {
if (typeof _child === 'string') {
return getStringChild(_child);
}
return getTextChild(_child);
});
}
return null;
};
export const linesToCharacters = lines => {
return lines.map(_line => _line?.text || '').join('');
};
export const insertAt = (str, sub, pos) =>
`${str.slice(0, pos)}${sub}${str.slice(pos)}`;