Utility Extension 1.0

I’ve made an extension with useful utility blocks, there are blocks for things like JSON and text. To use it open the extensions menu, put in https://github.com/usertheusernamer/pxt-utilities inside the top textbox, and click on the extension.

Here is the currently available blocks in 1.0:

I’d like to add two new blocks that act like try and try-catch statements (Handles errors and prevents crashing), but it seems like try-catch statements aren’t supported. If you have any more block ideas, tell me and I’ll add them.

5 Likes

I have a utility idea! Long strings of if/else can get confusing, perhaps implementing something like the The ‘case’ function in swift that can have one output for each possible input without long lines of if/else.

2 Likes

Sounds like a great idea but after thinking a while, I don’t think it’s possible to implement that as an extension and wouldn’t it look like the same as if-else blocks? Maybe you are placing separate if-else blocks into other if-else blocks? You could just click the plus icon on an if statement and that expands into an if-else statement, another click and it turns into a if-elseif-else statement. It’s not really necessary to implement a switch-case block (also seems impossible due to extension limitations, not the code but how the block behaves like how to expand it) and a large if statement (No nests, just a single giant if-elseif-elseif-elseif-elseif-else block.) would basically be the same if I ever implemented a switch-case block. Sorry if my English is not that easy to read and understand but I like the idea.

1 Like

Could you implement some missing array methods? Like includes and concatenation? It would be really useful! Anyway, nice idea for an extension!

Are you sure? This source states they’re supported.

2 Likes

True, it would work almost exactly like a long if/elseif statement, but cases are different. One input is put up top and each case compares to that. So, instead of having something like ‘If x=0 then this, if x=1 then this, if x=2 then this’ you could have ‘case x: equals 0 then this, equals 1 then this, equals 2 then this’! I’m guessing it probably still can’t be done but thanks for explaining and considering!! : D

2 Likes

I’ll add those, nice idea!

Weird, I don’t think they work in extensions though. Like running the user’s code in a try-catch statement still produces an error and crashes.

Do you mean something like this?

Yes, I used scratchblocks, the quickest way I can create an example on mobile.

2 Likes

Could you provide an example or sample code that reproduces this error?

Anyways, I just remembered I already made a concatenation method in one of my extensions before!

Here’s both the code for try and try_catch blocks:

namespace utilities {
    //% block="try"
    //% blockId="ue_try"
    //% group='Error Handling'
    //% handlerStatement=true
    //% weight=7
    export function ue_try(run: () => void): void {
        try {
            run();
        } catch {}
    }

    //% block="try catch"
    //% blockId="ue_try_catch"
    //% group='Error Handling'
    //% handlerStatement=true
    //% weight=6
    export function ue_tryCatch(run: () => void, c: (errorName: string) => void): void {
        try {
            run();
        } catch (error) {
            c(error.name);
        }
    }
}

These are inside the utilities namespace in the extension, I’ve put a block in that causes an error and that did not work even when my code was like this:
error

Nice, I might add all these blocks for arrays into my extension:

  • include - <array [array] includes [any]?>
  • join - (join [array] with [text])
  • concat - (concat [array] and [array])
  • indexOf - (index of [any] in [array])
  • unshift - (unshift [any] into [array])
  • slice - (slice [array] with [number] items)
  • sort - (sort [array] by [ascending, descending v])
1 Like

Forgot to mention/reply @Sarge (So that he sees the notification)

1 Like

I’m making a List extension that will add a custom datatype: Lists, similar to those in Python.
The methods implemented at the moment are the following:

Item operations

  • get - get item at index
  • set - set item at index
  • delete - delete item at index
  • push - push item to the end of list
  • pop - remove and return item at index
  • insert - insert item at index
  • find - return index of item if found
  • remove - remove the first occurence of an item
  • swap - swap items at two indexes
  • replace - replace all occurences of item with value
  • random - return random item from list
  • count - count item repetitions in list

List operations

  • extend - append other list to the end of list
  • flatten - unpack all lists within the list recurively (depth=0 is infinite depth)
  • slice - return list items from start to end
  • isList - static method; return true if item is of list type
  • inRange - return if number is within length of the list
  • contains - return true if item is in list
  • compress - remove all undefined values
  • patch - fill all undefined values with static value
  • fill - fill with static value from start to end
  • zip - return pairs of elements from two lists (until smaller length is exceeded)
  • reverse - reverse the list
  • forEach - iterate over the list items with value and index
  • clear - remove all items from list
  • copy - return new instance with matching items
  • isEmpty - return true if list contains no elements
  • all - return true if all items in list are true
  • any - return true if any item in list is true
  • union - return items that appear in both lists
  • purge - remove all duplicates from list

Some of these are inspired by Python methods, some by JS methods and some are my own. These are not final, I might change or update them in the future. Yeahhh, I kind of spoiled my next project after the mini game jam, if anyone’s reading this.
I’m still planning to implement splice and some other missing methods.

At the moment, i’m working on porting all of the methods to blocks, so I can release it as an extension at last.

1 Like


That looks so cool!! Its a little more like this:

1 Like

That’s a lot! Pretty impressive, I’m sure a lot of people will use that. Are you gonna define all these blocks into a single main.ts file or are you gonna make them apart like maybe every category has it’s own file?

1 Like

The datatype (as a class) and its metods are in lists.ts, and the block interface functions are in main.ts

1 Like

Ah, I might have forgotten how a switch statement looked like. I might be able to add that to my extensions.

1 Like

thats so cool! thanks for helping!

1 Like