Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor to use exported GenerateFunction type
  • Loading branch information
Alex Lende committed Aug 25, 2022
commit 52c02c096dc396cb39d20c125ae316a100d75ebf
17 changes: 6 additions & 11 deletions packages/style-engine/src/styles/border/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
* Internal dependencies
*/
import { camelCase } from 'lodash';
import type {
BorderIndividualProperty,
Style,
StyleDefinition,
StyleOptions,
} from '../../types';
import type { BoxEdges, GenerateFunction, StyleDefinition } from '../../types';
import { generateRule, generateBoxRules } from '../utils';

function makeGenerateRule( path: string[] ) {
return ( style: Style, options: StyleOptions ) =>
function makeGenerateRule( path: string[] ): GenerateFunction {
return ( style, options ) =>
generateRule( style, options, path, camelCase( path.join( ' ' ) ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we don't need an external library here.

There's already a util called upperFirst. See the unit test.

So, until we need extra functionality, maybe we could do the following:

const path = [ 'some', 'path', 'to', 'somewhere' ];

console.log( path.map( ( value, index) => index !== 0 ? upperFirst( value ) : value ).join( '' ) );  
// -> somePathToSomewhere

Or we could roll our own camelCase using upperFirst():

const path = [ 'some', 'path', 'to', 'somewhere' ];

function camelCase( [ firstItem, ...rest ] ) {
	return firstItem.toLowerCase() + rest.map( upperFirst ).join( '' );
}

console.log( camelCase( path ) );
// -> somePathToSomewhere

What do you reckon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using the lodash version of camelCase first which didn't require adding a dependency, but apparently that is being phased out elsewhere in Gutenberg in favor of change-case.

But adding our own function is easy enough and it can be trimmed a bit more to our use-case, so that's fixed in 990f0a1.

}

function createBorderGenerateFunction(
individualProperty: BorderIndividualProperty
) {
return ( style: Style, options: StyleOptions ) => {
individualProperty: BoxEdges
): GenerateFunction {
return ( style, options ) => {
return [ 'color', 'style', 'width' ].flatMap( ( key ) => {
const path = [ 'border', individualProperty, key ];
return makeGenerateRule( path )( style, options );
Expand Down
6 changes: 5 additions & 1 deletion packages/style-engine/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export type GeneratedCSSRule = {
key: string;
};

export interface GenerateFunction {
( style: Style, options: StyleOptions ): GeneratedCSSRule[];
}

export interface StyleDefinition {
name: string;
generate?: ( style: Style, options: StyleOptions ) => GeneratedCSSRule[];
generate?: GenerateFunction;
}