Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isMajorScale and isMinorScale functions #45

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 additions & 0 deletions src/isMajorScale/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @flow

import isDiatonicScale from '../isDiatonicScale';
import normalizeScale from '../normalizeScale';
import getIntervals from '../getIntervals';
import IONIAN from '../constants/Mode/IONIAN';

type options = {
direction?: direction
};

const isMajorScale = (scale: Scale, { direction = 1 }: options = {}) => {
if (![-1, 1].includes(direction)) {
throw new Error('Direction should be 1 (up) or -1 (down)');
}

if (!isDiatonicScale(scale)) return false;

if (direction === -1) scale.reverse();

const normalizedScale = normalizeScale(scale);
const intervals = getIntervals(normalizedScale);

return IONIAN.join(',') === intervals.join(',');
};

export default isMajorScale;
18 changes: 18 additions & 0 deletions src/isMajorScale/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://en.wikipedia.org/wiki/Major_scale

import isMajorScale from './';

describe('isMajorScale', () => {
it('should return true on major scale ', () => {
const scale = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
expect(isMajorScale(scale)).toBe(true);
});
it('should return true on major scale', () => {
const scale = ['C1', 'D1', 'E1', 'F1', 'G1', 'A1', 'B1'];
expect(isMajorScale(scale)).toBe(true);
});
it('should return false on minor scale ', () => {
const scale = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
expect(isMajorScale(scale)).toBe(false);
});
});
27 changes: 27 additions & 0 deletions src/isMinorScale/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @flow

import isDiatonicScale from '../isDiatonicScale';
import normalizeScale from '../normalizeScale';
import getIntervals from '../getIntervals';
import AEOLIAN from '../constants/Mode/AEOLIAN';

type options = {
direction?: direction
};

const isMinorScale = (scale: Scale, { direction = 1 }: options = {}) => {
if (![-1, 1].includes(direction)) {
throw new Error('Direction should be 1 (up) or -1 (down)');
}

if (!isDiatonicScale(scale)) return false;

if (direction === -1) scale.reverse();

const normalizedScale = normalizeScale(scale);
const intervals = getIntervals(normalizedScale);

return AEOLIAN.join(',') === intervals.join(',');
};

export default isMinorScale;
18 changes: 18 additions & 0 deletions src/isMinorScale/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://en.wikipedia.org/wiki/Minor_scale

import isMinorScale from './';

describe('isMinorScale', () => {
it('should return true on minor scale ', () => {
const scale = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
expect(isMinorScale(scale)).toBe(true);
});
it('should return true on minor scale', () => {
const scale = ['A1', 'B1', 'C2', 'D2', 'E2', 'F2', 'G2'];
expect(isMinorScale(scale)).toBe(true);
});
it('should return false on major scale ', () => {
const scale = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
expect(isMinorScale(scale)).toBe(false);
});
});