header-list.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. var _ = require('../../lodash'), PropertyList = require('./property-list').PropertyList, Header = require('./header').Header, E = '', CRLF = '\r\n', PROP_NAME = '_postman_propertyName', HeaderList;
  3. _.inherit((
  4. /**
  5. * Contains a list of header elements
  6. *
  7. * @constructor
  8. * @param {Object} parent
  9. * @param {Header[]} headers
  10. * @extends {PropertyList}
  11. */
  12. HeaderList = function (parent, headers) {
  13. // this constructor is intended to inherit and as such the super constructor is required to be executed
  14. HeaderList.super_.call(this, Header, parent, headers);
  15. }), PropertyList);
  16. _.assign(HeaderList.prototype, /** @lends HeaderList.prototype */ {
  17. /**
  18. * Gets size of a list of headers excluding standard header prefix.
  19. *
  20. * @returns {Number}
  21. */
  22. contentSize: function () {
  23. if (!this.count()) {
  24. return 0;
  25. }
  26. var raw = this.reduce(function (acc, header) {
  27. // unparse header only if it has a valid key and is not disabled
  28. if (header && !header.disabled) {
  29. // *( header-field CRLF )
  30. acc += Header.unparseSingle(header) + CRLF;
  31. }
  32. return acc;
  33. }, E);
  34. return raw.length;
  35. }
  36. });
  37. _.assign(HeaderList, /** @lends HeaderList */ {
  38. /**
  39. * Defines the name of this property for internal use.
  40. * @private
  41. * @readOnly
  42. * @type {String}
  43. */
  44. _postman_propertyName: 'HeaderList',
  45. /**
  46. * Checks if the given object is a HeaderList
  47. *
  48. * @param {*} obj
  49. * @returns {Boolean}
  50. */
  51. isHeaderList: function (obj) {
  52. return Boolean(obj) && ((obj instanceof HeaderList) ||
  53. _.inSuperChain(obj.constructor, PROP_NAME, HeaderList._postman_propertyName));
  54. }
  55. });
  56. module.exports = {
  57. HeaderList: HeaderList
  58. };