Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
New has() method that replaces arr.indexOf where applicable
Browse files Browse the repository at this point in the history
Left out the occasions where indexOf was checking a particular index (instead of just checking wether an index exists or not)
  • Loading branch information
Coriou committed May 22, 2019
1 parent e968072 commit 5ec1f87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ exports.formatDuration = function (seconds) {
* @param {String} str
* @return {Boolean}
*/
exports.isString = str => typeof str === "string";
exports.isString = str => typeof str === "string";

/**
* Checks arr contains value
*
* @param {Array} arr
* @param {string|number} arr
* @return {Boolean}
*/
exports.has = (arr, value) => arr && arr.indexOf(value) > -1;
8 changes: 4 additions & 4 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function youtubeDl(urls, args, options, callback) {
return call(video, args1, cleanupOpt, options, callback);
}

if (isDebug.test(stderr) && args.indexOf('--verbose') > -1) {
if (isDebug.test(stderr) && util.has(args, "--verbose")) {
console.log('\n' + stderr);
} else if (isWarning.test(stderr)) {
console.warn(stderr);
Expand Down Expand Up @@ -308,7 +308,7 @@ ytdl.getInfo = function getInfo(url, args, options, callback) {
args = [];
}
const defaultArgs = ['--dump-json'];
if (!args || args.indexOf('-f') < 0 && args.indexOf('--format') < 0 &&
if (!args || !util.has(args, "-f") && !util.has(args, "--format") &&
args.every(function (a) {
return a.indexOf('--format=') !== 0;
})) {
Expand All @@ -322,7 +322,7 @@ ytdl.getInfo = function getInfo(url, args, options, callback) {

// If using the "-g" or "--get-url" flag youtube-dl will return just a string (the URL to the video) which messes up the parsing
// This fixes this behaviour
if (args && (args.indexOf("-g") > -1 || args.indexOf("--get-url") > -1))
if (util.has(args, "-g") || util.has(args, "--get-url"))
if (Array.isArray(data) && data.length >= 2) data.splice(0, 1);


Expand Down Expand Up @@ -414,7 +414,7 @@ ytdl.getThumbs = function getThumbs(url, options, callback) {
for (let i = 0, len = data.length; i < len; i++) {
const line = data[i];
const info = 'Writing thumbnail to: ';
if (line.indexOf(info) !== -1) {
if (util.has(line, info)) {
files.push(line.slice(line.indexOf(info) + info.length));
}
}
Expand Down

0 comments on commit 5ec1f87

Please sign in to comment.