all files / lib/ headers.es6

100% Statements 8/8
95.83% Branches 23/24
100% Functions 2/2
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                        24×                       13×     13×                 16×     16×     16×     16×                                                                                                                  
import * as _ from 'lodash';
 
/**
 @module headers
 */
 
const castString = (value) =>  _.isString(value) ? value : String(value);
 
const normalizeName = (name) => {
 
  const string = castString(name);
 
  if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(string))
    throw new TypeError('Invalid character in header field name');
 
  return string.toLowerCase();
 
};
 
class Headers {
 
  constructor(headers) {
 
    this.map = {};
 
    if (headers instanceof Headers)
      _.map(headers.map, (value, key) => this.append(key, value));
 
    if (_.isPlainObject(headers))
      _.map(headers, (value, key) => this.append(key, value));
 
  }
 
  append(name, value) {
 
    const normalizedName = normalizeName(name);
 
    Eif (!_.has(this.map, normalizedName))
      this.map[normalizedName] = [];
 
    if (_.isArray(value))
      this.map[normalizedName].push(..._.map(value, castString));
 
    if (_.isString(value) && _.includes(value, ','))
      this.map[normalizedName].push(...value.split(','));
 
    if (!_.isArray(value) && !_.includes(value, ','))
      this.map[normalizedName].push(castString(value));
 
  }
 
  'delete'(name) {
 
    this.map = _.omit(this.map, name)
 
  }
 
  'get'(name) {
 
    const values = this.map[normalizeName(name)];
 
    return values ? values[0] : null
 
  }
 
  getAll(name) {
 
    return this.map[normalizeName(name)] || []
 
  }
 
  has(name) {
 
    return _.has(this.map, normalizeName(name));
 
  }
 
  'set'(name, value) {
 
    this.map[normalizeName(name)] = [ castString(value) ]
 
  }
 
  entries() {
 
    throw new Error('Method has not been implemented by farfetchd yet');
 
  }
 
  keys() {
 
    throw new Error('Method has not been implemented by farfetchd yet');
 
  }
 
  values() {
 
    throw new Error('Method has not been implemented by farfetchd yet');
 
  }
 
}
 
export default Headers;