Skip to content

Commit

Permalink
Fixed #43 - Implemented roString methods startsWith() and `endsWi…
Browse files Browse the repository at this point in the history
…th()` (#44)

* Fixed # 43 - Implemented `roString` methods `startsWith()` and `endsWith()`

* Fixed test code formatting
  • Loading branch information
lvcabral authored Feb 7, 2024
1 parent b5f9d5c commit 91af226
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/brsTypes/components/RoString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class RoString extends BrsComponent implements BrsValue, Comparable, Unbo
this.decodeUri,
this.encodeUriComponent,
this.decodeUriComponent,
this.startsWith,
this.endsWith,
this.isEmpty,
],
ifToStr: [this.toStr],
Expand Down Expand Up @@ -478,6 +480,44 @@ export class RoString extends BrsComponent implements BrsValue, Comparable, Unbo
},
});

/** Checks whether the string starts with the substring specified in matchString, starting at the matchPos parameter (0-based character offset). */
private startsWith = new Callable("startsWith", {
signature: {
args: [
new StdlibArgument("matchString", ValueKind.String),
new StdlibArgument("position", ValueKind.Int32, BrsInvalid.Instance),
],
returns: ValueKind.Boolean,
},
impl: (_: Interpreter, matchString: BrsString, position: Int32 | BrsInvalid) => {
if (position instanceof BrsInvalid) {
return BrsBoolean.from(this.intrinsic.value.startsWith(matchString.value));
}
return BrsBoolean.from(
this.intrinsic.value.startsWith(matchString.value, position.getValue())
);
},
});

/** Checks whether the string ends with the substring specified in matchString, starting at the position specified in the length parameter. */
private endsWith = new Callable("endsWith", {
signature: {
args: [
new StdlibArgument("matchString", ValueKind.String),
new StdlibArgument("position", ValueKind.Int32, BrsInvalid.Instance),
],
returns: ValueKind.Boolean,
},
impl: (_: Interpreter, matchString: BrsString, position: Int32 | BrsInvalid) => {
if (position instanceof BrsInvalid) {
return BrsBoolean.from(this.intrinsic.value.endsWith(matchString.value));
}
return BrsBoolean.from(
this.intrinsic.value.endsWith(matchString.value, position.getValue())
);
},
});

private toStr = new Callable("toStr", {
signature: {
args: [],
Expand Down
35 changes: 35 additions & 0 deletions test/brsTypes/components/RoString.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,41 @@ describe("RoString", () => {
});
});

describe("startsWith and endsWith", () => {
let s;
beforeEach(() => {
s = new RoString(new BrsString("Hello, World!"));
});

it("startsWith", () => {
let startsWith = s.getMethod("startsWith");
expect(startsWith).toBeInstanceOf(Callable);
expect(startsWith.call(interpreter, new BrsString("Hello"))).toEqual(
BrsBoolean.True
);
expect(startsWith.call(interpreter, new BrsString("World"), new Int32(7))).toEqual(
BrsBoolean.True
);
expect(
startsWith.call(interpreter, new BrsString("Universe"), new Int32(0))
).toEqual(BrsBoolean.False);
});

it("endsWith", () => {
let endsWith = s.getMethod("endsWith");
expect(endsWith).toBeInstanceOf(Callable);
expect(endsWith.call(interpreter, new BrsString("World!"))).toEqual(
BrsBoolean.True
);
expect(endsWith.call(interpreter, new BrsString("Hello"), new Int32(5))).toEqual(
BrsBoolean.True
);
expect(
endsWith.call(interpreter, new BrsString("Universe!"), new Int32(0))
).toEqual(BrsBoolean.False);
});
});

describe("isEmpty", () => {
it("check if empty string is empty", () => {
let s = new RoString(new BrsString(""));
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/BrsComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ describe("end to end brightscript functions", () => {
"🐶", // uri-encoded dog emoji, decoded
"true", // isEmpty for empty string
"false", // isEmpty for filled string
"true", // startsWith no position
"true", // startsWith with position
"true", // endsWith no position
"true", // endsWith with position
]);
});

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/resources/components/roString.brs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ sub main()
print "%F0%9F%90%B6".decodeUriComponent() ' => 🐶
print "".isEmpty() ' => true
print "<3".isEmpty() ' => false

print "1234567890".startsWith("123") ' => true
print "1234567890".endsWith("890") ' => true
print "1234567890".startsWith("567", 4) ' => true
print "1234567890".endsWith("567", 7) ' => true
end sub

0 comments on commit 91af226

Please sign in to comment.