1- import getParams from 'get-params' ;
21import jsan from 'jsan' ;
32import { nanoid } from 'nanoid/non-secure' ;
43import { immutableSerialize } from '@redux-devtools/serialize' ;
@@ -15,13 +14,84 @@ export interface ActionCreatorObject {
1514 readonly args : readonly string [ ] ;
1615}
1716
17+ function getParams ( func : Function ) {
18+ if ( typeof func !== 'function' ) return [ ] ;
19+
20+ const src = func
21+ . toString ( )
22+ // remove comments
23+ . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' )
24+ . replace ( / \/ \/ .* $ / gm, '' )
25+ . trim ( ) ;
26+
27+ let paramsSrc = '' ;
28+
29+ // function foo(a, b)
30+ const fnMatch = src . match ( / ^ [ ^ ( ] * \( \s * ( [ ^ ) ] * ) \) / ) ;
31+
32+ // (a, b) => or a =>
33+ const arrowMatch = src . match ( / ^ (?: \( \s * ( [ ^ ) ] * ) \) | ( [ ^ \s = ] + ) ) \s * = > / ) ;
34+
35+ if ( fnMatch ) {
36+ paramsSrc = fnMatch [ 1 ] ?? '' ;
37+ } else if ( arrowMatch ) {
38+ paramsSrc = arrowMatch [ 1 ] ?? arrowMatch [ 2 ] ?? '' ;
39+ } else {
40+ return [ ] ;
41+ }
42+
43+ if ( ! paramsSrc ) return [ ] ;
44+
45+ const params = [ ] ;
46+ let current = '' ;
47+ let depth = 0 ;
48+
49+ for ( let i = 0 ; i < paramsSrc . length ; i ++ ) {
50+ const ch = paramsSrc [ i ] ;
51+
52+ if ( ch === ',' && depth === 0 ) {
53+ params . push ( current . trim ( ) ) ;
54+ current = '' ;
55+ continue ;
56+ }
57+
58+ if ( ch === '{' || ch === '[' || ch === '(' ) depth ++ ;
59+ if ( ch === '}' || ch === ']' || ch === ')' ) depth -- ;
60+
61+ current += ch ;
62+ }
63+
64+ if ( current . trim ( ) ) {
65+ params . push ( current . trim ( ) ) ;
66+ }
67+
68+ // remove default values: a = 1 → a
69+ return params . map ( ( p , i ) => {
70+ const cleaned = p . replace ( / = .* / , '' ) . trim ( ) ;
71+
72+ // destructured parameter
73+ if (
74+ cleaned . startsWith ( '{' ) ||
75+ cleaned . startsWith ( '[' ) ||
76+ cleaned . startsWith ( '...{' ) ||
77+ cleaned . startsWith ( '...[' )
78+ ) {
79+ return `arg_${ i } ` ;
80+ }
81+
82+ return cleaned ;
83+ } ) ;
84+ }
85+
1886function flatTree (
1987 obj : { [ key : string ] : ActionCreator < Action < string > > } ,
2088 namespace = '' ,
2189) {
90+ if ( ! obj ) return [ ] ;
2291 let functions : ActionCreatorObject [ ] = [ ] ;
2392 Object . keys ( obj ) . forEach ( ( key ) => {
2493 const prop = obj [ key ] ;
94+ if ( ! prop ) return ;
2595 if ( typeof prop === 'function' ) {
2696 functions . push ( {
2797 name : namespace + ( key || prop . name || 'anonymous' ) ,
0 commit comments