[Extension] BetterArrays - 60 useful array blocks!

Checks

The Checks category (new category) contains blocks that return true or false based on a condition inside of an array. BetterArrays adds 7 blocks to it.

Equal

The equal method returns true if two arrays are equal (regular javascript equality operation does not work on arrays, for some reason)

function equal(first: any[], second: any[]): boolean

image

Examples

image

This example does not return true, because javascript.

image

This example returns true (as it should)

In Range

The inRange method returns true if index is in range (between 0 and array length) of array.

function inRange(array: any[], index: number): boolean

image

Examples

image

This example returns true (0 < 1 < 2 is true)

image

This example returns false (0 < 2 < 2 is false)

Exceptions

NON_INTEGER_VALUE if index is not an integer

Includes

The includes method returns true if item is included in an array.

function includes(array: any[], item: any): boolean

image

Examples

image

This example returns true

image

This example returns false

Is Empty

The isEmpty method returns true if an array is empty (length is 0).

function isEmpty(array: any[]): boolean

image

Examples

image

This example returns true

image

This example returns false

All True

The allTrue method returns true if all elements in an array evaluate to true.

function allTrue(array: any[]): boolean

image

Examples

image

This example returns true

image

This example returns false (0 evaluates to false)

Any True

The anyTrue method returns true if any element in an array evaluates to true.

function anyTrue(array: any[]): boolean

image

This example returns true (only true evaluates to true, but that’s enough to return true)

image

This example returns false (all elements evaluates to false)

All Equal

The allEqual method returns true if every element in an array is equal to item.

function allEqual(array: any[], item: any): boolean

image

Examples

image

This example returns true

image

This example returns false

Mutual operations

The Mutual Operations category (new category) contains blocks that perform operations comparing elements of two arrays and return the result. BetterArrays adds 3 blocks to it.

To Union

The toUnion method returns a union (all elements from both arrays together, no duplicates) of two arrays. This method does not modify the first array.

function toUnion(first: any[], second: any[]): any[]

image

Examples

image

This example returns ["foo", "bar", "baz"]

To Intersection

The toIntersection method returns an intersection (only elements that appear in both arrays, no duplicates) of two arrays. This method does not modify the first array.

function toIntersection(first: any[], second: any[]): any[]

image

Examples

image

This example returns ["baz"]

To Difference

The toDifference method returns a difference (elements that appear in the first array, without the items from the second array) of two arrays. This method does not modify the first array.

function toDifference(first: any[], second: any[]): any[]

image

Examples

image

This example returns ["foo"]


Exceptions

As you can gather from the blocks chapter (see above), some blocks have an exceptions category. This category describes which exceptions a block can throw, and under which condition that can occur.

BetterArrays throws exceptions for illegal values to make debugging easier and prevent unexpected behaviour. Below is an explanation of each of those exceptions.

NON_INTEGER_VALUE

This exception is thrown if a given value is not an integer (whole number)
Certain values (mostly indicies) cannot be decimal values

Value must be integer (not [value])

NEGATIVE_VALUE

This exception is thrown if a given value is negative
Certain values (mostly indicies) cannot be lower than 0

Value must not be negative (not [value])

OUT_OF_RANGE

This exception is thrown if a given value is not within an array range
Values used for accessing values within an array must not be outside of array bounds

Index ([value]) must be in list range (0, [array length; excluded])

ZERO_STEP

This exception is thrown if a step value is equal to 0
Stepping values cannot be 0, as it would cause an infinite loop

Stepping value cannot be 0

INVALID_RANGE

This exception is thrown if an end value is lower than a start value
Start values must be lower than end values in order to create a valid range

Start value ([start]) must be lower than end value ([end])

EMPTY_ARRAY

This exception is thrown if a given array is empty
Certain methods require arrays containing at least one element

Operation cannot be performed on empty array

NOT_ARRAY

This exception is thrown if a given value type is not array
Certain methods expect elements to be an array type

Expected array type (not [typeof value])

INVALID_SHIFT

This exception is thrown if a shift value is bigger than an array length
The shift and toShifted methods can throw this exception

Shift value ([shift]) cannot be bigger than array length ([length of array])


Hardware Support

@danger_kitty pointed out that this extension (because it’s so huge) could take up too much device storage, making this extension unusable for projects that are intended for hardware ports.

After testing this theory, it was discovered that adding this extension to an empty project increases the compiled file size by a whopping 0.4%! Therefore, we can conclude that this extension can be used for hardware games as well.


And that’s the BetterArrays extension! I’ve sank a lot of my time making this extension, both writing code, and this post. If you find this useful, smash like and star the repo, would appreciate. If you found a mistake in this post, or unexpected behaviour in the extension, please let me know. Also be sure to submit any feature requests/ideas, as I’m open to adding new stuff!

Import the extension here

Now, I’m going to take a very long nap. Peace :v:

16 Likes