Remove tweak to work with both snake_case and camelCase

This commit is contained in:
Lucas Bustamante 2022-02-03 11:26:49 -03:00
parent 23b1c10518
commit c1cf58437f
2 changed files with 14 additions and 25 deletions

View File

@ -43,6 +43,10 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT
* @template T
*/
public constructor( property: string, modelClass: ModelConstructor< T >, transformer: ModelTransformer< T > ) {
// Developer-friendly error to make sure this doesn't go unnoticed.
if (property.includes('_')) {
throw new Error('The property must be camelCase');
}
this.property = property;
this.modelClass = modelClass;
this.transformer = transformer;
@ -74,27 +78,12 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT
* @return {*} The transformed properties.
*/
public toModel( properties: any ): any {
let propertyName = this.property;
let val = properties[ propertyName ];
if ( ! val ) {
/*
* Properties are defined in snake_case format, but the properties in the models are camelCase.
* Due to how the hydration of the model works, using TypeScript's Partial, the properties object
* might have been transformed from snake_case to camelCase already, so we try to convert
* the property name to camelCase before giving up.
*/
propertyName = propertyName.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, function (_match, chr) {
return chr.toUpperCase();
});
val = properties[ propertyName ];
}
const val = properties[ this.property ];
if ( val ) {
if ( Array.isArray( val ) ) {
properties[ propertyName ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) );
properties[ this.property ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) );
} else {
properties[ propertyName ] = this.transformer.toModel( this.modelClass, val );
properties[ this.property ] = this.transformer.toModel( this.modelClass, val );
}
}

View File

@ -30,13 +30,13 @@ export function createOrderTransformer(): ModelTransformer< Order > {
new IgnorePropertyTransformation( [ 'date_created', 'date_modified' ] ),
new ModelTransformerTransformation( 'billing', BillingOrderAddress, createBillingAddressTransformer() ),
new ModelTransformerTransformation( 'shipping', ShippingOrderAddress, createShippingAddressTransformer() ),
new ModelTransformerTransformation( 'tax_lines', OrderTaxRate, createOrderTaxRateTransformer() ),
new ModelTransformerTransformation( 'taxLines', OrderTaxRate, createOrderTaxRateTransformer() ),
new ModelTransformerTransformation( 'refunds', OrderRefundLine, createOrderRefundLineTransformer() ),
new ModelTransformerTransformation( 'coupon_lines', OrderCouponLine, createOrdeCouponLineTransformer() ),
new ModelTransformerTransformation( 'fee_lines', OrderFeeLine, createOrderFeeLineTransformer() ),
new ModelTransformerTransformation( 'line_items', OrderLineItem, createOrderLineItemTransformer() ),
new ModelTransformerTransformation( 'shipping_lines', OrderShippingLine, createOrderShippingItemTransformer() ),
new ModelTransformerTransformation( 'meta_data', MetaData, createMetaDataTransformer() ),
new ModelTransformerTransformation( 'couponLines', OrderCouponLine, createOrdeCouponLineTransformer() ),
new ModelTransformerTransformation( 'feeLines', OrderFeeLine, createOrderFeeLineTransformer() ),
new ModelTransformerTransformation( 'lineItems', OrderLineItem, createOrderLineItemTransformer() ),
new ModelTransformerTransformation( 'shippingLines', OrderShippingLine, createOrderShippingItemTransformer() ),
new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ),
new PropertyTypeTransformation(
{
@ -212,7 +212,7 @@ function createOrderRefundLineTransformer(): ModelTransformer< OrderRefundLine >
function createOrdeCouponLineTransformer(): ModelTransformer< OrderCouponLine > {
return new ModelTransformer(
[
new ModelTransformerTransformation( 'meta_data', MetaData, createMetaDataTransformer() ),
new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ),
new PropertyTypeTransformation(
{
code: PropertyType.String,