-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbooleans.js
57 lines (45 loc) · 1.43 KB
/
booleans.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
53
54
55
56
57
// @flow
import isPlainObject from 'is-plain-object';
import printComplexDataStructure from './printComplexDataStructure';
/*
Based on
https://github.com/algolia/react-element-to-jsx-string/blob/master/src/formatter/formatPropValue.js
*/
const escape = (s: string): string => s.replace(/"/g, '"');
/*
* Print a tag attribute, for example as 'key={value}' or 'key="value"' or 'key'
*/
function printAttributeValue(value: any): string {
if (typeof value === 'number') {
return `{${String(value)}}`;
}
if (typeof value === 'string') {
return `"${escape(value)}"`;
}
// > "Symbols (new in ECMAScript 2015, not yet supported in Flow)"
// @see: https://flow.org/en/docs/types/primitives/
// $FlowFixMe: Flow does not support Symbol
if (typeof value === 'symbol') {
throw new Error('not implemented');
}
if (typeof value === 'function') {
throw new Error('not implemented');
}
if (value instanceof Date) {
return `{new Date("${value.toISOString()}")}`;
}
if (isPlainObject(value) || Array.isArray(value)) {
return `{${printComplexDataStructure(value)}}`;
}
return `{${String(value)}}`;
}
/*
* Print a tag attribute to 'key={value}' or 'key' for `true`
*/
function printAttribute(key: string, value: any) {
if (value === true) {
return key;
}
return `${key}=${printAttributeValue(value)}`;
}
export default printAttribute;