Skip to content

Commit

Permalink
Added explainer comment for BorderIndividualStyles type
Browse files Browse the repository at this point in the history
Removing lodash camelCase and upperFirst in favour of own implementation + test
Removing unnecessary lodash get()
  • Loading branch information
ramonjd committed Jun 21, 2022
1 parent 70a0652 commit 93abab8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
15 changes: 5 additions & 10 deletions packages/style-engine/src/styles/border/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { camelCase, get } from 'lodash';

/**
* Internal dependencies
*/
Expand All @@ -14,7 +9,7 @@ import type {
StyleDefinition,
StyleOptions,
} from '../../types';
import { generateRule, generateBoxRules } from '../utils';
import { generateRule, generateBoxRules, upperFirst } from '../utils';

const color = {
name: 'color',
Expand Down Expand Up @@ -86,7 +81,7 @@ const createBorderGenerateFunction =
( style: Style, options: StyleOptions ) => {
const styleValue:
| BorderIndividualStyles< typeof individualProperty >
| undefined = get( style, [ 'border', individualProperty ] );
| undefined = style?.border?.[ individualProperty ];

if ( ! styleValue ) {
return [];
Expand All @@ -102,9 +97,9 @@ const createBorderGenerateFunction =
styleValue.hasOwnProperty( key ) &&
typeof borderDefinition.generate === 'function'
) {
const ruleKey = camelCase(
`border-${ individualProperty }-${ key }`
);
const ruleKey = `border${ upperFirst(
individualProperty
) }${ upperFirst( key ) }`;
acc.push(
...borderDefinition.generate(
style,
Expand Down
20 changes: 17 additions & 3 deletions packages/style-engine/src/styles/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, upperFirst } from 'lodash';
import { get } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -57,7 +57,7 @@ export function generateRule(
* @param ruleKeys An array of CSS property keys and patterns.
* @param individualProperties The "sides" or individual properties for which to generate rules.
*
* @return GeneratedCSSRule[] CSS rules.
* @return GeneratedCSSRule[] CSS rules.
*/
export function generateBoxRules(
style: Style,
Expand Down Expand Up @@ -110,7 +110,10 @@ export function generateBoxRules(
* @return string A CSS var value.
*/
export function getCSSVarFromStyleValue( styleValue: string ): string {
if ( styleValue.startsWith( VARIABLE_REFERENCE_PREFIX ) ) {
if (
typeof styleValue === 'string' &&
styleValue.startsWith( VARIABLE_REFERENCE_PREFIX )
) {
const variable = styleValue
.slice( VARIABLE_REFERENCE_PREFIX.length )
.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
Expand All @@ -119,3 +122,14 @@ export function getCSSVarFromStyleValue( styleValue: string ): string {
}
return styleValue;
}

/**
* Capitalizes the first letter in a string.
*
* @param {string} str The string whose first letter the function will capitalize.
*
* @return string A CSS var value.
*/
export function upperFirst( [ firstLetter, ...rest ]: string ) {
return firstLetter.toUpperCase() + rest.join( '' );
}
12 changes: 12 additions & 0 deletions packages/style-engine/src/test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Internal dependencies
*/
import { upperFirst } from '../styles/utils';

describe( 'utils', () => {
describe( 'upperFirst()', () => {
it( 'should return an string with a capitalized first letter', () => {
expect( upperFirst( 'toontown' ) ).toEqual( 'Toontown' );
} );
} );
} );
1 change: 1 addition & 0 deletions packages/style-engine/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Box< T extends BoxVariants = undefined > = {
};

export type BorderIndividualProperty = 'top' | 'right' | 'bottom' | 'left';
// `T` is one of the values in `BorderIndividualProperty`. The expected CSSProperties key is something like `borderTopColor`.
export type BorderIndividualStyles< T extends BorderIndividualProperty > = {
color?: CSSProperties[ `border${ Capitalize< string & T > }Color` ];
style?: CSSProperties[ `border${ Capitalize< string & T > }Style` ];
Expand Down

0 comments on commit 93abab8

Please sign in to comment.