@@ -24,7 +24,7 @@ export type SchemaGenerationOptions = {
2424/**
2525 * Local counterpart to the dataset API's `clean=true` — empty arrays carry no schema info.
2626 * Strips only empty arrays; keeps null / '' / empty objects so schema inference still sees those fields.
27- * Stricter sibling: `cleanEmptyProperties` in `src/tools/common/get_actor_output.ts` also strips nullish and empty strings.
27+ * Stricter sibling: { @link cleanEmptyProperties} also strips nullish and empty strings.
2828 */
2929export function cleanEmptyArrays ( obj : unknown ) : unknown {
3030 if ( Array . isArray ( obj ) ) {
@@ -43,6 +43,39 @@ export function cleanEmptyArrays(obj: unknown): unknown {
4343 } , { } as Record < string , unknown > ) ;
4444}
4545
46+ /**
47+ * Cleans empty properties (null, undefined, empty strings, empty arrays, empty objects) from an object.
48+ * Looser sibling: {@link cleanEmptyArrays} strips only empty arrays.
49+ * @param obj - The object to clean
50+ * @returns The cleaned object or undefined if the result is empty
51+ */
52+ export function cleanEmptyProperties ( obj : unknown ) : unknown {
53+ if ( obj === null || obj === undefined || obj === '' ) {
54+ return undefined ;
55+ }
56+
57+ if ( typeof obj !== 'object' ) {
58+ return obj ;
59+ }
60+
61+ if ( Array . isArray ( obj ) ) {
62+ const cleaned = obj
63+ . map ( ( item ) => cleanEmptyProperties ( item ) )
64+ . filter ( ( item ) => item !== undefined ) ;
65+ return cleaned . length > 0 ? cleaned : undefined ;
66+ }
67+
68+ const cleaned : Record < string , unknown > = { } ;
69+ for ( const [ key , value ] of Object . entries ( obj ) ) {
70+ const cleanedValue = cleanEmptyProperties ( value ) ;
71+ if ( cleanedValue !== undefined ) {
72+ cleaned [ key ] = cleanedValue ;
73+ }
74+ }
75+
76+ return Object . keys ( cleaned ) . length > 0 ? cleaned : undefined ;
77+ }
78+
4679const FORMAT_DETECTORS : [ string , ( s : string ) => boolean ] [ ] = [
4780 [ 'date-time' , ( s ) => / ^ \d { 4 } - \d { 2 } - \d { 2 } [ T t ] \d { 2 } : \d { 2 } : \d { 2 } ( \. \d + ) ? ( [ Z z ] | [ + - ] \d { 2 } : \d { 2 } ) ? $ / . test ( s ) ] ,
4881 [ 'date' , ( s ) => / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( s ) ] ,
0 commit comments