site/node_modules/property-information/lib/util/create.js
2024-10-14 08:09:33 +02:00

59 lines
1.4 KiB
JavaScript

/**
* @typedef {import('./schema.js').Properties} Properties
* @typedef {import('./schema.js').Normal} Normal
*
* @typedef {Record<string, string>} Attributes
*
* @typedef {Object} Definition
* @property {Record<string, number|null>} properties
* @property {(attributes: Attributes, property: string) => string} transform
* @property {string} [space]
* @property {Attributes} [attributes]
* @property {Array<string>} [mustUseProperty]
*/
import {normalize} from '../normalize.js'
import {Schema} from './schema.js'
import {DefinedInfo} from './defined-info.js'
const own = {}.hasOwnProperty
/**
* @param {Definition} definition
* @returns {Schema}
*/
export function create(definition) {
/** @type {Properties} */
const property = {}
/** @type {Normal} */
const normal = {}
/** @type {string} */
let prop
for (prop in definition.properties) {
if (own.call(definition.properties, prop)) {
const value = definition.properties[prop]
const info = new DefinedInfo(
prop,
definition.transform(definition.attributes || {}, prop),
value,
definition.space
)
if (
definition.mustUseProperty &&
definition.mustUseProperty.includes(prop)
) {
info.mustUseProperty = true
}
property[prop] = info
normal[normalize(prop)] = prop
normal[normalize(info.attribute)] = prop
}
}
return new Schema(property, normal, definition.space)
}