Skip to content

Commit

Permalink
special case empty separators in string split
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jan 17, 2024
1 parent 6b0f915 commit 9a585ca
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions crates/dash_vm/src/js_std/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,17 @@ pub fn split(cx: CallContext) -> Result<Value, Value> {
.res(cx.scope)
.to_owned();

let result = string
.split(&separator)
.map(|s| PropertyValue::static_default(Value::String(cx.scope.intern(s).into())))
.collect();
let result = if separator.is_empty() {
string
.chars()
.map(|c| PropertyValue::static_default(Value::String(cx.scope.intern_char(c).into())))
.collect()
} else {
string
.split(&separator)
.map(|s| PropertyValue::static_default(Value::String(cx.scope.intern(s).into())))
.collect()
};

let array = Array::from_vec(cx.scope, result);
Ok(cx.scope.register(array).into())
Expand Down

0 comments on commit 9a585ca

Please sign in to comment.