Skip to content

Commit

Permalink
feat: add string startsWith method (#514)
Browse files Browse the repository at this point in the history
Adds startsWith method to the String builtins.
  • Loading branch information
rorymalcolm authored Dec 19, 2023
1 parent 9f9bfb9 commit c902ab4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,17 @@ func builtinStringSubstr(call FunctionCall) Value {
return stringValue(string(target[start : start+length]))
}

func builtinStringStartsWith(call FunctionCall) Value {
checkObjectCoercible(call.runtime, call.This)
target := call.This.string()
search := call.Argument(0).string()
length := len(search)
if length > len(target) {
return boolValue(false)
}
return boolValue(target[:length] == search)
}

func builtinStringToLowerCase(call FunctionCall) Value {
checkObjectCoercible(call.runtime, call.This)
return stringValue(strings.ToLower(call.This.string()))
Expand Down
38 changes: 38 additions & 0 deletions inline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestGetOwnPropertyNames(t *testing.T) {
"substr",
"substring",
// "sup",
// "startsWith",
"startsWith",
"toString",
"trim",
// "trimStart",
Expand Down
9 changes: 9 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,12 @@ func TestString_localeCompare(t *testing.T) {
test(`'a'.localeCompare('a');`, 0)
})
}

func TestString_startsWith(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`'a'.startsWith('c');`, false)
test(`'aa'.startsWith('a');`, true)
})
}
2 changes: 2 additions & 0 deletions tools/gen-jscore/.gen-jscore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ types:
function: 2
- name: substring
function: 2
- name: startsWith
function: 1
- name: toString
function: -1
- name: trim
Expand Down

0 comments on commit c902ab4

Please sign in to comment.