Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactivity API: Move all utils inside utils.ts #61721

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { deepSignal, peek, type DeepSignal } from 'deepsignal';
/**
* Internal dependencies
*/
import { useWatch, useInit } from './utils';
import { useWatch, useInit, kebabToCamelCase, warn } from './utils';
import { directive, getScope, getEvaluate } from './hooks';
import { kebabToCamelCase } from './utils/kebab-to-camelcase';
import { warn } from './utils/warn';

// Assigned objects should be ignore during proxification.
const contextAssignedObjects = new WeakMap();
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { VNode, Context, RefObject } from 'preact';
* Internal dependencies
*/
import { store, stores, universalUnlock } from './store';
import { warn } from './utils/warn';
import { warn } from './utils';
interface DirectiveEntry {
value: string | object;
namespace: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { kebabToCamelCase } from '../kebab-to-camelcase';
import { kebabToCamelCase } from '../utils';

describe( 'kebabToCamelCase', () => {
it( 'should work exactly as the PHP version', async () => {
Expand Down
45 changes: 45 additions & 0 deletions packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,48 @@ export const createRootFragment = (
},
} );
};

/**
* Transforms a kebab-case string to camelCase.
*
* @param str The kebab-case string to transform to camelCase.
* @return The transformed camelCase string.
*/
export function kebabToCamelCase( str: string ): string {
return str
.replace( /^-+|-+$/g, '' )
.toLowerCase()
.replace( /-([a-z])/g, function ( _match, group1: string ) {
return group1.toUpperCase();
} );
}

const logged: Set< string > = new Set();

/**
* Shows a warning with `message` if environment is not `production`.
*
* Based on the `@wordpress/warning` package.
*
* @param message Message to show in the warning.
*/
export const warn = ( message: string ): void => {
// @ts-expect-error
if ( typeof SCRIPT_DEBUG !== 'undefined' && SCRIPT_DEBUG === true ) {
if ( logged.has( message ) ) {
return;
}

// eslint-disable-next-line no-console
console.warn( message );

// Throwing an error and catching it immediately to improve debugging
// A consumer can use 'pause on caught exceptions'
try {
throw Error( message );
} catch ( e ) {
// Do nothing.
}
logged.add( message );
}
};
14 changes: 0 additions & 14 deletions packages/interactivity/src/utils/kebab-to-camelcase.ts

This file was deleted.

21 changes: 0 additions & 21 deletions packages/interactivity/src/utils/warn.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/interactivity/src/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { h, type ComponentChild, type JSX } from 'preact';
* Internal dependencies
*/
import { directivePrefix as p } from './constants';
import { warn } from './utils/warn';
import { warn } from './utils';

const ignoreAttr = `data-${ p }-ignore`;
const islandAttr = `data-${ p }-interactive`;
Expand Down
Loading