String.prototype.split with undefined separator and limit 0

Steven Levithan steves_list at hotmail.com
Fri Jul 27 09:01:38 PDT 2012


[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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120727/c082d2be/attachment.html>


More information about the es-discuss mailing list