fix: Util.basename being unreliable (#2679)

* fix: Util.basename being unreliable

new URL for WHATWG parsing was not chosen because it only works for URLs and threw in local pathes, path.basename is unreliable (according to the devs' note), and path.parse seems to work well.

* docs: Update Util.basename's description
This commit is contained in:
Kyra
2018-08-10 11:23:22 +02:00
committed by Crawl
parent 2c8e15e31c
commit 0f63c50c06

View File

@@ -2,7 +2,7 @@ const { Colors, DefaultOptions, Endpoints } = require('./Constants');
const fetch = require('node-fetch');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
const { parse } = require('path');
/**
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
@@ -323,16 +323,15 @@ class Util {
}
/**
* Alternative to Node's `path.basename` that we have for some (probably stupid) reason.
* Alternative to Node's `path.basename`, removing query string after the extension if it exists.
* @param {string} path Path to get the basename of
* @param {string} [ext] File extension to remove
* @returns {string} Basename of the path
* @private
*/
static basename(path, ext) {
let f = splitPathRe.exec(path)[3];
if (ext && f.endsWith(ext)) f = f.slice(0, -ext.length);
return f;
let res = parse(path);
return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0];
}
/**