1- const ority = require ( 'ority' ) ;
2- const deepmerge = require ( 'deepmerge' ) ;
31const _ = exports ;
42
53_ . symbol = Symbol ( 'symbol' ) ;
64
7- _ . arrify = _ => _ ? Array . isArray ( _ ) ? _ : [ _ ] : [ ] ;
85_ . flat = a => a . reduce ( ( f , v ) => Array . isArray ( v ) ? f . concat ( _ . flat ( v ) ) : f . concat ( v ) , [ ] ) ;
9- _ . ifToClass = _ => _ && { class : _ } || { } ;
10- _ . arrifyClass = _ => {
11- if ( ! _ ) return [ ] ;
12- if ( Array . isArray ( _ ) ) return _ ;
13- if ( typeof _ === 'string' ) return _ . split ( / + / g) ;
14- if ( typeof _ === 'object' ) return Object . keys ( _ ) . reduce ( ( c , k ) => [ ...c , _ [ k ] && k ] . filter ( Boolean ) , [ ] ) ;
15- throw new Error ( `Invalid class: ${ typeof _ } ${ JSON . stringify ( _ ) } ` ) ;
16- } ;
176
187const childTypes = 'string|number|function|boolean' . split ( '|' ) ;
8+ _ . isChild = it =>
9+ childTypes . includes ( typeof it )
10+ || Array . isArray ( it )
11+ || ( it && it [ _ . symbol ] )
12+ // || (it && it.children)
13+ // || (it && it.attributes)
14+ || ( it && it . $$typeof ) ;
1915
2016_ . getPropsAndChildren = args => {
2117
22- const isChild = it =>
23- childTypes . includes ( typeof it )
24- || Array . isArray ( it )
25- || ( it && it [ _ . symbol ] )
26- // || (it && it.children)
27- // || (it && it.attributes)
28- || ( it && it . $$typeof ) ;
29-
3018 let props ;
3119 let children = [ ] ;
3220
33- // console.log(`args:`, args);
34-
3521 if ( ! args || ! args . length ) {
3622 //
3723 } else if ( args . length === 1 ) {
38- // console.log(`args[0][_.symbol]:`, args[0][_.symbol]);
39- if ( isChild ( args [ 0 ] ) ) {
24+ if ( _ . isChild ( args [ 0 ] ) ) {
4025 props = null ;
4126 if ( Array . isArray ( args [ 0 ] ) ) {
4227 children = args [ 0 ] ;
@@ -45,12 +30,9 @@ _.getPropsAndChildren = args => {
4530 }
4631 } else {
4732 props = args [ 0 ] ;
48- // if (props && props.children) {
49- // children = props.children;
50- // }
5133 }
5234 } else {
53- if ( isChild ( args [ 0 ] ) ) {
35+ if ( _ . isChild ( args [ 0 ] ) ) {
5436 props = null ;
5537 children = args ;
5638 } else {
@@ -59,87 +41,5 @@ _.getPropsAndChildren = args => {
5941 }
6042 }
6143
62- // console.log({ props, children });
63-
64- // if (isChild(args[0])) {
65- // props = null;
66- // children = args;
67- // } else if (args.length > 1) {
68- // props = args[0] || null;
69- // children = args.slice(1);
70- // } else {
71- // props = args[0] || null;
72- // children = args.slice(1);
73- // }
74-
7544 return { props, children } ;
76-
77- // if (typeof args[0])
78- // if (!Array.isArray(args)) {
79- // throw new Error(`Need an array of args, got: ${args} (${typeof args})`);
80- // }
81- // if (_.isTTL(args)) {
82- // return { props: {}, children: _.parseTTL(...args) };
83- // } else {
84- // let { props, children } = ority(args, [{
85- // props: 'object',
86- // children: ['string', 'number', 'function'],
87- // }, {
88- // children: ['string', 'number', 'function'],
89- // props: 'object',
90- // }, {
91- // props: 'object',
92- // }, {
93- // children: ['string', 'number', 'function'],
94- // }, {}]);
95- // props = props || {};
96- // children = _.arrify(children || []);
97- // return { props, children };
98- // }
99- }
100-
101- _ . getAttribValue = args => {
102- if ( args . length > 1 || ! args [ 0 ] || typeof args [ 0 ] !== 'string' ) {
103- throw new Error ( `Expected a single string argument, got: ${ args . map ( _ => JSON . stringify ( _ ) ) } ` ) ;
104- } else {
105- return args [ 0 ] ;
106- }
107- }
108-
109- // TTL = Tagged Template Literals, i.e: `...${...}...`
110- _ . isTTL = args => Array . isArray ( args [ 0 ] ) && 'raw' in args [ 0 ] ;
111- _ . parseTTL = ( [ str , ...keys ] ) => str . reduce ( ( children , child , i ) => children . concat ( [ child , keys [ i ] !== undefined && keys [ i ] ] . filter ( Boolean ) ) , [ ] ) ;
112- _ . parseIfTTL = args => _ . isTTL ( args ) ? _ . parseTTL ( ...args ) : args ;
113-
114- _ . mergeProps = ( a , ...rest ) => {
115- const last = rest [ rest . length - 1 ] ;
116- let shouldMerge = false ;
117- if ( typeof last === 'boolean' ) {
118- rest = rest . slice ( 0 , - 1 ) ;
119- shouldMerge = last ;
120- }
121- for ( let i = rest . length - 1 ; i >= 0 ; i -- ) {
122- const b = rest [ i ] ;
123- for ( const key in b ) {
124- if ( key === 'class' ) {
125- try {
126- a . class = [ ..._ . arrifyClass ( a . class ) , ..._ . arrifyClass ( b . class ) ] ;
127- } catch ( error ) {
128- console . log ( { a, b } ) ;
129- throw error
130- }
131- } else if ( shouldMerge ) {
132- if ( Array . isArray ( a [ key ] ) || Array . isArray ( b [ key ] ) ) {
133- a [ key ] = b [ key ] ;
134- } else if ( typeof a [ key ] === 'object' || typeof b [ key ] === 'object' ) {
135- a [ key ] = deepmerge . all ( [ { } , a [ key ] || { } , b [ key ] || { } ] ) ;
136- } else {
137- a [ key ] = b [ key ] ;
138- }
139- } else {
140- a [ key ] = b [ key ] ;
141- }
142- }
143- }
144- return a ;
14545}
0 commit comments