Skip to content

Commit

Permalink
feat(command): Add new split command for strings (#258)
Browse files Browse the repository at this point in the history
* feat(command): Add new split command for strings

* fix(command): Add split command in index

* fix(command): Split command: Missing typeof causing arguments to not be seen as strings

---------

Co-authored-by: Liana <[email protected]>
  • Loading branch information
AlisonSelby and liana-p committed Apr 10, 2024
1 parent 511eae3 commit fa1a823
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/commands/all-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ do_things element:
| -------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [concat](string-commands/concat) | `concat "Hello" "World"` | Concatenates two or more strings |
| [join](string-commands/join) | `join ", " "Hello" "World"` | Joins x strings, with the first character being the join string between them |
| [split](string-commands/split) | `split " " "Hello World"` | Splits a string into an array by the splitting character |
| str_search | `var result (str_search "Hello world" "world")` | Searches for a substring in a string and returns the index of the first occurrence. Returns -1 if not found. |
| regex_search | `var result (regex_search "Hello world" "world")` | Searches for a regex pattern in a string and returns the index of the first occurrence. Returns -1 if not found. |

Expand Down
13 changes: 13 additions & 0 deletions docs/commands/string-commands/split.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Split

The `split` command splits strings into arrays:

Syntax: `split [splitter] [string]`

Example:

```narrat
main:
var arr (split " " "Hello world") // arr = ["Hello", "world"]
var arr1 (split "" "Hello world") // arr1 = [ "H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
```
2 changes: 2 additions & 0 deletions packages/narrat/src/vm/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import {
randomIntPlugin,
} from './random-commands';
import {
stringSplitPlugin,
regexSearchPlugin,
stringConcatPlugin,
stringJoinPlugin,
Expand Down Expand Up @@ -309,6 +310,7 @@ export function registerBaseCommands(vm: VM) {
// Strings
vm.addCommand(stringConcatPlugin);
vm.addCommand(stringJoinPlugin);
vm.addCommand(stringSplitPlugin);
vm.addCommand(stringSearchPlugin);
vm.addCommand(regexSearchPlugin);
// Maths
Expand Down
17 changes: 17 additions & 0 deletions packages/narrat/src/vm/commands/string-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ export const stringJoinPlugin = new CommandPlugin<{ a: number; b: number }>(
},
);

export const stringSplitPlugin = new CommandPlugin<{ a: number; b: number }>(
'split',
'any',
async (cmd) => {
if (cmd.args.length < 2) {
commandRuntimeError(cmd, `requires at least 2 arguments`);
}
const splitter = cmd.args[0];
const stringToSplit = cmd.args[1];
if (typeof splitter === 'string' && typeof stringToSplit === 'string') {
return stringToSplit.split(splitter);
} else {
commandRuntimeError(cmd, `requires all arguments to be strings`);
}
},
);

export const stringSearchPlugin = CommandPlugin.FromOptions<{
str: string;
matcher: string;
Expand Down

0 comments on commit fa1a823

Please sign in to comment.