From a4279342d0d6c3ad92b04afff4c306ce55f10afc Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Sat, 1 Aug 2015 22:28:12 -0400 Subject: start this puppy up --- .travis.yml | 5 ++ package.json | 21 +++++ src/utils.js | 106 ++++++++++++++++++++++++ test/test_denormalize.js | 56 +++++++++++++ test/test_normalize.js | 207 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 395 insertions(+) create mode 100644 .travis.yml create mode 100644 package.json create mode 100644 src/utils.js create mode 100644 test/test_denormalize.js create mode 100644 test/test_normalize.js diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7ce05b5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.12" +script: + - npm test diff --git a/package.json b/package.json new file mode 100644 index 0000000..876842d --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "solfege", + "version": "0.0.1", + "description": "A solfege database", + "main": "src/index.js", + "scripts": { + "test": "./node_modules/.bin/mocha -u tdd --reporter spec" + }, + "keywords": [ + "solfege" + ], + "author": "Ben Burwell ", + "license": "MIT", + "dependencies": { + "pg": "^4.4.1" + }, + "devDependencies": { + "mocha": "^2.2.5", + "should": "^7.0.2" + } +} diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..f2d1dc1 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,106 @@ +var fs = require('fs'); + +module.exports = { + + // Take some solfege input and normalize it + normalize: function(str) { + var tokens = str.toLowerCase().split(/\s+/); + var normalized = ''; + var table = { + 'do': 'a', + 'do+': 'b', + 'do#': 'b', + 'doh': 'a', + 'doh+': 'b', + 'doh#': 'b', + 're-': 'b', + 'reb': 'b', + 're': 'c', + 're+': 'd', + 're#': 'd', + 'mi-': 'd', + 'mib': 'd', + 'mi': 'e', + 'fa': 'f', + 'fa+': 'g', + 'fa#': 'g', + 'so-': 'g', + 'sob': 'g', + 'so': 'h', + 'so+': 'i', + 'so#': 'i', + 'sol-': 'g', + 'solb': 'g', + 'sol': 'h', + 'sol+': 'i', + 'sol#': 'i', + 'la-': 'i', + 'lab': 'i', + 'la': 'j', + 'la+': 'k', + 'la#': 'k', + 'ti-': 'k', + 'tib': 'k', + 'ti': 'l', + 'si-': 'k', + 'sib': 'k', + 'si': 'l' + }; + + for (var i = 0; i < tokens.length; i++) { + if (table[tokens[i]]) { + normalized += table[tokens[i]]; + } + } + + return normalized; + }, + + // Take some solfege from the DB and translate it back + // to something readable + denormalize: function(str) { + var table = { + 'a': 'do', + 'b': 'do+', + 'c': 're', + 'd': 're+', + 'e': 'mi', + 'f': 'fa', + 'g': 'fa+', + 'h': 'so', + 'i': 'so+', + 'j': 'la', + 'k': 'la+', + 'l': 'ti' + }; + var ret = ''; + + for (var i = 0; i < str.length; i++) { + ret += table[str.charAt(i)] + ' '; + } + + return ret.trim(); + }, + + // Initialize the SQL database + initializeDatabase: function(db, done) { + + // Fetch the SQL we need to run + fs.readFile('../db/schema.sql', function(err, sql) { + if (err) { + return console.error('Error reading SQL from file', err); + } + + // Execute the query + db.query(sql, function(err, result) { + + if (err) { + console.error('Error executing SQL query', err); + } + + // Run the callback + done(); + }); + }); + } +}; diff --git a/test/test_denormalize.js b/test/test_denormalize.js new file mode 100644 index 0000000..5ac6464 --- /dev/null +++ b/test/test_denormalize.js @@ -0,0 +1,56 @@ +var should = require('should'); +var denormalize = require('../src/utils').denormalize; + +describe('Denormalize', function() { + it('should denormalize a', function() { + denormalize('a').should.be.exactly('do'); + }); + + it('should denormalize b', function() { + denormalize('b').should.be.exactly('do+'); + }); + + it('should denormalize c', function() { + denormalize('c').should.be.exactly('re'); + }); + + it('should denormalize d', function() { + denormalize('d').should.be.exactly('re+'); + }); + + it('should denormalize e', function() { + denormalize('e').should.be.exactly('mi'); + }); + + it('should denormalize f', function() { + denormalize('f').should.be.exactly('fa'); + }); + + it('should denormalize g', function() { + denormalize('g').should.be.exactly('fa+'); + }); + + it('should denormalize h', function() { + denormalize('h').should.be.exactly('so'); + }); + + it('should denormalize i', function() { + denormalize('i').should.be.exactly('so+'); + }); + + it('should denormalize j', function() { + denormalize('j').should.be.exactly('la'); + }); + + it('should denormalize k', function() { + denormalize('k').should.be.exactly('la+'); + }); + + it('should denormalize l', function() { + denormalize('l').should.be.exactly('ti'); + }); + + it('should denormalize multiple syllables', function() { + denormalize('ace').should.be.exactly('do re mi'); + }); +}); diff --git a/test/test_normalize.js b/test/test_normalize.js new file mode 100644 index 0000000..cfc7d15 --- /dev/null +++ b/test/test_normalize.js @@ -0,0 +1,207 @@ +var should = require('should'); +var normalize = require('../src/utils').normalize; + +describe('Normalize', function() { + + describe('Uppercasing', function() { + it('should normalize uppercase input', function() { + normalize('DO').should.be.exactly('a'); + }); + }); + + describe('Basic tests', function() { + + describe('A', function() { + it('should convert do to a', function() { + normalize('do').should.be.exactly('a'); + }); + + it('should convert doh to a', function() { + normalize('doh').should.be.exactly('a'); + }); + }); + + describe('B', function() { + it('should convert do+ to b', function() { + normalize('do+').should.be.exactly('b'); + }); + + it('should convert do# to b', function() { + normalize('do#').should.be.exactly('b'); + }); + + it('should convert doh+ to b', function() { + normalize('doh+').should.be.exactly('b'); + }); + + it('should convert doh# to b', function() { + normalize('doh#').should.be.exactly('b'); + }); + + it('should convert re- to b', function() { + normalize('re-').should.be.exactly('b'); + }); + + it('should convert reb to b', function() { + normalize('reb').should.be.exactly('b'); + }); + }); + + describe('C', function() { + it('should convert re to c', function() { + normalize('re').should.be.exactly('c'); + }); + }); + + describe('D', function() { + it('should convert re+ to d', function() { + normalize('re+').should.be.exactly('d'); + }); + + it('should convert re# to d', function() { + normalize('re#').should.be.exactly('d'); + }); + + it('should convert mi- to d', function() { + normalize('mi-').should.be.exactly('d'); + }); + + it('should convert mib to d', function() { + normalize('mib').should.be.exactly('d'); + }); + }); + + describe('E', function() { + it('should convert mi to e', function() { + normalize('mi').should.be.exactly('e'); + }); + }); + + describe('F', function() { + it('should convert fa to f', function() { + normalize('fa').should.be.exactly('f'); + }); + }); + + describe('G', function() { + it('should convert fa+ to g', function() { + normalize('fa+').should.be.exactly('g'); + }); + + it('should convert fa# to g', function() { + normalize('fa#').should.be.exactly('g'); + }); + + it('should convert so- to g', function() { + normalize('so-').should.be.exactly('g'); + }); + + it('should convert sob to g', function() { + normalize('sob').should.be.exactly('g'); + }); + + it('should convert sol- to g', function() { + normalize('sol-').should.be.exactly('g'); + }); + + it('should convert solb to g', function() { + normalize('solb').should.be.exactly('g'); + }); + }); + + describe('H', function() { + it('should convert so to h', function() { + normalize('so').should.be.exactly('h'); + }); + + it('should convert sol to h', function() { + normalize('sol').should.be.exactly('h'); + }); + }); + + describe('I', function() { + it('should convert so+ to i', function() { + normalize('so+').should.be.exactly('i'); + }); + + it('should convert so# to i', function() { + normalize('so#').should.be.exactly('i'); + }); + + it('should convert sol+ to i', function() { + normalize('sol+').should.be.exactly('i'); + }); + + it('should convert sol# to i', function() { + normalize('sol#').should.be.exactly('i'); + }); + + it('should convert la- to i', function() { + normalize('la-').should.be.exactly('i'); + }); + + it('should convert lab to i', function() { + normalize('lab').should.be.exactly('i'); + }); + }); + + describe('J', function() { + it('should convert la to j', function() { + normalize('la').should.be.exactly('j'); + }); + }); + + describe('K', function() { + it('should convert la+ to k', function() { + normalize('la+').should.be.exactly('k'); + }); + + it('should convert la# to k', function() { + normalize('la#').should.be.exactly('k'); + }); + + it('should convert ti- to k', function() { + normalize('ti-').should.be.exactly('k'); + }); + + it('should convert tib to k', function() { + normalize('tib').should.be.exactly('k'); + }); + + it('should convert si- to k', function() { + normalize('si-').should.be.exactly('k'); + }); + + it('should convert sib to k', function() { + normalize('sib').should.be.exactly('k'); + }); + }); + + describe('L', function() { + it('should convert ti to l', function() { + normalize('ti').should.be.exactly('l'); + }); + + it('should convert si to l', function() { + normalize('si').should.be.exactly('l'); + }); + }); + + }); + + describe('Challenging input', function() { + it('should correctly interpret multiple syllables', function() { + normalize('do re mi').should.be.exactly('ace'); + }); + + it('should correctly tokenize unusual spacing', function() { + normalize('do re mi').should.be.exactly('ace'); + normalize('do\ + re mi').should.be.exactly('ace'); + }); + + it('should ignore non-tokenizable input', function() { + normalize('do asdn3 aweh fnhi re 2hha 1nnua mi').should.be.exactly('ace'); + }); + }); +}); -- cgit v1.2.3