modular-scale
Advanced tools
Comparing version 4.2.0 to 4.3.0
95
index.js
@@ -21,23 +21,30 @@ var msBases = [1]; | ||
doubleOctave : 4 | ||
} | ||
}; | ||
module.exports = function modularscale(options) { | ||
options = options || {} | ||
value = options.value || 0 | ||
bases = options.bases || msBases | ||
ratios = options.ratios || msRatios | ||
options = options || {}; | ||
value = options.value || 0; | ||
bases = options.bases || msBases; | ||
ratios = options.ratios || msRatios; | ||
bases = bases.map(function(base) { | ||
if (typeof base === 'string') { | ||
base = parseFloat(base, 10) || 1; | ||
} | ||
return base; | ||
}); | ||
ratios = ratios.map(function(ratio) { | ||
if (typeof ratio === 'string') { | ||
ratio = ratioNames[ratio] || parseFloat(ratio, 10) || 0 | ||
ratio = ratioNames[ratio] || parseFloat(ratio, 10) || 0; | ||
} | ||
return ratio | ||
return ratio; | ||
}); | ||
return function ms(value) { | ||
var r = [] | ||
var strand = null | ||
var ratio | ||
var base | ||
var i = 0 | ||
var r = []; | ||
var strand = null; | ||
var ratio; | ||
var base; | ||
var i = 0; | ||
@@ -52,29 +59,29 @@ for (ratio = 0; ratio < ratios.length; ratio++) { | ||
// Find lower values on the scale | ||
i = 0 | ||
i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) >= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]) | ||
i-- | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i--; | ||
} | ||
// Find higher possible values on the scale | ||
i = 0 | ||
i = 0; | ||
while(Math.pow(ratios[ratio], i) * bases[base] <= Math.pow(ratios[ratio], value + 1) * bases[base]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]) | ||
i++ | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i++; | ||
} | ||
} else { | ||
// Find values on a negative scale | ||
i = 0 | ||
i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) <= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]) | ||
i++ | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i++; | ||
} | ||
// // Find higher possible values on the scale | ||
i = 0 | ||
i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) >= (Math.pow(ratios[ratio], value - 1) * bases[base])) { | ||
if (Math.pow(ratios[ratio], i) * bases[base] <= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]) | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
} | ||
i-- | ||
i--; | ||
} | ||
@@ -85,27 +92,27 @@ } | ||
r = msUnique(r) | ||
r = msUnique(r); | ||
// reverse array if value is negative | ||
if(value < 0) { | ||
r = r.reverse() | ||
r = r.reverse(); | ||
} | ||
return r[Math.abs(value)][0] | ||
} | ||
} | ||
return round(r[Math.abs(value)][0]); | ||
}; | ||
}; | ||
function msUnique(origArr) { | ||
var x | ||
var y | ||
var lastVal | ||
var i | ||
var x; | ||
var y; | ||
var lastVal; | ||
var i; | ||
origArr = origArr.sort(function(a,b) { | ||
x = a[0] | ||
y = b[0] | ||
return x-y | ||
}) | ||
x = a[0]; | ||
y = b[0]; | ||
return x-y; | ||
}); | ||
newArr = [] | ||
lastVal = null | ||
newArr = []; | ||
lastVal = null; | ||
@@ -115,9 +122,13 @@ for (i = 0; i < origArr.length; i++) { | ||
if (currentVal != lastVal) { | ||
newArr.push(origArr[i]) | ||
newArr.push(origArr[i]); | ||
} | ||
lastVal = currentVal | ||
lastVal = currentVal; | ||
} | ||
return newArr | ||
return newArr; | ||
} | ||
function round(value) { | ||
return Math.round(value * 1000) / 1000; | ||
} | ||
{ | ||
"name": "modular-scale", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Module for generating a modular scale.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
51
test.js
var mocha = require('mocha'), | ||
expect = require('chai').expect, | ||
modularScale = require('./'), | ||
ms | ||
ms; | ||
@@ -12,6 +12,22 @@ describe('Modular Scale', function() { | ||
bases: [1] | ||
}) | ||
expect(ms(3)).to.equal(27) | ||
}); | ||
expect(ms(3)).to.equal(27); | ||
}); | ||
it('should accept bases with units', function() { | ||
ms = modularScale({ | ||
ratios: ['majorTwelfth'], | ||
bases: ['1em'] | ||
}); | ||
expect(ms(3)).to.equal(27); | ||
}); | ||
it('should use accept ratio numbers as strings', function() { | ||
ms = modularScale({ | ||
ratios: ['2.667'], | ||
bases: [1] | ||
}); | ||
expect(ms(3)).to.equal(18.97); | ||
}); | ||
it('should return octave scale with multiple bases', function() { | ||
@@ -21,23 +37,24 @@ ms = modularScale({ | ||
bases: ['16',12] | ||
}) | ||
}); | ||
expect(ms(-1)).to.equal(12) | ||
expect(ms(0)).to.equal(12) | ||
expect(ms('1')).to.equal(16) | ||
expect(ms(2)).to.equal(24) | ||
expect(ms(3)).to.equal(32) | ||
expect(ms('4')).to.equal(48) | ||
expect(ms(-1)).to.equal(12); | ||
expect(ms(0)).to.equal(12); | ||
expect(ms('1')).to.equal(16); | ||
expect(ms(2)).to.equal(24); | ||
expect(ms(3)).to.equal(32); | ||
expect(ms('4')).to.equal(48); | ||
}); | ||
it('should return default scale', function() { | ||
ms = modularScale() | ||
ms = modularScale(); | ||
expect(Math.round(ms(-1) * 1000) / 1000).to.equal(0.618) | ||
expect(Math.round(ms(0) * 1000) / 1000).to.equal(1) | ||
expect(Math.round(ms(1) * 1000) / 1000).to.equal(1.618) | ||
expect(Math.round(ms(2) * 1000) / 1000).to.equal(2.618) | ||
expect(Math.round(ms(3) * 1000) / 1000).to.equal(4.236) | ||
expect(Math.round(ms(4) * 1000) / 1000).to.equal(6.854) | ||
expect(ms(-1)).to.equal(0.618); | ||
expect(ms(0)).to.equal(1); | ||
expect(ms(1)).to.equal(1.618); | ||
expect(ms(2)).to.equal(2.618); | ||
expect(ms(3)).to.equal(4.236); | ||
expect(ms(4)).to.equal(6.854); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7273
159
6