hed-javascript and hed-typescript¶
The hed-validator npm package has two implementations of the same validator:
hed-javascript, written in JavaScript, and
hed-typescript, written in TypeScript.
Both validate HED strings, BIDS sidecars and tabular files, and whole BIDS datasets. Both run the hed-tests conformance suite and must give the same results on it. Changes are ported in both directions. As a rule, the two implementations share behavior, and hed-javascript follows hed-typescript’s code style.
This page lists the differences that are intended. Any difference in behavior that is not listed here is unintended and is treated as a bug in one of the two.
Language and packaging¶
hed-javascript |
hed-typescript |
|
|---|---|---|
Source |
JavaScript modules in |
TypeScript in |
Build |
esbuild to |
esbuild to |
Type declarations |
Hand-written, |
Not published yet; the source is type-checked |
Node-only modules |
|
|
Browser replacements |
|
|
In both packages the browser field of package.json swaps each Node-only module for its browser replacement, so
the ES module build used by browser bundlers contains no Node built-in imports.
Objects that must be created through a factory method¶
Some classes must not be built with new, because their factory methods check the input and return issues, and a
direct constructor call would skip those checks. TypeScript expresses this with a private constructor, which the
compiler enforces when TypeScript code is compiled. JavaScript has no private constructors, so hed-javascript either
checks at run time or leaves the constructor public and relies on the documented factory method.
Class |
Factory method |
hed-typescript |
hed-javascript |
|---|---|---|---|
|
|
|
Throws unless called by a factory method (hidden second argument) |
|
|
|
Throws if an instance already exists |
|
|
|
Public; throws if the argument is not a |
|
|
|
Public |
|
|
|
Public |
What this means for callers:
In TypeScript code that uses hed-typescript,
new Definition(...)and the others do not compile.In plain JavaScript that uses either package, the compiled hed-typescript code no longer has the
privatekeyword, sonew Definition({})does not throw there, while hed-javascript throws. In both packages the factory methods are the supported way to create these objects; code that calls the constructors directly is outside the documented API.hed-typescript keeps the language feature rather than copying hed-javascript’s run-time check. The tests that check the run-time error, the
Constructortests oftests/otherTests/definitionManager.spec.js, exist only in hed-javascript.