Skip to content

Commit

Permalink
Merge pull request #108 from Aeryle/master
Browse files Browse the repository at this point in the history
Make rel() and loc() return default values if no argument is passed
  • Loading branch information
TheMrZZ authored Dec 6, 2021
2 parents 467585a + 511d30e commit 35291b3
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/variables/Coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ export function absolute<T extends Tuple<number>>(...coordinates: T): string | V
return new VectorClass(coordinates.map((coord) => coord.toString()) as unknown as MappedArray<T, string>)
}

/**
* Transforms numbers into relative coordinates, using the tilde notation `~`.
*
* @example
*
* rel() => ['~', '~', '~']
*
* rel(0, 0, 0) => ['~', '~', '~']
*
* rel(0, 180) => ['~', '~180']
*
* rel(-1, 10, 5) => ['~-1', '~10', '~5']
*
* @alias {@link relative}
* @see {@link absolute} for absolute coordinates (e.g. `10`)
* @see {@link local} for local coordinates (e.g. `^10`)
*/
export function relative<T extends [0, 0, 0]>(): VectorClass<MappedArray<T, '~'>>

/**
* Transforms a number into a relative coordinate, using the tilde notation `~`.
*
Expand All @@ -92,6 +111,8 @@ export function relative(coordinate: number): `~${string}`
*
* @example
*
* rel() => ['~', '~', '~']
*
* rel(0, 0, 0) => ['~', '~', '~']
*
* rel(0, 180) => ['~', '~180']
Expand All @@ -105,13 +126,38 @@ export function relative(coordinate: number): `~${string}`
export function relative<T extends Tuple<number>>(...coordinates: T): VectorClass<MappedArray<T, `~${string}`>>

export function relative<T extends Tuple<number>>(...coordinates: T): string | VectorClass<MappedArray<T, string>> {
if (coordinates.length === 0) {
return new VectorClass(['~', '~', '~'] as unknown as MappedArray<T, string>)
}

if (coordinates.length === 1) {
return `~${coordinates[0] || ''}`
}

return new VectorClass(coordinates.map((coord) => `~${coord || ''}`) as unknown as MappedArray<T, string>)
}

/**
* Transforms numbers into local coordinates, using the caret notation `^`.
*
* First coordinate is leftward, second is upward, third is frontward.
*
* @example
*
* loc() => ['^', '^', '^']
*
* loc(0, 0, 0) => ['^', '^', '^']
*
* loc(0, 180, 0) => ['^', '^180', '^']
*
* loc(-1, 10, 5) => ['^-1', '^10', '^5']
*
* @alias {@link local}
* @see {@link absolute} for absolute coordinates (e.g. `10`)
* @see {@link relative} for relative coordinates (e.g. `~10`)
*/
export function local<T extends [0, 0, 0]>(): VectorClass<MappedArray<T, '^'>>

/**
* Transforms a number into a local coordinate, using the caret notation `^`.
*
Expand All @@ -136,6 +182,8 @@ export function local(coordinate: number): `^${string}`
*
* @example
*
* loc() => ['^', '^', '^']
*
* loc(0, 0, 0) => ['^', '^', '^']
*
* loc(0, 180, 0) => ['^', '^180', '^']
Expand All @@ -149,6 +197,10 @@ export function local(coordinate: number): `^${string}`
export function local<T extends Tuple<number>>(...coordinates: T): VectorClass<MappedArray<T, `^${string}`>>

export function local<T extends Tuple<number>>(...coordinates: T): string | VectorClass<MappedArray<T, string>> {
if (coordinates.length === 0) {
return new VectorClass(['^', '^', '^'] as unknown as MappedArray<T, string>)
}

if (coordinates.length === 1) {
return `^${coordinates[0] || ''}`
}
Expand Down

0 comments on commit 35291b3

Please sign in to comment.