Source: lib/kss_parameter.js

  1. 'use strict';
  2. /**
  3. * The `kss/lib/kss_parameter` module is normally accessed via the
  4. * [`KssParameter()`]{@link module:kss.KssParameter} class of the `kss` module:
  5. * ```
  6. * const KssParameter = require('kss').KssParameter;
  7. * ```
  8. * @private
  9. * @module kss/lib/kss_parameter
  10. */
  11. /**
  12. * A KssParameter object represents a single parameter of a `KssSection`.
  13. *
  14. * This class is normally accessed via the [`kss`]{@link module:kss} module:
  15. * ```
  16. * const KssParameter = require('kss').KssParameter;
  17. * ```
  18. *
  19. * @alias module:kss.KssParameter
  20. */
  21. class KssParameter {
  22. /**
  23. * Creates a KssParameter object and stores the given data.
  24. *
  25. * If passed an object, it will add `section`, `name`, and `description`
  26. * properties.
  27. *
  28. * @param {Object} [data] An object of data.
  29. */
  30. constructor(data) {
  31. data = data || {};
  32. this.meta = {
  33. section: null
  34. };
  35. this.data = {
  36. name: '',
  37. defaultValue: '',
  38. description: ''
  39. };
  40. // Loop through the given properties.
  41. for (let name in data) {
  42. // If the property is defined in this.data or this.meta, add it via our API.
  43. if (data.hasOwnProperty(name) && (this.data.hasOwnProperty(name) || this.meta.hasOwnProperty(name))) {
  44. this[name](data[name]);
  45. }
  46. }
  47. }
  48. /**
  49. * Gets or sets the `KssSection` object this `KssParameter` is associated with.
  50. *
  51. * If the `section` value is provided, the `KssSection` for this parameter is
  52. * set. Otherwise, the `KssSection` of the parameter is returned.
  53. *
  54. * @param {KssSection} [section] Optional. The `KssSection` that owns the
  55. * `KssParameter`.
  56. * @returns {KssSection|KssParameter} If section is given, the current
  57. * `KssParameter` object is returned to allow chaining of methods. Otherwise,
  58. * the `KssSection` object the parameter belongs to is returned.
  59. */
  60. section(section) {
  61. if (typeof section === 'undefined') {
  62. return this.meta.section;
  63. }
  64. this.meta.section = section;
  65. // Allow chaining.
  66. return this;
  67. }
  68. /**
  69. * Gets or sets the name of the `KssParameter`.
  70. *
  71. * If the `name` value is provided, the name of this `KssParameter` is set.
  72. * Otherwise, the name of the `KssParameter` is returned.
  73. *
  74. * @param {string} [name] Optional. The name of the `KssParameter`.
  75. * @returns {string|KssParameter} If name is given, the current `KssParameter`
  76. * object is returned to allow chaining of methods. Otherwise, the name of the
  77. * `KssParameter` is returned.
  78. */
  79. name(name) {
  80. if (typeof name === 'undefined') {
  81. return this.data.name;
  82. }
  83. this.data.name = name;
  84. // Allow chaining.
  85. return this;
  86. }
  87. /**
  88. * Gets or sets the default value of the `KssParameter`.
  89. *
  90. * If the `defaultValue` value is provided, the default value of this
  91. * `KssParameter` is set. Otherwise, the default value of the `KssParameter` is
  92. * returned.
  93. *
  94. * @param {string} defaultValue Optional. The default value of the
  95. * `KssParameter`.
  96. * @returns {string|KssParameter} If `defaultValue` is given, the current
  97. * `KssParameter` object is returned to allow chaining of methods. Otherwise,
  98. * the default value of the `KssParameter` is returned.
  99. */
  100. defaultValue(defaultValue) {
  101. if (typeof defaultValue !== 'undefined') {
  102. this.data.defaultValue = defaultValue;
  103. // Allow chaining.
  104. return this;
  105. } else {
  106. return this.data.defaultValue;
  107. }
  108. }
  109. /**
  110. * Gets or sets the description of the `KssParameter`.
  111. *
  112. * If the `description` is provided, the description of this `KssParameter` is set.
  113. * Otherwise, the description of the `KssParameter` is returned.
  114. *
  115. * @param {string} [description] Optional. The description of the `KssParameter`.
  116. * @returns {string|KssParameter} If description is given, the current
  117. * `KssParameter` object is returned to allow chaining of methods. Otherwise,
  118. * the description of the `KssParameter` is returned.
  119. */
  120. description(description) {
  121. if (typeof description === 'undefined') {
  122. return this.data.description;
  123. }
  124. this.data.description = description;
  125. // Allow chaining.
  126. return this;
  127. }
  128. /**
  129. * Return the `KssParameter` as a JSON object.
  130. *
  131. * @returns {Object} A JSON object representation of the `KssParameter`.
  132. */
  133. toJSON() {
  134. return {
  135. name: this.name(),
  136. defaultValue: this.defaultValue(),
  137. description: this.description()
  138. };
  139. }
  140. }
  141. module.exports = KssParameter;