Collection of single dispatch and multiple dispatch functions to traverse the MEI structure.
The type signature for all traversals are element -> *
Note that the API is currently considered unstable, and subject to change.
Complete list of available functions by category
By default, this library operates under the assumption the MEI is immutable. To change this see the next section on configuration.
Each function gets added to a pristine lodash instance (using _.runInContext
and _.mixin
) and that instance is exported. This is a heavy dependency.
The main power of this library comes from multiple dispatch functions which allow us to navigate the MEI using common musical semantics without exposing the MEI structure. You shouldn't have to worry about the fact that some elements are hierarchically related, while others are related by attribute.
Consider the following MEI snippets:
<staff>
<note/>
</staff>
<measure>
<staff n="1"/>
<dynam staff="1"/>
<measure/>
In the first example, calling traversals.staff
with the <note/>
will return the parent <staff>
. In the second example, Calling traversals.staff
with the <dynam staff="1"/>
will return the <staff>
which has a matching n
attribute.
import traversals from 'mei-traversals';
import { DOMParser } from 'xmldom';
const domParser = new DOMParser();
const doc = domParser.parseFromString(`
<mei>
<music>
<mdiv>
<score>
<measure>
<staff n="1">
<layer>
<note />
<note />
</layer>
</staff>
</measure>
</score>
</mdiv>
</music>
</mei>
`)
const notes = traversals.notes(doc);
const staffN = traversals.staffN(notes[1]);
console.log(staffN); // => 1
To configure the traversals, use the .configure(options)
function provided on the main traversals object.
options
cache
- If
true
, the result of each traversal is cached on the input DOM element. - default:
true
- If
log
- If
true
, each traversal is logged with the input DOM element, output data, and duration - default:
false
- If
tryCatch
- If
true
, each traversal is wrapped in atry catch
- default:
true
- If
import traversals from 'tido-mei-traversals';
traversals.configure({ log: true, cache: true, tryCatch: true });
No benchmarks yet.
npm install --save tido-mei-traversals
If you are not installing from npm
, you can compile the source on your own. To compile, run the following command:
npm run prepublish
npm install
npm test