diff options
author | Saivan <savian@me.com> | 2018-11-25 16:21:53 +1300 |
---|---|---|
committer | Saivan <savian@me.com> | 2018-11-25 16:21:53 +1300 |
commit | 62de7d0a1b994b69032a759b796b486e6bc382e3 (patch) | |
tree | 112b19f2903b4dc5b4cf61ebef0d021c6ca2f14d /src/types/SVGNumber.js | |
parent | 2b37d7ba5b4267b39c86f9aba5fb14a1b376e846 (diff) | |
download | svg.js-62de7d0a1b994b69032a759b796b486e6bc382e3.tar.gz svg.js-62de7d0a1b994b69032a759b796b486e6bc382e3.zip |
Changed the esLint rules to avoid silly ternary operators, and to let code breathe!
This commit modifies some of the eslint rules, to allow our code to be a little bit more
readable. This came about because we had a particularly pesky problem, where the code
was indenting ternary operators. This fixes that, and makes it easy to add new rules
to eslint as we please in the future.
Changes
=======
- Rebuilt the library with new eslint rules
- Changed the eslintrc file to a yaml file by default
Diffstat (limited to 'src/types/SVGNumber.js')
-rw-r--r-- | src/types/SVGNumber.js | 94 |
1 files changed, 66 insertions, 28 deletions
diff --git a/src/types/SVGNumber.js b/src/types/SVGNumber.js index ea21cbd..a35ed66 100644 --- a/src/types/SVGNumber.js +++ b/src/types/SVGNumber.js @@ -2,88 +2,126 @@ import { numberAndUnit } from '../modules/core/regex.js' // Module for unit convertions export default class SVGNumber { + // Initialize - constructor (...args) { - this.init(...args) + constructor ( ...args ) { + + this.init( ...args ) + } - init (value, unit) { - unit = Array.isArray(value) ? value[1] : unit - value = Array.isArray(value) ? value[0] : value + init ( value, unit ) { + + unit = Array.isArray( value ) ? value[1] : unit + value = Array.isArray( value ) ? value[0] : value // initialize defaults this.value = 0 this.unit = unit || '' // parse value - if (typeof value === 'number') { + if ( typeof value === 'number' ) { + // ensure a valid numeric value - this.value = isNaN(value) ? 0 : !isFinite(value) ? (value < 0 ? -3.4e+38 : +3.4e+38) : value - } else if (typeof value === 'string') { - unit = value.match(numberAndUnit) + this.value = isNaN( value ) ? 0 : !isFinite( value ) ? ( value < 0 ? -3.4e+38 : +3.4e+38 ) : value + + } else if ( typeof value === 'string' ) { + + unit = value.match( numberAndUnit ) + + if ( unit ) { - if (unit) { // make value numeric - this.value = parseFloat(unit[1]) + this.value = parseFloat( unit[1] ) // normalize - if (unit[5] === '%') { this.value /= 100 } else if (unit[5] === 's') { + if ( unit[5] === '%' ) { + + this.value /= 100 + + } else if ( unit[5] === 's' ) { + this.value *= 1000 + } // store unit this.unit = unit[5] + } + } else { - if (value instanceof SVGNumber) { + + if ( value instanceof SVGNumber ) { + this.value = value.valueOf() this.unit = value.unit + } + } return this + } toString () { - return (this.unit === '%' ? ~~(this.value * 1e8) / 1e6 + + return ( this.unit === '%' ? ~~( this.value * 1e8 ) / 1e6 : this.unit === 's' ? this.value / 1e3 - : this.value + : this.value ) + this.unit + } toJSON () { + return this.toString() + } toArray () { - return [this.value, this.unit] + + return [ this.value, this.unit ] + } valueOf () { + return this.value + } // Add number - plus (number) { - number = new SVGNumber(number) - return new SVGNumber(this + number, this.unit || number.unit) + plus ( number ) { + + number = new SVGNumber( number ) + return new SVGNumber( this + number, this.unit || number.unit ) + } // Subtract number - minus (number) { - number = new SVGNumber(number) - return new SVGNumber(this - number, this.unit || number.unit) + minus ( number ) { + + number = new SVGNumber( number ) + return new SVGNumber( this - number, this.unit || number.unit ) + } // Multiply number - times (number) { - number = new SVGNumber(number) - return new SVGNumber(this * number, this.unit || number.unit) + times ( number ) { + + number = new SVGNumber( number ) + return new SVGNumber( this * number, this.unit || number.unit ) + } // Divide number - divide (number) { - number = new SVGNumber(number) - return new SVGNumber(this / number, this.unit || number.unit) + divide ( number ) { + + number = new SVGNumber( number ) + return new SVGNumber( this / number, this.unit || number.unit ) + } + } |