diff options
Diffstat (limited to 'src/number.js')
-rw-r--r-- | src/number.js | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/src/number.js b/src/number.js index 8198e7c..6413f94 100644 --- a/src/number.js +++ b/src/number.js @@ -1,16 +1,16 @@ + // Module for unit convertions SVG.Number = SVG.invent({ // Initialize - create: function(value, unit) { + create: function (value, unit) { // initialize defaults this.value = 0 - this.unit = unit || '' + this.unit = unit || '' // parse value 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(SVG.regex.numberAndUnit) @@ -19,83 +19,77 @@ SVG.Number = SVG.invent({ 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 SVG.Number) { this.value = value.valueOf() - this.unit = value.unit + this.unit = value.unit } } - - } + }, // Add methods -, extend: { + extend: { // Stringalize - toString: function() { - return ( - this.unit == '%' ? - ~~(this.value * 1e8) / 1e6: - this.unit == 's' ? - this.value / 1e3 : - this.value + toString: function () { + return (this.unit === '%' ? ~~(this.value * 1e8) / 1e6 + : this.unit === 's' ? this.value / 1e3 + : this.value ) + this.unit - } - , toJSON: function() { + }, + toJSON: function () { return this.toString() - } - , // Convert to primitive - valueOf: function() { + }, // Convert to primitive + valueOf: function () { return this.value - } + }, // Add number - , plus: function(number) { + plus: function (number) { number = new SVG.Number(number) return new SVG.Number(this + number, this.unit || number.unit) - } + }, // Subtract number - , minus: function(number) { + minus: function (number) { number = new SVG.Number(number) return new SVG.Number(this - number, this.unit || number.unit) - } + }, // Multiply number - , times: function(number) { + times: function (number) { number = new SVG.Number(number) return new SVG.Number(this * number, this.unit || number.unit) - } + }, // Divide number - , divide: function(number) { + divide: function (number) { number = new SVG.Number(number) return new SVG.Number(this / number, this.unit || number.unit) - } + }, // Convert to different unit - , to: function(unit) { + to: function (unit) { var number = new SVG.Number(this) - if (typeof unit === 'string') + if (typeof unit === 'string') { number.unit = unit + } return number - } + }, // Make number morphable - , morph: function(number) { + morph: function (number) { this.destination = new SVG.Number(number) - if(number.relative) { + if (number.relative) { this.destination.value += this.value } return this - } + }, // Get morphed number at given position - , at: function(pos) { + at: function (pos) { // Make sure a destination is defined if (!this.destination) return this |