Skip to content

Commit

Permalink
moving contains arrays and lists to the compat module
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed May 20, 2024
1 parent acaac8f commit 66833fc
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,8 @@
@BoxBIF( alias = "ArrayFindNoCase" )
@BoxMember( type = BoxLangType.ARRAY )
@BoxMember( type = BoxLangType.ARRAY, name = "findNoCase" )
@BoxBIF( alias = "ArrayContains" )
@BoxBIF( alias = "ArrayContainsNoCase" )
@BoxMember( type = BoxLangType.ARRAY, name = "contains" )
@BoxMember( type = BoxLangType.ARRAY, name = "containsNoCase" )
public class ArrayFind extends BIF {

/**
* These are the functions that return boolean instead of the index
*/
private static final Array booleanReturnFunctions = new Array(
new Object[] {
Key.arrayContains,
Key.arrayContainsNoCase,
Key.contains,
Key.containsNoCase,
Key.listContains,
Key.listContainsNoCase
}
);

/**
* Constructor
*/
Expand All @@ -73,11 +55,6 @@ public ArrayFind() {
* @function.arrayFindNoCase This function searches the array for the specified value. Returns the index in the array of the first match, or 0 if
* there is no match. The search is case insensitive.
*
* @function.arrayContains This function searches the array for the specified value. Returns a boolean indicating if the value was found or not.
*
* @function.arrayContainsNoCase This function searches the array for the specified value. Returns a boolean indicating if the value was found or not.
* The search is case insensitive.
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
Expand All @@ -95,17 +72,18 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Object value = arguments.get( Key.value );
Boolean substringMatch = arguments.getAsBoolean( Key.substringMatch );

// This case might exist. If it does, we need to set it to false
if ( substringMatch == null ) {
substringMatch = false;
}

// Go search by function or by value
Integer indexFound = value instanceof Function
return value instanceof Function castedValueFunction
// Search by function
? actualArray.findIndex( ( Function ) value, context )
? actualArray.findIndex( castedValueFunction, context )
// Search by value or by substring
: ( substringMatch ? actualArray.findIndexWithSubstring( value, isCaseSensitive( bifMethodKey ) )
: actualArray.findIndex( value, isCaseSensitive( bifMethodKey ) ) );

// If the function is a boolean return function, return a boolean
// Else the index
return booleanReturnFunctions.contains( bifMethodKey ) ? indexFound > 0 : indexFound;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
@BoxBIF( alias = "ListFindNoCase" )
@BoxMember( type = BoxLangType.STRING, name = "listFind" )
@BoxMember( type = BoxLangType.STRING, name = "listFindNoCase" )
@BoxBIF( alias = "ListContains" )
@BoxBIF( alias = "ListContainsNoCase" )
@BoxMember( type = BoxLangType.STRING, name = "listContains" )
@BoxMember( type = BoxLangType.STRING, name = "listContainsNoCase" )
public class ListFind extends ArrayFind {

/**
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/ortus/boxlang/runtime/scopes/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key array2 = Key.of( "array2" );
public static final Key arrayFind = Key.of( "arrayFind" );
public static final Key arrayFindAll = Key.of( "arrayFindAll" );
public static final Key arrayContains = Key.of( "arrayContains" );
public static final Key arrayContainsNoCase = Key.of( "arrayContainsNoCase" );
public static final Key asOptional = Key.of( "asOptional" );
public static final Key assocAttribs = Key.of( "assocAttribs" );
public static final Key asyncService = Key.of( "asyncService" );
Expand Down Expand Up @@ -152,8 +150,6 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key component = Key.of( "component" );
public static final Key condition = Key.of( "condition" );
public static final Key configure = Key.of( "configure" );
public static final Key contains = Key.of( "contains" );
public static final Key containsNoCase = Key.of( "containsNoCase" );
public static final Key content = Key.of( "content" );
public static final Key context = Key.of( "context" );
public static final Key contextual = Key.of( "contextual" );
Expand Down Expand Up @@ -333,8 +329,6 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key limit = Key.of( "limit" );
public static final Key line = Key.of( "line" );
public static final Key list = Key.of( "list" );
public static final Key listContains = Key.of( "listContains" );
public static final Key listContainsNoCase = Key.of( "listContainsNoCase" );
public static final Key listInfo = Key.of( "listInfo" );
public static final Key listToJSON = Key.of( "listToJSON" );
public static final Key lJustify = Key.of( "lJustify" );
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/TestCases/ScratchPad.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ void testIt() {
// @formatter:off
instance.executeSource(
"""
cl = createObject("java","java.net.URLClassLoader");
test = cl.getClass().getName();
test = [ 1, 2 ,3 ]
for( item, index in test ) {
println( item )
}
""", context);
// @formatter:on

var result = variables.get( resultKey );
System.out.println( result );
}

}

This file was deleted.

This file was deleted.

0 comments on commit 66833fc

Please sign in to comment.