Skip to content

Commit

Permalink
fix: typescript return values
Browse files Browse the repository at this point in the history
  • Loading branch information
nico b committed Feb 7, 2024
1 parent ffe8488 commit 31b9501
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 94 deletions.
36 changes: 11 additions & 25 deletions dist/zmooth.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var BaseZmooth = class {
to;
speed;
_alive = true;
paused = false;
constructor(value, to, speed = 1) {
this._value = value;
this.to = to;
Expand All @@ -44,6 +45,10 @@ var BaseZmooth = class {
get alive() {
return this._alive;
}
reset(value) {
this._value = value;
this.to = value;
}
smoothValue(from, to, delta) {
return from + (to - from) * this.speed * delta;
}
Expand Down Expand Up @@ -103,34 +108,16 @@ var ZmoothManager = class {
let i = this.zmooths.length;
while (i-- > 0) {
const zmooth = this.zmooths[i];
if (zmooth.paused) {
continue;
}
if (zmooth.alive) {
zmooth.update(delta);
} else {
this.zmooths.splice(i, 1);
}
}
}
/**
* Smooth a value to its destination value
* Each time you assign a value via myZmooth.to, it will smoothly interpolate to the destination value
* You can assign a new value via .to any time you want
* @example
* const myZmooth = zmooth.val(0, 5, function(value) {
* console.log(value);
* });
*
* // will smoothly change from 0 to 10
* myZmooth.to = 10;
*
* setTimeout(function() {
* // will smoothly change from current myZmooth value to 20
* myZmooth.to = 20;
* }, 2000);
* @param value Start value
* @param speed Speed of change. The formula is: value = value + (toValue - currValue) * delta * speed
* @param onChange Callback each time the value is updated
* @returns
*/
val(value, speed, onChange) {
const zmooth = Array.isArray(value) ? new ZmoothArray(value, speed, onChange) : new ZmoothNumber(value, speed, onChange);
this.zmooths.push(zmooth);
Expand All @@ -143,17 +130,17 @@ var ZmoothManager = class {
* const myObj = {
* myValue: 0,
* };
*
*
* // now property 'myValue' on object myObj is managed automatically
* const myZmooth = zmooth.prop(myObj, 'myValue');
*
*
* // now myObj.value will automatically be interpolated to 10
* myZmooth.to = 10;
* @param obj Any javascript object
* @param propertyName Property that must be smoothed
* @param speed Speed of change
* @param onChange Callback each time the value is updated
* @returns
* @returns
*/
prop(obj, propertyName, speed, onChange) {
return this.val(obj[propertyName], speed, (value) => {
Expand Down Expand Up @@ -195,4 +182,3 @@ var src_default = {
prop: globalManager.prop.bind(globalManager),
killAll: globalManager.killAll.bind(globalManager)
};
//# sourceMappingURL=zmooth.cjs.map
1 change: 0 additions & 1 deletion dist/zmooth.cjs.map

This file was deleted.

24 changes: 15 additions & 9 deletions dist/zmooth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ declare abstract class BaseZmooth<T> {
to: T;
speed: number;
private _alive;
paused: boolean;
constructor(value: T, to: T, speed?: number);
abstract update(delta: number): void;
kill(): void;
get value(): T;
get alive(): boolean;
reset(value: T): void;
protected smoothValue(from: number, to: number, delta: number): number;
}

declare class ZmoothNumber extends BaseZmooth<number> {
onChange?: (value: number) => void;
constructor(value?: number, speed?: number, onChange?: (value: number) => void);
update(delta: number): void;
}

declare class ZmoothArray extends BaseZmooth<number[]> {
onChange?: (values: number[]) => void;
constructor(value?: number[], speed?: number, onChange?: (values: number[]) => void);
update(delta: number): void;
}

declare class ZmoothNumber extends BaseZmooth<number> {
onChange?: (value: number) => void;
constructor(value?: number, speed?: number, onChange?: (value: number) => void);
update(delta: number): void;
}

declare class ZmoothManager {
private zmooths;
private lastTime;
Expand Down Expand Up @@ -55,7 +57,8 @@ declare class ZmoothManager {
* @param onChange Callback each time the value is updated
* @returns
*/
val<T extends number | number[]>(value: T, speed?: number, onChange?: (value: T) => void): ZmoothArray | ZmoothNumber;
val<T extends number>(value: T, speed?: number, onChange?: (value: T) => void): ZmoothNumber;
val<T extends number[]>(value: T, speed?: number, onChange?: (value: T) => void): ZmoothArray;
/**
* Identical to val()
* The difference is that the new value will be automatically assigned to the property in the object you passed in parameters
Expand Down Expand Up @@ -93,8 +96,11 @@ declare class ZmoothManager {

declare const _default: {
inst: (autoUpdate?: boolean) => ZmoothManager;
val: <T extends number | number[]>(value: T, speed?: number | undefined, onChange?: ((value: T) => void) | undefined) => ZmoothArray | ZmoothNumber;
prop: <T_1 extends Record<any, any>>(obj: T_1, propertyName: keyof T_1, speed?: number | undefined, onChange?: ((value: any) => void) | undefined) => BaseZmooth<any>;
val: {
<T extends number>(value: T, speed?: number | undefined, onChange?: ((value: T) => void) | undefined): ZmoothNumber;
<T_1 extends number[]>(value: T_1, speed?: number | undefined, onChange?: ((value: T_1) => void) | undefined): ZmoothArray;
};
prop: <T_2 extends Record<any, any>>(obj: T_2, propertyName: keyof T_2, speed?: number | undefined, onChange?: ((value: any) => void) | undefined) => BaseZmooth<any>;
killAll: () => void;
};

Expand Down
36 changes: 11 additions & 25 deletions dist/zmooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var zmooth = (() => {
to;
speed;
_alive = true;
paused = false;
constructor(value, to, speed = 1) {
this._value = value;
this.to = to;
Expand All @@ -44,6 +45,10 @@ var zmooth = (() => {
get alive() {
return this._alive;
}
reset(value) {
this._value = value;
this.to = value;
}
smoothValue(from, to, delta) {
return from + (to - from) * this.speed * delta;
}
Expand Down Expand Up @@ -103,34 +108,16 @@ var zmooth = (() => {
let i = this.zmooths.length;
while (i-- > 0) {
const zmooth = this.zmooths[i];
if (zmooth.paused) {
continue;
}
if (zmooth.alive) {
zmooth.update(delta);
} else {
this.zmooths.splice(i, 1);
}
}
}
/**
* Smooth a value to its destination value
* Each time you assign a value via myZmooth.to, it will smoothly interpolate to the destination value
* You can assign a new value via .to any time you want
* @example
* const myZmooth = zmooth.val(0, 5, function(value) {
* console.log(value);
* });
*
* // will smoothly change from 0 to 10
* myZmooth.to = 10;
*
* setTimeout(function() {
* // will smoothly change from current myZmooth value to 20
* myZmooth.to = 20;
* }, 2000);
* @param value Start value
* @param speed Speed of change. The formula is: value = value + (toValue - currValue) * delta * speed
* @param onChange Callback each time the value is updated
* @returns
*/
val(value, speed, onChange) {
const zmooth = Array.isArray(value) ? new ZmoothArray(value, speed, onChange) : new ZmoothNumber(value, speed, onChange);
this.zmooths.push(zmooth);
Expand All @@ -143,17 +130,17 @@ var zmooth = (() => {
* const myObj = {
* myValue: 0,
* };
*
*
* // now property 'myValue' on object myObj is managed automatically
* const myZmooth = zmooth.prop(myObj, 'myValue');
*
*
* // now myObj.value will automatically be interpolated to 10
* myZmooth.to = 10;
* @param obj Any javascript object
* @param propertyName Property that must be smoothed
* @param speed Speed of change
* @param onChange Callback each time the value is updated
* @returns
* @returns
*/
prop(obj, propertyName, speed, onChange) {
return this.val(obj[propertyName], speed, (value) => {
Expand Down Expand Up @@ -198,4 +185,3 @@ var zmooth = (() => {
return __toCommonJS(src_exports);
})();
zmooth = zmooth.default;
//# sourceMappingURL=zmooth.js.map
Loading

0 comments on commit 31b9501

Please sign in to comment.