@behaver/angle
Advanced tools
32
index.js
@@ -9,3 +9,3 @@ 'use strict'; | ||
* @author 董 三碗 <qianxing@yeah.net> | ||
* @version 2.0.0 | ||
* @version 2.0.2 | ||
*/ | ||
@@ -70,3 +70,4 @@ class Angle { | ||
getValue(unit) { | ||
if (unit && unit in this.scalesMap) { | ||
if (typeof(unit) !== 'string') throw Error('The param unit should be a String.'); | ||
if (unit in this.scalesMap) { | ||
if (this.cache[unit] === undefined) return this.cache[unit] = this.milliseconds / this.scalesMap[unit]; | ||
@@ -85,5 +86,9 @@ else return this.cache[unit]; | ||
setValue(unit, num) { | ||
this.cache = {}; | ||
this.cache[unit] = num; | ||
this.milliseconds = num * this.scalesMap[unit]; | ||
if (typeof(unit) !== 'string') throw Error('The param unit should be a String.'); | ||
if (typeof(num) !== 'number') throw Error('The param num should be a Number.'); | ||
if (unit in this.scalesMap) { | ||
this.cache = {}; | ||
this.cache[unit] = num; | ||
this.milliseconds = num * this.scalesMap[unit]; | ||
} else throw Error('Illagelity param unit.'); | ||
return this; | ||
@@ -98,3 +103,3 @@ } | ||
getMilliseconds() { | ||
return this.milliseconds; | ||
return this.milliseconds; | ||
} | ||
@@ -109,5 +114,6 @@ | ||
setMilliseconds(num) { | ||
this.cache = {}; | ||
this.milliseconds = num; | ||
return this; | ||
if (typeof(num) !== 'number') throw Error('The param num should be a Number.'); | ||
this.cache = {}; | ||
this.milliseconds = num; | ||
return this; | ||
} | ||
@@ -406,2 +412,3 @@ | ||
parseDACString(str) { | ||
if (typeof(str) !== 'string') throw Error('The param str should be a String.'); | ||
let r = str.match(/^([-+]?\d+)[°|d]\s*(?:(\d+)[′|m]\s*(?:(\d+)(?:\.(\d+))?[″|s]\s*(?:(\d+)ms\s*)?)?)?/); | ||
@@ -451,2 +458,3 @@ if (r) { | ||
parseHACString(str) { | ||
if (typeof(str) !== 'string') throw Error('The param str should be a String.'); | ||
let r = str.match(/^([-+]?\d+)h\s*(?:(\d+)m\s*(?:(\d+)(?:\.(\d+))?s\s*(?:(\d+)ms\s*)?)?)?/); | ||
@@ -493,3 +501,3 @@ if (r) { | ||
* @param {Number} from 限定圆周范围的起始角度数值,缺省为 0 | ||
* @param {Number} unit 设定起始角度数值的 单位,缺省为 'd',有下列可选值: | ||
* @param {String} unit 设定起始角度数值的 单位,缺省为 'd',有下列可选值: | ||
* 'd' 角度 | ||
@@ -507,2 +515,4 @@ * 'm' 角分 | ||
inRound(from = 0, unit = 'd') { | ||
if (typeof(unit) !== 'string') throw Error('The param unit should be a String.'); | ||
if (typeof(from) !== 'number') throw Error('The param from should be a Number.'); | ||
if (unit in this.scalesMap) { | ||
@@ -522,3 +532,3 @@ let from_ms = from * this.scalesMap[unit]; | ||
*/ | ||
toString(unit = 'dac') { | ||
toString() { | ||
return this.makeDACString(); | ||
@@ -525,0 +535,0 @@ } |
{ | ||
"name": "@behaver/angle", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "The entity of angle.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,3 +17,63 @@ const Angle = require('../index'); | ||
describe('#setMilliseconds', () => { | ||
it('The param num should be a Number', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setMilliseconds('d'); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setMilliseconds({}); | ||
}).to.throw(); | ||
}); | ||
}) | ||
describe('#setValue() && #getValue()', () => { | ||
it('The param unit of getValue should be a String', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).getValue(12); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).getValue({}); | ||
}).to.throw(); | ||
}); | ||
it('The param unit of getValue should be valueble', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).getValue('x'); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).getValue(''); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).getValue('ts'); | ||
}).not.to.throw(); | ||
}); | ||
it('The param unit of setValue should be a String', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue(12, 12); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue({}, 12); | ||
}).to.throw(); | ||
}); | ||
it('The param unit of setValue should be valueble', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue('x', 12); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue('', 12); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue('ts', 12); | ||
}).not.to.throw(); | ||
}); | ||
it('The param num of setValue should be a Number', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue('d', 'd'); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).setValue('d', {}); | ||
}).to.throw(); | ||
}); | ||
it('1° = 60′ = 3600″ = 360000ms', () => { | ||
@@ -74,3 +134,18 @@ expect((new Angle(1, 'd')).getMilliseconds()) | ||
describe('#parseDACString()', () => { | ||
describe('#parseDACString(str)', () => { | ||
it('The param str should be a String.', () => { | ||
expect(() => { | ||
(new Angle).parseDACString({}); | ||
}).to.throw(); | ||
expect(() => { | ||
(new Angle).parseDACString(12); | ||
}).to.throw(); | ||
}); | ||
it('The param str should be a HAC String.', () => { | ||
expect(() => { | ||
(new Angle).parseDACString('123'); | ||
}).to.throw(); | ||
}); | ||
it('`parseDACString("128°56′28.45″").getDAComplex()` should equal `{ d: 128, m: 56, s: 28, ms: 450 }`', () => { | ||
@@ -93,3 +168,18 @@ expect((new Angle).parseDACString("128°56′28.45″").getDAComplex()).to.deep.equal({ d: 128, m: 56, s: 28, ms: 450 }); | ||
describe('#parseHACString()', () => { | ||
describe('#parseHACString(str)', () => { | ||
it('The param str should be a String.', () => { | ||
expect(() => { | ||
(new Angle).parseHACString({}); | ||
}).to.throw(); | ||
expect(() => { | ||
(new Angle).parseHACString(12); | ||
}).to.throw(); | ||
}); | ||
it('The param str should be a HAC String.', () => { | ||
expect(() => { | ||
(new Angle).parseHACString('123'); | ||
}).to.throw(); | ||
}); | ||
it('`parseHACString("128h 56m 28s 45ms").getHAComplex()` should equal `{ h: 128, m: 56, s: 28, ms: 45 }`', () => { | ||
@@ -113,2 +203,31 @@ expect((new Angle).parseHACString("128h 56m 28s 45ms").getHAComplex()).to.deep.equal({ h: 128, m: 56, s: 28, ms: 45 }); | ||
describe('#inRound()', () => { | ||
it('The param unit should be a String', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound(12, 12); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound(12, {}); | ||
}).to.throw(); | ||
}); | ||
it('The param unit should be valueble', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound(12, 'x'); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound(12, ''); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound(12, 'ts'); | ||
}).not.to.throw(); | ||
}); | ||
it('The param from should be a Number', () => { | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound('d', 'd'); | ||
}).to.throw(); | ||
expect(() => { | ||
let x = (new Angle(120, 'd')).inRound({}, 'd'); | ||
}).to.throw(); | ||
}); | ||
it('360° inRound from 361° should equal 720°', () => { | ||
@@ -115,0 +234,0 @@ expect((new Angle(360, 'd')).inRound(361, 'd').getDegrees()).equal(720); |
33150
14.55%727
19.57%