PageSourceSearch

https://static.random.org/lib/split-1.1.1/split.js

js random.org collected 2026-09-24 06:43:12 UTC 4,720 bytes, 116 lines download raw bytes

1/*!
2 * Cross-Browser Split 1.1.1
3 * Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
4 * Available under the MIT License
5 * ECMAScript compliant, uniform cross-browser split method
6 */
7
8/**
9 * Splits a string into an array of strings using a regex or string separator. Matches of the
10 * separator are not included in the result array. However, if `separator` is a regex that contains
11 * capturing groups, backreferences are spliced into the result each time `separator` is matched.
12 * Fixes browser bugs compared to the native `String.prototype.split` and can be used reliably
13 * cross-browser.
14 * @param {String} str String to split.
15 * @param {RegExp|String} separator Regex or string to use for separating the string.
16 * @param {Number} [limit] Maximum number of items to include in the result array.
17 * @returns {Array} Array of substrings.
18 * @example
19 *
20 * // Basic use
21 * split('a b c d', ' ');
22 * // -> ['a', 'b', 'c', 'd']
23 *
24 * // With limit
25 * split('a b c d', ' ', 2);
26 * // -> ['a', 'b']
27 *
28 * // Backreferences in result array
29 * split('..word1 word2..', /([a-z]+)(\d+)/i);
30 * // -> ['..', 'word', '1', ' ', 'word', '2', '..']
31 */
32var split;
33
34// Avoid running twice; that would break the `nativeSplit` reference
35split = split || function (undef) {
36
37    var nativeSplit = String.prototype.split,
38        compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group
39        self;
40
41    self = function (str, separator, limit) {
42        // If `separator` is not a regex, use `nativeSplit`
43        if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
44            return nativeSplit.call(str, separator, limit);
45        }
46        var output = [],
47            flags = (separator.ignoreCase ? "i" : "") +
48                    (separator.multiline  ? "m" : "") +
49                    (separator.extended   ? "x" : "") + // Proposed for ES6
50                    (separator.sticky     ? "y" : ""), // Firefox 3+
51            lastLastIndex = 0,
52            // Make `global` and avoid `lastIndex` issues by working with a copy
53            separator = new RegExp(separator.source, flags + "g"),
54            separator2, match, lastIndex, lastLength;
55        str += ""; // Type-convert
56        if (!compliantExecNpcg) {
57            // Doesn't need flags gy, but they don't hurt
58            separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
59        }
60        /* Values for `limit`, per the spec:
61         * If undefined: 4294967295 // Math.pow(2, 32) - 1
62         * If 0, Infinity, or NaN: 0
63         * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
64         * If negative number: 4294967296 - Math.floor(Math.abs(limit))
65         * If other: Type-convert, then use the above rules
66         */
67        limit = limit === undef ?
68            -1 >>> 0 : // Math.pow(2, 32) - 1
69            limit >>> 0; // ToUint32(limit)
70        while (match = separator.exec(str)) {
71            // `separator.lastIndex` is not reliable cross-browser
72            lastIndex = match.index + match[0].length;
73            if (lastIndex > lastLastIndex) {
74                output.push(str.slice(lastLastIndex, match.index));
75                // Fix browsers whose `exec` methods don't consistently return `undefined` for
76                // nonparticipating capturing groups
77                if (!compliantExecNpcg && match.length > 1) {
78                    match[0].replace(separator2, function () {
79                        for (var i = 1; i < arguments.length - 2; i++) {
80                            if (arguments[i] === undef) {
81                                match[i] = undef;
82                            }
83                        }
84                    });
85                }
86                if (match.length > 1 && match.index < str.length) {
87                    Array.prototype.push.apply(output, match.slice(1));
88                }
89                lastLength = match[0].length;
90                lastLastIndex = lastIndex;
91                if (output.length >= limit) {
92                    break;
93                }
94            }
95            if (separator.lastIndex === match.index) {
96                separator.lastIndex++; // Avoid an infinite loop
97            }
98        }
99        if (lastLastIndex === str.length) {
100            if (lastLength || !separator.test("")) {
101                output.push("");
102            }
103        } else {
104            output.push(str.slice(lastLastIndex));
105        }
106        return output.length > limit ? output.slice(0, limit) : output;
107    };
108
109    // For convenience
110    String.prototype.split = function (separator, limit) {
111        return self(this, separator, limit);
112    };
113
114    return self;
115
116}();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.