Skip to content

Commit

Permalink
Update upstream (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Nov 14, 2023
1 parent 408973c commit dddf592
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 95 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ jobs:
- run: yarn lint
- run: yarn test
- run: yarn build
- run: yarn readme
99 changes: 14 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ subject to breaking changes between major versions.
<dd><p>Represents a chord with the corresponding (partial) lyrics</p></dd>
<dt><a href="#Comment">Comment</a></dt>
<dd><p>Represents a comment. See https://www.chordpro.org/chordpro/chordpro-file-format-specification/#overview</p></dd>
<dt><a href="#Line">Line</a> : <code><a href="#Font">Font</a></code></dt>
<dt><a href="#Line">Line</a></dt>
<dd><p>Represents a line in a chord sheet, consisting of items of type ChordLyricsPair or Tag</p></dd>
<dt><a href="#Metadata">Metadata</a></dt>
<dd><p>Stores song metadata. Properties can be accessed using the get() method:</p>
Expand Down Expand Up @@ -452,15 +452,6 @@ Inherits from [ChordSheetParser](#ChordSheetParser)</p></dd>
<p>The only function considered public API is <code>Key.distance</code></p></dd>
</dl>

## Members

<dl>
<dt><a href="#Font">Font</a> : <code>string</code> | <code>null</code></dt>
<dd><p>The font color</p></dd>
<dt><a href="#FontSize">FontSize</a> : <code>number</code></dt>
<dd><p>The font size</p></dd>
</dl>

## Constants

<dl>
Expand Down Expand Up @@ -536,6 +527,9 @@ See https://www.chordpro.org/chordpro/directives-key/</p></dd>
<dt><a href="#CHORUS">CHORUS</a> : <code>string</code></dt>
<dd><p>Chorus directive. Support repeating an earlier defined section.
See https://www.chordpro.org/chordpro/directives-env_chorus/</p></dd>
<dt><a href="#CHORD_STYLE">CHORD_STYLE</a> : <code>string</code></dt>
<dd><p>Chord type directive. Determines the type of chords used in the rendered chord sheet.
Possible values are 'symbol', 'numeral' and 'number'</p></dd>
<dt><a href="#VERSE">VERSE</a> : <code>string</code></dt>
<dd><p>Used to mark a paragraph as verse</p></dd>
<dt><a href="#CHORUS">CHORUS</a> : <code>string</code></dt>
Expand Down Expand Up @@ -635,13 +629,12 @@ See https://www.chordpro.org/chordpro/directives-env_chorus/</p></dd>
**Kind**: instance method of [<code>Comment</code>](#Comment)
<a name="Line"></a>

## Line : [<code>Font</code>](#Font)
## Line
<p>Represents a line in a chord sheet, consisting of items of type ChordLyricsPair or Tag</p>

**Kind**: global class

* [Line](#Line) : [<code>Font</code>](#Font)
* [new Line()](#new_Line_new)
* [Line](#Line)
* [.isEmpty()](#Line+isEmpty) ⇒ <code>boolean</code>
* [.addItem(item)](#Line+addItem)
* [.hasRenderableItems()](#Line+hasRenderableItems) ⇒ <code>boolean</code>
Expand All @@ -650,13 +643,6 @@ See https://www.chordpro.org/chordpro/directives-env_chorus/</p></dd>
* [.isChorus()](#Line+isChorus) ⇒ <code>boolean</code>
* ~~[.hasContent()](#Line+hasContent) ⇒ <code>boolean</code>~~

<a name="new_Line_new"></a>

### new Line()
<p>The chord font that applies to this line. Is derived from the directives:
<code>chordfont</code>, <code>chordsize</code> and <code>chordcolour</code>
See: https://www.chordpro.org/chordpro/directives-props_chord_legacy/</p>

<a name="Line+isEmpty"></a>

### line.isEmpty() ⇒ <code>boolean</code>
Expand Down Expand Up @@ -759,16 +745,9 @@ else it returns an array of strings.</p>
**Kind**: global class

* [Paragraph](#Paragraph)
* [.addLine](#Paragraph+addLine) : [<code>Array.&lt;Line&gt;</code>](#Line)
* [.type](#Paragraph+type) ⇒ <code>string</code>
* [.hasRenderableItems()](#Paragraph+hasRenderableItems) ⇒ <code>boolean</code>

<a name="Paragraph+addLine"></a>

### paragraph.addLine : [<code>Array.&lt;Line&gt;</code>](#Line)
<p>The [Line](#Line) items of which the paragraph consists</p>

**Kind**: instance property of [<code>Paragraph</code>](#Paragraph)
<a name="Paragraph+type"></a>

### paragraph.type ⇒ <code>string</code>
Expand Down Expand Up @@ -1662,64 +1641,6 @@ Can be deserialized using [deserialize](deserialize)</p>
| oneKey | [<code>Key</code>](#Key) \| <code>string</code> | <p>the key</p> |
| otherKey | [<code>Key</code>](#Key) \| <code>string</code> | <p>the other key</p> |

<a name="Font"></a>

## Font : <code>string</code> \| <code>null</code>
<p>The font color</p>

**Kind**: global variable
<a name="Font+toCssString"></a>

### font.toCssString() ⇒ <code>string</code>
<p>Converts the font, size and color to a CSS string.
If possible, font and size are combined to the <code>font</code> shorthand.
If <code>font</code> contains double quotes (<code>&quot;</code>) those will be converted to single quotes (<code>'</code>).</p>

**Kind**: instance method of [<code>Font</code>](#Font)
**Returns**: <code>string</code> - <p>The CSS string</p>
**Example**
```js
// Returns "font-family: 'Times New Roman'"
new Font({ font: '"Times New Roman"' }).toCssString()
```
**Example**
```js
// Returns "color: red; font-family: Verdana"
new Font({ font: 'Verdana', colour: 'red' }).toCssString()
```
**Example**
```js
// Returns "font: 30px Verdana"
new Font({ font: 'Verdana', size: '30' }).toCssString()
```
**Example**
```js
// Returns "color: blue; font: 30% Verdana"
new Font({ font: 'Verdana', size: '30%', colour: 'blue' }).toCssString()
```
<a name="FontSize"></a>

## FontSize : <code>number</code>
<p>The font size</p>

**Kind**: global variable
<a name="FontSize+toString"></a>

### fontSize.toString() ⇒ <code>string</code>
<p>Stringifies the font size by concatenating size and unit</p>

**Kind**: instance method of [<code>FontSize</code>](#FontSize)
**Returns**: <code>string</code> - <p>The font size</p>
**Example**
```js
// Returns "30px"
new FontSize(30, 'px').toString()
```
**Example**
```js
// Returns "120%"
new FontSize(120, '%').toString()
```
<a name="ALBUM"></a>

## ALBUM : <code>string</code>
Expand Down Expand Up @@ -1932,6 +1853,14 @@ See https://www.chordpro.org/chordpro/directives-key/</p>
See https://www.chordpro.org/chordpro/directives-env_chorus/</p>

**Kind**: global constant
<a name="CHORD_STYLE"></a>

## CHORD\_STYLE : <code>string</code>
<p>Chord type directive. Determines the type of chords used in the rendered chord sheet.
Possible values are 'symbol', 'numeral' and 'number'</p>

**Kind**: global constant
**See**: https://github.com/bettermusic/ChordSheetJS/issues/352
<a name="VERSE"></a>

## VERSE : <code>string</code>
Expand Down
9 changes: 9 additions & 0 deletions bin/open_inspector
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

browser_app_id=$(defaults read com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers | sed -n -e "/LSHandlerURLScheme = https;/{x;p;d;}" -e 's/.*=[^"]"\(.*\)";/\1/g' -e x)

osascript <<APPLE_SCRIPT
tell application id "${browser_app_id}"
open location "chrome://inspect"
end tell
APPLE_SCRIPT
2 changes: 1 addition & 1 deletion jsdoc2md.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"ignore": ["**/*.(test|spec).ts"],
"babelrc": false,
"presets": [["@babel/preset-env", { "targets": { "node": true } }], "@babel/preset-typescript"],
"plugins": ["@babel/proposal-class-properties", "@babel/proposal-object-rest-spread"]
"plugins": ["@babel/transform-class-properties", "@babel/transform-object-rest-spread"]
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"dev": "parcel watch --no-cache",
"jest": "jest",
"test": "yarn lint && yarn jest",
"test:watch": "jest --watch",
"test:debug": "open -a \"Brave Browser\" chrome://inspect && node --nolazy --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --colors --verbose",
"jest:watch": "jest --watch",
"jest:debug": "bin/open_inspector && node --nolazy --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --colors --verbose",
"eslint": "node_modules/.bin/eslint -c .eslintrc.js --ext .ts",
"eslint:fix": "yarn eslint --fix",
"prelint": "yarn build:code-generate",
Expand Down
11 changes: 11 additions & 0 deletions src/chord_sheet/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ export const TITLECOLOUR = 'titlecolour';
*/
export const CHORUS = 'chorus';

/**
* Chord type directive. Determines the type of chords used in the rendered chord sheet.
* Possible values are 'symbol', 'numeral' and 'number'
* @see https://github.com/bettermusic/ChordSheetJS/issues/352
* @type {string}
*/
export const CHORD_STYLE = 'chord_style';

export type ChordType = 'symbol' | 'numeral' | 'number' | null;

const TITLE_SHORT = 't';
const SUBTITLE_SHORT = 'st';
const COMMENT_SHORT = 'c';
Expand All @@ -231,6 +241,7 @@ export const META_TAGS = [
ALBUM,
ARTIST,
CAPO,
CHORD_STYLE,
COMPOSER,
COPYRIGHT,
DURATION,
Expand Down
29 changes: 26 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Chord from './chord';
import Key from './key';
import { capos, majorKeys, minorKeys } from './key_config';
import Song from './chord_sheet/song';
import { CAPO } from './chord_sheet/tag';
import { CAPO, CHORD_STYLE, ChordType } from './chord_sheet/tag';
import Line from './chord_sheet/line';

export function transposeDistance(transposeKey: string, songKey: string): number {
Expand All @@ -13,7 +13,12 @@ export function transposeDistance(transposeKey: string, songKey: string): number
return Key.distance(songKey, transposeKey);
}

function chordTransposeDistance(capo: number, transposeKey: string | null, songKey: string, renderKey: Key | null) {
function chordTransposeDistance(
capo: number,
transposeKey: string | null,
songKey: string,
renderKey: Key | null | undefined,
) {
let transpose = -1 * (capo || 0);

if (songKey) {
Expand All @@ -29,6 +34,23 @@ function chordTransposeDistance(capo: number, transposeKey: string | null, songK
return transpose;
}

function changeChordType(
chord: Chord,
type: ChordType,
referenceKey: Key | null,
): Chord {
switch (type) {
case 'symbol':
return chord.toChordSymbol(referenceKey);
case 'numeral':
return chord.toNumeral(referenceKey);
case 'number':
return chord.toNumeric(referenceKey);
default:
return chord;
}
}

interface RenderChordOptions {
renderKey?: Key | null;
useUnicodeModifier?: boolean;
Expand All @@ -48,6 +70,7 @@ export function renderChord(
const chord = Chord.parse(chordString);
const songKey = song.key;
const capo = parseInt(song.metadata.getSingle(CAPO), 10);
const chordStyle = song.metadata.getSingle(CHORD_STYLE) as ChordType;

if (!chord) {
return chordString;
Expand All @@ -58,7 +81,7 @@ export function renderChord(
const transposedChord = chord.transpose(effectiveTransposeDistance);
const normalizedChord = (normalizeChords ? transposedChord.normalize(effectiveKey) : transposedChord);

return normalizedChord.toString({ useUnicodeModifier });
return changeChordType(normalizedChord, chordStyle, effectiveKey).toString({ useUnicodeModifier });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface KeyProperties {
}

const KEY_TYPES: ChordType[] = [SYMBOL, NUMERIC, NUMERAL];
const NATURAL_MINORS = [1, 2, 5];
const NATURAL_MINORS = [1, 2, 3, 4, 5, 8, 9, 10];
const NO_FLAT_GRADES = [4, 11];
const NO_FLAT_NUMBERS = [1, 4];
const NO_SHARP_GRADES = [5, 0];
Expand Down Expand Up @@ -410,7 +410,7 @@ class Key implements KeyProperties {
type: NUMERIC,
grade: Key.shiftGrade(this.effectiveGrade - referenceKeyGrade),
referenceKeyGrade: 0,
modifier: referenceKey.modifier,
modifier: null,
preferredModifier: referenceKey.modifier,
});
}
Expand Down
1 change: 1 addition & 0 deletions src/template_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface WhenCallback {
}

export { isEvaluatable } from './utilities';
export { renderChord } from './helpers';

export const isChordLyricsPair = (item: Item): boolean => item instanceof ChordLyricsPair;

Expand Down
22 changes: 22 additions & 0 deletions test/chord_symbol/to_numeral.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Chord, Key } from '../../src';
import '../matchers';

describe('Chord', () => {
describe('toNumeral', () => {
describe('for a chord symbol', () => {
it('returns a the numeral version', () => {
const key = Key.parseOrFail('Ab');

expect(Chord.parse('Dsus/F#')?.toNumeral(key).toString()).toEqual('bVsus/bVII');
});

it('accepts a string key', () => {
expect(Chord.parse('Dsus/F#')?.toNumeral('Ab').toString()).toEqual('bVsus/bVII');
});

xit('supports a minor chord', () => {
expect(Chord.parse('Gm')?.toNumeral('Bb').toString()).toEqual('vi');
});
});
});
});
4 changes: 4 additions & 0 deletions test/chord_symbol/to_numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('Chord', () => {
it('accepts a string key', () => {
expect(Chord.parse('Dsus/F#')?.toNumeric('Ab').toString()).toEqual('b5sus/b7');
});

xit('supports a minor chord', () => {
expect(Chord.parse('Gm')?.toNumeric('Bb')?.toString()).toEqual('6');
});
});
});
});
34 changes: 34 additions & 0 deletions test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,25 @@ import ChordSheetJS, {
SYMBOL,
TAB,
VERSE,
templateHelpers,
} from '../src';

const {
isEvaluatable,
isChordLyricsPair,
lineHasContents,
isTag,
isComment,
stripHTML,
each,
when,
hasTextContents,
lineClasses,
paragraphClasses,
evaluate,
fontStyleTag,
} = templateHelpers;

describe('exports', () => {
it('supplies all required constants as named exports', () => {
expect(Chord).toBeDefined();
Expand Down Expand Up @@ -63,6 +80,23 @@ describe('exports', () => {
expect(SYMBOL).toBeDefined();
expect(TAB).toBeDefined();
expect(VERSE).toBeDefined();
expect(templateHelpers).toBeDefined();
});

it('supplies all template helpers', () => {
expect(isEvaluatable).toBeDefined();
expect(isChordLyricsPair).toBeDefined();
expect(lineHasContents).toBeDefined();
expect(isTag).toBeDefined();
expect(isComment).toBeDefined();
expect(stripHTML).toBeDefined();
expect(each).toBeDefined();
expect(when).toBeDefined();
expect(hasTextContents).toBeDefined();
expect(lineClasses).toBeDefined();
expect(paragraphClasses).toBeDefined();
expect(evaluate).toBeDefined();
expect(fontStyleTag).toBeDefined();
});

it('supplies all constants as properties of the default export', () => {
Expand Down
Loading

0 comments on commit dddf592

Please sign in to comment.