Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@
* @module schema/config
*/

// This list defines the base names of HED XML schema files
// that are considered "bundled" with the library.
// The actual loading mechanism is handled by the schema loader,
// which may use an application-provided loader for browser environments
// or a Node.js fs-based loader for server-side/test environments.
const _localSchemaNames = [
'HED8.4.0',
'HED_lang_1.1.0',
'HED_score_2.1.0',
// Add other bundled schema base names here if needed
// Static imports of bundled HED schema XML files. These are resolved at build
// time by esbuild (loader: { '.xml': 'text' }) for the published bundles, and
// at test time by xml-transformer.js (Jest transform). The XML strings are
// inlined into the resulting bundle in both Node and browser builds, so the
// "local cache" of standard schemas is available offline in every environment.
//
// To add a new bundled schema: drop the XML file in src/data/schemas/ and add
// the matching import + map entry below.

import HED_standard from '../data/schemas/HED8.4.0.xml'
import HED_lang from '../data/schemas/HED_lang_1.1.0.xml'
import HED_score from '../data/schemas/HED_score_2.1.0.xml'

const bundledSchemas: [string, string][] = [
['HED8.4.0', HED_standard],
['HED_lang_1.1.0', HED_lang],
['HED_score_2.1.0', HED_score],
]

const _localSchemaNames = bundledSchemas.map(([name]) => name)

/**
* This list defines the base names of HED XML schema files that are considered "bundled" with the library.
* The actual loading mechanism is handled by the schema loader, which may use an application-provided loader
* for browser environments or a Node.js fs-based loader for server-side/test environments.
*/
export const localSchemaNames = Object.freeze(_localSchemaNames)

let _localSchemaMap

// @ts-expect-error __VITE_ENV__ is defined by Vite in browser builds
if (typeof __VITE_ENV__ !== 'undefined' && __VITE_ENV__) {
// In the browser, this map is not used. The loader uses import.meta.glob.
_localSchemaMap = new Map<string, string>()
} else {
// For Node.js, pre-load the schemas.
_localSchemaMap = new Map<string, string>(
localSchemaNames.map((localSchema) => [localSchema, require(`../data/schemas/${localSchema}.xml`)]),
)
}
const _localSchemaMap = new Map(bundledSchemas)

/**
* A mapping from the bundled schema names to their XML data (as strings).
Expand All @@ -45,6 +43,7 @@ export const localSchemaMap = Object.freeze(_localSchemaMap)
*
* @returns The list of unprefixed bundled schema names.
*/
export function getLocalSchemaVersions(): string[] {
export function getLocalSchemaVersions() {
// Return a copy of the local schema names to avoid external modifications
return localSchemaNames.map((name) => name.replace(/^HED_?/, ''))
}
2 changes: 1 addition & 1 deletion src/schema/entries/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class SchemaTag extends SchemaEntryWithAttributes {
*
* @remarks
*
* Schema tags are deemed equivalent if they have the same name and equivalent attributes, unit and value classes, and parents.
* Schema tags are deemed equivalent if they have the same name and equivalent attributes, parent tags, and value tags.
*
* @param other - A schema tag to compare with this one.
* @returns Whether the other tag is equivalent to this schema tag.
Expand Down
9 changes: 5 additions & 4 deletions src/schema/entries/valueTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ export default class SchemaValueTag extends SchemaTag {
}

/**
* Determine if this schema tag is equivalent to another schema tag.
* Determine if this schema value tag is equivalent to another schema value tag.
*
* @remarks
*
* Schema tags are deemed equivalent if they have the same name and equivalent attributes, unit and value classes, and parents.
* Schema value tags are deemed equivalent if they have the same name and equivalent attributes, equivalent unit and
* value classes, and parent tags equivalent based on their names and attributes *only*.
*
* @param other - A schema tag to compare with this one.
* @returns Whether the other tag is equivalent to this schema tag.
* @param other - A schema value tag to compare with this one.
* @returns Whether the other value tag is equivalent to this schema value tag.
*/
public override equivalent(other: unknown): boolean {
if (!(other instanceof SchemaValueTag)) {
Expand Down
3 changes: 2 additions & 1 deletion src/schema/parser/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ export default class TagParser extends SchemaEntryWithAttributesParser<SchemaTag
for (const childTag of this.getAllChildTags(tagElement)) {
const childTagName = getElementTagName(childTag)
const newBooleanAttributes =
booleanAttributeDefinitions.get(childTagName)?.union(recursiveAttributes) ?? new Set<SchemaAttribute>()
booleanAttributeDefinitions.get(childTagName)?.union(recursiveAttributes) ??
new Set<SchemaAttribute>(recursiveAttributes)
booleanAttributeDefinitions.set(childTagName, newBooleanAttributes)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/schema/xmlFile.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.xml' {
const contents: string
export default contents
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"types": ["node"],
"declaration": true,
"strict": true,
"strictFunctionTypes": false,
"strictPropertyInitialization": false,
Expand Down
Loading