refactor: ♻️ refactor file/exports name
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Entity } from '../../types'
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { When, Options } from '../types'
|
||||
import { Easing } from '../stepper/easing'
|
||||
import { Morpher } from '../morpher/morpher'
|
||||
@ -553,7 +553,7 @@ export namespace Animator {
|
||||
|
||||
export function mixin(...source: any[]) {
|
||||
return (ctor: Registry.Definition) => {
|
||||
Obj.applyMixins(ctor, ...source)
|
||||
Mixin.applyMixins(ctor, ...source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { Dom } from '../../element/dom/dom'
|
||||
import { Primer } from '../../element/dom/primer'
|
||||
import { When, Options } from '../types'
|
||||
@ -29,4 +29,4 @@ declare module '../../element/dom/dom' {
|
||||
interface Dom<TNode extends Node = Node> extends ElementExtension<TNode> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Dom, ElementExtension)
|
||||
Mixin.applyMixins(Dom, ElementExtension)
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Morphable } from './morphable'
|
||||
|
||||
export class MorphableUnitNumber
|
||||
extends UNumber
|
||||
implements Morphable<UNumber.UNumberArray, number> {
|
||||
fromArray(arr: UNumber.UNumberArray) {
|
||||
extends UnitNumber
|
||||
implements Morphable<UnitNumber.UnitNumberArray, number> {
|
||||
fromArray(arr: UnitNumber.UnitNumberArray) {
|
||||
this.unit = arr[1] || ''
|
||||
if (typeof arr[0] === 'string') {
|
||||
const obj = UNumber.parse(arr[0])
|
||||
const obj = UnitNumber.parse(arr[0])
|
||||
if (obj) {
|
||||
this.value = obj.value
|
||||
this.unit = obj.unit
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { Dom } from '../../element/dom/dom'
|
||||
import { Timeline } from './timeline'
|
||||
|
||||
@ -25,4 +25,4 @@ declare module '../../element/dom/primer' {
|
||||
interface Primer<TNode extends Node = Node> extends ElementExtension {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Dom, ElementExtension)
|
||||
Mixin.applyMixins(Dom, ElementExtension)
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../util/obj'
|
||||
import { Mixin } from '../util/mixin'
|
||||
import { Registry } from './registry'
|
||||
|
||||
export abstract class Base {}
|
||||
@ -12,7 +12,7 @@ export namespace Base {
|
||||
|
||||
export function mixin(...source: any[]) {
|
||||
return (ctor: Registry.Definition) => {
|
||||
Obj.applyMixins(ctor, ...source)
|
||||
Mixin.applyMixins(ctor, ...source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Box } from '../../struct/box'
|
||||
import { Point } from '../../struct/point'
|
||||
import { Matrix } from '../../struct/matrix'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { VectorElement } from '../element'
|
||||
import { Util } from '../util'
|
||||
import { Container } from './container'
|
||||
@ -16,7 +16,7 @@ export abstract class GeometryContainer<
|
||||
// Translate childs matrix by amount and
|
||||
// transform it back into parents space
|
||||
const matrix = m
|
||||
.translate(UNumber.toNumber(dx), UNumber.toNumber(dy))
|
||||
.translate(UnitNumber.toNumber(dx), UnitNumber.toNumber(dy))
|
||||
.transform(m.inverse())
|
||||
// Calculate new x and y from old box
|
||||
const p = new Point(bbox.x, bbox.y).transform(matrix)
|
||||
@ -28,8 +28,8 @@ export abstract class GeometryContainer<
|
||||
}
|
||||
|
||||
move(x: number | string = 0, y: number | string = 0, box = this.bbox()) {
|
||||
const dx = UNumber.toNumber(x) - box.x
|
||||
const dy = UNumber.toNumber(y) - box.y
|
||||
const dx = UnitNumber.toNumber(x) - box.x
|
||||
const dy = UnitNumber.toNumber(y) - box.y
|
||||
|
||||
return this.dmove(dx, dy)
|
||||
}
|
||||
@ -66,8 +66,8 @@ export abstract class GeometryContainer<
|
||||
box = this.bbox(),
|
||||
) {
|
||||
const size = Util.proportionalSize(this, width, height, box)
|
||||
const sx = UNumber.toNumber(size.width) / box.width
|
||||
const sy = UNumber.toNumber(size.height) / box.height
|
||||
const sx = UnitNumber.toNumber(size.width) / box.width
|
||||
const sy = UnitNumber.toNumber(size.height) / box.height
|
||||
|
||||
this.eachChild<VectorElement>((child) => {
|
||||
const o = new Point(box).transform(new Matrix(child).inverse())
|
||||
@ -83,7 +83,7 @@ export abstract class GeometryContainer<
|
||||
if (width == null) {
|
||||
return box.width
|
||||
}
|
||||
return this.size(new UNumber(width).value, box.height, box)
|
||||
return this.size(new UnitNumber(width).value, box.height, box)
|
||||
}
|
||||
|
||||
height(): number
|
||||
@ -92,6 +92,6 @@ export abstract class GeometryContainer<
|
||||
if (height == null) {
|
||||
return box.height
|
||||
}
|
||||
return this.size(box.width, new UNumber(height).value, box)
|
||||
return this.size(box.width, new UnitNumber(height).value, box)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
// containers
|
||||
import { ContainerExtension as AExtension } from './a-ext'
|
||||
import { ContainerExtension as GExtension } from './g-ext'
|
||||
@ -52,7 +52,7 @@ declare module './container' {
|
||||
ForeignObjectExtension<TSVGElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(
|
||||
Mixin.applyMixins(
|
||||
Container,
|
||||
AExtension,
|
||||
GExtension,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { DefsExtension as MaskExtension } from './mask-ext'
|
||||
import { DefsExtension as MarkerExtension } from './marker-ext'
|
||||
import { DefsExtension as PatternExtension } from './pattern-ext'
|
||||
@ -35,7 +35,7 @@ declare module './defs' {
|
||||
PolylineExtension<SVGDefsElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(
|
||||
Mixin.applyMixins(
|
||||
Defs,
|
||||
MaskExtension,
|
||||
MarkerExtension,
|
||||
|
@ -1,23 +1,23 @@
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { VectorElement } from '../element'
|
||||
|
||||
@Stop.register('Stop')
|
||||
export class Stop extends VectorElement<SVGStopElement> {
|
||||
update(
|
||||
offset?: number | string | UNumber,
|
||||
offset?: number | string | UnitNumber,
|
||||
color?: string,
|
||||
opacity?: number | string | UNumber,
|
||||
opacity?: number | string | UnitNumber,
|
||||
): this
|
||||
update(options: Stop.Options): this
|
||||
update(
|
||||
offset?: Stop.Options | number | string | UNumber,
|
||||
offset?: Stop.Options | number | string | UnitNumber,
|
||||
color?: string,
|
||||
opacity?: number | string | UNumber,
|
||||
opacity?: number | string | UnitNumber,
|
||||
): this
|
||||
update(
|
||||
offset?: Stop.Options | number | string | UNumber,
|
||||
offset?: Stop.Options | number | string | UnitNumber,
|
||||
color?: string,
|
||||
opacity?: number | string | UNumber,
|
||||
opacity?: number | string | UnitNumber,
|
||||
) {
|
||||
const options: {
|
||||
offset?: number
|
||||
@ -29,26 +29,26 @@ export class Stop extends VectorElement<SVGStopElement> {
|
||||
offset == null ||
|
||||
typeof offset === 'number' ||
|
||||
typeof offset === 'string' ||
|
||||
offset instanceof UNumber
|
||||
offset instanceof UnitNumber
|
||||
) {
|
||||
if (offset != null) {
|
||||
options.offset = UNumber.create(offset).value
|
||||
options.offset = UnitNumber.create(offset).value
|
||||
}
|
||||
if (color != null) {
|
||||
options.color = color
|
||||
}
|
||||
if (opacity != null) {
|
||||
options.opacity = UNumber.create(opacity).value
|
||||
options.opacity = UnitNumber.create(opacity).value
|
||||
}
|
||||
} else {
|
||||
if (offset.offset != null) {
|
||||
options.offset = UNumber.create(offset.offset).value
|
||||
options.offset = UnitNumber.create(offset.offset).value
|
||||
}
|
||||
if (offset.color != null) {
|
||||
options.color = offset.color
|
||||
}
|
||||
if (offset.opacity != null) {
|
||||
options.opacity = UNumber.create(offset.opacity).value
|
||||
options.opacity = UnitNumber.create(offset.opacity).value
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,8 +68,8 @@ export class Stop extends VectorElement<SVGStopElement> {
|
||||
|
||||
export namespace Stop {
|
||||
export interface Options {
|
||||
offset?: number | string | UNumber
|
||||
offset?: number | string | UnitNumber
|
||||
color?: string
|
||||
opacity?: number | string | UNumber
|
||||
opacity?: number | string | UnitNumber
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Attrs } from '../../types'
|
||||
import { DomUtil } from '../../util/dom'
|
||||
import { Box } from '../../struct/box'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { VectorElement } from '../element'
|
||||
import { Container } from './container'
|
||||
import { Stop } from './gradient-stop'
|
||||
@ -27,37 +27,37 @@ export class Gradient extends Container<
|
||||
from(x: number | string, y: number | string) {
|
||||
return this.type === 'radialGradient'
|
||||
? this.attr({
|
||||
fx: UNumber.create(x).toString(),
|
||||
fy: UNumber.create(y).toString(),
|
||||
fx: UnitNumber.create(x).toString(),
|
||||
fy: UnitNumber.create(y).toString(),
|
||||
})
|
||||
: this.attr({
|
||||
x1: UNumber.create(x).toString(),
|
||||
y1: UNumber.create(y).toString(),
|
||||
x1: UnitNumber.create(x).toString(),
|
||||
y1: UnitNumber.create(y).toString(),
|
||||
})
|
||||
}
|
||||
|
||||
to(x: number | string, y: number | string) {
|
||||
return this.type === 'radialGradient'
|
||||
? this.attr({
|
||||
cx: UNumber.create(x).toString(),
|
||||
cy: UNumber.create(x).toString(),
|
||||
cx: UnitNumber.create(x).toString(),
|
||||
cy: UnitNumber.create(x).toString(),
|
||||
})
|
||||
: this.attr({
|
||||
x2: UNumber.create(y).toString(),
|
||||
y2: UNumber.create(y).toString(),
|
||||
x2: UnitNumber.create(y).toString(),
|
||||
y2: UnitNumber.create(y).toString(),
|
||||
})
|
||||
}
|
||||
|
||||
stop(
|
||||
offset?: number | string | UNumber,
|
||||
offset?: number | string | UnitNumber,
|
||||
color?: string,
|
||||
opacity?: number | string | UNumber,
|
||||
opacity?: number | string | UnitNumber,
|
||||
): Stop
|
||||
stop(options: Stop.Options): Stop
|
||||
stop(
|
||||
offset?: Stop.Options | number | string | UNumber,
|
||||
offset?: Stop.Options | number | string | UnitNumber,
|
||||
color?: string,
|
||||
opacity?: number | string | UNumber,
|
||||
opacity?: number | string | UnitNumber,
|
||||
) {
|
||||
return new Stop().update(offset, color, opacity).appendTo(this)
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Attrs } from '../../types'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Container } from './container'
|
||||
import { Viewbox } from './container-viewbox'
|
||||
|
||||
@ -103,7 +103,7 @@ export namespace Marker {
|
||||
if (typeof width === 'object') {
|
||||
marker.size(0, 0).ref(0, 0).viewbox(0, 0, 0, 0).attr(width)
|
||||
} else {
|
||||
const w = UNumber.toNumber(width)
|
||||
const w = UnitNumber.toNumber(width)
|
||||
if (height != null) {
|
||||
if (typeof height === 'function') {
|
||||
marker
|
||||
@ -122,7 +122,7 @@ export namespace Marker {
|
||||
.viewbox(0, 0, w, w)
|
||||
.attr(height)
|
||||
} else {
|
||||
const h = UNumber.toNumber(height)
|
||||
const h = UnitNumber.toNumber(height)
|
||||
marker
|
||||
.size(w, h)
|
||||
.ref(w / 2, h / 2)
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../util/obj'
|
||||
import { Mixin } from '../util/mixin'
|
||||
import { ElementExtension as StyleExtension } from './shape/style-ext'
|
||||
import { ElementExtension as MaskExtension } from './container/mask-ext'
|
||||
import { ElementExtension as ClipPathExtension } from './container/clippath-ext'
|
||||
@ -11,4 +11,9 @@ declare module './element' {
|
||||
MaskExtension<TSVGElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(VectorElement, MaskExtension, StyleExtension, ClipPathExtension)
|
||||
Mixin.applyMixins(
|
||||
VectorElement,
|
||||
MaskExtension,
|
||||
StyleExtension,
|
||||
ClipPathExtension,
|
||||
)
|
||||
|
@ -7,7 +7,7 @@ import { Box } from '../struct/box'
|
||||
import { Point } from '../struct/point'
|
||||
import { Color } from '../struct/color'
|
||||
import { Matrix } from '../struct/matrix'
|
||||
import { UNumber } from '../struct/unumber'
|
||||
import { UnitNumber } from '../struct/unit-number'
|
||||
import { Vector } from './vector'
|
||||
|
||||
@VectorElement.register('Vector')
|
||||
@ -56,7 +56,7 @@ export class VectorElement<
|
||||
cx(x?: string | number | null) {
|
||||
return x == null
|
||||
? this.x() + this.width() / 2
|
||||
: this.x(UNumber.minus(x, this.width() / 2))
|
||||
: this.x(UnitNumber.minus(x, this.width() / 2))
|
||||
}
|
||||
|
||||
cy(): number
|
||||
@ -65,7 +65,7 @@ export class VectorElement<
|
||||
cy(y?: string | number | null) {
|
||||
return y == null
|
||||
? this.y() + this.height() / 2
|
||||
: this.y(UNumber.minus(y, this.height() / 2))
|
||||
: this.y(UnitNumber.minus(y, this.height() / 2))
|
||||
}
|
||||
|
||||
center(x: string | number, y: string | number) {
|
||||
@ -73,11 +73,11 @@ export class VectorElement<
|
||||
}
|
||||
|
||||
dx(x: string | number) {
|
||||
return this.x(UNumber.plus(x, this.x()))
|
||||
return this.x(UnitNumber.plus(x, this.x()))
|
||||
}
|
||||
|
||||
dy(y: string | number) {
|
||||
return this.y(UNumber.plus(y, this.y()))
|
||||
return this.y(UnitNumber.plus(y, this.y()))
|
||||
}
|
||||
|
||||
dmove(x: string | number = 0, y: string | number = 0) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Attrs } from '../../types'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Shape } from './shape'
|
||||
|
||||
@Circle.register('Circle')
|
||||
@ -23,7 +23,7 @@ export class Circle extends Shape<SVGCircleElement> {
|
||||
}
|
||||
|
||||
size(size: string | number) {
|
||||
return this.radius(UNumber.divide(size, 2))
|
||||
return this.radius(UnitNumber.divide(size, 2))
|
||||
}
|
||||
|
||||
cx(): number
|
||||
@ -44,7 +44,7 @@ export class Circle extends Shape<SVGCircleElement> {
|
||||
x(x?: string | number | null) {
|
||||
return x == null
|
||||
? this.cx() - this.rx()
|
||||
: this.cx(UNumber.plus(x, this.rx()))
|
||||
: this.cx(UnitNumber.plus(x, this.rx()))
|
||||
}
|
||||
|
||||
y(): number
|
||||
@ -53,21 +53,21 @@ export class Circle extends Shape<SVGCircleElement> {
|
||||
y(y?: string | number | null) {
|
||||
return y == null
|
||||
? this.cy() - this.ry()
|
||||
: this.cy(UNumber.plus(y, this.ry()))
|
||||
: this.cy(UnitNumber.plus(y, this.ry()))
|
||||
}
|
||||
|
||||
width(): number
|
||||
width(w: null): number
|
||||
width(w: string | number): this
|
||||
width(w?: string | number | null) {
|
||||
return w == null ? this.rx() * 2 : this.rx(UNumber.divide(w, 2))
|
||||
return w == null ? this.rx() * 2 : this.rx(UnitNumber.divide(w, 2))
|
||||
}
|
||||
|
||||
height(): number
|
||||
height(h: null): number
|
||||
height(h: string | number): this
|
||||
height(h?: string | number | null) {
|
||||
return h == null ? this.ry() * 2 : this.ry(UNumber.divide(h, 2))
|
||||
return h == null ? this.ry() * 2 : this.ry(UnitNumber.divide(h, 2))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Attrs } from '../../types'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Util } from '../util'
|
||||
import { Shape } from './shape'
|
||||
|
||||
@ -26,8 +26,8 @@ export class Ellipse extends Shape<SVGEllipseElement> {
|
||||
size(width: string | number | null | undefined, height: string | number): this
|
||||
size(width?: string | number | null, height?: string | number | null) {
|
||||
const size = Util.proportionalSize(this, width, height)
|
||||
const rx = UNumber.divide(size.width, 2)
|
||||
const ry = UNumber.divide(size.height, 2)
|
||||
const rx = UnitNumber.divide(size.width, 2)
|
||||
const ry = UnitNumber.divide(size.height, 2)
|
||||
return this.rx(rx).ry(ry)
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ export class Ellipse extends Shape<SVGEllipseElement> {
|
||||
x(x?: string | number | null) {
|
||||
return x == null
|
||||
? this.cx() - this.rx()
|
||||
: this.cx(UNumber.plus(x, this.rx()))
|
||||
: this.cx(UnitNumber.plus(x, this.rx()))
|
||||
}
|
||||
|
||||
y(): number
|
||||
@ -46,7 +46,7 @@ export class Ellipse extends Shape<SVGEllipseElement> {
|
||||
y(y?: string | number | null) {
|
||||
return y == null
|
||||
? this.cy() - this.ry()
|
||||
: this.cy(UNumber.plus(y, this.ry()))
|
||||
: this.cy(UnitNumber.plus(y, this.ry()))
|
||||
}
|
||||
|
||||
cx(): number
|
||||
@ -65,14 +65,14 @@ export class Ellipse extends Shape<SVGEllipseElement> {
|
||||
width(w: null): number
|
||||
width(w: string | number): this
|
||||
width(w?: string | number | null) {
|
||||
return w == null ? this.rx() * 2 : this.rx(UNumber.divide(w, 2))
|
||||
return w == null ? this.rx() * 2 : this.rx(UnitNumber.divide(w, 2))
|
||||
}
|
||||
|
||||
height(): number
|
||||
height(h: null): number
|
||||
height(h: string | number): this
|
||||
height(h?: string | number | null) {
|
||||
return h == null ? this.ry() * 2 : this.ry(UNumber.divide(h, 2))
|
||||
return h == null ? this.ry() * 2 : this.ry(UnitNumber.divide(h, 2))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { LineExtension as MarkerLineExtension } from '../container/marker-ext'
|
||||
import { Line } from './line'
|
||||
|
||||
@ -6,4 +6,4 @@ declare module './line' {
|
||||
interface Line extends MarkerLineExtension<SVGLineElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Line, MarkerLineExtension)
|
||||
Mixin.applyMixins(Line, MarkerLineExtension)
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { PathExtension as TextpathExtension } from './textpath-ext'
|
||||
import { LineExtension as MarkerLineExtension } from '../container/marker-ext'
|
||||
import { Path } from './path'
|
||||
@ -9,4 +9,4 @@ declare module './path' {
|
||||
TextpathExtension<SVGPathElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Path, TextpathExtension, MarkerLineExtension)
|
||||
Mixin.applyMixins(Path, TextpathExtension, MarkerLineExtension)
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { Poly } from './poly'
|
||||
import { LineExtension as MarkerLineExtension } from '../container/marker-ext'
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
|
||||
declare module './poly' {
|
||||
interface Poly<TSVGPolyElement extends SVGPolygonElement | SVGPolylineElement>
|
||||
extends MarkerLineExtension<TSVGPolyElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Poly, MarkerLineExtension)
|
||||
Mixin.applyMixins(Poly, MarkerLineExtension)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Global } from '../../global'
|
||||
import { Box } from '../../struct/box'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Shape } from './shape'
|
||||
|
||||
export class TextBase<
|
||||
@ -34,7 +34,10 @@ export class TextBase<
|
||||
return box.x
|
||||
}
|
||||
|
||||
return this.attr('x', this.attr<number>('x') + UNumber.toNumber(x) - box.x)
|
||||
return this.attr(
|
||||
'x',
|
||||
this.attr<number>('x') + UnitNumber.toNumber(x) - box.x,
|
||||
)
|
||||
}
|
||||
|
||||
y(): number
|
||||
@ -44,7 +47,10 @@ export class TextBase<
|
||||
return box.y
|
||||
}
|
||||
|
||||
return this.attr('y', this.attr<number>('y') + UNumber.toNumber(y) - box.y)
|
||||
return this.attr(
|
||||
'y',
|
||||
this.attr<number>('y') + UnitNumber.toNumber(y) - box.y,
|
||||
)
|
||||
}
|
||||
|
||||
move(x: number | string, y: number | string, box = this.bbox()) {
|
||||
@ -58,7 +64,10 @@ export class TextBase<
|
||||
return box.cx
|
||||
}
|
||||
|
||||
return this.attr('x', this.attr<number>('x') + UNumber.toNumber(x) - box.cx)
|
||||
return this.attr(
|
||||
'x',
|
||||
this.attr<number>('x') + UnitNumber.toNumber(x) - box.cx,
|
||||
)
|
||||
}
|
||||
|
||||
cy(): number
|
||||
@ -68,7 +77,10 @@ export class TextBase<
|
||||
return box.cy
|
||||
}
|
||||
|
||||
return this.attr('y', this.attr<number>('y') + UNumber.toNumber(y) - box.cy)
|
||||
return this.attr(
|
||||
'y',
|
||||
this.attr<number>('y') + UnitNumber.toNumber(y) - box.cy,
|
||||
)
|
||||
}
|
||||
|
||||
center(x: number | string, y: number | string, box = this.bbox()) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../../util/obj'
|
||||
import { Mixin } from '../../util/mixin'
|
||||
import { TextExtension as TextpathExtension } from './textpath-ext'
|
||||
import { TextExtension as TspanExtension } from './tspan-ext'
|
||||
import { Text } from './text'
|
||||
@ -9,4 +9,4 @@ declare module './text' {
|
||||
TspanExtension<TSVGTextElement> {}
|
||||
}
|
||||
|
||||
Obj.applyMixins(Text, TspanExtension, TextpathExtension)
|
||||
Mixin.applyMixins(Text, TspanExtension, TextpathExtension)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Attrs } from '../../types'
|
||||
import { Global } from '../../global'
|
||||
import { UNumber } from '../../struct/unumber'
|
||||
import { UnitNumber } from '../../struct/unit-number'
|
||||
import { Adopter } from '../adopter'
|
||||
import { TSpan } from './tspan'
|
||||
import { TextBase } from './text-base'
|
||||
@ -58,13 +58,13 @@ export class Text<
|
||||
}
|
||||
|
||||
leading(): number
|
||||
leading(value: UNumber.Raw): this
|
||||
leading(value?: UNumber.Raw) {
|
||||
leading(value: UnitNumber.Raw): this
|
||||
leading(value?: UnitNumber.Raw) {
|
||||
if (value == null) {
|
||||
return this.affixes.leading
|
||||
}
|
||||
|
||||
this.affixes.leading = UNumber.create(value).valueOf()
|
||||
this.affixes.leading = UnitNumber.create(value).valueOf()
|
||||
return this.rebuild()
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { Box } from '../struct/box'
|
||||
import type { VectorElement } from './element'
|
||||
import { UNumber } from '../struct/unumber'
|
||||
import { UnitNumber } from '../struct/unit-number'
|
||||
|
||||
export namespace Util {
|
||||
export function proportionalSize(
|
||||
@ -15,9 +15,9 @@ export namespace Util {
|
||||
let w = width
|
||||
let h = height
|
||||
if (w == null) {
|
||||
w = (bbox.width / bbox.height) * UNumber.toNumber(h!)
|
||||
w = (bbox.width / bbox.height) * UnitNumber.toNumber(h!)
|
||||
} else if (h == null) {
|
||||
h = (bbox.height / bbox.width) * UNumber.toNumber(w)
|
||||
h = (bbox.height / bbox.width) * UnitNumber.toNumber(w)
|
||||
}
|
||||
|
||||
return { width: w, height: h! }
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { TArray } from './tarray'
|
||||
import { TArray } from './type-array'
|
||||
|
||||
export class NumberArray extends TArray<number> {
|
||||
parse(raw: string | number[] = []): number[] {
|
||||
|
@ -2,8 +2,8 @@ import { DomUtil } from '../util/dom'
|
||||
import type { Path } from '../element/shape/path'
|
||||
import * as PathUtil from '../element/shape/path-util'
|
||||
import { Box } from './box'
|
||||
import { TArray } from './tarray'
|
||||
import { UNumber } from './unumber'
|
||||
import { TArray } from './type-array'
|
||||
import { UnitNumber } from './unit-number'
|
||||
|
||||
export class PathArray extends TArray<Path.Segment> {
|
||||
bbox() {
|
||||
@ -15,8 +15,8 @@ export class PathArray extends TArray<Path.Segment> {
|
||||
|
||||
move(x?: number | string, y?: number | string) {
|
||||
const box = this.bbox()
|
||||
const dx = typeof x === 'undefined' ? NaN : UNumber.toNumber(x) - box.x
|
||||
const dy = typeof y === 'undefined' ? NaN : UNumber.toNumber(y) - box.y
|
||||
const dx = typeof x === 'undefined' ? NaN : UnitNumber.toNumber(x) - box.x
|
||||
const dy = typeof y === 'undefined' ? NaN : UnitNumber.toNumber(y) - box.y
|
||||
|
||||
if (!Number.isNaN(dx) && !Number.isNaN(dy)) {
|
||||
for (let i = this.length - 1; i >= 0; i -= 1) {
|
||||
@ -52,8 +52,8 @@ export class PathArray extends TArray<Path.Segment> {
|
||||
|
||||
size(width: number | string, height: number | string) {
|
||||
const box = this.bbox()
|
||||
const w = UNumber.toNumber(width)
|
||||
const h = UNumber.toNumber(height)
|
||||
const w = UnitNumber.toNumber(width)
|
||||
const h = UnitNumber.toNumber(height)
|
||||
|
||||
// If the box width or height is 0 then we ignore
|
||||
// transformations on the respective axis
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Box } from './box'
|
||||
import { Matrix } from './matrix'
|
||||
import { TArray } from './tarray'
|
||||
import { UNumber } from './unumber'
|
||||
import { TArray } from './type-array'
|
||||
import { UnitNumber } from './unit-number'
|
||||
|
||||
export class PointArray extends TArray<[number, number]> {
|
||||
clone(): PointArray {
|
||||
@ -26,8 +26,8 @@ export class PointArray extends TArray<[number, number]> {
|
||||
|
||||
move(x?: number | string, y?: number | string) {
|
||||
const box = this.bbox()
|
||||
const dx = typeof x === 'undefined' ? NaN : UNumber.toNumber(x) - box.x
|
||||
const dy = typeof y === 'undefined' ? NaN : UNumber.toNumber(y) - box.y
|
||||
const dx = typeof x === 'undefined' ? NaN : UnitNumber.toNumber(x) - box.x
|
||||
const dy = typeof y === 'undefined' ? NaN : UnitNumber.toNumber(y) - box.y
|
||||
|
||||
if (!Number.isNaN(dx) && !Number.isNaN(dy)) {
|
||||
for (let i = this.length - 1; i >= 0; i -= 1) {
|
||||
@ -60,8 +60,8 @@ export class PointArray extends TArray<[number, number]> {
|
||||
|
||||
size(width: number | string, height: number | string) {
|
||||
const box = this.bbox()
|
||||
const w = UNumber.toNumber(width)
|
||||
const h = UNumber.toNumber(height)
|
||||
const w = UnitNumber.toNumber(width)
|
||||
const h = UnitNumber.toNumber(height)
|
||||
|
||||
for (let i = this.length - 1; i >= 0; i -= 1) {
|
||||
if (box.width) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Obj } from '../util/obj'
|
||||
import { Mixin } from '../util/mixin'
|
||||
|
||||
export abstract class TArray<T, I = any> {
|
||||
constructor()
|
||||
@ -43,7 +43,7 @@ export abstract class TArray<T, I = any> {
|
||||
export interface TArray<T, I> extends Array<T> {}
|
||||
|
||||
export namespace TArray {
|
||||
Obj.applyMixins(TArray as any, Array)
|
||||
Mixin.applyMixins(TArray as any, Array)
|
||||
|
||||
TArray.prototype.valueOf = function () {
|
||||
return this.toArray()
|
@ -1,10 +1,10 @@
|
||||
import { UNumber } from './unumber'
|
||||
import { UnitNumber } from './unit-number'
|
||||
|
||||
describe('SVGNumber', () => {
|
||||
let number: UNumber
|
||||
let number: UnitNumber
|
||||
|
||||
beforeEach(() => {
|
||||
number = new UNumber()
|
||||
number = new UnitNumber()
|
||||
})
|
||||
|
||||
describe('constructor()', () => {
|
||||
@ -14,59 +14,59 @@ describe('SVGNumber', () => {
|
||||
})
|
||||
|
||||
it('should create a SVGNumber from array', () => {
|
||||
number = new UNumber([30, '%'])
|
||||
number = new UnitNumber([30, '%'])
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should create a SVGNumber from object', () => {
|
||||
number = new UNumber({ value: 30, unit: '%' })
|
||||
number = new UnitNumber({ value: 30, unit: '%' })
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should accept the unit as a second argument', () => {
|
||||
number = new UNumber(30, '%')
|
||||
number = new UnitNumber(30, '%')
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should parse a pixel value', () => {
|
||||
number = new UNumber('20px')
|
||||
number = new UnitNumber('20px')
|
||||
expect(number.value).toBe(20)
|
||||
expect(number.unit).toBe('px')
|
||||
})
|
||||
|
||||
it('should parse a percent value', () => {
|
||||
number = new UNumber('99%')
|
||||
number = new UnitNumber('99%')
|
||||
expect(number.value).toBe(0.99)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should parse a seconds value', () => {
|
||||
number = new UNumber('2s')
|
||||
number = new UnitNumber('2s')
|
||||
expect(number.value).toBe(2000)
|
||||
expect(number.unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should parse a negative percent value', () => {
|
||||
number = new UNumber('-89%')
|
||||
number = new UnitNumber('-89%')
|
||||
expect(number.value).toBe(-0.89)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should fall back to 0 if given value is NaN', () => {
|
||||
number = new UNumber(NaN)
|
||||
number = new UnitNumber(NaN)
|
||||
expect(number.value).toBe(0)
|
||||
})
|
||||
|
||||
it('should fall back to maximum value if given number is positive infinite', () => {
|
||||
number = new UNumber(1.7976931348623157e10308)
|
||||
number = new UnitNumber(1.7976931348623157e10308)
|
||||
expect(number.value).toBe(3.4e38)
|
||||
})
|
||||
|
||||
it('should fall back to minimum value if given number is negative infinite', () => {
|
||||
number = new UNumber(-1.7976931348623157e10308)
|
||||
number = new UnitNumber(-1.7976931348623157e10308)
|
||||
expect(number.value).toBe(-3.4e38)
|
||||
})
|
||||
})
|
||||
@ -101,7 +101,7 @@ describe('SVGNumber', () => {
|
||||
expect(obj1.value).toBe(0)
|
||||
expect(obj1.unit).toBe('')
|
||||
|
||||
const obj2 = new UNumber(1, 'px').toJSON()
|
||||
const obj2 = new UnitNumber(1, 'px').toJSON()
|
||||
expect(obj2.value).toBe(1)
|
||||
expect(obj2.unit).toBe('px')
|
||||
})
|
||||
@ -113,7 +113,7 @@ describe('SVGNumber', () => {
|
||||
expect(arr1[0]).toBe(0)
|
||||
expect(arr1[1]).toBe('')
|
||||
|
||||
const arr2 = new UNumber(1, 'px').toArray()
|
||||
const arr2 = new UnitNumber(1, 'px').toArray()
|
||||
expect(arr2[0]).toBe(1)
|
||||
expect(arr2[1]).toBe('px')
|
||||
})
|
||||
@ -122,19 +122,19 @@ describe('SVGNumber', () => {
|
||||
describe('valueOf()', () => {
|
||||
it('should return a numeric value for default units', () => {
|
||||
expect(number.valueOf()).toBe(0)
|
||||
number = new UNumber('12')
|
||||
number = new UnitNumber('12')
|
||||
expect(number.valueOf()).toBe(12)
|
||||
number = new UNumber(13)
|
||||
number = new UnitNumber(13)
|
||||
expect(number.valueOf()).toBe(13)
|
||||
})
|
||||
|
||||
it('should return a numeric value for pixel units', () => {
|
||||
number = new UNumber('10px')
|
||||
number = new UnitNumber('10px')
|
||||
expect(number.valueOf()).toBe(10)
|
||||
})
|
||||
|
||||
it('should return a numeric value for percent units', () => {
|
||||
number = new UNumber('20%')
|
||||
number = new UnitNumber('20%')
|
||||
expect(number.valueOf()).toBe(0.2)
|
||||
})
|
||||
|
||||
@ -163,7 +163,7 @@ describe('SVGNumber', () => {
|
||||
})
|
||||
|
||||
it('should use the unit of this number as the unit of the returned number by default', () => {
|
||||
expect(new UNumber('12s').plus('3%').unit).toBe('s')
|
||||
expect(new UnitNumber('12s').plus('3%').unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should use the unit of the passed number as the unit of the returned number when this number as no unit', () => {
|
||||
@ -185,7 +185,7 @@ describe('SVGNumber', () => {
|
||||
})
|
||||
|
||||
it('should use the unit of this number as the unit of the returned number by default', () => {
|
||||
expect(new UNumber('12s').minus('3%').unit).toBe('s')
|
||||
expect(new UnitNumber('12s').minus('3%').unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should use the unit of the passed number as the unit of the returned number when this number as no unit', () => {
|
||||
@ -211,7 +211,7 @@ describe('SVGNumber', () => {
|
||||
})
|
||||
|
||||
it('should use the unit of this number as the unit of the returned number by default', () => {
|
||||
expect(new UNumber('12s').times('3%').unit).toBe('s')
|
||||
expect(new UnitNumber('12s').times('3%').unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should use the unit of the passed number as the unit of the returned number when this number as no unit', () => {
|
||||
@ -237,7 +237,7 @@ describe('SVGNumber', () => {
|
||||
})
|
||||
|
||||
it('should use the unit of this number as the unit of the returned number by default', () => {
|
||||
expect(new UNumber('12s').divide('3%').unit).toBe('s')
|
||||
expect(new UnitNumber('12s').divide('3%').unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should use the unit of the passed number as the unit of the returned number when this number as no unit', () => {
|
||||
@ -247,7 +247,7 @@ describe('SVGNumber', () => {
|
||||
|
||||
describe('convert()', () => {
|
||||
it('should change the unit of the number', () => {
|
||||
const number = new UNumber('12px').convert('%')
|
||||
const number = new UnitNumber('12px').convert('%')
|
||||
expect(number.toString()).toBe('1200%')
|
||||
})
|
||||
})
|
||||
@ -255,243 +255,243 @@ describe('SVGNumber', () => {
|
||||
describe('Static Methods', () => {
|
||||
describe('create()', () => {
|
||||
it('should create a SVGNumber with default values', () => {
|
||||
number = UNumber.create()
|
||||
number = UnitNumber.create()
|
||||
expect(number.value).toBe(0)
|
||||
expect(number.unit).toBe('')
|
||||
})
|
||||
|
||||
it('should create a SVGNumber from array', () => {
|
||||
number = UNumber.create([30, '%'])
|
||||
number = UnitNumber.create([30, '%'])
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should create a SVGNumber from object', () => {
|
||||
number = UNumber.create({ value: 30, unit: '%' })
|
||||
number = UnitNumber.create({ value: 30, unit: '%' })
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should accept the unit as a second argument', () => {
|
||||
number = UNumber.create(30, '%')
|
||||
number = UnitNumber.create(30, '%')
|
||||
expect(number.value).toBe(30)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should parse a pixel value', () => {
|
||||
number = UNumber.create('20px')
|
||||
number = UnitNumber.create('20px')
|
||||
expect(number.value).toBe(20)
|
||||
expect(number.unit).toBe('px')
|
||||
})
|
||||
|
||||
it('should parse a percent value', () => {
|
||||
number = UNumber.create('99%')
|
||||
number = UnitNumber.create('99%')
|
||||
expect(number.value).toBe(0.99)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should parse a seconds value', () => {
|
||||
number = UNumber.create('2s')
|
||||
number = UnitNumber.create('2s')
|
||||
expect(number.value).toBe(2000)
|
||||
expect(number.unit).toBe('s')
|
||||
})
|
||||
|
||||
it('should parse a negative percent value', () => {
|
||||
number = UNumber.create('-89%')
|
||||
number = UnitNumber.create('-89%')
|
||||
expect(number.value).toBe(-0.89)
|
||||
expect(number.unit).toBe('%')
|
||||
})
|
||||
|
||||
it('should fall back to 0 if given value is NaN', () => {
|
||||
number = UNumber.create(NaN)
|
||||
number = UnitNumber.create(NaN)
|
||||
expect(number.value).toBe(0)
|
||||
})
|
||||
|
||||
it('should fall back to maximum value if given number is positive infinite', () => {
|
||||
number = UNumber.create(1.7976931348623157e10308)
|
||||
number = UnitNumber.create(1.7976931348623157e10308)
|
||||
expect(number.value).toBe(3.4e38)
|
||||
})
|
||||
|
||||
it('should fall back to minimum value if given number is negative infinite', () => {
|
||||
number = UNumber.create(-1.7976931348623157e10308)
|
||||
number = UnitNumber.create(-1.7976931348623157e10308)
|
||||
expect(number.value).toBe(-3.4e38)
|
||||
})
|
||||
})
|
||||
|
||||
describe('toNumber()', () => {
|
||||
it('should return it if given value is a number', () => {
|
||||
expect(UNumber.toNumber(1)).toBe(1)
|
||||
expect(UnitNumber.toNumber(1)).toBe(1)
|
||||
})
|
||||
|
||||
it('should conver it to number take unit into account if given value is a string', () => {
|
||||
expect(UNumber.toNumber('1')).toBe(1)
|
||||
expect(UNumber.toNumber('1%')).toBe(0.01)
|
||||
expect(UNumber.toNumber('1s')).toBe(1000)
|
||||
expect(UNumber.toNumber('1px')).toBe(1)
|
||||
expect(UnitNumber.toNumber('1')).toBe(1)
|
||||
expect(UnitNumber.toNumber('1%')).toBe(0.01)
|
||||
expect(UnitNumber.toNumber('1s')).toBe(1000)
|
||||
expect(UnitNumber.toNumber('1px')).toBe(1)
|
||||
})
|
||||
|
||||
it('should ignore unknown unit', () => {
|
||||
expect(UNumber.toNumber('1k')).toBe(1)
|
||||
expect(UnitNumber.toNumber('1k')).toBe(1)
|
||||
})
|
||||
|
||||
it('should fall back to 0 if given value is NaN', () => {
|
||||
expect(UNumber.toNumber(NaN)).toBe(0)
|
||||
expect(UnitNumber.toNumber(NaN)).toBe(0)
|
||||
})
|
||||
|
||||
it('should fall back to maximum value if given number is positive infinite', () => {
|
||||
expect(UNumber.toNumber(1.7976931348623157e10308)).toBe(3.4e38)
|
||||
expect(UnitNumber.toNumber(1.7976931348623157e10308)).toBe(3.4e38)
|
||||
})
|
||||
|
||||
it('should fall back to minimum value if given number is negative infinite', () => {
|
||||
expect(UNumber.toNumber(-1.7976931348623157e10308)).toBe(-3.4e38)
|
||||
expect(UnitNumber.toNumber(-1.7976931348623157e10308)).toBe(-3.4e38)
|
||||
})
|
||||
})
|
||||
|
||||
describe('plus()', () => {
|
||||
it('should plus with two number values', () => {
|
||||
expect(UNumber.plus(1, 2)).toBe(3)
|
||||
expect(UnitNumber.plus(1, 2)).toBe(3)
|
||||
})
|
||||
|
||||
it('should plus with a number and a string take unit into account', () => {
|
||||
expect(UNumber.plus(1, '2')).toBe('3')
|
||||
expect(UNumber.plus('1', 2)).toBe('3')
|
||||
expect(UnitNumber.plus(1, '2')).toBe('3')
|
||||
expect(UnitNumber.plus('1', 2)).toBe('3')
|
||||
|
||||
expect(UNumber.plus(1, '2%')).toBe('102%')
|
||||
expect(UNumber.plus('1%', 2)).toBe('200.999999%')
|
||||
expect(UnitNumber.plus(1, '2%')).toBe('102%')
|
||||
expect(UnitNumber.plus('1%', 2)).toBe('200.999999%')
|
||||
|
||||
expect(UNumber.plus(1, '2s')).toBe('2.001s')
|
||||
expect(UNumber.plus('1s', 2)).toBe('1.002s')
|
||||
expect(UnitNumber.plus(1, '2s')).toBe('2.001s')
|
||||
expect(UnitNumber.plus('1s', 2)).toBe('1.002s')
|
||||
|
||||
expect(UNumber.plus(1, '2px')).toBe('3px')
|
||||
expect(UNumber.plus('1px', 2)).toBe('3px')
|
||||
expect(UnitNumber.plus(1, '2px')).toBe('3px')
|
||||
expect(UnitNumber.plus('1px', 2)).toBe('3px')
|
||||
})
|
||||
|
||||
it('should plus with two strings take unit into account', () => {
|
||||
expect(UNumber.plus('1', '2')).toBe('3')
|
||||
expect(UNumber.plus('1', '2')).toBe('3')
|
||||
expect(UnitNumber.plus('1', '2')).toBe('3')
|
||||
expect(UnitNumber.plus('1', '2')).toBe('3')
|
||||
|
||||
expect(UNumber.plus('1', '2%')).toBe('102%')
|
||||
expect(UNumber.plus('1%', '2')).toBe('200.999999%')
|
||||
expect(UnitNumber.plus('1', '2%')).toBe('102%')
|
||||
expect(UnitNumber.plus('1%', '2')).toBe('200.999999%')
|
||||
|
||||
expect(UNumber.plus('1', '2s')).toBe('2.001s')
|
||||
expect(UNumber.plus('1s', '2')).toBe('1.002s')
|
||||
expect(UnitNumber.plus('1', '2s')).toBe('2.001s')
|
||||
expect(UnitNumber.plus('1s', '2')).toBe('1.002s')
|
||||
|
||||
expect(UNumber.plus('1', '2px')).toBe('3px')
|
||||
expect(UNumber.plus('1px', '2')).toBe('3px')
|
||||
expect(UnitNumber.plus('1', '2px')).toBe('3px')
|
||||
expect(UnitNumber.plus('1px', '2')).toBe('3px')
|
||||
|
||||
expect(UNumber.plus('1s', '2%')).toBe('1.00002s')
|
||||
expect(UNumber.plus('1px', '2s')).toBe('2001px')
|
||||
expect(UnitNumber.plus('1s', '2%')).toBe('1.00002s')
|
||||
expect(UnitNumber.plus('1px', '2s')).toBe('2001px')
|
||||
})
|
||||
})
|
||||
|
||||
describe('minus()', () => {
|
||||
it('should minus with two number values', () => {
|
||||
expect(UNumber.minus(1, 2)).toBe(-1)
|
||||
expect(UnitNumber.minus(1, 2)).toBe(-1)
|
||||
})
|
||||
|
||||
it('should minus with a number and a string take unit into account', () => {
|
||||
expect(UNumber.minus(1, '2')).toBe('-1')
|
||||
expect(UNumber.minus('1', 2)).toBe('-1')
|
||||
expect(UnitNumber.minus(1, '2')).toBe('-1')
|
||||
expect(UnitNumber.minus('1', 2)).toBe('-1')
|
||||
|
||||
expect(UNumber.minus(1, '2%')).toBe('98%')
|
||||
expect(UNumber.minus('1%', 2)).toBe('-199%')
|
||||
expect(UnitNumber.minus(1, '2%')).toBe('98%')
|
||||
expect(UnitNumber.minus('1%', 2)).toBe('-199%')
|
||||
|
||||
expect(UNumber.minus(1, '2s')).toBe('-1.999s')
|
||||
expect(UNumber.minus('1s', 2)).toBe('0.998s')
|
||||
expect(UnitNumber.minus(1, '2s')).toBe('-1.999s')
|
||||
expect(UnitNumber.minus('1s', 2)).toBe('0.998s')
|
||||
|
||||
expect(UNumber.minus(1, '2px')).toBe('-1px')
|
||||
expect(UNumber.minus('1px', 2)).toBe('-1px')
|
||||
expect(UnitNumber.minus(1, '2px')).toBe('-1px')
|
||||
expect(UnitNumber.minus('1px', 2)).toBe('-1px')
|
||||
})
|
||||
|
||||
it('should minus with two strings take unit into account', () => {
|
||||
expect(UNumber.minus('1', '2')).toBe('-1')
|
||||
expect(UNumber.minus('1', '2')).toBe('-1')
|
||||
expect(UnitNumber.minus('1', '2')).toBe('-1')
|
||||
expect(UnitNumber.minus('1', '2')).toBe('-1')
|
||||
|
||||
expect(UNumber.minus('1', '2%')).toBe('98%')
|
||||
expect(UNumber.minus('1%', '2')).toBe('-199%')
|
||||
expect(UnitNumber.minus('1', '2%')).toBe('98%')
|
||||
expect(UnitNumber.minus('1%', '2')).toBe('-199%')
|
||||
|
||||
expect(UNumber.minus('1', '2s')).toBe('-1.999s')
|
||||
expect(UNumber.minus('1s', '2')).toBe('0.998s')
|
||||
expect(UnitNumber.minus('1', '2s')).toBe('-1.999s')
|
||||
expect(UnitNumber.minus('1s', '2')).toBe('0.998s')
|
||||
|
||||
expect(UNumber.minus('1', '2px')).toBe('-1px')
|
||||
expect(UNumber.minus('1px', '2')).toBe('-1px')
|
||||
expect(UnitNumber.minus('1', '2px')).toBe('-1px')
|
||||
expect(UnitNumber.minus('1px', '2')).toBe('-1px')
|
||||
|
||||
expect(UNumber.minus('1s', '2%')).toBe('0.99998s')
|
||||
expect(UNumber.minus('1px', '2s')).toBe('-1999px')
|
||||
expect(UnitNumber.minus('1s', '2%')).toBe('0.99998s')
|
||||
expect(UnitNumber.minus('1px', '2s')).toBe('-1999px')
|
||||
})
|
||||
})
|
||||
|
||||
describe('times()', () => {
|
||||
it('should times with two number values', () => {
|
||||
expect(UNumber.times(1, 2)).toBe(2)
|
||||
expect(UnitNumber.times(1, 2)).toBe(2)
|
||||
})
|
||||
|
||||
it('should times with a number and a string take unit into account', () => {
|
||||
expect(UNumber.times(1, '2')).toBe('2')
|
||||
expect(UNumber.times('1', 2)).toBe('2')
|
||||
expect(UnitNumber.times(1, '2')).toBe('2')
|
||||
expect(UnitNumber.times('1', 2)).toBe('2')
|
||||
|
||||
expect(UNumber.times(1, '2%')).toBe('2%')
|
||||
expect(UNumber.times('1%', 2)).toBe('2%')
|
||||
expect(UnitNumber.times(1, '2%')).toBe('2%')
|
||||
expect(UnitNumber.times('1%', 2)).toBe('2%')
|
||||
|
||||
expect(UNumber.times(1, '2s')).toBe('2s')
|
||||
expect(UNumber.times('1s', 2)).toBe('2s')
|
||||
expect(UnitNumber.times(1, '2s')).toBe('2s')
|
||||
expect(UnitNumber.times('1s', 2)).toBe('2s')
|
||||
|
||||
expect(UNumber.times(1, '2px')).toBe('2px')
|
||||
expect(UNumber.times('1px', 2)).toBe('2px')
|
||||
expect(UnitNumber.times(1, '2px')).toBe('2px')
|
||||
expect(UnitNumber.times('1px', 2)).toBe('2px')
|
||||
})
|
||||
|
||||
it('should times with two strings take unit into account', () => {
|
||||
expect(UNumber.times('1', '2')).toBe('2')
|
||||
expect(UNumber.times('1', '2')).toBe('2')
|
||||
expect(UnitNumber.times('1', '2')).toBe('2')
|
||||
expect(UnitNumber.times('1', '2')).toBe('2')
|
||||
|
||||
expect(UNumber.times('1', '2%')).toBe('2%')
|
||||
expect(UNumber.times('1%', '2')).toBe('2%')
|
||||
expect(UnitNumber.times('1', '2%')).toBe('2%')
|
||||
expect(UnitNumber.times('1%', '2')).toBe('2%')
|
||||
|
||||
expect(UNumber.times('1', '2s')).toBe('2s')
|
||||
expect(UNumber.times('1s', '2')).toBe('2s')
|
||||
expect(UnitNumber.times('1', '2s')).toBe('2s')
|
||||
expect(UnitNumber.times('1s', '2')).toBe('2s')
|
||||
|
||||
expect(UNumber.times('1', '2px')).toBe('2px')
|
||||
expect(UNumber.times('1px', '2')).toBe('2px')
|
||||
expect(UnitNumber.times('1', '2px')).toBe('2px')
|
||||
expect(UnitNumber.times('1px', '2')).toBe('2px')
|
||||
|
||||
expect(UNumber.times('1s', '2%')).toBe('0.02s')
|
||||
expect(UNumber.times('1px', '2s')).toBe('2000px')
|
||||
expect(UnitNumber.times('1s', '2%')).toBe('0.02s')
|
||||
expect(UnitNumber.times('1px', '2s')).toBe('2000px')
|
||||
})
|
||||
})
|
||||
|
||||
describe('divide()', () => {
|
||||
it('should divide with two number values', () => {
|
||||
expect(UNumber.divide(1, 2)).toBe(0.5)
|
||||
expect(UnitNumber.divide(1, 2)).toBe(0.5)
|
||||
})
|
||||
|
||||
it('should divide with a number and a string take unit into account', () => {
|
||||
expect(UNumber.divide(1, '2')).toBe('0.5')
|
||||
expect(UNumber.divide('1', 2)).toBe('0.5')
|
||||
expect(UnitNumber.divide(1, '2')).toBe('0.5')
|
||||
expect(UnitNumber.divide('1', 2)).toBe('0.5')
|
||||
|
||||
expect(UNumber.divide(1, '2%')).toBe('5000%')
|
||||
expect(UNumber.divide('1%', 2)).toBe('0.5%')
|
||||
expect(UnitNumber.divide(1, '2%')).toBe('5000%')
|
||||
expect(UnitNumber.divide('1%', 2)).toBe('0.5%')
|
||||
|
||||
expect(UNumber.divide(1, '2s')).toBe('5e-7s')
|
||||
expect(UNumber.divide('1s', 2)).toBe('0.5s')
|
||||
expect(UnitNumber.divide(1, '2s')).toBe('5e-7s')
|
||||
expect(UnitNumber.divide('1s', 2)).toBe('0.5s')
|
||||
|
||||
expect(UNumber.divide(1, '2px')).toBe('0.5px')
|
||||
expect(UNumber.divide('1px', 2)).toBe('0.5px')
|
||||
expect(UnitNumber.divide(1, '2px')).toBe('0.5px')
|
||||
expect(UnitNumber.divide('1px', 2)).toBe('0.5px')
|
||||
})
|
||||
|
||||
it('should divide with two strings take unit into account', () => {
|
||||
expect(UNumber.divide('1', '2')).toBe('0.5')
|
||||
expect(UNumber.divide('1', '2')).toBe('0.5')
|
||||
expect(UnitNumber.divide('1', '2')).toBe('0.5')
|
||||
expect(UnitNumber.divide('1', '2')).toBe('0.5')
|
||||
|
||||
expect(UNumber.divide('1', '2%')).toBe('5000%')
|
||||
expect(UNumber.divide('1%', '2')).toBe('0.5%')
|
||||
expect(UnitNumber.divide('1', '2%')).toBe('5000%')
|
||||
expect(UnitNumber.divide('1%', '2')).toBe('0.5%')
|
||||
|
||||
expect(UNumber.divide('1', '2s')).toBe('5e-7s')
|
||||
expect(UNumber.divide('1s', '2')).toBe('0.5s')
|
||||
expect(UnitNumber.divide('1', '2s')).toBe('5e-7s')
|
||||
expect(UnitNumber.divide('1s', '2')).toBe('0.5s')
|
||||
|
||||
expect(UNumber.divide('1', '2px')).toBe('0.5px')
|
||||
expect(UNumber.divide('1px', '2')).toBe('0.5px')
|
||||
expect(UnitNumber.divide('1', '2px')).toBe('0.5px')
|
||||
expect(UnitNumber.divide('1px', '2')).toBe('0.5px')
|
||||
|
||||
expect(UNumber.divide('1s', '2%')).toBe('50s')
|
||||
expect(UNumber.divide('1px', '2s')).toBe('0.0005px')
|
||||
expect(UnitNumber.divide('1s', '2%')).toBe('50s')
|
||||
expect(UnitNumber.divide('1px', '2s')).toBe('0.0005px')
|
||||
})
|
||||
})
|
||||
})
|
@ -1,14 +1,14 @@
|
||||
export class UNumber implements UNumber.UNumberLike {
|
||||
export class UnitNumber implements UnitNumber.UnitNumberLike {
|
||||
public value: number
|
||||
public unit: string
|
||||
|
||||
constructor()
|
||||
constructor(
|
||||
value: number | string | UNumber.UNumberLike | null | undefined,
|
||||
value: number | string | UnitNumber.UnitNumberLike | null | undefined,
|
||||
unit?: string,
|
||||
)
|
||||
constructor(array: [number | string, string?])
|
||||
constructor(arg1?: UNumber.Raw, arg2?: string) {
|
||||
constructor(arg1?: UnitNumber.Raw, arg2?: string) {
|
||||
const value = Array.isArray(arg1) ? arg1[0] : arg1
|
||||
const unit = Array.isArray(arg1) ? arg1[1] : arg2
|
||||
|
||||
@ -17,21 +17,12 @@ export class UNumber implements UNumber.UNumberLike {
|
||||
|
||||
if (value != null) {
|
||||
if (typeof value === 'number') {
|
||||
this.value = UNumber.normalizeNumber(value)
|
||||
this.value = UnitNumber.normalize(value)
|
||||
} else if (typeof value === 'string') {
|
||||
const matches = value.match(
|
||||
/^([+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?)([%a-z]*)$/i,
|
||||
)
|
||||
if (matches) {
|
||||
this.value = Number.parseFloat(matches[1])
|
||||
this.unit = matches[5]
|
||||
|
||||
// normalize
|
||||
if (this.unit === '%') {
|
||||
this.value /= 100
|
||||
} else if (this.unit === 's') {
|
||||
this.value *= 1000
|
||||
}
|
||||
const obj = UnitNumber.parse(value)
|
||||
if (obj) {
|
||||
this.value = obj.value
|
||||
this.unit = obj.unit
|
||||
}
|
||||
} else if (typeof value === 'object') {
|
||||
this.value = value.value
|
||||
@ -40,35 +31,35 @@ export class UNumber implements UNumber.UNumberLike {
|
||||
}
|
||||
}
|
||||
|
||||
minus(number: UNumber.Raw) {
|
||||
const input = UNumber.create(number)
|
||||
return new UNumber(this.value - input.value, this.unit || input.unit)
|
||||
minus(number: UnitNumber.Raw) {
|
||||
const input = UnitNumber.create(number)
|
||||
return new UnitNumber(this.value - input.value, this.unit || input.unit)
|
||||
}
|
||||
|
||||
plus(number: UNumber.Raw) {
|
||||
const input = UNumber.create(number)
|
||||
return new UNumber(this.value + input.value, this.unit || input.unit)
|
||||
plus(number: UnitNumber.Raw) {
|
||||
const input = UnitNumber.create(number)
|
||||
return new UnitNumber(this.value + input.value, this.unit || input.unit)
|
||||
}
|
||||
|
||||
times(number: UNumber.Raw) {
|
||||
const input = UNumber.create(number)
|
||||
return new UNumber(this.value * input.value, this.unit || input.unit)
|
||||
times(number: UnitNumber.Raw) {
|
||||
const input = UnitNumber.create(number)
|
||||
return new UnitNumber(this.value * input.value, this.unit || input.unit)
|
||||
}
|
||||
|
||||
divide(number: UNumber.Raw) {
|
||||
const input = UNumber.create(number)
|
||||
return new UNumber(this.value / input.value, this.unit || input.unit)
|
||||
divide(number: UnitNumber.Raw) {
|
||||
const input = UnitNumber.create(number)
|
||||
return new UnitNumber(this.value / input.value, this.unit || input.unit)
|
||||
}
|
||||
|
||||
convert(unit: string) {
|
||||
return new UNumber(this.value, unit)
|
||||
return new UnitNumber(this.value, unit)
|
||||
}
|
||||
|
||||
toArray(): UNumber.SVGNumberArray {
|
||||
toArray(): UnitNumber.UnitNumberArray {
|
||||
return [this.value, this.unit]
|
||||
}
|
||||
|
||||
toJSON(): UNumber.UNumberLike {
|
||||
toJSON(): UnitNumber.UnitNumberLike {
|
||||
return { value: this.value, unit: this.unit }
|
||||
}
|
||||
|
||||
@ -88,29 +79,29 @@ export class UNumber implements UNumber.UNumberLike {
|
||||
}
|
||||
}
|
||||
|
||||
export namespace UNumber {
|
||||
export namespace UnitNumber {
|
||||
export type Raw =
|
||||
| undefined
|
||||
| null
|
||||
| number
|
||||
| string
|
||||
| UNumberLike
|
||||
| UnitNumberLike
|
||||
| [number | string, string?]
|
||||
|
||||
export interface UNumberLike {
|
||||
export interface UnitNumberLike {
|
||||
value: number
|
||||
unit: string
|
||||
}
|
||||
|
||||
export type SVGNumberArray = [number, string]
|
||||
export type UnitNumberArray = [number, string]
|
||||
|
||||
export function create(): UNumber
|
||||
export function create(number: number, unit?: string): UNumber
|
||||
export function create(number: Raw): UNumber
|
||||
export function create(): UnitNumber
|
||||
export function create(number: number, unit?: string): UnitNumber
|
||||
export function create(number: Raw): UnitNumber
|
||||
export function create(number?: Raw, unit?: string) {
|
||||
return Array.isArray(number)
|
||||
? new UNumber(number[0], number[1])
|
||||
: new UNumber(number, unit)
|
||||
? new UnitNumber(number[0], number[1])
|
||||
: new UnitNumber(number, unit)
|
||||
}
|
||||
|
||||
export function plus(left: Raw, right: Raw) {
|
||||
@ -141,7 +132,7 @@ export namespace UNumber {
|
||||
return create(left).divide(right).toString()
|
||||
}
|
||||
|
||||
export function normalizeNumber(v: number) {
|
||||
export function normalize(v: number) {
|
||||
return Number.isNaN(v)
|
||||
? 0
|
||||
: !Number.isFinite(v)
|
||||
@ -151,7 +142,26 @@ export namespace UNumber {
|
||||
: v
|
||||
}
|
||||
|
||||
export const REGEX_NUMBER_UNIT = /^([+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?)([a-z%]*)$/i
|
||||
|
||||
export function parse(str: string): UnitNumberLike | null {
|
||||
const matches = str.match(REGEX_NUMBER_UNIT)
|
||||
if (matches) {
|
||||
let value = normalize(Number.parseFloat(matches[1]))
|
||||
const unit = matches[5] || ''
|
||||
|
||||
// normalize
|
||||
if (unit === '%') {
|
||||
value /= 100
|
||||
} else if (unit === 's') {
|
||||
value *= 1000
|
||||
}
|
||||
return { value, unit }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function toNumber(v: number | string) {
|
||||
return typeof v === 'number' ? normalizeNumber(v) : create(v).valueOf()
|
||||
return typeof v === 'number' ? normalize(v) : create(v).valueOf()
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
import './lib.dom'
|
||||
|
||||
export type Primitive =
|
||||
| string
|
||||
| number
|
||||
@ -39,3 +41,7 @@ export type Attrs = Record<string, string | number | null | undefined>
|
||||
export interface Class<Args extends [] = any, ReturnType = any> {
|
||||
new (...args: Args): ReturnType
|
||||
}
|
||||
|
||||
export type CSSKeys = Exclude<keyof CSSStyleDeclaration, number>
|
||||
|
||||
export type Entity = Record<any, any>
|
||||
|
15
packages/x6-vector/src/util/mixin.ts
Normal file
15
packages/x6-vector/src/util/mixin.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Class } from '../types'
|
||||
|
||||
export namespace Mixin {
|
||||
export function applyMixins(target: Class, ...sources: Class[]) {
|
||||
sources.forEach((source) => {
|
||||
Object.getOwnPropertyNames(source.prototype).forEach((name) => {
|
||||
Object.defineProperty(
|
||||
target.prototype,
|
||||
name,
|
||||
Object.getOwnPropertyDescriptor(source.prototype, name)!,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
import { Class } from '../types'
|
||||
|
||||
export namespace Obj {
|
||||
export function applyMixin<TClass extends Class, Key extends keyof TClass>(
|
||||
target: any,
|
||||
source: TClass,
|
||||
pick?: Key[],
|
||||
) {
|
||||
Object.getOwnPropertyNames(source.prototype).forEach((name) => {
|
||||
if (pick == null || !pick.includes(name as Key)) {
|
||||
return
|
||||
}
|
||||
|
||||
Object.defineProperty(
|
||||
target.prototype,
|
||||
name,
|
||||
Object.getOwnPropertyDescriptor(source.prototype, name)!,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export function applyMixins(target: Class, ...sources: Class[]) {
|
||||
sources.forEach((source) => {
|
||||
Object.getOwnPropertyNames(source.prototype).forEach((name) => {
|
||||
Object.defineProperty(
|
||||
target.prototype,
|
||||
name,
|
||||
Object.getOwnPropertyDescriptor(source.prototype, name)!,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function extend<TObject extends Record<string, unknown>>(
|
||||
target: Class,
|
||||
...sources: TObject[]
|
||||
) {
|
||||
sources.forEach((source) => {
|
||||
Object.getOwnPropertyNames(source).forEach((name) => {
|
||||
Object.defineProperty(
|
||||
target.prototype,
|
||||
name,
|
||||
Object.getOwnPropertyDescriptor(source, name)!,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user