Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created August 16, 2019 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/ca843557a79706a92dc581ac135ff317 to your computer and use it in GitHub Desktop.
Save bennadel/ca843557a79706a92dc581ac135ff317 to your computer and use it in GitHub Desktop.
Installing User Defined Functions (UDF) As An Extension For Built-In Functions (BIF) In Lucee 5.3.2.77
<cfscript>
// The {lucee-server} placeholder brings us to the "context" directory for the
// server. But, for the deployment of extensions, we need to go up one level to find
// the "deploy" directory.
deployPath = expandPath( "{lucee-server}/../deploy/" );
// To deploy our extension, we need to place our LEX file in the deploy directory.
lexPath = ( deployPath & "TruthyFalsy.lex" );
// The cool thing is, we can Zip / Compress the local extension directory directly
// into the server's deploy directory in our CommandBox instance. Woot woot!
compress( "zip", "./extension", lexPath, false );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
// Since it takes some time (up to a minute according to a video I watched) for the
// LEX file to get installed, we need to increase the request timeout before we start
// monitoring the file-system for changes.
cfsetting( requestTimeout = ( 2 * 60 ) );
echo( "Waiting for <code>.lex</code> file to be installed " );
// Lucee monitors the deploy directory for changes, and then moves the new files out
// of the deploy directory. As such, we can use the existence of our LEX file as a
// way to determine when the extension has been installed in the server.
while ( fileExists( lexPath ) ) {
echo( "<strong>.</strong> " );
cfflush();
sleep( 500 );
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
// At this point, the LEX file has been installed; however, it appears that the Lucee
// service SOMETIMES needs to be restarted (based on my testing). I am not sure why
// this is inconsistently required.
echo( "<p> The <code>.lex</code> file has been deployed. </p>" );
echo( "<p> You may need to <strong>" );
echo( "<a href='./restart.cfm' target='_blank'>restart the Lucee service</a>" );
echo( "</strong> in order to see the changes.</p>" );
</cfscript>
/extension/
/extension/functions/
/extension/functions/isfalsy.cfm
/extension/functions/istruthy.cfm
/extension/META-INF/
/extension/META-INF/MANIFEST.MF
<cfscript>
/**
* I determine if the given value is a Falsy (according to JavaScript rules).
*
* See JavaScript definition on the Mozilla Developer Network (MDN):
* https://developer.mozilla.org/en-US/docs/Glossary/Falsy
*
* @value I am the value being tested.
*/
public boolean function isFalsy( any value ) {
if ( isNull( value ) ) {
return( true );
}
if ( ! isSimpleValue( value ) ) {
return( false );
}
if ( isInstanceOf( value, "java.lang.String" ) ) {
return( ! value.len() );
}
if ( isBoolean( value ) || isNumeric( value ) ) {
return( ! value );
}
return( false );
}
</cfscript>
<cfscript>
/**
* I determine if the given value is a Truthy (according to JavaScript rules). A
* Truthy value is any value that is not explicitly designated as a Falsy.
*
* See JavaScript definition on the Mozilla Developer Network (MDN):
* https://developer.mozilla.org/en-US/docs/Glossary/Truthy
*
* @value I am the value being tested.
*/
public boolean function isTruthy( any value ) {
if ( isNull( value ) ) {
return( false );
}
return( ! isFalsy( value ) );
}
</cfscript>
Manifest-Version: 1.0
id: "b6304293-81fa-4bcf-a6aa4bf9bd673697"
version: "1.0.0.0"
name: "TruthyFalsy"
description: "Decision functions based on JavaScript's notion of Truthy and Falsy values."
category: "Decision Logic"
<p>
<strong>Restarting</strong> the Lucee server context....
</p>
<cfflush />
<cfadmin
action = "restart"
type = "server"
password = "commandbox"
/>
<p>
Restart complete.
</p>
<cfscript>
// These should all be true.
echoLine( isFalsy( nullValue() ) === true );
echoLine( isFalsy( false ) === true );
echoLine( isFalsy( "" ) === true );
echoLine( isFalsy( 0 ) === true );
// These should all be false.
echoLine( isFalsy( 1 ) === false );
echoLine( isFalsy( "0" ) === false );
echoLine( isFalsy( [] ) === false );
echoLine( isFalsy( {} ) === false );
echoLine( isFalsy( now() ) === false );
// These should all be true.
echoLine( isTruthy( {} ) === true );
echoLine( isTruthy( [] ) === true );
echoLine( isTruthy( "0" ) === true );
echoLine( isTruthy( true ) === true );
echoLine( isTruthy( 1 ) === true );
echoLine( isTruthy( now() ) === true );
// These should all be false.
echoLine( isTruthy( nullValue() ) === false );
echoLine( isTruthy( false ) === false );
echoLine( isTruthy( 0 ) === false );
echoLine( isTruthy( "" ) === false );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
public void function echoLine( required any value ) {
echo( value & "<br />" );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment