String.prototype.split with undefined separator and limit 0

# Steven Levithan (13 years ago)

[Sorry if this is a duplicate. I tried sending it yesterday but it didn’t seem to go through.]

The following paragraph appears in ES 5.1 S15.5.4.14:

“If separator is undefined, then the result array contains just one String, which is the this value (converted to a String). If limit is not undefined, then the output array is truncated so that it contains no more than limit elements.”

This paragraph remains verbatim in the latest ES 6 draft (Rev 9).

This is ambiguous about whether the limit argument applies when separator is undefined. The algorithm steps that follow, by my reading, specify that limit should not affect the result if separator is undefined. In other words, 'undefined'.split(undefined, 0) should return ['undefined'], not []. Was this special case for the limit argument intentional? Every browser I’ve tested, with the exception of Chrome, gets something wrong about the handling of undefined as the separator. Test results follow:

Chrome 20, 22: 'undefined'.split(); // –> ['undefined'] 'undefined'.split(undefined); // –> ['undefined'] 'undefined'.split(undefined, 0); // –> ['undefined']

IE 9 (IE 9 standards mode): 'undefined'.split(); // –> ['undefined'] 'undefined'.split(undefined); // –> ['', ''] 'undefined'.split(undefined, 0); // –> []

Firefox 14.0.1, Opera 12, and Safari 5.1.7: 'undefined'.split(); // –> ['undefined'] 'undefined'.split(undefined); // –> ['undefined'] 'undefined'.split(undefined, 0); // –> []

Safari 5.1.2: 'undefined'.split(); // –> Didn't test 'undefined'.split(undefined); // –> ['', ''] 'undefined'.split(undefined, 0); // –> []

-- Steven Levithan

# Rick Waldron (13 years ago)

On Fri, Jul 27, 2012 at 9:01 AM, Steven Levithan <steves_list at hotmail.com>wrote:

snip

This paragraph remains verbatim in the latest ES 6 draft (Rev 9).

This is ambiguous about whether the limit argument applies when separator is undefined. The algorithm steps that follow, by my reading, specify that limit should not affect the result if separator is undefined. In other words, 'undefined'.split(undefined, 0) should return ['undefined'], not []. Was this special case for the limit argument intentional? Every browser I’ve tested, with the exception of Chrome, gets something wrong about the handling of undefined as the separator. Test results follow:

snip

Would you mind filing a spec bug for this? bugs.ecmascript.org/enter_bug.cgi