Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add startsWith to String builtins #514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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