Skip to content

Commit d872067

Browse files
draganescuclaude
andcommitted
Block Editor: Move cleanEmptyObject into utils/object
The block style state helpers only needed `hooks/utils` for `cleanEmptyObject`, and that module imports the components and the store. Through them, `utils/rotation.ts` couldn't load outside a browser environment. `cleanEmptyObject` is a plain object helper, so it now lives in `utils/object.ts`, and `hooks/utils` re-exports it for its existing importers. The rotation utils test runs in node again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5617881 commit d872067

5 files changed

Lines changed: 36 additions & 31 deletions

File tree

‎packages/block-editor/src/hooks/block-style-state.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { createContext, useContext } from '@wordpress/element';
2-
import { cleanEmptyObject } from './utils';
3-
import { getValueFromObjectPath, setImmutably } from '../utils/object';
2+
import {
3+
cleanEmptyObject,
4+
getValueFromObjectPath,
5+
setImmutably,
6+
} from '../utils/object';
47
import type { BlockStyle, BlockStyleState, ResetAllFilter } from './types';
58

69
const DEFAULT_STATE_VALUE = 'default';

‎packages/block-editor/src/hooks/utils.tsx‎

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
import { useSettings } from '../components';
1717
import { useSettingsForBlockElement } from '../components/global-styles/hooks';
1818
import {
19+
cleanEmptyObject,
1920
getValueFromObjectPath,
20-
isPlainObject,
2121
setImmutably,
2222
} from '../utils/object';
2323
import { store as blockEditorStore } from '../store';
@@ -32,35 +32,10 @@ import type {
3232
BlockListBlockHookProps,
3333
BlockSaveFeature,
3434
BlockWrapperProps,
35-
CleanedObject,
3635
StyleOverrideOptions,
3736
} from './types';
3837

39-
/**
40-
* Removed falsy values from nested object.
41-
*
42-
* @param object
43-
* @return Object cleaned from falsy values
44-
*/
45-
export const cleanEmptyObject = < T, >(
46-
object: T
47-
): CleanedObject< T > | undefined => {
48-
if ( ! isPlainObject( object ) ) {
49-
// Only plain objects are cleaned, so anything else keeps its type.
50-
return object as CleanedObject< T >;
51-
}
52-
53-
const cleanedNestedObjects: [ string, unknown ][] = Object.entries( object )
54-
.map( ( [ key, value ] ): [ string, unknown ] => [
55-
key,
56-
cleanEmptyObject( value ),
57-
] )
58-
.filter( ( [ , value ] ) => value !== undefined );
59-
// The cleaned object keeps the shape, with the empty values left out.
60-
return ! cleanedNestedObjects.length
61-
? undefined
62-
: ( Object.fromEntries( cleanedNestedObjects ) as CleanedObject< T > );
63-
};
38+
export { cleanEmptyObject };
6439

6540
export function transformStyles(
6641
activeSupports: Record< string, unknown >,

‎packages/block-editor/src/utils/object.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CleanedObject } from '../hooks/types';
2+
13
type AnyObject = Record< string, unknown >;
24
type ObjectOrArray = AnyObject | unknown[];
35
type ObjectPathKey = number | string;
@@ -83,3 +85,29 @@ export function isPlainObject( value: unknown ): value is AnyObject {
8385
typeof value === 'object' && value !== null && ! Array.isArray( value )
8486
);
8587
}
88+
89+
/**
90+
* Removed falsy values from nested object.
91+
*
92+
* @param object
93+
* @return Object cleaned from falsy values
94+
*/
95+
export const cleanEmptyObject = < T >(
96+
object: T
97+
): CleanedObject< T > | undefined => {
98+
if ( ! isPlainObject( object ) ) {
99+
// Only plain objects are cleaned, so anything else keeps its type.
100+
return object as CleanedObject< T >;
101+
}
102+
103+
const cleanedNestedObjects: [ string, unknown ][] = Object.entries( object )
104+
.map( ( [ key, value ] ): [ string, unknown ] => [
105+
key,
106+
cleanEmptyObject( value ),
107+
] )
108+
.filter( ( [ , value ] ) => value !== undefined );
109+
// The cleaned object keeps the shape, with the empty values left out.
110+
return ! cleanedNestedObjects.length
111+
? undefined
112+
: ( Object.fromEntries( cleanedNestedObjects ) as CleanedObject< T > );
113+
};

‎packages/block-editor/src/utils/rotation.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { isPlainObject } from './object';
2-
import { cleanEmptyObject } from '../hooks/utils';
1+
import { cleanEmptyObject, isPlainObject } from './object';
32
import {
43
DEFAULT_BLOCK_STYLE_STATE,
54
getStyleForState,

packages/block-editor/src/utils/test/rotation.jsdom.test.ts renamed to packages/block-editor/src/utils/test/rotation.ts

File renamed without changes.

0 commit comments

Comments
 (0)