MCPcopy Create free account
hub / github.com/dop251/goja / stringproto_split

Method stringproto_split

builtin_string.go:815–865  ·  view source on GitHub ↗
(call FunctionCall)

Source from the content-addressed store, hash-verified

813}
814
815func (r *Runtime) stringproto_split(call FunctionCall) Value {
816 r.checkObjectCoercible(call.This)
817 separatorValue := call.Argument(0)
818 limitValue := call.Argument(1)
819 if _, ok := separatorValue.(*Object); ok {
820 if splitter := toMethod(r.getV(separatorValue, SymSplit)); splitter != nil {
821 return splitter(FunctionCall{
822 This: separatorValue,
823 Arguments: []Value{call.This, limitValue},
824 })
825 }
826 }
827 s := call.This.toString()
828
829 limit := -1
830 if limitValue != _undefined {
831 limit = int(toUint32(limitValue))
832 }
833
834 separatorValue = separatorValue.ToString()
835
836 if limit == 0 {
837 return r.newArrayValues(nil)
838 }
839
840 if separatorValue == _undefined {
841 return r.newArrayValues([]Value{s})
842 }
843
844 separator := separatorValue.String()
845
846 str := s.String()
847 splitLimit := limit
848 if limit > 0 {
849 splitLimit = limit + 1
850 }
851
852 // TODO handle invalid UTF-16
853 split := strings.SplitN(str, separator, splitLimit)
854
855 if limit > 0 && len(split) > limit {
856 split = split[:limit]
857 }
858
859 valueArray := make([]Value, len(split))
860 for index, value := range split {
861 valueArray[index] = newStringValue(value)
862 }
863
864 return r.newArrayValues(valueArray)
865}
866
867func (r *Runtime) stringproto_startsWith(call FunctionCall) Value {
868 r.checkObjectCoercible(call.This)

Callers

nothing calls this directly

Calls 10

checkObjectCoercibleMethod · 0.95
getVMethod · 0.95
newArrayValuesMethod · 0.95
toMethodFunction · 0.85
toUint32Function · 0.85
newStringValueFunction · 0.85
toStringMethod · 0.65
ToStringMethod · 0.65
StringMethod · 0.65
ArgumentMethod · 0.45

Tested by

no test coverage detected