-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move away from ValuEType class and just to an interface.
- Loading branch information
Showing
13 changed files
with
118 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { ValueType } from '../../../Values/ValueType'; | ||
|
||
export const BooleanValue = new ValueType( | ||
'boolean', | ||
() => false, | ||
(value: string | boolean) => | ||
export const BooleanValue: ValueType = { | ||
name: 'boolean', | ||
creator: () => false, | ||
deserialize: (value: string | boolean) => | ||
typeof value === 'string' ? value.toLowerCase() === 'true' : value, | ||
(value: boolean) => value, | ||
(start: boolean, end: boolean, t: number) => (t < 0.5 ? start : end) | ||
); | ||
serialize: (value: boolean) => value, | ||
lerp: (start: boolean, end: boolean, t: number) => (t < 0.5 ? start : end), | ||
equals: (a: boolean, b: boolean) => a === b, | ||
clone: (value: boolean) => value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { parseSafeFloat } from '../../../parseFloats'; | ||
import { ValueType } from '../../../Values/ValueType'; | ||
|
||
export const FloatValue = new ValueType( | ||
'float', | ||
() => 0, | ||
(value: string | number) => | ||
export const FloatValue: ValueType = { | ||
name: 'float', | ||
creator: () => 0, | ||
deserialize: (value: string | number) => | ||
typeof value === 'string' ? parseSafeFloat(value, 0) : value, | ||
(value: number) => value, | ||
(start: number, end: number, t: number) => start * (1 - t) + end * t | ||
); | ||
serialize: (value: number) => value, | ||
lerp: (start: number, end: number, t: number) => start * (1 - t) + end * t, | ||
equals: (a: number, b: number) => a === b, | ||
clone: (value: number) => value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { ValueType } from '../../../Values/ValueType'; | ||
|
||
export const IntegerValue = new ValueType( | ||
'integer', | ||
() => BigInt(0), | ||
(value: string | number): bigint => BigInt(value), | ||
(value: bigint) => | ||
export const IntegerValue: ValueType = { | ||
name: 'integer', | ||
creator: () => BigInt(0), | ||
deserialize: (value: string | number): bigint => BigInt(value), | ||
serialize: (value: bigint) => | ||
Number.MIN_SAFE_INTEGER <= value && value <= Number.MAX_SAFE_INTEGER | ||
? Number(value) | ||
: value.toString(), // prefer string to ensure full range is covered | ||
|
||
(start: bigint, end: bigint, t: number) => | ||
BigInt(Number(start) * (1 - t) + Number(end) * t) | ||
); | ||
lerp: (start: bigint, end: bigint, t: number) => | ||
BigInt(Number(start) * (1 - t) + Number(end) * t), | ||
equals: (a: bigint, b: bigint) => a === b, | ||
clone: (value: bigint) => value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { ValueType } from '../../../Values/ValueType'; | ||
|
||
export const StringValue = new ValueType( | ||
'string', | ||
() => '', | ||
(value: string) => value, | ||
(value: string) => value, | ||
(start: string, end: string, t: number) => (t < 0.5 ? start : end), | ||
(a: string, b: string) => a === b, | ||
(value: string) => value | ||
); | ||
export const StringValue: ValueType = { | ||
name: 'string', | ||
creator: () => '', | ||
deserialize: (value: string) => value, | ||
serialize: (value: string) => value, | ||
lerp: (start: string, end: string, t: number) => (t < 0.5 ? start : end), | ||
equals: (a: string, b: string) => a === b, | ||
clone: (value: string) => value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { ValueType } from '../../../Values/ValueType'; | ||
import { Vec2, Vec2JSON, vec2Mix, vec2Parse } from './Internal/Vec2'; | ||
|
||
export const Vec2Value = new ValueType( | ||
'vec2', | ||
() => new Vec2(), | ||
(value: string | Vec2JSON) => | ||
export const Vec2Value: ValueType = { | ||
name: 'vec2', | ||
creator: () => new Vec2(), | ||
deserialize: (value: string | Vec2JSON) => | ||
typeof value === 'string' ? vec2Parse(value) : new Vec2(value[0], value[1]), | ||
(value) => [value.x, value.y] as Vec2JSON, | ||
(start: Vec2, end: Vec2, t: number) => vec2Mix(start, end, t) | ||
); | ||
serialize: (value) => [value.x, value.y] as Vec2JSON, | ||
lerp: (start: Vec2, end: Vec2, t: number) => vec2Mix(start, end, t), | ||
equals: (a: Vec2, b: Vec2) => a.x === b.x && a.y === b.y, | ||
clone: (value: Vec2) => value.clone() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
export class ValueType<TValue = any, TJson = any> { | ||
constructor( | ||
public readonly name: string, | ||
public readonly creator: () => TValue, | ||
public readonly deserialize: (value: TJson) => TValue, | ||
public readonly serialize: (value: TValue) => TJson, | ||
public readonly lerp: (start: TValue, end: TValue, t: number) => TValue, | ||
public readonly equals: (a: TValue, b: TValue) => boolean, | ||
public readonly clone: (value: TValue) => TValue | ||
) {} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export interface ValueType<TValue = any, TJson = any> { | ||
name: string; | ||
creator: () => TValue; | ||
deserialize: (value: TJson) => TValue; | ||
serialize: (value: TValue) => TJson; | ||
lerp: (start: TValue, end: TValue, t: number) => TValue; | ||
equals: (a: TValue, b: TValue) => boolean; | ||
clone: (value: TValue) => TValue; | ||
} |