Skip to content

Commit

Permalink
Use map() method instead of for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed Feb 6, 2023
1 parent 1c7aaaf commit 169a9e1
Showing 1 changed file with 52 additions and 43 deletions.
95 changes: 52 additions & 43 deletions src/scriptlets/m3u-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,34 @@ export function m3uPrune(source, propsToRemove, urlToMatch) {
* Sets an item in array to undefined, if it contains one of the
* AD_MARKER: AD_MARKER.CUE, AD_MARKER.ASSET, AD_MARKER.SCTE35, AD_MARKER.CUE_IN
*
* @param {Array} lines
* @param {number} i
* @returns {Array}
* @param {string} line
* @param {number} index
* @param {Array} array
* @returns {string|undefined}
*/
const pruneSpliceoutBlock = (lines, i) => {
if (!lines[i]?.startsWith(AD_MARKER.CUE)) {
return lines;

const pruneSpliceoutBlock = (line, index, array) => {
if (!line.startsWith(AD_MARKER.CUE)) {
return line;
}
lines[i] = undefined;
i += 1;
if (lines[i]?.startsWith(AD_MARKER.ASSET)) {
lines[i] = undefined;
i += 1;
line = undefined;
index += 1;
if (array[index].startsWith(AD_MARKER.ASSET)) {
array[index] = undefined;
index += 1;
}
if (lines[i]?.startsWith(AD_MARKER.SCTE35)) {
lines[i] = undefined;
i += 1;
if (array[index].startsWith(AD_MARKER.SCTE35)) {
array[index] = undefined;
index += 1;
}
if (lines[i]?.startsWith(AD_MARKER.CUE_IN)) {
lines[i] = undefined;
i += 1;
if (array[index].startsWith(AD_MARKER.CUE_IN)) {
array[index] = undefined;
index += 1;
}
if (lines[i]?.startsWith(AD_MARKER.SCTE35)) {
lines[i] = undefined;
if (array[index].startsWith(AD_MARKER.SCTE35)) {
array[index] = undefined;
}
return lines;
return line;
};

const removeM3ULineRegexp = toRegExp(propsToRemove);
Expand All @@ -190,29 +192,31 @@ export function m3uPrune(source, propsToRemove, urlToMatch) {
* Sets an item in array to undefined, if it contains removeM3ULineRegexp and one of the
* AD_MARKER: AD_MARKER.EXTINF, AD_MARKER.DISCONTINUITY
*
* @param {Array} lines
* @param {number} i
* @returns {Array}
* @param {string} line
* @param {number} index
* @param {Array} array
* @returns {string|undefined}
*/
const pruneInfBlock = (lines, i) => {
if (!lines[i]?.startsWith(AD_MARKER.EXTINF)) {
return lines;

const pruneInfBlock = (line, index, array) => {
if (!line.startsWith(AD_MARKER.EXTINF)) {
return line;
}
if (!removeM3ULineRegexp.test(lines[i + 1])) {
return lines;
if (!removeM3ULineRegexp.test(array[index + 1])) {
return line;
}
if (!isAllowedTag(lines[i])) {
lines[i] = undefined;
if (!isAllowedTag(array[index])) {
array[index] = undefined;
}
i += 1;
if (!isAllowedTag(lines[i])) {
lines[i] = undefined;
index += 1;
if (!isAllowedTag(array[index])) {
array[index] = undefined;
}
i += 1;
if (lines[i]?.startsWith(AD_MARKER.DISCONTINUITY)) {
lines[i] = undefined;
index += 1;
if (array[index].startsWith(AD_MARKER.DISCONTINUITY)) {
array[index] = undefined;
}
return lines;
return line;
};

/**
Expand Down Expand Up @@ -281,16 +285,21 @@ export function m3uPrune(source, propsToRemove, urlToMatch) {

if (text.includes(COMCAST_AD_MARKER.VMAP_AD_BREAK)) {
lines = pruneVmapBlock(lines);
return lines.filter((l) => l !== undefined).join('\n');
return lines.filter((l) => !!l).join('\n');
}

lines = pruneSegments(lines);
for (let i = 0; i < lines.length; i += 1) {
lines = pruneSpliceoutBlock(lines, i);
lines = pruneInfBlock(lines, i);
}

return lines.filter((l) => l !== undefined).join('\n');
return lines.map((line, index, array) => {
if (typeof line === 'undefined') {
return line;
}
line = pruneSpliceoutBlock(line, index, array);
if (typeof line !== 'undefined') {
line = pruneInfBlock(line, index, array);
}
return line;
}).filter((l) => !!l).join('\n');
};

const xhrWrapper = (target, thisArg, args) => {
Expand Down

0 comments on commit 169a9e1

Please sign in to comment.