create.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @import {Info, Space} from 'property-information'
  3. */
  4. /**
  5. * @typedef Definition
  6. * Definition of a schema.
  7. * @property {Record<string, string> | undefined} [attributes]
  8. * Normalzed names to special attribute case.
  9. * @property {ReadonlyArray<string> | undefined} [mustUseProperty]
  10. * Normalized names that must be set as properties.
  11. * @property {Record<string, number | null>} properties
  12. * Property names to their types.
  13. * @property {Space | undefined} [space]
  14. * Space.
  15. * @property {Transform} transform
  16. * Transform a property name.
  17. */
  18. /**
  19. * @callback Transform
  20. * Transform.
  21. * @param {Record<string, string>} attributes
  22. * Attributes.
  23. * @param {string} property
  24. * Property.
  25. * @returns {string}
  26. * Attribute.
  27. */
  28. import {normalize} from '../normalize.js'
  29. import {DefinedInfo} from './defined-info.js'
  30. import {Schema} from './schema.js'
  31. /**
  32. * @param {Definition} definition
  33. * Definition.
  34. * @returns {Schema}
  35. * Schema.
  36. */
  37. export function create(definition) {
  38. /** @type {Record<string, Info>} */
  39. const properties = {}
  40. /** @type {Record<string, string>} */
  41. const normals = {}
  42. for (const [property, value] of Object.entries(definition.properties)) {
  43. const info = new DefinedInfo(
  44. property,
  45. definition.transform(definition.attributes || {}, property),
  46. value,
  47. definition.space
  48. )
  49. if (
  50. definition.mustUseProperty &&
  51. definition.mustUseProperty.includes(property)
  52. ) {
  53. info.mustUseProperty = true
  54. }
  55. properties[property] = info
  56. normals[normalize(property)] = property
  57. normals[normalize(info.attribute)] = property
  58. }
  59. return new Schema(properties, normals, definition.space)
  60. }