react-code-dataset / tabler-react /example /src /ComponentDemo /react-element-to-jsx-string /formatter /formatTreeNode.js.flow
Download tabler-react/example/src/ComponentDemo/react-element-to-jsx-string/formatter/formatTreeNode.js.flow from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/tabler-react/example/src/ComponentDemo/react-element-to-jsx-string/formatter/formatTreeNode.js.flow
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/tabler-react/example/src/ComponentDemo/react-element-to-jsx-string/formatter/formatTreeNode.js.flow
-
curl -L -o formatTreeNode.js.flow https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/tabler-react/example/src/ComponentDemo/react-element-to-jsx-string/formatter/formatTreeNode.js.flow
1.34 kB
| /* @flow */ | |
| import formatReactElementNode from './formatReactElementNode'; | |
| import formatReactFragmentNode from './formatReactFragmentNode'; | |
| import type { Options } from './../options'; | |
| import type { TreeNode } from './../tree'; | |
| const jsxStopChars = ['<', '>', '{', '}']; | |
| const shouldBeEscaped = (s: string) => | |
| jsxStopChars.some(jsxStopChar => s.includes(jsxStopChar)); | |
| const escape = (s: string) => { | |
| if (!shouldBeEscaped(s)) { | |
| return s; | |
| } | |
| return `{\`${s}\`}`; | |
| }; | |
| const preserveTrailingSpace = (s: string) => { | |
| let result = s; | |
| if (result.endsWith(' ')) { | |
| result = result.replace(/^(\S*)(\s*)$/, "$1{'$2'}"); | |
| } | |
| if (result.startsWith(' ')) { | |
| result = result.replace(/^(\s*)(\S*)$/, "{'$1'}$2"); | |
| } | |
| return result; | |
| }; | |
| export default ( | |
| node: TreeNode, | |
| inline: boolean, | |
| lvl: number, | |
| options: Options | |
| ): string => { | |
| if (node.type === 'number') { | |
| return String(node.value); | |
| } | |
| if (node.type === 'string') { | |
| return node.value | |
| ? `${preserveTrailingSpace(escape(String(node.value)))}` | |
| : ''; | |
| } | |
| if (node.type === 'ReactElement') { | |
| return formatReactElementNode(node, inline, lvl, options); | |
| } | |
| if (node.type === 'ReactFragment') { | |
| return formatReactFragmentNode(node, inline, lvl, options); | |
| } | |
| throw new TypeError(`Unknow format type "${node.type}"`); | |
| }; | |