0
Help us understand the problem. What are the problem?

posted at

Content-Dispositionのデコードに見るリーダブルコードとは

Content-Dispositionのデコード例

なにやってるのかわからんコード

let headerLine = axiosResponse.headers['Content-Disposition'] || axiosResponse.headers['content-disposition'];
const fnameArr = headerLine.split(';');
let filename = fnameArr[fnameArr.length - 1].split('=')[1].replace('"', '').replace('"', '');
if (filename.indexOf(utf8Name) !== -1) {
    filename = decodeURI(filename.replace(utf8Name, ''));
  }
return filename

リーダブルなコード

var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(res.headers["content-disposition"]);
    if (matches != null && matches[1]) {
      fileName = matches[1].replace(/['"]/g, '');
    }
return filename

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?