Skip to content

Commit 5d75124

Browse files
committed
Polyfill for Array.filter; MERGE with different line separators
1 parent 969e5d3 commit 5d75124

4 files changed

Lines changed: 38 additions & 19 deletions

File tree

Controller.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -561,26 +561,31 @@ Controller.prototype = {
561561
}
562562
},
563563

564+
// We simulate input.split(/^(\s*\d+)/m) here since it does not work with IE8
565+
// (see also: https://blog.stevenlevithan.com/archives/cross-browser-split )
566+
// This function also works for different line separators (IE8 textArea stores "\r\n" as line separator)
564567
splitLines: function (input) {
565-
var lines = [],
566-
lineParts = input.split(/^(\s*\d+)/m), // get numbers starting at the beginning of a line
567-
i, number, content;
568-
569-
if (lineParts[0] === "") {
570-
lineParts.shift(); // remove first empty item
571-
}
572-
573-
for (i = 0; i < lineParts.length; i += 2) {
574-
number = lineParts[i];
575-
content = lineParts[i + 1];
576-
577-
if (content.endsWith("\n")) {
578-
content = content.substring(0, content.length - 1);
568+
var reFormat = new RegExp("\\r?\\n(\\d+)", "g"),
569+
aOutput = [],
570+
aMatch, iLastIndex, iLastLastIndex;
571+
572+
input = "\n" + input;
573+
while ((aMatch = reFormat.exec(input)) !== null) {
574+
iLastIndex = aMatch.index + aMatch[0].length; // separator.lastIndex is not reliable cross-browser
575+
if (iLastIndex > iLastLastIndex) {
576+
aOutput[aOutput.length - 1] += input.slice(iLastLastIndex, aMatch.index);
577+
}
578+
aOutput.push(aMatch[1]);
579+
iLastLastIndex = iLastIndex;
580+
}
581+
if (iLastLastIndex < input.length) {
582+
if (aOutput.length) {
583+
aOutput[aOutput.length - 1] += input.slice(iLastLastIndex);
584+
} else {
585+
aOutput.push(input.slice(iLastLastIndex));
579586
}
580-
lines.push(number + content);
581587
}
582-
583-
return lines;
588+
return aOutput;
584589
},
585590

586591
// merge two scripts with sorted line numbers, lines from script2 overwrite lines from script1

Polyfills.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ if ((typeof globalThis !== "undefined") && !globalThis.window) { // nodeJS
4242
globalThis.window = {};
4343
}
4444

45+
if (!Array.prototype.filter) { // IE8
46+
Array.prototype.filter = function (callbackFn) { // eslint-disable-line no-extend-native
47+
var arr = [],
48+
i;
49+
50+
for (i = 0; i < this.length; i += 1) {
51+
if (callbackFn.call(this, this[i], i, this)) {
52+
arr.push(this[i]);
53+
}
54+
}
55+
return arr;
56+
};
57+
}
58+
4559
if (!Array.prototype.indexOf) { // IE8
4660
Array.prototype.indexOf = function (element, iFrom) { // eslint-disable-line no-extend-native
4761
var iLen = this.length >>> 0; // eslint-disable-line no-bitwise

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<link rel="stylesheet" href="cpcbasic.css" />
7-
<title id="title">CPC Basic v0.10.13</title>
7+
<title id="title">CPC Basic v0.10.14</title>
88
</head>
99

1010
<body id="pageBody">

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cpcbasic",
3-
"version": "0.10.13",
3+
"version": "0.10.14",
44
"description": "# CPCBasic - Run CPC BASIC in a Browser",
55
"main": "cpcbasic.js",
66
"directories": {

0 commit comments

Comments
 (0)