aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2023-06-13 01:43:37 +0200
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2023-06-13 01:43:37 +0200
commit03322672782a6318b019eff33fe44ec800d6f12c (patch)
tree9d807e1585cf28a1ea98e87b855932dba00f1880 /src/utils
parent8e18b4698edd00f83af0be05bb3374f92fecaeb9 (diff)
downloadsvg.js-03322672782a6318b019eff33fe44ec800d6f12c.tar.gz
svg.js-03322672782a6318b019eff33fe44ec800d6f12c.zip
dependency updates, easier formatting
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/adopter.js34
-rw-r--r--src/utils/methods.js10
-rw-r--r--src/utils/pathParser.js64
-rw-r--r--src/utils/utils.js56
-rw-r--r--src/utils/window.js10
5 files changed, 91 insertions, 83 deletions
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
index 7e1c85e..48814ec 100644
--- a/src/utils/adopter.js
+++ b/src/utils/adopter.js
@@ -8,12 +8,12 @@ const elements = {}
export const root = '___SYMBOL___ROOT___'
// Method for element creation
-export function create (name, ns = svg) {
+export function create(name, ns = svg) {
// create element
return globals.document.createElementNS(ns, name)
}
-export function makeInstance (element, isHTML = false) {
+export function makeInstance(element, isHTML = false) {
if (element instanceof Base) return element
if (typeof element === 'object') {
@@ -41,12 +41,16 @@ export function makeInstance (element, isHTML = false) {
return element
}
-export function nodeOrNew (name, node) {
- return (node && node.ownerDocument && node instanceof node.ownerDocument.defaultView.Node) ? node : create(name)
+export function nodeOrNew(name, node) {
+ return node &&
+ node.ownerDocument &&
+ node instanceof node.ownerDocument.defaultView.Node
+ ? node
+ : create(name)
}
// Adopt existing svg elements
-export function adopt (node) {
+export function adopt(node) {
// check for presence of node
if (!node) return null
@@ -64,7 +68,7 @@ export function adopt (node) {
if (className === 'LinearGradient' || className === 'RadialGradient') {
className = 'Gradient'
- // Fallback to Dom if element is not known
+ // Fallback to Dom if element is not known
} else if (!elements[className]) {
className = 'Dom'
}
@@ -74,11 +78,11 @@ export function adopt (node) {
let adopter = adopt
-export function mockAdopt (mock = adopt) {
+export function mockAdopt(mock = adopt) {
adopter = mock
}
-export function register (element, name = element.name, asRoot = false) {
+export function register(element, name = element.name, asRoot = false) {
elements[name] = element
if (asRoot) elements[root] = element
@@ -87,7 +91,7 @@ export function register (element, name = element.name, asRoot = false) {
return element
}
-export function getClass (name) {
+export function getClass(name) {
return elements[name]
}
@@ -95,12 +99,12 @@ export function getClass (name) {
let did = 1000
// Get next named element id
-export function eid (name) {
- return 'Svgjs' + capitalize(name) + (did++)
+export function eid(name) {
+ return 'Svgjs' + capitalize(name) + did++
}
// Deep new id assignment
-export function assignNewId (node) {
+export function assignNewId(node) {
// do the same for SVG child nodes as well
for (let i = node.children.length - 1; i >= 0; i--) {
assignNewId(node.children[i])
@@ -115,10 +119,10 @@ export function assignNewId (node) {
}
// Method for extending objects
-export function extend (modules, methods) {
+export function extend(modules, methods) {
let key, i
- modules = Array.isArray(modules) ? modules : [ modules ]
+ modules = Array.isArray(modules) ? modules : [modules]
for (i = modules.length - 1; i >= 0; i--) {
for (key in methods) {
@@ -127,7 +131,7 @@ export function extend (modules, methods) {
}
}
-export function wrapWithAttrCheck (fn) {
+export function wrapWithAttrCheck(fn) {
return function (...args) {
const o = args[args.length - 1]
diff --git a/src/utils/methods.js b/src/utils/methods.js
index 45525e3..9f61f91 100644
--- a/src/utils/methods.js
+++ b/src/utils/methods.js
@@ -1,7 +1,7 @@
const methods = {}
const names = []
-export function registerMethods (name, m) {
+export function registerMethods(name, m) {
if (Array.isArray(name)) {
for (const _name of name) {
registerMethods(_name, m)
@@ -20,14 +20,14 @@ export function registerMethods (name, m) {
methods[name] = Object.assign(methods[name] || {}, m)
}
-export function getMethodsFor (name) {
+export function getMethodsFor(name) {
return methods[name] || {}
}
-export function getMethodNames () {
- return [ ...new Set(names) ]
+export function getMethodNames() {
+ return [...new Set(names)]
}
-export function addMethodNames (_names) {
+export function addMethodNames(_names) {
names.push(..._names)
}
diff --git a/src/utils/pathParser.js b/src/utils/pathParser.js
index 1d0ee86..6d3f4e2 100644
--- a/src/utils/pathParser.js
+++ b/src/utils/pathParser.js
@@ -1,57 +1,68 @@
import { isPathLetter } from '../modules/core/regex.js'
import Point from '../types/Point.js'
-const segmentParameters = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 }
+const segmentParameters = {
+ M: 2,
+ L: 2,
+ H: 1,
+ V: 1,
+ C: 6,
+ S: 4,
+ Q: 4,
+ T: 2,
+ A: 7,
+ Z: 0
+}
const pathHandlers = {
M: function (c, p, p0) {
p.x = p0.x = c[0]
p.y = p0.y = c[1]
- return [ 'M', p.x, p.y ]
+ return ['M', p.x, p.y]
},
L: function (c, p) {
p.x = c[0]
p.y = c[1]
- return [ 'L', c[0], c[1] ]
+ return ['L', c[0], c[1]]
},
H: function (c, p) {
p.x = c[0]
- return [ 'H', c[0] ]
+ return ['H', c[0]]
},
V: function (c, p) {
p.y = c[0]
- return [ 'V', c[0] ]
+ return ['V', c[0]]
},
C: function (c, p) {
p.x = c[4]
p.y = c[5]
- return [ 'C', c[0], c[1], c[2], c[3], c[4], c[5] ]
+ return ['C', c[0], c[1], c[2], c[3], c[4], c[5]]
},
S: function (c, p) {
p.x = c[2]
p.y = c[3]
- return [ 'S', c[0], c[1], c[2], c[3] ]
+ return ['S', c[0], c[1], c[2], c[3]]
},
Q: function (c, p) {
p.x = c[2]
p.y = c[3]
- return [ 'Q', c[0], c[1], c[2], c[3] ]
+ return ['Q', c[0], c[1], c[2], c[3]]
},
T: function (c, p) {
p.x = c[0]
p.y = c[1]
- return [ 'T', c[0], c[1] ]
+ return ['T', c[0], c[1]]
},
Z: function (c, p, p0) {
p.x = p0.x
p.y = p0.y
- return [ 'Z' ]
+ return ['Z']
},
A: function (c, p) {
p.x = c[5]
p.y = c[6]
- return [ 'A', c[0], c[1], c[2], c[3], c[4], c[5], c[6] ]
+ return ['A', c[0], c[1], c[2], c[3], c[4], c[5], c[6]]
}
}
@@ -76,26 +87,30 @@ for (let i = 0, il = mlhvqtcsaz.length; i < il; ++i) {
})(mlhvqtcsaz[i].toUpperCase())
}
-function makeAbsolut (parser) {
+function makeAbsolut(parser) {
const command = parser.segment[0]
return pathHandlers[command](parser.segment.slice(1), parser.p, parser.p0)
}
-function segmentComplete (parser) {
- return parser.segment.length && parser.segment.length - 1 === segmentParameters[parser.segment[0].toUpperCase()]
+function segmentComplete(parser) {
+ return (
+ parser.segment.length &&
+ parser.segment.length - 1 ===
+ segmentParameters[parser.segment[0].toUpperCase()]
+ )
}
-function startNewSegment (parser, token) {
+function startNewSegment(parser, token) {
parser.inNumber && finalizeNumber(parser, false)
const pathLetter = isPathLetter.test(token)
if (pathLetter) {
- parser.segment = [ token ]
+ parser.segment = [token]
} else {
const lastCommand = parser.lastCommand
const small = lastCommand.toLowerCase()
const isSmall = lastCommand === small
- parser.segment = [ small === 'm' ? (isSmall ? 'l' : 'L') : lastCommand ]
+ parser.segment = [small === 'm' ? (isSmall ? 'l' : 'L') : lastCommand]
}
parser.inSegment = true
@@ -104,7 +119,7 @@ function startNewSegment (parser, token) {
return pathLetter
}
-function finalizeNumber (parser, inNumber) {
+function finalizeNumber(parser, inNumber) {
if (!parser.inNumber) throw new Error('Parser Error')
parser.number && parser.segment.push(parseFloat(parser.number))
parser.inNumber = inNumber
@@ -117,7 +132,7 @@ function finalizeNumber (parser, inNumber) {
}
}
-function finalizeSegment (parser) {
+function finalizeSegment(parser) {
parser.inSegment = false
if (parser.absolute) {
parser.segment = makeAbsolut(parser)
@@ -125,7 +140,7 @@ function finalizeSegment (parser) {
parser.segments.push(parser.segment)
}
-function isArcFlag (parser) {
+function isArcFlag(parser) {
if (!parser.segment.length) return false
const isArc = parser.segment[0].toUpperCase() === 'A'
const length = parser.segment.length
@@ -133,12 +148,11 @@ function isArcFlag (parser) {
return isArc && (length === 4 || length === 5)
}
-function isExponential (parser) {
+function isExponential(parser) {
return parser.lastToken.toUpperCase() === 'E'
}
-export function pathParser (d, toAbsolute = true) {
-
+export function pathParser(d, toAbsolute = true) {
let index = 0
let token = ''
const parser = {
@@ -155,7 +169,7 @@ export function pathParser (d, toAbsolute = true) {
p: new Point()
}
- while ((parser.lastToken = token, token = d.charAt(index++))) {
+ while (((parser.lastToken = token), (token = d.charAt(index++)))) {
if (!parser.inSegment) {
if (startNewSegment(parser, token)) {
continue
@@ -175,7 +189,6 @@ export function pathParser (d, toAbsolute = true) {
}
if (!isNaN(parseInt(token))) {
-
if (parser.number === '0' || isArcFlag(parser)) {
parser.inNumber = true
parser.number = token
@@ -233,5 +246,4 @@ export function pathParser (d, toAbsolute = true) {
}
return parser.segments
-
}
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 7d08e22..c6e6d3b 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,5 +1,5 @@
// Map function
-export function map (array, block) {
+export function map(array, block) {
let i
const il = array.length
const result = []
@@ -12,7 +12,7 @@ export function map (array, block) {
}
// Filter function
-export function filter (array, block) {
+export function filter(array, block) {
let i
const il = array.length
const result = []
@@ -27,43 +27,43 @@ export function filter (array, block) {
}
// Degrees to radians
-export function radians (d) {
- return d % 360 * Math.PI / 180
+export function radians(d) {
+ return ((d % 360) * Math.PI) / 180
}
// Radians to degrees
-export function degrees (r) {
- return r * 180 / Math.PI % 360
+export function degrees(r) {
+ return ((r * 180) / Math.PI) % 360
}
// Convert dash-separated-string to camelCase
-export function camelCase (s) {
+export function camelCase(s) {
return s.toLowerCase().replace(/-(.)/g, function (m, g) {
return g.toUpperCase()
})
}
// Convert camel cased string to dash separated
-export function unCamelCase (s) {
+export function unCamelCase(s) {
return s.replace(/([A-Z])/g, function (m, g) {
return '-' + g.toLowerCase()
})
}
// Capitalize first letter of a string
-export function capitalize (s) {
+export function capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1)
}
// Calculate proportional width and height values when necessary
-export function proportionalSize (element, width, height, box) {
+export function proportionalSize(element, width, height, box) {
if (width == null || height == null) {
box = box || element.bbox()
if (width == null) {
- width = box.width / box.height * height
+ width = (box.width / box.height) * height
} else if (height == null) {
- height = box.height / box.width * width
+ height = (box.height / box.width) * width
}
}
@@ -77,28 +77,20 @@ export function proportionalSize (element, width, height, box) {
* This function adds support for string origins.
* It searches for an origin in o.origin o.ox and o.originX.
* This way, origin: {x: 'center', y: 50} can be passed as well as ox: 'center', oy: 50
-**/
-export function getOrigin (o, element) {
+ **/
+export function getOrigin(o, element) {
const origin = o.origin
// First check if origin is in ox or originX
- let ox = o.ox != null
- ? o.ox
- : o.originX != null
- ? o.originX
- : 'center'
- let oy = o.oy != null
- ? o.oy
- : o.originY != null
- ? o.originY
- : 'center'
+ let ox = o.ox != null ? o.ox : o.originX != null ? o.originX : 'center'
+ let oy = o.oy != null ? o.oy : o.originY != null ? o.originY : 'center'
// Then check if origin was used and overwrite in that case
if (origin != null) {
- [ ox, oy ] = Array.isArray(origin)
+ ;[ox, oy] = Array.isArray(origin)
? origin
: typeof origin === 'object'
- ? [ origin.x, origin.y ]
- : [ origin, origin ]
+ ? [origin.x, origin.y]
+ : [origin, origin]
}
// Make sure to only call bbox when actually needed
@@ -112,19 +104,19 @@ export function getOrigin (o, element) {
ox = ox.includes('left')
? x
: ox.includes('right')
- ? x + width
- : x + width / 2
+ ? x + width
+ : x + width / 2
}
if (condY) {
oy = oy.includes('top')
? y
: oy.includes('bottom')
- ? y + height
- : y + height / 2
+ ? y + height
+ : y + height / 2
}
}
// Return the origin as it is if it wasn't a string
- return [ ox, oy ]
+ return [ox, oy]
}
diff --git a/src/utils/window.js b/src/utils/window.js
index 626fde3..5009c77 100644
--- a/src/utils/window.js
+++ b/src/utils/window.js
@@ -3,30 +3,30 @@ export const globals = {
document: typeof document === 'undefined' ? null : document
}
-export function registerWindow (win = null, doc = null) {
+export function registerWindow(win = null, doc = null) {
globals.window = win
globals.document = doc
}
const save = {}
-export function saveWindow () {
+export function saveWindow() {
save.window = globals.window
save.document = globals.document
}
-export function restoreWindow () {
+export function restoreWindow() {
globals.window = save.window
globals.document = save.document
}
-export function withWindow (win, fn) {
+export function withWindow(win, fn) {
saveWindow()
registerWindow(win, win.document)
fn(win, win.document)
restoreWindow()
}
-export function getWindow () {
+export function getWindow() {
return globals.window
}