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,
|
curveEdge,
|
||||||
round,
|
round,
|
||||||
__addNonEnumProp,
|
__addNonEnumProp,
|
||||||
|
__asNumber,
|
||||||
} from './utils.mjs'
|
} from './utils.mjs'
|
||||||
|
|
||||||
//////////////////////////////////////////////
|
//////////////////////////////////////////////
|
||||||
|
@ -540,20 +541,8 @@ Path.prototype.noop = function (id = false) {
|
||||||
* @return {object} this - The Path instance
|
* @return {object} this - The Path instance
|
||||||
*/
|
*/
|
||||||
Path.prototype.offset = function (distance) {
|
Path.prototype.offset = function (distance) {
|
||||||
if (typeof distance !== 'number') {
|
distance = __asNumber(distance, 'distance', 'Path.offset', this.log)
|
||||||
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')
|
|
||||||
}
|
|
||||||
return __pathOffset(this, distance, 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
|
* @return {Point} point - The point that lies distance along this Path
|
||||||
*/
|
*/
|
||||||
Path.prototype.shiftAlong = function (distance, stepsPerMm = 10) {
|
Path.prototype.shiftAlong = function (distance, stepsPerMm = 10) {
|
||||||
if (typeof distance !== 'number')
|
distance = __asNumber(distance, 'distance', 'Path.shiftAlong', this.log)
|
||||||
this.log.error('Called `Path.shiftAlong(distance)` but `distance` is not a number')
|
|
||||||
let len = 0
|
let len = 0
|
||||||
let current
|
let current
|
||||||
for (let i in this.ops) {
|
for (let i in this.ops) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Attributes } from './attributes.mjs'
|
import { Attributes } from './attributes.mjs'
|
||||||
import { __isCoord, rad2deg, deg2rad } from './utils.mjs'
|
import { __asNumber, __isCoord, rad2deg, deg2rad } from './utils.mjs'
|
||||||
|
|
||||||
//////////////////////////////////////////////
|
//////////////////////////////////////////////
|
||||||
// CONSTRUCTOR //
|
// CONSTRUCTOR //
|
||||||
|
@ -224,9 +224,8 @@ Point.prototype.setText = function (text = '', className = false) {
|
||||||
* @return {Point} shifted - The new shifted Point instance
|
* @return {Point} shifted - The new shifted Point instance
|
||||||
*/
|
*/
|
||||||
Point.prototype.shift = function (deg, dist) {
|
Point.prototype.shift = function (deg, dist) {
|
||||||
if (typeof deg !== 'number') this.log.warning('Called `Point.shift` but `deg` is not a number')
|
deg = __asNumber(deg, 'deg', 'Point.shift', this.log)
|
||||||
if (typeof dist !== 'number')
|
dist = __asNumber(dist, 'dist', 'Point.shift', this.log)
|
||||||
this.log.warning('Called `Point.shift` but `distance` is not a number')
|
|
||||||
let p = this.__check().copy()
|
let p = this.__check().copy()
|
||||||
p.x += dist
|
p.x += dist
|
||||||
|
|
||||||
|
@ -259,12 +258,11 @@ Point.prototype.shiftFractionTowards = function (that, fraction) {
|
||||||
* @return {Point} shifted - The new shifted Point instance
|
* @return {Point} shifted - The new shifted Point instance
|
||||||
*/
|
*/
|
||||||
Point.prototype.shiftOutwards = function (that, distance) {
|
Point.prototype.shiftOutwards = function (that, distance) {
|
||||||
|
distance = __asNumber(distance, 'distance', 'Point.shiftOutwards', this.log)
|
||||||
if (that instanceof Point !== true)
|
if (that instanceof Point !== true)
|
||||||
this.log.warning(
|
this.log.warning(
|
||||||
'Called `Point.shiftOutwards(that, distance)` but `that` is not a `Point` object'
|
'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()
|
this.__check()
|
||||||
that.__check()
|
that.__check()
|
||||||
|
|
||||||
|
@ -279,8 +277,7 @@ Point.prototype.shiftOutwards = function (that, distance) {
|
||||||
* @return {Point} shifted - The new shifted Point instance
|
* @return {Point} shifted - The new shifted Point instance
|
||||||
*/
|
*/
|
||||||
Point.prototype.shiftTowards = function (that, dist) {
|
Point.prototype.shiftTowards = function (that, dist) {
|
||||||
if (typeof dist !== 'number')
|
dist = __asNumber(dist, 'dist', 'Point.shiftTowards', this.log)
|
||||||
this.log.warning('Called `Point.shiftTowards` but `distance` is not a number')
|
|
||||||
if (that instanceof Point !== true)
|
if (that instanceof Point !== true)
|
||||||
this.log.warning(
|
this.log.warning(
|
||||||
'Called `Point.shiftTowards(that, distance)` but `that` is not a `Point` object'
|
'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
|
* @param {Stack} stack - The Stack instance
|
||||||
* @return {string} transform - The SVG transform value
|
* @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 = []
|
const transforms = []
|
||||||
let xTotal = x || 0
|
let xTotal = x || 0
|
||||||
let yTotal = y || 0
|
let yTotal = y || 0
|
||||||
|
@ -603,6 +610,36 @@ export function __addNonEnumProp(obj, name, value) {
|
||||||
return obj
|
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)
|
* 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