feat(core): Cast distance to number. See #2897
This commit is contained in:
parent
693243d8e9
commit
f759f3986a
3 changed files with 47 additions and 25 deletions
|
@ -10,6 +10,7 @@ import {
|
|||
curveEdge,
|
||||
round,
|
||||
__addNonEnumProp,
|
||||
__asNumber,
|
||||
} from './utils.mjs'
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
@ -540,20 +541,8 @@ Path.prototype.noop = function (id = false) {
|
|||
* @return {object} this - The Path instance
|
||||
*/
|
||||
Path.prototype.offset = function (distance) {
|
||||
if (typeof distance !== 'number') {
|
||||
if (typeof distance === 'string') {
|
||||
this.log.error(
|
||||
'Called `Path.offset(distance)` but `distance` is not a number. Will attempt to cast to Number'
|
||||
)
|
||||
try {
|
||||
distance = Number(distance)
|
||||
} catch {
|
||||
this.log.error(
|
||||
'Called `Path.offset(distance)` but `distance` is not a number nor can it be cast to one'
|
||||
)
|
||||
}
|
||||
} else this.log.error('Called `Path.offset(distance)` but `distance` is not a number')
|
||||
}
|
||||
distance = __asNumber(distance, 'distance', 'Path.offset', this.log)
|
||||
|
||||
return __pathOffset(this, distance, this.log)
|
||||
}
|
||||
|
||||
|
@ -661,8 +650,7 @@ Path.prototype.setText = function (text = '', className = false) {
|
|||
* @return {Point} point - The point that lies distance along this Path
|
||||
*/
|
||||
Path.prototype.shiftAlong = function (distance, stepsPerMm = 10) {
|
||||
if (typeof distance !== 'number')
|
||||
this.log.error('Called `Path.shiftAlong(distance)` but `distance` is not a number')
|
||||
distance = __asNumber(distance, 'distance', 'Path.shiftAlong', this.log)
|
||||
let len = 0
|
||||
let current
|
||||
for (let i in this.ops) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Attributes } from './attributes.mjs'
|
||||
import { __isCoord, rad2deg, deg2rad } from './utils.mjs'
|
||||
import { __asNumber, __isCoord, rad2deg, deg2rad } from './utils.mjs'
|
||||
|
||||
//////////////////////////////////////////////
|
||||
// CONSTRUCTOR //
|
||||
|
@ -224,9 +224,8 @@ Point.prototype.setText = function (text = '', className = false) {
|
|||
* @return {Point} shifted - The new shifted Point instance
|
||||
*/
|
||||
Point.prototype.shift = function (deg, dist) {
|
||||
if (typeof deg !== 'number') this.log.warning('Called `Point.shift` but `deg` is not a number')
|
||||
if (typeof dist !== 'number')
|
||||
this.log.warning('Called `Point.shift` but `distance` is not a number')
|
||||
deg = __asNumber(deg, 'deg', 'Point.shift', this.log)
|
||||
dist = __asNumber(dist, 'dist', 'Point.shift', this.log)
|
||||
let p = this.__check().copy()
|
||||
p.x += dist
|
||||
|
||||
|
@ -259,12 +258,11 @@ Point.prototype.shiftFractionTowards = function (that, fraction) {
|
|||
* @return {Point} shifted - The new shifted Point instance
|
||||
*/
|
||||
Point.prototype.shiftOutwards = function (that, distance) {
|
||||
distance = __asNumber(distance, 'distance', 'Point.shiftOutwards', this.log)
|
||||
if (that instanceof Point !== true)
|
||||
this.log.warning(
|
||||
'Called `Point.shiftOutwards(that, distance)` but `that` is not a `Point` object'
|
||||
)
|
||||
if (typeof distance !== 'number')
|
||||
this.log.warning('Called `Point.shiftOutwards(that, distance)` but `distance` is not a number')
|
||||
this.__check()
|
||||
that.__check()
|
||||
|
||||
|
@ -279,8 +277,7 @@ Point.prototype.shiftOutwards = function (that, distance) {
|
|||
* @return {Point} shifted - The new shifted Point instance
|
||||
*/
|
||||
Point.prototype.shiftTowards = function (that, dist) {
|
||||
if (typeof dist !== 'number')
|
||||
this.log.warning('Called `Point.shiftTowards` but `distance` is not a number')
|
||||
dist = __asNumber(dist, 'dist', 'Point.shiftTowards', this.log)
|
||||
if (that instanceof Point !== true)
|
||||
this.log.warning(
|
||||
'Called `Point.shiftTowards(that, distance)` but `that` is not a `Point` object'
|
||||
|
|
|
@ -288,7 +288,14 @@ export function deg2rad(degrees) {
|
|||
* @param {Stack} stack - The Stack instance
|
||||
* @return {string} transform - The SVG transform value
|
||||
*/
|
||||
export const generateStackTransform = (x=0, y=0, rotate=0, flipX=false, flipY=false, stack) => {
|
||||
export const generateStackTransform = (
|
||||
x = 0,
|
||||
y = 0,
|
||||
rotate = 0,
|
||||
flipX = false,
|
||||
flipY = false,
|
||||
stack
|
||||
) => {
|
||||
const transforms = []
|
||||
let xTotal = x || 0
|
||||
let yTotal = y || 0
|
||||
|
@ -603,6 +610,36 @@ export function __addNonEnumProp(obj, name, value) {
|
|||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure a passed argument is a number if it can be cast
|
||||
* Will log warnings/errors accordingly
|
||||
*
|
||||
* @private
|
||||
* @param {mixed} value - The value to check
|
||||
* @param {string} param - The name of the parameter to use in the logs
|
||||
* @param {string} method - The name of the method to use in the logs
|
||||
* @param {object} log - A logging object
|
||||
* @return {bool} result - True if it is a valid coordinate, false when not
|
||||
*/
|
||||
export function __asNumber(value, param, method, log) {
|
||||
if (typeof value === 'number') return value
|
||||
if (typeof value === 'string') {
|
||||
log.warning(
|
||||
`Called \`${method}(${param})\` but \`${param}\` is not a number. Will attempt to cast to Number`
|
||||
)
|
||||
try {
|
||||
value = Number(value)
|
||||
return value
|
||||
} catch {
|
||||
this.log.error(
|
||||
`Called \`${method}(${param})\` but \`${param}\` is not a number nor can it be cast to one`
|
||||
)
|
||||
}
|
||||
} else log.error(`Called \`${method}(${param})\` but \`${param}\` is not a number`)
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the paramater passed to it is a valid coordinate (x and y attribute)
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue