defined-info.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @import {Space} from 'property-information'
  3. */
  4. import {Info} from './info.js'
  5. import * as types from './types.js'
  6. const checks = /** @type {ReadonlyArray<keyof typeof types>} */ (
  7. Object.keys(types)
  8. )
  9. export class DefinedInfo extends Info {
  10. /**
  11. * @constructor
  12. * @param {string} property
  13. * Property.
  14. * @param {string} attribute
  15. * Attribute.
  16. * @param {number | null | undefined} [mask]
  17. * Mask.
  18. * @param {Space | undefined} [space]
  19. * Space.
  20. * @returns
  21. * Info.
  22. */
  23. constructor(property, attribute, mask, space) {
  24. let index = -1
  25. super(property, attribute)
  26. mark(this, 'space', space)
  27. if (typeof mask === 'number') {
  28. while (++index < checks.length) {
  29. const check = checks[index]
  30. mark(this, checks[index], (mask & types[check]) === types[check])
  31. }
  32. }
  33. }
  34. }
  35. DefinedInfo.prototype.defined = true
  36. /**
  37. * @template {keyof DefinedInfo} Key
  38. * Key type.
  39. * @param {DefinedInfo} values
  40. * Info.
  41. * @param {Key} key
  42. * Key.
  43. * @param {DefinedInfo[Key]} value
  44. * Value.
  45. * @returns {undefined}
  46. * Nothing.
  47. */
  48. function mark(values, key, value) {
  49. if (value) {
  50. values[key] = value
  51. }
  52. }