Skip to content

Commit

Permalink
Interactivity API: Move all utils inside utils.ts (#61721)
Browse files Browse the repository at this point in the history
* Move utils file inside folder

* Revert "Move utils file inside folder"

This reverts commit 5f669c1.

* Move all utils inside `utils.ts`

* Fix utils tests and move them to TS

Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
  • Loading branch information
3 people committed May 16, 2024
1 parent 4fb90bc commit 4d543e6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 42 deletions.
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
2 changes: 1 addition & 1 deletion test/unit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
'^.+\\.[jt]sx?$': '<rootDir>/test/unit/scripts/babel-transformer.js',
},
transformIgnorePatterns: [
'/node_modules/(?!(docker-compose|yaml)/)',
'/node_modules/(?!(docker-compose|yaml|preact|@preact|deepsignal)/)',
'\\.pnp\\.[^\\/]+$',
],
snapshotSerializers: [
Expand Down

0 comments on commit 4d543e6

Please sign in to comment.