modular-scale
Advanced tools
Comparing version 4.4.1 to 5.0.1
176
index.js
@@ -1,140 +0,58 @@ | ||
var defaultBase = 1; | ||
var defaultRatio = ((1+ Math.sqrt(5)) * 0.5); | ||
var msBases = [defaultBase]; | ||
var msRatios = [defaultRatio]; | ||
var defaultBase = 1 | ||
var defaultRatio = 'goldenSection' | ||
var ratioNames = { | ||
minorSecond : 1.067, | ||
majorSecond : 1.125, | ||
minorThird : 1.2, | ||
majorThird : 1.25, | ||
perfectFourth : 1.333, | ||
augFourth : 1.414, | ||
perfectFifth : 1.5, | ||
minorSixth : 1.6, | ||
goldenSection : 1.618, | ||
majorSixth : 1.667, | ||
minorSeventh : 1.778, | ||
majorSeventh : 1.875, | ||
octave : 2, | ||
majorTenth : 2.5, | ||
majorEleventh : 2.667, | ||
majorTwelfth : 3, | ||
doubleOctave : 4 | ||
}; | ||
minorSecond: 1.067, | ||
majorSecond: 1.125, | ||
minorThird: 1.2, | ||
majorThird: 1.25, | ||
perfectFourth: 1.333, | ||
augFourth: 1.414, | ||
perfectFifth: 1.5, | ||
minorSixth: 1.6, | ||
goldenSection: 1.618, | ||
majorSixth: 1.667, | ||
minorSeventh: 1.778, | ||
majorSeventh: 1.875, | ||
octave: 2, | ||
majorTenth: 2.5, | ||
majorEleventh: 2.667, | ||
majorTwelfth: 3, | ||
doubleOctave: 4 | ||
} | ||
module.exports = function modularscale(options) { | ||
options = options || {}; | ||
value = options.value || 0; | ||
bases = options.bases && | ||
Array.isArray(options.bases) && | ||
options.bases.length? | ||
options.bases: msBases; | ||
ratios = options.ratios && | ||
Array.isArray(options.ratios) && | ||
options.ratios.length? | ||
options.ratios: msRatios; | ||
module.exports = function modularscale (options) { | ||
options = options || {} | ||
var base = options.base || defaultBase | ||
var ratio = options.ratio || defaultRatio | ||
bases = bases.map(function(base) { | ||
if (typeof base === 'string') { | ||
base = parseFloat(base, 10); | ||
if (typeof base === 'string') { | ||
base = parseFloat(base, 10) | ||
if (Number.isNaN(base)) { | ||
base = defaultBase | ||
} | ||
return base || defaultBase; | ||
}); | ||
} | ||
ratios = ratios.map(function(ratio) { | ||
if (typeof ratio === 'string') { | ||
ratio = ratioNames[ratio]? ratioNames[ratio]: | ||
parseFloat(ratio, 10); | ||
} | ||
if (typeof ratio === 'string') { | ||
ratio = ratioNames[ratio] | ||
? ratioNames[ratio] | ||
: parseFloat(ratio, 10) | ||
} | ||
return ratio || defaultRatio; | ||
}); | ||
return function ms (value) { | ||
return value > 0 | ||
? up(value) | ||
: down(value * -1) | ||
} | ||
return function ms(value) { | ||
var r = []; | ||
var strand = null; | ||
var ratio; | ||
var base; | ||
var i = 0; | ||
function up (value) { | ||
return round(Math.pow(ratio, value) * base) | ||
} | ||
for (ratio = 0; ratio < ratios.length; ratio++) { | ||
for (base = 0; base < bases.length; base++) { | ||
function down (value) { | ||
return round(base / Math.pow(ratio, value)) | ||
} | ||
strand = (base + ratio); | ||
// Find values on a positive scale | ||
if (value >= 0) { | ||
// Find lower values on the scale | ||
i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) >= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i--; | ||
} | ||
// Find higher possible values on the scale | ||
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++; | ||
} | ||
} else { | ||
// Find values on a negative scale | ||
i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) <= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i++; | ||
} | ||
// // Find higher possible values on the scale | ||
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]); | ||
} | ||
i--; | ||
} | ||
} | ||
} | ||
} | ||
r = msUnique(r); | ||
// reverse array if value is negative | ||
if(value < 0) { | ||
r = r.reverse(); | ||
} | ||
return round(r[Math.abs(value)][0]); | ||
}; | ||
}; | ||
function msUnique(origArr) { | ||
var x; | ||
var y; | ||
var lastVal; | ||
var i; | ||
origArr = origArr.sort(function(a,b) { | ||
x = a[0]; | ||
y = b[0]; | ||
return x-y; | ||
}); | ||
newArr = []; | ||
lastVal = null; | ||
for (i = 0; i < origArr.length; i++) { | ||
var currentVal = origArr[i][0]; | ||
if (currentVal != lastVal) { | ||
newArr.push(origArr[i]); | ||
} | ||
lastVal = currentVal; | ||
function round (value) { | ||
return Math.round(value * 1000) / 1000 | ||
} | ||
return newArr; | ||
} | ||
function round(value) { | ||
return Math.round(value * 1000) / 1000; | ||
} | ||
{ | ||
"name": "modular-scale", | ||
"version": "4.4.1", | ||
"version": "5.0.1", | ||
"description": "Module for generating a modular scale.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test.js" | ||
"test": "tape test.js | tap-format-spec" | ||
}, | ||
@@ -27,5 +27,6 @@ "repository": { | ||
"devDependencies": { | ||
"@tap-format/spec": "^0.2.0", | ||
"chai": "^2.3.0", | ||
"mocha": "^1.21.4" | ||
"tape": "^4.9.0" | ||
} | ||
} |
@@ -7,27 +7,25 @@ modular-scale | ||
Install | ||
install | ||
------- | ||
`npm i --save` | ||
`npm i modular-scale --save` | ||
Use | ||
use | ||
--- | ||
> To generate relative values use target / base font size | ||
Example: | ||
1 = 16px ( target ) / 16px ( base font size ) | ||
0.75 = 12px ( target ) / 16px ( base font size ) | ||
Generates `em` or `rem` relative values | ||
``` | ||
var modularScale = require('modular-scale'), | ||
ms = modularScale({ | ||
ratios: [2], | ||
bases: [1,0.75] | ||
ratio: 'goldenSection', | ||
base: '16px' | ||
}) | ||
ms(4) //48 | ||
ms(4) // 109.656 | ||
``` | ||
notes | ||
----- | ||
Default output is in pixels, but you can get relative units by dividing the returned value by the base font-size. | ||
> ms(4) = 109.656px / 16px = 6.854em|rem | ||
Inspiration | ||
@@ -34,0 +32,0 @@ ----------- |
143
test.js
@@ -1,86 +0,73 @@ | ||
var mocha = require('mocha'), | ||
expect = require('chai').expect, | ||
modularScale = require('./'), | ||
ms; | ||
var test = require('tape') | ||
var modularScale = require('./') | ||
describe('Modular Scale', function() { | ||
test('Modular Scale', t => { | ||
var ms = modularScale({}) | ||
t.ok(ms, 'exists') | ||
t.end() | ||
}) | ||
it('should use ratio names', function() { | ||
ms = modularScale({ | ||
ratios: ['majorTwelfth'], | ||
bases: [1] | ||
}); | ||
expect(ms(3)).to.equal(27); | ||
}); | ||
test('should use ratio names', t => { | ||
var ms = modularScale({ | ||
ratio: 'majorTwelfth', | ||
base: 1 | ||
}) | ||
t.equals(ms(3), 27) | ||
t.end() | ||
}) | ||
it('should accept bases with units', function() { | ||
ms = modularScale({ | ||
ratios: ['majorTwelfth'], | ||
bases: ['1em'] | ||
}); | ||
expect(ms(3)).to.equal(27); | ||
}); | ||
test('should accept bases with units', t => { | ||
var ms = modularScale({ | ||
ratio: 'majorTwelfth', | ||
base: '1em' | ||
}) | ||
t.equals(ms(3), 27) | ||
t.end() | ||
}) | ||
it('should accept ratio numbers as strings', function() { | ||
ms = modularScale({ | ||
ratios: ['2.667'], | ||
bases: [1] | ||
}); | ||
expect(ms(3)).to.equal(18.97); | ||
}); | ||
test('should accept ratio numbers as strings', t => { | ||
var ms = modularScale({ | ||
ratio: '2.667', | ||
base: 1 | ||
}) | ||
t.equals(ms(3), 18.97) | ||
t.end() | ||
}) | ||
it('should return octave scale with multiple bases', function() { | ||
ms = modularScale({ | ||
ratios: [2], | ||
bases: ['16',12] | ||
}); | ||
test('should return default scale', t => { | ||
var ms = modularScale() | ||
t.equals(ms(-2), 0.382, 'ms(-2)') | ||
t.equals(ms(-1), 0.618, 'ms(-1)') | ||
t.equals(ms(1), 1.618, 'ms(1)') | ||
t.equals(ms(2), 2.618, 'ms(2)') | ||
t.equals(ms(3), 4.236, 'ms(3)') | ||
t.end() | ||
}) | ||
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); | ||
}); | ||
test('should return specified scale', t => { | ||
var ms = modularScale({ | ||
ratio: 'perfectFourth', | ||
base: 18 | ||
}) | ||
t.equals(ms(0), 18, 'ms(0)') | ||
t.equals(ms(1), 23.994, 'ms(1)') | ||
t.end() | ||
}) | ||
it('should return default scale', function() { | ||
ms = modularScale(); | ||
test('should not bomb on empty bases and ratios', t => { | ||
var ms = modularScale({ | ||
ratio: undefined, | ||
base: undefined | ||
}) | ||
t.equals(ms(1), 1.618, 'ms(1)') | ||
t.end() | ||
}) | ||
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); | ||
}); | ||
it('should not bomb on empty bases and ratios', function() { | ||
ms = modularScale({ | ||
ratios: [], | ||
bases: [] | ||
}); | ||
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); | ||
}); | ||
it('should not bomb when pased completely wrong values', function() { | ||
ms = modularScale({ | ||
ratios: [0], | ||
bases: 'fuuuu' | ||
}); | ||
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); | ||
}); | ||
}); | ||
test('should not bomb when pased completely wrong values', t => { | ||
var ms = modularScale({ | ||
ratio: 0, | ||
base: 'nope' | ||
}) | ||
t.equals(ms(1), 1.618) | ||
t.end() | ||
}) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
5102
3
5
116
34
1