Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 5x 31x 31x 31x 10964x 13706x 13706x 10933x 10964x 10964x 38284x 7715x 30569x 4942x 6022x 23796x 21783x 6022x 5892x 23352x 21684x 5892x 8x 13706x 31x 1x 31x 1x 31x 1x 31x 1x | import { makeFluent } from "../../base";
import type { Props, Return } from "../../base/mixin";
import { type MaxDepth } from "../../internal";
import { Math } from "../math";
type Axis = typeof axis;
type Vec<
T,
N extends number,
TOut extends T[] = [],
> = TOut["length"] extends N
? TOut
: TOut["length"] extends MaxDepth
? T[]
: Vec<T, N, [...TOut, T]>;
type Inc<N extends number | undefined> = N extends number
? [...Vec<null, N>, null]["length"]
: 0;
type Dec<N extends number> =
Vec<null, N> extends [...infer Rest extends null[], null]
? Rest["length"]
: -1;
type Next<
N extends number,
State extends readonly (number | undefined)[],
TOut extends (number | undefined)[] = [],
> = State extends readonly [
infer Current extends number | undefined,
...infer Rest extends (number | undefined)[],
]
? Inc<Current> extends infer TInc
? TInc extends N
? Next<N, Rest, [...TOut, undefined]>
: [...TOut, TInc, ...Rest]
: never
: null;
type Key<
State extends (number | undefined)[],
TOut extends string = "",
> = State extends readonly [infer Only extends number, ...undefined[]]
? `${TOut}${Axis[Only]}`
: State extends readonly [
infer Current extends number,
...infer Rest extends (number | undefined)[],
]
? Key<Rest, `${TOut}${Axis[Current]}`>
: never;
type IsAscending<
State extends (number | undefined)[],
Prev extends number | undefined = undefined,
> = State extends readonly [
infer Current extends number,
...infer Rest extends number[],
]
? Current extends Inc<Prev>
? IsAscending<Rest, Inc<Prev>>
: false
: State extends readonly []
? true
: false;
type Pretty<T> = { [K in keyof T]: T[K] };
type SwizzlePermutations<
N extends number,
State extends Vec<number | undefined, N> = Vec<undefined, N>,
TOut extends Record<string, readonly number[]> = Record<
never,
never
>,
> =
Next<N, State> extends infer TNext
? TNext extends Vec<number | undefined, N>
? SwizzlePermutations<
N,
TNext,
TOut &
(IsAscending<State> extends true
? unknown
: Record<Key<State>, State>)
>
: Pretty<TOut & Record<Key<State>, State>>
: never;
type Sequence<
T extends readonly unknown[],
Indexes extends (number | undefined)[],
TOut extends T[number][] = [],
> = Indexes extends readonly [
infer Current extends number,
...infer Rest extends (number | undefined)[],
]
? Sequence<T, Rest, [...TOut, T[Current]]>
: TOut;
type Primative<
T extends readonly unknown[],
t extends Props,
Axes extends readonly string[] = Axis,
Acc = unknown,
TOut extends unknown[] = [],
> = Axes extends readonly [
infer Current extends string,
...infer Rest extends string[],
]
? Acc &
Record<
Current,
Return<T[TOut["length"]], t>
> extends infer TAcc
? Primative<T, t, Rest, TAcc, [...TOut, TAcc]>
: never
: TOut;
type SwizzleCache = [
{ x: [0] },
SwizzlePermutations<2>,
SwizzlePermutations<3>,
SwizzlePermutations<4>,
];
export type Swizzle<
T extends readonly unknown[],
t extends Props,
> = number extends T["length"]
? unknown
: (
Dec<T["length"]> extends infer K extends 0 | 1 | 2 | 3
? { k: K; c: SwizzleCache[K] }
: SwizzleCache extends [...unknown[], infer Last]
? { k: Dec<SwizzleCache["length"]>; c: Last }
: never
) extends {
k: infer K extends number;
c: infer C extends Record<
PropertyKey,
(number | undefined)[]
>;
}
? Omit<
{
readonly [K in keyof C]: Return<
Sequence<T, C[K]>,
t
>;
},
Axis[number]
> &
Primative<T, t>[K]
: never;
const axis = ["x", "y", "z", "w"] as const;
export function applySwizzle(
value: number[],
$: object,
fluent: (value: unknown) => never,
) {
const length = globalThis.Math.min(value.length, axis.length);
const state = new Array<number>(length).fill(-1);
main: do {
for (let i = 0; i < state.length; i++) {
state[i] += 1;
if (state[i] >= length) state[i] = -1;
else break;
}
let reachedEmpty = false;
for (const item of state) {
if (item === -1) {
reachedEmpty = true;
} else if (reachedEmpty) {
continue main;
}
}
const key = state
.filter((x) => x !== -1)
.map((x) => axis[x])
.join("");
if (key.length <= 1 || key in $) continue;
const permutation = state
.filter((x) => x !== -1)
.map((x) => value[x]);
Object.defineProperty($, key, {
get: () => {
// OPTIMIZE? Reduce amount of arrays allocated and held for the lambda..?
return fluent(permutation);
},
});
} while (state.some((x) => x !== -1));
Object.defineProperty($, "x", {
get: () => {
return fluent(value[0]);
},
});
Object.defineProperty($, "y", {
get: () => {
return fluent(value[1]);
},
});
Object.defineProperty($, "z", {
get: () => {
return fluent(value[2]);
},
});
Object.defineProperty($, "w", {
get: () => {
return fluent(value[3]);
},
});
}
if (import.meta.vitest) {
const { test, expect, expectTypeOf } = import.meta.vitest;
const registry = [Math] as const;
const $ = makeFluent(registry);
test("Swizzling (.xx, .xy, etc.)", () => {
const x = 5 as const;
const y = 7 as const;
const vec = [x, y] as const;
expect($(vec).xx.value).toMatchObject([x, x]);
expect($(vec).yy.value).toMatchObject([y, y]);
const yx = $(vec).yx.value;
expect(yx).toMatchObject([y, x]);
expectTypeOf(yx).toEqualTypeOf<[typeof y, typeof x]>();
const large = [x, y, x, y] as const;
expect($(large).wzyx.value).toMatchObject([y, x, y, x]);
expect($(large).xzyw.value).toMatchObject([x, x, y, y]);
expect($(large).ywxz.value).toMatchObject([y, y, x, x]);
expect($(large).xxxx.value).toMatchObject([x, x, x, x]);
expect($(large).wwww.value).toMatchObject([y, y, y, y]);
});
test(".x", () => {
const x = 5 as const;
const arr = [x] as const;
const v = $(arr).x.value;
expect(v).toBe(x);
expectTypeOf(v).toEqualTypeOf(x);
});
test(".y", () => {
const y = 2 as const;
const arr = [y, y] as const;
const v = $(arr).y.value;
expect(v).toBe(y);
expectTypeOf(v).toEqualTypeOf(y);
});
test(".z", () => {
const z = 9 as const;
const arr = [z, z, z] as const;
const v = $(arr).z.value;
expect(v).toBe(z);
expectTypeOf(v).toEqualTypeOf(z);
});
test(".w", () => {
const w = 0 as const;
const arr = [w, w, w, w] as const;
const v = $(arr).w.value;
expect(v).toBe(w);
expectTypeOf(v).toEqualTypeOf(w);
});
test("Next", () => {
type N = 3;
expectTypeOf<
Next<N, [undefined, undefined, undefined]>
>().toEqualTypeOf<[0, undefined, undefined]>();
expectTypeOf<
Next<N, [2, undefined, undefined]>
>().toEqualTypeOf<[undefined, 0, undefined]>();
expectTypeOf<Next<N, [2, 2, 2]>>().toEqualTypeOf<null>();
});
test("Key", () => {
expectTypeOf<Key<[1, 2, undefined]>>().toEqualTypeOf<"yz">();
expectTypeOf<Key<[1, 1, 1]>>().toEqualTypeOf<"yyy">();
});
test("Sequence", () => {
type In = [1, 2, 3, 4, 5];
type Out = [5, 4, 3, 2, 1];
type Reverse = [4, 3, 2, 1, 0];
expectTypeOf<Sequence<In, Reverse>>().toEqualTypeOf<Out>();
});
test("Primative", () => {
interface Props {
value: [number, number];
meta: { registry: [] };
}
type V = Primative<[number, number], Props>[1];
expectTypeOf<V>().toHaveProperty("x");
expectTypeOf<V>().toHaveProperty("y");
expectTypeOf<V>().not.toHaveProperty("z");
});
}
|