Skip to content

Commit a84f33d

Browse files
committed
Use satifies operator
1 parent 581c43e commit a84f33d

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

.changeset/cool-seahorses-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Use `satisfies` operator

packages/create-svelte/templates/default/src/routes/sverdle/+page.server.ts

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { invalid } from '@sveltejs/kit';
2-
import { Game } from './game';
2+
import { words, allowed } from './words.server';
33
import type { PageServerLoad, Actions } from './$types';
44

55
/** @type {import('./$types').PageServerLoad} */
6-
export const load: PageServerLoad = ({ cookies }) => {
6+
export const load = (({ cookies }) => {
77
const game = new Game(cookies.get('sverdle'));
88

99
return {
@@ -23,10 +23,10 @@ export const load: PageServerLoad = ({ cookies }) => {
2323
*/
2424
answer: game.answers.length >= 6 ? game.answer : null
2525
};
26-
};
26+
}) satisfies PageServerLoad;
2727

2828
/** @type {import('./$types').Actions} */
29-
export const actions: Actions = {
29+
export const actions = {
3030
/**
3131
* Modify game state in reaction to a keypress. If client-side JavaScript
3232
* is available, this will happen in the browser instead of here
@@ -68,4 +68,80 @@ export const actions: Actions = {
6868
restart: async ({ cookies }) => {
6969
cookies.delete('sverdle');
7070
}
71-
};
71+
} satisfies Actions;
72+
73+
class Game {
74+
index: number;
75+
guesses: string[];
76+
answers: string[];
77+
answer: string;
78+
79+
/**
80+
* Create a game object from the player's cookie, or initialise a new game
81+
* @param {string | undefined} serialized
82+
*/
83+
constructor(serialized: string | undefined) {
84+
if (serialized) {
85+
const [index, guesses, answers] = serialized.split('-');
86+
87+
this.index = +index;
88+
this.guesses = guesses ? guesses.split(' ') : [];
89+
this.answers = answers ? answers.split(' ') : [];
90+
} else {
91+
this.index = Math.floor(Math.random() * words.length);
92+
this.guesses = ['', '', '', '', '', ''];
93+
this.answers = /** @type {string[]} */ [] /***/;
94+
}
95+
96+
this.answer = words[this.index];
97+
}
98+
99+
/**
100+
* Update game state based on a guess of a five-letter word. Returns
101+
* true if the guess was valid, false otherwise
102+
* @param {string[]} letters
103+
*/
104+
enter(letters: string[]) {
105+
const word = letters.join('');
106+
const valid = allowed.has(word);
107+
108+
if (!valid) return false;
109+
110+
this.guesses[this.answers.length] = word;
111+
112+
const available = Array.from(this.answer);
113+
const answer = Array(5).fill('_');
114+
115+
// first, find exact matches
116+
for (let i = 0; i < 5; i += 1) {
117+
if (letters[i] === available[i]) {
118+
answer[i] = 'x';
119+
available[i] = ' ';
120+
}
121+
}
122+
123+
// then find close matches (this has to happen
124+
// in a second step, otherwise an early close
125+
// match can prevent a later exact match)
126+
for (let i = 0; i < 5; i += 1) {
127+
if (answer[i] === '_') {
128+
const index = available.indexOf(letters[i]);
129+
if (index !== -1) {
130+
answer[i] = 'c';
131+
available[index] = ' ';
132+
}
133+
}
134+
}
135+
136+
this.answers.push(answer.join(''));
137+
138+
return true;
139+
}
140+
141+
/**
142+
* Serialize game state so it can be set as a cookie
143+
*/
144+
toString() {
145+
return `${this.index}-${this.guesses.join(' ')}-${this.answers.join(' ')}`;
146+
}
147+
}

packages/kit/src/core/sync/write_types/index.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,26 @@ test('Rewrites action types for a TypeScript module', () => {
265265
);
266266
});
267267

268+
test('Leaves satisfies operator untouched', () => {
269+
const source = `
270+
import type { Actions, PageServerLoad, RequestEvent } from './$types';
271+
export function load({ params }) {
272+
return {
273+
a: 1
274+
};
275+
} satisfies PageServerLoad
276+
export const actions = {
277+
a: () => {},
278+
b: (param: RequestEvent) => {},
279+
c: (param) => {},
280+
} satisfies Actions
281+
`;
282+
283+
const rewritten = tweak_types(source, true);
284+
285+
assert.equal(rewritten?.exports, ['load', 'actions']);
286+
assert.equal(rewritten?.modified, false);
287+
assert.equal(rewritten?.code, source);
288+
});
289+
268290
test.run();

0 commit comments

Comments
 (0)