Skip to content

Commit

Permalink
Add ability to trim string values
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Sep 16, 2024
1 parent 93170d0 commit 503f4e8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
26 changes: 23 additions & 3 deletions models/Component.cfc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
component output="true" {

property name="_globalSettings" inject="coldbox:modulesettings:cbwire";

property name="_CBWIREController" inject="CBWIREController@cbwire";

property name="_wirebox" inject="wirebox";
Expand Down Expand Up @@ -744,6 +746,12 @@ component output="true" {
local.updatedArrayProps = [];
// Loop over the updates and apply them
arguments.updates.each( function( key, value ) {

// Check if we should trim if simple value
if ( isSimpleValue( arguments.value ) && shouldTrimStringValues() ) {
arguments.value = trim( arguments.value );
}

// Determine if this is an array update
if ( reFindNoCase( "\.[0-9]+", arguments.key ) ) {
local.regexMatch = reFindNoCase( "(.+)\.([0-9]+)", arguments.key, 1, true );
Expand All @@ -755,10 +763,10 @@ component output="true" {
updatedArrayProps.append( local.propertyName );
}
} else {
var oldValue = variables.data[ key ];
variables.data[ key ] = value;
local.oldValue = variables.data[ key ];
variables.data[ key ] = arguments.value;
if ( structKeyExists( this, "onUpdate#key#") ) {
invoke( this, "onUpdate#key#", { value: value, oldValue: oldValue });
invoke( this, "onUpdate#key#", { value: arguments.value, oldValue: local.oldValue });
}
}
} );
Expand Down Expand Up @@ -1615,4 +1623,16 @@ component output="true" {
return false;
}
}

/**
* Returns true if trimStringValues is enabled, either globally
* or for the component.
*
* @return boolean
*/
function shouldTrimStringValues() {
return
( _globalSettings.keyExists( "trimStringValues" ) && _globalSettings.trimStringValues == true ) ||
( variables.keyExists( "trimStringValues" ) && variables.trimStringValues == true );
}
}
44 changes: 44 additions & 0 deletions test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,50 @@ component extends="coldbox.system.testing.BaseTestCase" {
prepareMock( cbwireController );
});

it( "should trim string values if global setting enabled on coldbox.cfc", () => {
var settings = getInstance( "coldbox:modulesettings:cbwire" );
settings.trimStringValues = true;
var payload = incomingRequest(
memo = {
"name": "test.should_trim_string_values_if_global_setting_enabled",
"id": "Z1Ruz1tGMPXSfw7osBW2",
"children": []
},
data = {
"name": "Jane Doe "
},
calls = [],
updates = {
"name": " Jane Doe "
}
);
var result = cbwireController.handleRequest( payload, event );
expect( result.components.first().effects.html ).toInclude( "<p>Name: Jane Doe</p>" );
settings.trimStringValues = false;
} );

it( "should trim string values if enabled on component", () => {
var settings = getInstance( "coldbox:modulesettings:cbwire" );
settings.trimStringValues = false;
var payload = incomingRequest(
memo = {
"name": "test.should_trim_string_values_if_enabled_on_component",
"id": "Z1Ruz1tGMPXSfw7osBW2",
"children": []
},
data = {
"name": "Jane Doe "
},
calls = [],
updates = {
"name": " Jane Doe "
}
);
var result = cbwireController.handleRequest( payload, event );
expect( result.components.first().effects.html ).toInclude( "<p>Name: Jane Doe</p>" );
settings.trimStringValues = false;
} );

it( "should support $refresh action", function() {
var payload = incomingRequest(
memo = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<cfoutput>
<div>
<p>Name: #name#</p>
</div>
</cfoutput>

<cfscript>
// @startWire
trimStringValues = true;
data = {
"name": "Jane Doe"
};
// @endWire
</cfscript>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<cfoutput>
<div>
<p>Name: #name#</p>
</div>
</cfoutput>

<cfscript>
// @startWire
data = {
"name": "Jane Doe"
};
// @endWire
</cfscript>

0 comments on commit 503f4e8

Please sign in to comment.