"use strict";

module.exports = function vary(res, field) {
  if (!res || !res.getHeader || !res.setHeader) return;
  var value = res.getHeader("Vary") || "";
  var fields = Array.isArray(field) ? field : String(field || "").split(/ *, */);
  var existing = String(value).split(/ *, */).filter(Boolean);
  fields.forEach(function add(item) {
    if (item && existing.map(function lower(x) { return x.toLowerCase(); }).indexOf(item.toLowerCase()) === -1) {
      existing.push(item);
    }
  });
  if (existing.length) res.setHeader("Vary", existing.join(", "));
};
