@nativewrappers/common 0.0.55 → 0.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Remco Troost <d0p3t>",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.0.55",
7
+ "version": "0.0.56",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/nativewrappers/nativewrappers.git"
package/utils/Vector.d.ts CHANGED
@@ -47,6 +47,11 @@ type VectorLike = Vec | Vector;
47
47
  * Utility type to get the vector type of an object based on its component.
48
48
  */
49
49
  type InferVector<T> = T extends Vec4 | VectorN<4> ? Vector4 : T extends Vec3 | VectorN<3> ? Vector3 : T extends Vec2 | VectorN<2> ? Vector2 : any;
50
+ type VectorKey = "x" | "y" | "z" | "w";
51
+ type VectorSwizzle = Vec2Swizzle | Vec3Swizzle | Vec4Swizzle;
52
+ type Vec2Swizzle = `${VectorKey}${VectorKey}`;
53
+ type Vec3Swizzle = `${VectorKey}${VectorKey}${VectorKey}`;
54
+ type Vec4Swizzle = `${VectorKey}${VectorKey}${VectorKey}${VectorKey}`;
50
55
  /**
51
56
  * A base vector class inherited by all vector classes.
52
57
  */
@@ -302,6 +307,7 @@ declare abstract class Vector {
302
307
  * @returns The magnitude of the vector.
303
308
  */
304
309
  get Length(): number;
310
+ swizzle<T extends VectorSwizzle>(components: T): T extends Vec2Swizzle ? Vector2 : T extends Vec3Swizzle ? Vector3 : Vector4;
305
311
  }
306
312
  /**
307
313
  * Represents a 2-dimensional vector.
package/utils/Vector.js CHANGED
@@ -436,6 +436,13 @@ class Vector {
436
436
  sum += value * value;
437
437
  return Math.sqrt(sum);
438
438
  }
439
+ swizzle(components) {
440
+ if (!/^[xyzw]+$/.test(components))
441
+ throw new Error(`Invalid key in swizzle components (${components}).`);
442
+ const arr = components.split("").map((char) => this[char] ?? 0);
443
+ //@ts-ignore
444
+ return Vector.create(...arr);
445
+ }
439
446
  }
440
447
  /**
441
448
  * Represents a 2-dimensional vector.
@@ -550,7 +557,7 @@ export class Vector4 extends Vector {
550
557
  */
551
558
  constructor(x, y = x, z = y, w = z) {
552
559
  super(x, y, z, w);
553
- this.z = w;
560
+ this.z = z;
554
561
  this.w = w;
555
562
  }
556
563
  /**