Skip to content

Commit

Permalink
Merge pull request #1298 from ronyeh/is-category
Browse files Browse the repository at this point in the history
Remove instanceof from isCategory() and other places in VexFlow
  • Loading branch information
0xfe authored Jan 30, 2022
2 parents fe237a3 + e45f695 commit 21f1282
Show file tree
Hide file tree
Showing 77 changed files with 363 additions and 286 deletions.
14 changes: 7 additions & 7 deletions docs/api/classes/System.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
</div></div><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameters"><li><h5>type: <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> = &#39;double&#39;</span></h5><div class="tsd-comment tsd-typography"><div class="lead">
<p>see <a href="StaveConnector.html#typeString">StaveConnector.typeString</a></p>
</div></div></li></ul><h4 class="tsd-returns-title">Returns <a href="StaveConnector.html" class="tsd-signature-type" data-tsd-kind="Class">StaveConnector</a></h4></li></ul></section><section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class"><a id="addStave" class="tsd-anchor"></a><h3>add<wbr/>Stave</h3><ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class"><li class="tsd-signature tsd-kind-icon">add<wbr/>Stave<span class="tsd-signature-symbol">(</span>params<span class="tsd-signature-symbol">: </span><a href="../interfaces/SystemStave.html" class="tsd-signature-type" data-tsd-kind="Interface">SystemStave</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="Stave.html" class="tsd-signature-type" data-tsd-kind="Class">Stave</a></li></ul><ul class="tsd-descriptions"><li class="tsd-description"><div class="tsd-comment tsd-typography"><div class="lead">
<p>Add stave to the system.</p>
</div><div><p>Examples:</p>
<p> (one voice)</p>
<p>Add a stave to the system.</p>
</div><div><p>Example (one voice):</p>
<p><code>system.addStave({voices: [score.voice(score.notes(&#39;C#5/q, B4, A4, G#4&#39;))]});</code></p>
<p> (two voices)</p>
<p><code>system.addStave({voices: [</code></p>
<p> <code>score.voice(score.notes(&#39;C#5/q, B4, A4, G#4&#39;, {stem: &#39;up&#39;})),</code></p>
<p> <code>score.voice(score.notes(&#39;C#4/h, C#4&#39;, {stem: &#39;down&#39;}))]});</code></p>
<p>Example (two voices):</p>
<p><code>system.addStave({voices: [</code>
<code>score.voice(score.notes(&#39;C#5/q, B4, A4, G#4&#39;, {stem: &#39;up&#39;})),</code>
<code>score.voice(score.notes(&#39;C#4/h, C#4&#39;, {stem: &#39;down&#39;}))</code>
<code>]});</code></p>
</div></div><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameters"><li><h5>params: <a href="../interfaces/SystemStave.html" class="tsd-signature-type" data-tsd-kind="Interface">SystemStave</a></h5></li></ul><h4 class="tsd-returns-title">Returns <a href="Stave.html" class="tsd-signature-type" data-tsd-kind="Class">Stave</a></h4></li></ul></section><section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><a id="applyStyle" class="tsd-anchor"></a><h3>apply<wbr/>Style</h3><ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><li class="tsd-signature tsd-kind-icon">apply<wbr/>Style<span class="tsd-signature-symbol">(</span>context<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><a href="RenderContext.html" class="tsd-signature-type" data-tsd-kind="Class">RenderContext</a>, style<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/ElementStyle.html" class="tsd-signature-type" data-tsd-kind="Interface">ElementStyle</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="System.html" class="tsd-signature-type" data-tsd-kind="Class">System</a></li></ul><ul class="tsd-descriptions"><li class="tsd-description"><aside class="tsd-sources"><p>Inherited from <a href="Element.html">Element</a>.<a href="Element.html#applyStyle">applyStyle</a></p></aside><div class="tsd-comment tsd-typography"><div class="lead">
<p>Apply the element style to <code>context</code>.</p>
</div></div><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameters"><li><h5>context: <span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><a href="RenderContext.html" class="tsd-signature-type" data-tsd-kind="Class">RenderContext</a><span class="tsd-signature-symbol"> = ...</span></h5></li><li><h5>style: <span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/ElementStyle.html" class="tsd-signature-type" data-tsd-kind="Interface">ElementStyle</a><span class="tsd-signature-symbol"> = ...</span></h5></li></ul><h4 class="tsd-returns-title">Returns <a href="System.html" class="tsd-signature-type" data-tsd-kind="Class">System</a></h4></li></ul></section><section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><a id="checkContext" class="tsd-anchor"></a><h3>check<wbr/>Context</h3><ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><li class="tsd-signature tsd-kind-icon">check<wbr/>Context<span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="RenderContext.html" class="tsd-signature-type" data-tsd-kind="Class">RenderContext</a></li></ul><ul class="tsd-descriptions"><li class="tsd-description"><aside class="tsd-sources"><p>Inherited from <a href="Element.html">Element</a>.<a href="Element.html#checkContext">checkContext</a></p></aside><div class="tsd-comment tsd-typography"><div class="lead">
Expand Down
1 change: 1 addition & 0 deletions docs/api/enums/Category.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/api/enums/TupletLocation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html class="default"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>TupletLocation | vexflow</title><meta name="description" content="Documentation for vexflow"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script async src="../assets/search.js" id="search-script"></script></head><body><script>document.body.classList.add(localStorage.getItem("tsd-theme") || "os")</script><header><div class="tsd-page-toolbar"><div class="container"><div class="table-wrap"><div class="table-cell" id="tsd-search" data-base=".."><div class="field"><label for="tsd-search-field" class="tsd-widget search no-caption">Search</label><input type="text" id="tsd-search-field"/></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">vexflow</a></div><div class="table-cell" id="tsd-widgets"><div id="tsd-filter"><a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a><div class="tsd-filter-group"><div class="tsd-select" id="tsd-filter-visibility"><span class="tsd-select-label">All</span><ul class="tsd-select-list"><li data-value="public">Public</li><li data-value="protected">Public/Protected</li><li data-value="private" class="selected">All</li></ul></div> <input type="checkbox" id="tsd-filter-inherited" checked/><label class="tsd-widget" for="tsd-filter-inherited">Inherited</label><input type="checkbox" id="tsd-filter-externals" checked/><label class="tsd-widget" for="tsd-filter-externals">Externals</label></div></div><a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a></div></div></div></div><div class="tsd-page-title"><div class="container"><ul class="tsd-breadcrumb"><li><a href="../modules.html">vexflow</a></li><li><a href="TupletLocation.html">TupletLocation</a></li></ul><h1>Enumeration TupletLocation</h1></div></div></header><div class="container container-main"><div class="row"><div class="col-8 col-content"><section class="tsd-panel-group tsd-index-group"><h2>Index</h2><section class="tsd-panel tsd-index-panel"><div class="tsd-index-content"><section class="tsd-index-section "><h3>Enumeration members</h3><ul class="tsd-index-list"><li class="tsd-kind-enum-member tsd-parent-kind-enum"><a href="TupletLocation.html#BOTTOM" class="tsd-kind-icon">BOTTOM</a></li><li class="tsd-kind-enum-member tsd-parent-kind-enum"><a href="TupletLocation.html#TOP" class="tsd-kind-icon">TOP</a></li></ul></section></div></section></section><section class="tsd-panel-group tsd-member-group "><h2>Enumeration members</h2><section class="tsd-panel tsd-member tsd-kind-enum-member tsd-parent-kind-enum"><a id="BOTTOM" class="tsd-anchor"></a><h3>BOTTOM</h3><div class="tsd-signature tsd-kind-icon">BOTTOM<span class="tsd-signature-symbol"> = -1</span></div></section><section class="tsd-panel tsd-member tsd-kind-enum-member tsd-parent-kind-enum"><a id="TOP" class="tsd-anchor"></a><h3>TOP</h3><div class="tsd-signature tsd-kind-icon">TOP<span class="tsd-signature-symbol"> = 1</span></div></section></section></div><div class="col-4 col-menu menu-sticky-wrap menu-highlight"><nav class="tsd-navigation primary"><ul><li class=""><a href="../modules.html">Exports</a></li></ul></nav><nav class="tsd-navigation secondary menu-sticky"><ul><li class="current tsd-kind-enum"><a href="TupletLocation.html" class="tsd-kind-icon">Tuplet<wbr/>Location</a><ul><li class="tsd-kind-enum-member tsd-parent-kind-enum"><a href="TupletLocation.html#BOTTOM" class="tsd-kind-icon">BOTTOM</a></li><li class="tsd-kind-enum-member tsd-parent-kind-enum"><a href="TupletLocation.html#TOP" class="tsd-kind-icon">TOP</a></li></ul></li></ul></nav></div></div></div><footer class="with-border-bottom"><div class="container"><h2>Legend</h2><div class="tsd-legend-group"><ul class="tsd-legend"><li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li><li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li><li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li><li class="tsd-kind-accessor tsd-parent-kind-class"><span class="tsd-kind-icon">Accessor</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-property tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited property</span></li><li class="tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited method</span></li><li class="tsd-kind-accessor tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited accessor</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-property tsd-parent-kind-interface"><span class="tsd-kind-icon">Property</span></li><li class="tsd-kind-method tsd-parent-kind-interface"><span class="tsd-kind-icon">Method</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-property tsd-parent-kind-class tsd-is-static"><span class="tsd-kind-icon">Static property</span></li><li class="tsd-kind-method tsd-parent-kind-class tsd-is-static"><span class="tsd-kind-icon">Static method</span></li></ul></div><h2>Settings</h2><p>Theme <select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></p></div></footer><div class="container tsd-generator"><p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div><div class="overlay"></div><script src="../assets/main.js"></script></body></html>
13 changes: 3 additions & 10 deletions src/accidental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@

import { Fraction } from './fraction';
import { Glyph } from './glyph';
import { isGraceNote } from './gracenote';
import { isGraceNoteGroup } from './gracenotegroup';
import { Modifier } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { Music } from './music';
import { Note } from './note';
import { isStaveNote } from './stavenote';
import { Tables } from './tables';
import { Tickable } from './tickable';
import { isCategory } from './typeguard';
import { Category, isAccidental, isGraceNote, isGraceNoteGroup, isStaveNote } from './typeguard';
import { defined, log } from './util';
import { Voice } from './voice';

Expand Down Expand Up @@ -62,7 +59,7 @@ export class Accidental extends Modifier {

/** Accidentals category string. */
static get CATEGORY(): string {
return 'Accidental';
return Category.Accidental;
}

/** Arrange accidentals inside a ModifierContext. */
Expand Down Expand Up @@ -437,11 +434,7 @@ export class Accidental extends Modifier {

// Remove accidentals
staveNote.getModifiers().forEach((modifier, index) => {
if (
isCategory(modifier, Accidental) &&
modifier.type == accidentalString &&
modifier.getIndex() == keyIndex
) {
if (isAccidental(modifier) && modifier.type == accidentalString && modifier.getIndex() == keyIndex) {
staveNote.getModifiers().splice(index, 1);
}
});
Expand Down
19 changes: 10 additions & 9 deletions src/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Stave } from './stave';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { Tables } from './tables';
import { TabNote } from './tabnote';
import { TextFormatter } from './textformatter';
import { Category, isStemmableNote, isTabNote } from './typeguard';
import { log } from './util';

// eslint-disable-next-line
Expand Down Expand Up @@ -43,7 +43,7 @@ export class Annotation extends Modifier {

/** Annotations category string. */
static get CATEGORY(): string {
return 'Annotation';
return Category.Annotation;
}

static TEXT_FONT: Required<FontInfo> = { ...Element.TEXT_FONT };
Expand Down Expand Up @@ -85,7 +85,8 @@ export class Annotation extends Modifier {
const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
let stemHeight = 0;
let lines = 5;
if (note instanceof TabNote) {

if (isTabNote(note)) {
if (note.render_options.draw_stem) {
const stem = (note as StemmableNote).getStem();
if (stem) {
Expand All @@ -94,8 +95,8 @@ export class Annotation extends Modifier {
} else {
stemHeight = 0;
}
} else if (note instanceof StemmableNote) {
const stem = (note as StemmableNote).getStem();
} else if (isStemmableNote(note)) {
const stem = note.getStem();
if (stem && note.getNoteType() === 'n') {
stemHeight = Math.abs(stem.getHeight()) / Tables.STAVE_LINE_DISTANCE;
}
Expand All @@ -109,8 +110,8 @@ export class Annotation extends Modifier {

if (annotation.verticalJustification === this.VerticalJustify.TOP) {
let noteLine = note.getLineNumber(true);
if (note instanceof TabNote) {
noteLine = lines - ((note as TabNote).leastString() - 0.5);
if (isTabNote(note)) {
noteLine = lines - (note.leastString() - 0.5);
}
if (stemDirection === Stem.UP) {
noteLine += stemHeight;
Expand All @@ -126,8 +127,8 @@ export class Annotation extends Modifier {
}
} else if (annotation.verticalJustification === this.VerticalJustify.BOTTOM) {
let noteLine = lines - note.getLineNumber();
if (note instanceof TabNote) {
noteLine = (note as TabNote).greatestString() - 1;
if (isTabNote(note)) {
noteLine = note.greatestString() - 1;
}
if (stemDirection === Stem.DOWN) {
noteLine += stemHeight;
Expand Down
11 changes: 5 additions & 6 deletions src/articulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

import { Builder } from './easyscore';
import { Glyph } from './glyph';
import { isGraceNote } from './gracenote';
import { Modifier } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { Note } from './note';
import { Stave } from './stave';
import { isStaveNote } from './stavenote';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { Tables } from './tables';
import { isTabNote } from './tabnote';
import { Category, isGraceNote, isStaveNote, isStemmableNote, isTabNote } from './typeguard';
import { defined, log, RuntimeError } from './util';

export interface ArticulationStruct {
Expand Down Expand Up @@ -171,7 +169,7 @@ export class Articulation extends Modifier {

/** Articulations category string. */
static get CATEGORY(): string {
return 'Articulation';
return Category.Articulation;
}

protected static readonly INITIAL_OFFSET: number = -0.5;
Expand Down Expand Up @@ -220,8 +218,9 @@ export class Articulation extends Modifier {
const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
let stemHeight = 0;
// Decide if we need to consider beam direction in placement.
if (note instanceof StemmableNote) {
const stem = (note as StemmableNote).getStem();

if (isStemmableNote(note)) {
const stem = note.getStem();
if (stem) {
stemHeight = Math.abs(stem.getHeight()) / Tables.STAVE_LINE_DISTANCE;
}
Expand Down
3 changes: 2 additions & 1 deletion src/barnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { ModifierContext } from './modifiercontext';
import { Note } from './note';
import { Barline, BarlineType } from './stavebarline';
import { Category } from './typeguard';
import { log } from './util';

// eslint-disable-next-line
Expand All @@ -23,7 +24,7 @@ export class BarNote extends Note {
static DEBUG: boolean = false;

static get CATEGORY(): string {
return 'BarNote';
return Category.BarNote;
}

protected metrics: { widths: Record<string, number> };
Expand Down
9 changes: 4 additions & 5 deletions src/beam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { Element } from './element';
import { Fraction } from './fraction';
import { Note } from './note';
import { RenderContext } from './rendercontext';
import { isStaveNote } from './stavenote';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { Tables } from './tables';
import { isTabNote } from './tabnote';
import { Tuplet } from './tuplet';
import { Tuplet, TupletLocation } from './tuplet';
import { Category, isStaveNote, isTabNote } from './typeguard';
import { RuntimeError } from './util';
import { Voice } from './voice';

Expand Down Expand Up @@ -45,7 +44,7 @@ const BEAM_BOTH = 'B';
/** `Beams` span over a set of `StemmableNotes`. */
export class Beam extends Element {
static get CATEGORY(): string {
return 'Beam';
return Category.Beam;
}

public render_options: {
Expand Down Expand Up @@ -420,7 +419,7 @@ export class Beam extends Element {
allTuplets.forEach((tuplet) => {
// Set the tuplet location based on the stem direction
const direction =
(tuplet.notes[0] as StemmableNote).stem_direction === Stem.DOWN ? Tuplet.LOCATION_BOTTOM : Tuplet.LOCATION_TOP;
(tuplet.notes[0] as StemmableNote).stem_direction === Stem.DOWN ? TupletLocation.BOTTOM : TupletLocation.TOP;
tuplet.setTupletLocation(direction);

// If any of the notes in the tuplet are not beamed, draw a bracket.
Expand Down
9 changes: 5 additions & 4 deletions src/bend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Element } from './element';
import { FontInfo } from './font';
import { Modifier } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { TabNote } from './tabnote';
import { TextFormatter } from './textformatter';
import { Category, isTabNote } from './typeguard';
import { RuntimeError } from './util';

export interface BendPhrase {
Expand All @@ -20,7 +20,7 @@ export interface BendPhrase {
/** Bend implements tablature bends. */
export class Bend extends Modifier {
static get CATEGORY(): string {
return 'Bend';
return Category.Bend;
}

static get UP(): number {
Expand All @@ -43,8 +43,9 @@ export class Bend extends Modifier {
for (let i = 0; i < bends.length; ++i) {
const bend = bends[i];
const note = bend.checkAttachedNote();
if (note instanceof TabNote) {
const stringPos = (note as TabNote).leastString() - 1;

if (isTabNote(note)) {
const stringPos = note.leastString() - 1;
if (state.top_text_line < stringPos) {
state.top_text_line = stringPos;
}
Expand Down
3 changes: 2 additions & 1 deletion src/chordsymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ModifierContextState } from './modifiercontext';
import { StemmableNote } from './stemmablenote';
import { Tables } from './tables';
import { TextFormatter } from './textformatter';
import { Category } from './typeguard';
import { log } from './util';

// To enable logging for this class. Set `Vex.Flow.ChordSymbol.DEBUG` to `true`.
Expand Down Expand Up @@ -67,7 +68,7 @@ export class ChordSymbol extends Modifier {
static DEBUG: boolean = false;

static get CATEGORY(): string {
return 'ChordSymbol';
return Category.ChordSymbol;
}

// Chord symbols can be positioned and justified relative to the note.
Expand Down
Loading

0 comments on commit 21f1282

Please sign in to comment.