Permalink
Browse files

Add isValidNote function

  • Loading branch information...
Sambego committed Mar 16, 2018
1 parent f05b338 commit 6f5aff9445ad557f24c78000ccd95891f1849593
Showing with 64 additions and 1 deletion.
  1. +2 −1 .babelrc
  2. +4 −0 package.json
  3. +1 −0 src/constants/Regex/Note.js
  4. +9 −0 src/isValidNote/index.js
  5. +48 −0 src/isValidNote/test.js
@@ -12,6 +12,7 @@
],
"plugins": [
"add-module-exports",
"transform-object-rest-spread"
"transform-object-rest-spread",
"transform-modern-regexp"
]
}
@@ -13,6 +13,9 @@
"type": "git",
"url": "git+https://github.com/madewithlove/music-fns.git"
},
"engines": {
"node": ">=8"
},
"keywords": [
"music",
"utility",
@@ -32,6 +35,7 @@
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-modern-regexp": "^0.0.4",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
@@ -0,0 +1 @@
export default /^(?<note>[a-gA-G])?(?<accidental>#{1,}|♯{1,}|b{1,}|♮{1,})?(?<octave>[0-9]+)*$/;
@@ -0,0 +1,9 @@
// @flow

import NOTE_REGEX from '../constants/Regex/Note';

const isValidNote = (note: string) => {
return NOTE_REGEX.test(note);
};

export default isValidNote;
@@ -0,0 +1,48 @@
import isValidNote from './';

describe('isValidNote', () => {
it('should return true on when it is a valid note', () => {
const note = 'A';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with a accidental of #', () => {
const note = 'A#';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with a accidental of b', () => {
const note = 'Ab';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with a accidental of ♯', () => {
const note = 'A♯';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with a accidental of ♮', () => {
const note = 'A♮';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with an octave', () => {
const note = 'A2';
expect(isValidNote(note)).toBe(true);
});

it('should return true on when it is a valid note with an accidental and an octave', () => {
const note = 'A#2';
expect(isValidNote(note)).toBe(true);
});

it('should return false on when it is an invalid note', () => {
const note = 'H';
expect(isValidNote(note)).toBe(false);
});

it('should return false on when it is a valid note with an invalid accidental', () => {
const note = 'Hx';
expect(isValidNote(note)).toBe(false);
});
});

0 comments on commit 6f5aff9

Please sign in to comment.