Logo
  • Features
  • Educators
  • What's New
  • Blog
  • Showcase
  • About us
  • Contact
Try it now!
4D Pocket
4D Pocket

4D Pocket

What is the 4D Pocket

4D Pocket (四次元ポケット) is a 22nd-century gadget from the manga series Doraemon that has a four-dimensional space containing millions of gadgets.

In Gandi IDE, the 4D Pocket extension contains a set of utils to make your coding experience easier.

Best Practices

[ will be attached very soon ]

Modules

The latest version contains five modules:

Module
Description
💛 General Utils
- Simply return a string - Get parts of a URL - Generate a random ID
⭕️ Regex Utils
Brings regular expressions into Scratch: -Does a string match a regex rule? -Returns matches of a regex for a string -Splits a string by a regex
📑 Array Utils
Utils for dealing with JSON Arrays: - Find the differences between two arrays - Find an object with a specific key in an array - Find the intersection of two arrays - Remove duplicate items in an array - Concat arrays - Sort items in an array and sort the objects in an array by a specific key
📐 Math Utils
Simple useful snippets
📈 Event Utils
Broadcast and listen to an event with customizable data

Functions and Definitions

💛 General Utils

This module will include syntactic sugar and useful code snippets for Scratch.

Return a string

Get a static string.

image

It is the same as this:

image

In 4D Pocket, it can be used to match a regex (as will be explained in the next module).

Get a part of the current URL

image

Take the URL `https://cocrea.world/@wangjiaming/PicosworldMMO?kontakt=ZGF0YQ%3D%3D#main` as an example:

Part
Description
Example Value
href
the full location of the URL
https://cocrea.world/@wangjiaming/PicosworldMMO?kontakt=ZGF0YQ%3D%3D#main
host
the host of the URL
cocrea.world
pathname
the path of the URL
/@wangjiaming/PicosworldMMO
search
the search part of the URL, starting from the symbol ‘?’
?kontakt=ZGF0YQ%3d%3d
hash
the hash of the URL, starting from the hash symbol ‘#’
#main

Q: What is this block for?

A: You can make a game with, for example, a god mode, which can only be enabled by a special URL `https://cocrea.world/@yourname/yourproject?godmode=1 `. Your code can be like this:

image

Generate a random string

Sometimes you want to generate a random string to store user configurations, to use as an archive code, or simply to represent a user. In that case, this block is for you:

image

The random ID (a string) contains a-z and 0-9, and is case-sensitive.

⭕️ Regex Utils

Regular expressions are very useful technologies for string-searching, matching, and find and replace.

4D Pocket brings regex into Scratch with many useful patterns.

String Match

If a given string matches with a specific regex, it will return true.

image

The built-in regex rules are shown in the following:

Rule
Description
Original Pattern
Example true case
Example false case
is digital
the string must be composed only of the digits 0-9. The period symbol is not included.
/^\d+$/gm
134 0 000 14132124
a123 12.0 1,000
has a digit
the string contains at least one digit
/\d/gm
a1b abc0efg
abcd
has letter
the string contains at least one letter
/[a-zA-Z]/gm
a1 aZ 1AZ
123 1,000
start with a digit
the string starts with a digit
/^\d/gm
0123 0abc 1z 1
abc a1
end with a digit
the string ends with a digit
/\d$/gm
abc0 a1 2
abc 1a
contains Chinese
the string contains at least one Chinese character
/[\u4e00-\u9fa5]/gm
中文 a中文 中文z
123 abc
an email address
the string is an email address
/^([\w+-]+(?:\.[\w+-]+))@((?:[\w-]+\.)\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/gm
a@b.com
a.com abc @b.com

For advanced developers, if you are familiar with regex, you can combine two blocks to use customized regex, as shown below.

image

For example, the blocks below will match a valid US phone number:

image

Sample valid strings:

1237774820
(281)123-0380
(979) 123-0970
(281)123-2470
281-123-2452

Matches of a regex in a string

It will return a JSON array of the matches of a rule in a string.

image

For example, the block above returns this value:

["Gandi","is","an","in-browser","IDE","for","creating","and",
"running","Scratch","projects","Es","ist","großartig"]

The built-in regex patterns are shown below:

Rule
Description
Example input
Example output
digits
only match digits
ab12b4c64def
["12","4","64"]
numbers
numbers, including +- symbols and dot
ab12b4.123c+64de-1.12f
["12","4.123","+64","-1.12"]
english and german words
match words, not letters
Schön means beautiful in German
["Schön","means","beautiful","in","German"]
within two single quotation marks
the parts of the string between ''
'a' and 'b' in 'c', and 'd’
["a","b","c","d"]

Split a string

Regex also can be used to split a string by a specific rule.

image

The block above returns a value like this, and you can keep or remove empty strings.

Keep

["1","2","3","","a","b","c","","e"]

Remove

["1","2","3","a","b","c","e"]

Built-in regex patterns include splitting a string by spaces, commas, periods, colons, semicolons, vertical bars |, and digits.

📑 Array Utils

Find Differences

Return the differences between two arrays.

image

In this case, the block returns ["a","d"].

Find by a k-v pair object in an array

image

In this case, the input JSON contains three objects, as shown on the left, and the block finds the object whose key "name" equals the value "Shawn”, as shown on the right.

Input

[{"name":"Shawn", "score":80}, 
 {"name":"Arkos", "score":100}, 
 {"name":"yk1boy", "score":90}]

Output


{"name":"Shawn","score":80}
⚠️
NOTICE: When two objects match the k-v pair, the block only returns the first one.

Find the intersection

image

When you run the code above, the block returns ["b","c"].

Remove duplicate items

image

Example output: ["a","b","c","d",1,2,3]

Concat two arrays

image

You can choose either just concat or uniquify.

Uniquify

will remove duplicates

["a","b","c","d"]

Just concat

simply concatenate

["a","b","c","b","c","d"]

Sort items in an array

image

You can sort the items by ASC or DESC.

ASC

[1,3,200,"a","b","c","d"]

DESC

["d","c","b","a",200,3,1]

📐 Math Utils

Restrict a number within the range [min, max]

image

This block is a shortcut to the following code:

image

If the given number (120) is ≤ 0, return 0; if ≥ 100, return 100. In this case, return 100.

📈 Event Utils

Event-driven programming in Scratch is the most common way for sprites to communicate. But the problem is that Scratch doesn’t support passing a specific value from an event dispatcher to its listeners. Normally, scratchers have to use multiple global variables to pass data.

Now, you can use 4D events, as shown below:

Broadcast an event:

image

Listen to an event:

image

You can use eventName and eventData under the hat.

For example, you can listen to a 4D message like this:

image

And dispatch an event like this:

image

Change Logs

Version
What’s New
v0.3
init launched, added: - General Utils - Regex Utils - Array Utils - Math Utils - Event Utils Shawn at 03.July 2022

Credit

💡
If you have any questions or suggestions, you can find us in the discord channel: https://discord.gg/U3nyveCvRa
Logo

Designed by 2ndR with love

Discord