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/modules/core/attr.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/modules/core/attr.js')
-rw-r--r-- | src/modules/core/attr.js | 104 |
1 files changed, 70 insertions, 34 deletions
diff --git a/src/modules/core/attr.js b/src/modules/core/attr.js index f90dcb9..7cb9e2a 100644 --- a/src/modules/core/attr.js +++ b/src/modules/core/attr.js @@ -5,77 +5,113 @@ import SVGArray from '../../types/SVGArray.js' import SVGNumber from '../../types/SVGNumber.js' const hooks = [] -export function registerAttrHook (fn) { - hooks.push(fn) +export function registerAttrHook ( fn ) { + + hooks.push( fn ) + } // Set svg element attribute -export default function attr (attr, val, ns) { +export default function attr ( attr, val, ns ) { + // act as full getter - if (attr == null) { + if ( attr == null ) { + // get an object of attributes attr = {} val = this.node.attributes - for (let node of val) { - attr[node.nodeName] = isNumber.test(node.nodeValue) - ? parseFloat(node.nodeValue) + for ( let node of val ) { + + attr[node.nodeName] = isNumber.test( node.nodeValue ) + ? parseFloat( node.nodeValue ) : node.nodeValue + } return attr - } else if (attr instanceof Array) { + + } else if ( attr instanceof Array ) { + // loop through array and get all values - return attr.reduce((last, curr) => { - last[curr] = this.attr(curr) + return attr.reduce( ( last, curr ) => { + + last[curr] = this.attr( curr ) return last - }, {}) - } else if (typeof attr === 'object') { + + }, {} ) + + } else if ( typeof attr === 'object' ) { + // apply every attribute individually if an object is passed - for (val in attr) this.attr(val, attr[val]) - } else if (val === null) { + for ( val in attr ) this.attr( val, attr[val] ) + + } else if ( val === null ) { + // remove value - this.node.removeAttribute(attr) - } else if (val == null) { + this.node.removeAttribute( attr ) + + } else if ( val == null ) { + // act as a getter if the first and only argument is not an object - val = this.node.getAttribute(attr) + val = this.node.getAttribute( attr ) return val == null ? defaults[attr] - : isNumber.test(val) ? parseFloat(val) - : val + : isNumber.test( val ) ? parseFloat( val ) + : val + } else { + // Loop through hooks and execute them to convert value - val = hooks.reduce((_val, hook) => { - return hook(attr, _val, this) - }, val) + val = hooks.reduce( ( _val, hook ) => { + + return hook( attr, _val, this ) + + }, val ) // ensure correct numeric values (also accepts NaN and Infinity) - if (typeof val === 'number') { - val = new SVGNumber(val) - } else if (Color.isColor(val)) { + if ( typeof val === 'number' ) { + + val = new SVGNumber( val ) + + } else if ( Color.isColor( val ) ) { + // ensure full hex color - val = new Color(val) - } else if (val.constructor === Array) { + val = new Color( val ) + + } else if ( val.constructor === Array ) { + // Check for plain arrays and parse array values - val = new SVGArray(val) + val = new SVGArray( val ) + } // if the passed attribute is leading... - if (attr === 'leading') { + if ( attr === 'leading' ) { + // ... call the leading method instead - if (this.leading) { - this.leading(val) + if ( this.leading ) { + + this.leading( val ) + } + } else { + // set given attribute on node - typeof ns === 'string' ? this.node.setAttributeNS(ns, attr, val.toString()) - : this.node.setAttribute(attr, val.toString()) + typeof ns === 'string' ? this.node.setAttributeNS( ns, attr, val.toString() ) + : this.node.setAttribute( attr, val.toString() ) + } // rebuild if required - if (this.rebuild && (attr === 'font-size' || attr === 'x')) { + if ( this.rebuild && ( attr === 'font-size' || attr === 'x' ) ) { + this.rebuild() + } + } return this + } |