koffing 1.0.0 → 2.0.0

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/dist/index.js CHANGED
@@ -1,431 +1,433 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ import { a as KoffingError, c as finish, i as DEFAULT_LIMITS, l as report, n as sanitizeTeam, o as checkInput, r as validateTeam, s as fail, t as parseJSON, u as resolveLimits } from "./data-CRHBhbCJ.js";
2
+ //#region src/parse.ts
3
+ const stats$1 = {
4
+ hp: "hp",
5
+ atk: "atk",
6
+ attack: "atk",
7
+ def: "def",
8
+ defense: "def",
9
+ spa: "spa",
10
+ spatk: "spa",
11
+ specialattack: "spa",
12
+ spd: "spd",
13
+ spdef: "spd",
14
+ specialdefense: "spd",
15
+ spe: "spe",
16
+ speed: "spe"
9
17
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- Koffing: () => Koffing,
24
- Pokemon: () => Pokemon,
25
- PokemonTeam: () => PokemonTeam,
26
- PokemonTeamSet: () => PokemonTeamSet,
27
- ShowdownParser: () => ShowdownParser
28
- });
29
- module.exports = __toCommonJS(src_exports);
30
-
31
- // src/Pokemon.ts
32
- var POKEMON_STAT_NAMES = ["HP", "Atk", "Def", "SpA", "SpD", "Spe"];
33
- var Pokemon = class {
34
- constructor() {
35
- this.moves = [];
36
- }
37
- static fromObject(obj) {
38
- const p = new Pokemon();
39
- p.name = obj.name;
40
- p.nickname = obj.nickname;
41
- p.gender = obj.gender;
42
- p.item = obj.item;
43
- p.ability = obj.ability;
44
- p.level = obj.level;
45
- p.shiny = obj.shiny;
46
- p.happiness = obj.happiness;
47
- p.nature = obj.nature;
48
- p.evs = obj.evs;
49
- p.ivs = obj.ivs;
50
- p.teraType = obj.teraType;
51
- p.dynamaxLevel = obj.dynamaxLevel;
52
- p.gigantamax = obj.gigantamax;
53
- p.pokeball = obj.pokeball;
54
- p.moves = Array.isArray(obj.moves) ? obj.moves : [];
55
- return p;
56
- }
57
- toJson(indentation = 2) {
58
- return JSON.stringify(this, null, indentation);
59
- }
60
- toShowdown() {
61
- let str = "";
62
- if (this.nickname) {
63
- str += `${this.nickname} (${this.name})`;
64
- } else {
65
- str += `${this.name}`;
66
- }
67
- if (this.gender && this.gender.match(/^[MF]$/i)) {
68
- str += ` (${this.gender.toUpperCase()})`;
69
- }
70
- if (this.item) {
71
- str += ` @ ${this.item}`;
72
- }
73
- str += "\n";
74
- if (this.ability) {
75
- str += `Ability: ${this.ability}
76
- `;
77
- }
78
- if (!Number.isNaN(this.level)) {
79
- str += `Level: ${this.level}
80
- `;
81
- }
82
- if (this.shiny === true) {
83
- str += `Shiny: Yes
84
- `;
85
- }
86
- if (!Number.isNaN(this.happiness)) {
87
- str += `Happiness: ${this.happiness}
88
- `;
89
- }
90
- if (this.pokeball) {
91
- str += `Pokeball: ${this.pokeball}
92
- `;
93
- }
94
- if (!Number.isNaN(this.dynamaxLevel)) {
95
- str += `Dynamax Level: ${this.dynamaxLevel}
96
- `;
97
- }
98
- if (this.gigantamax === true) {
99
- str += `Gigantamax: Yes
100
- `;
101
- }
102
- if (this.teraType) {
103
- str += `Tera Type: ${this.teraType}
104
- `;
105
- }
106
- if (this.evs) {
107
- const evs = this.evs;
108
- str += `EVs: ` + POKEMON_STAT_NAMES.filter(function(prop) {
109
- return !isNaN(evs[prop.toLowerCase()]);
110
- }).map(function(prop) {
111
- const val = evs[prop.toLowerCase()];
112
- return `${val} ${prop}`;
113
- }).join(" / ") + "\n";
114
- }
115
- if (this.nature) {
116
- str += `${this.nature} Nature
117
- `;
118
- }
119
- if (this.ivs) {
120
- const ivs = this.ivs;
121
- str += `IVs: ` + POKEMON_STAT_NAMES.filter(function(prop) {
122
- return !isNaN(ivs[prop.toLowerCase()]);
123
- }).map(function(prop) {
124
- const val = ivs[prop.toLowerCase()];
125
- return `${val} ${prop}`;
126
- }).join(" / ") + "\n";
127
- }
128
- if (this.moves) {
129
- str += this.moves.map(function(move) {
130
- return `- ${move}`;
131
- }).join("\n") + "\n";
132
- }
133
- return str.trim();
134
- }
135
- toString() {
136
- return this.toShowdown();
137
- }
138
- };
139
-
140
- // src/PokemonTeam.ts
141
- var PokemonTeam = class {
142
- constructor(format = "gen9", name = "Untitled", folder = void 0) {
143
- this.pokemon = [];
144
- this.name = name;
145
- this.format = format;
146
- this.folder = folder;
147
- }
148
- static fromObject(obj) {
149
- const team = new PokemonTeam();
150
- team.name = obj.name;
151
- team.format = obj.format;
152
- team.folder = obj.folder;
153
- team.pokemon = obj.pokemon ? obj.pokemon.map(function(pokemon) {
154
- return Pokemon.fromObject(pokemon);
155
- }) : [];
156
- return team;
157
- }
158
- toJson(indentation = 2) {
159
- return JSON.stringify(this, null, indentation);
160
- }
161
- toShowdown() {
162
- const name = this.folder ? `${this.folder}/${this.name}` : this.name;
163
- let str = `=== [${this.format}] ${name} ===
164
-
165
- `;
166
- str += this.pokemon.map(function(p) {
167
- return p.toString();
168
- }).join("\n\n");
169
- return str.trim();
170
- }
171
- toString() {
172
- return this.toShowdown();
173
- }
174
- };
175
-
176
- // src/PokemonTeamSet.ts
177
- var PokemonTeamSet = class {
178
- constructor(teams = []) {
179
- this.teams = teams;
180
- }
181
- static fromObject(obj) {
182
- const teamSet = new PokemonTeamSet();
183
- teamSet.teams = obj.teams ? obj.teams.map(function(team) {
184
- return PokemonTeam.fromObject(team);
185
- }) : [];
186
- return teamSet;
187
- }
188
- toJson(indentation = 2) {
189
- return JSON.stringify(this, null, indentation);
190
- }
191
- toShowdown() {
192
- return this.teams.map(function(p) {
193
- return p.toString();
194
- }).join("\n\n").trim();
195
- }
196
- toString() {
197
- return this.toShowdown();
198
- }
199
- };
200
-
201
- // src/ShowdownParser.ts
202
- var clamp = (num, min, max) => {
203
- if (Number.isNaN(num)) {
204
- return min;
205
- }
206
- return Math.min(Math.max(num, min), max);
207
- };
208
- var _ShowdownParser = class {
209
- constructor(code) {
210
- this.code = code.toString().trim();
211
- }
212
- parse() {
213
- const regexes = _ShowdownParser.regexes;
214
- const teams = [];
215
- const current = {
216
- team: null,
217
- pokemon: null
218
- };
219
- const lines = this.code.trim().split("\n").map(function(line) {
220
- return line.trim();
221
- });
222
- lines.forEach((line) => {
223
- if (line.match(regexes.team)) {
224
- current.team = new PokemonTeam();
225
- this._parseTeam(line, current.team);
226
- teams.push(current.team);
227
- return;
228
- }
229
- if (line === "" || line.match(/^[- ]+$/)) {
230
- this._saveCurrent(teams, current);
231
- return;
232
- }
233
- if (!current.pokemon) {
234
- current.pokemon = new Pokemon();
235
- this._parseNameLine(line, current.pokemon);
236
- return;
237
- }
238
- if (this._parseKeyValuePairs(line, current.pokemon)) {
239
- return;
240
- }
241
- if (this._parseEvsIvs(line, current.pokemon)) {
242
- return;
243
- }
244
- if (current.pokemon.moves.length < 4 && line.match(regexes.move)) {
245
- const moveMatches = regexes.move.exec(line);
246
- if (moveMatches !== null) {
247
- current.pokemon.moves.push(moveMatches[1].trim());
248
- }
249
- }
250
- });
251
- this._saveCurrent(teams, current);
252
- return new PokemonTeamSet(teams);
253
- }
254
- _parseTeam(line, team) {
255
- const rg = _ShowdownParser.regexes;
256
- const teamDataMatches = rg.team.exec(line);
257
- if (teamDataMatches && teamDataMatches.length >= 2) {
258
- const teamNames = teamDataMatches[2].split("/");
259
- let teamName, teamFolder;
260
- if (teamNames.length > 1) {
261
- teamFolder = teamNames.shift();
262
- teamName = teamNames.join("/");
263
- } else {
264
- teamName = teamDataMatches[2];
265
- }
266
- team.format = teamDataMatches[1].trim();
267
- team.name = teamName.trim();
268
- team.folder = teamFolder ? teamFolder.trim() : void 0;
269
- }
270
- }
271
- _parseNameLine(line, pokemon) {
272
- const rg = _ShowdownParser.regexes;
273
- if (line.match(rg.nickname_name)) {
274
- const nameMatches = rg.nickname_name.exec(line);
275
- if (nameMatches) {
276
- pokemon.nickname = nameMatches[1].trim();
277
- pokemon.name = nameMatches[2].trim();
278
- }
279
- } else if (line.match(rg.name)) {
280
- const nameMatches = rg.name.exec(line);
281
- if (nameMatches) {
282
- pokemon.name = nameMatches[1].trim();
283
- }
284
- }
285
- if (line.match(rg.gender)) {
286
- const genderMatches = rg.gender.exec(line);
287
- if (genderMatches) {
288
- pokemon.gender = genderMatches[1].toUpperCase().trim();
289
- }
290
- }
291
- if (line.match(rg.item)) {
292
- const itemMatches = rg.item.exec(line);
293
- if (itemMatches) {
294
- pokemon.item = itemMatches[1].trim();
295
- }
296
- }
297
- }
298
- _parseEvsIvs(line, pokemon) {
299
- const rg = _ShowdownParser.regexes;
300
- if (line.match(rg.eivs)) {
301
- const data = rg.eivs.exec(line);
302
- if (data === null) {
303
- return false;
304
- }
305
- const prop = data[1].toLowerCase();
306
- const values = data[2].split("/");
307
- const limit = prop === "evs" ? 255 : 31;
308
- values.forEach(function(stat) {
309
- const statData = rg.eivs_value.exec(stat.trim().toLowerCase());
310
- if (!statData) {
311
- console.error("Invalid syntax for " + prop + ": " + stat);
312
- return;
313
- }
314
- if (!pokemon[prop]) {
315
- pokemon[prop] = {};
316
- }
317
- pokemon[prop][statData[2]] = clamp(parseInt(statData[1]), 0, limit);
318
- });
319
- return true;
320
- }
321
- return false;
322
- }
323
- _parseKeyValuePairs(line, pokemon) {
324
- const propNames = [
325
- "nature",
326
- "ability",
327
- "level",
328
- "shiny",
329
- "happiness",
330
- "pokeball",
331
- "dynamaxLevel",
332
- "gigantamax",
333
- "teraType"
334
- ];
335
- return propNames.some(function(key) {
336
- const matches = _ShowdownParser.regexes[key].exec(line);
337
- if (matches === null) {
338
- return false;
339
- }
340
- let value = matches[1].trim();
341
- if (key === "happiness") {
342
- value = clamp(parseInt(value), 0, 255);
343
- } else if (key === "level") {
344
- value = clamp(parseInt(value), 1, 100);
345
- } else if (key === "dynamaxLevel") {
346
- value = clamp(parseInt(value), 0, 10);
347
- } else if (key.match(/^(shiny|gigantamax)$/i)) {
348
- value = value.match(/yes/i) !== null ? true : void 0;
349
- }
350
- pokemon[key] = value;
351
- return true;
352
- });
353
- }
354
- _saveCurrent(teams, current) {
355
- if (!current.team) {
356
- current.team = new PokemonTeam();
357
- teams.push(current.team);
358
- }
359
- if (current.pokemon) {
360
- current.team.pokemon.push(current.pokemon);
361
- current.pokemon = null;
362
- }
363
- return this;
364
- }
365
- format() {
366
- this.code = this.parse().toString();
367
- return this;
368
- }
369
- toString() {
370
- return this.code;
371
- }
372
- };
373
- var ShowdownParser = _ShowdownParser;
374
- ShowdownParser.regexes = {
375
- team: /^===\s+\[(.*)\]\s+(.*)\s+===$/,
376
- nickname_name: /^([^()=@]*)\s+\(([^()=@]{2,})\)/i,
377
- name: /^([^()=@]{2,})/i,
378
- gender: /\((F|M)\)/i,
379
- item: /@\s?(.*)$/i,
380
- eivs: /^([EI]Vs):\s?(.*)$/i,
381
- eivs_value: /^([0-9]+)\s+(hp|atk|def|spa|spd|spe)$/i,
382
- move: /^[-~]\s?(.*)$/i,
383
- nature: /^(.*)\s+Nature$/,
384
- ability: /^(?:Ability|Trait):\s?(.*)$/i,
385
- level: /^Level:\s?([0-9]{1,3})$/i,
386
- shiny: /^Shiny:\s?(Yes|No)$/i,
387
- happiness: /^(?:Happiness|Friendship):\s?([0-9]{1,3})$/i,
388
- pokeball: /^(?:Pokeball|Ball):\s?(.*)$/i,
389
- dynamaxLevel: /^Dynamax Level:\s?([0-9]{1,2})$/i,
390
- gigantamax: /^Gigantamax:\s?(Yes|No)$/i,
391
- teraType: /^Tera Type:\s?(.*)$/i
392
- };
393
-
394
- // src/Koffing.ts
395
- var Koffing = class {
396
- static parse(data) {
397
- if (data instanceof PokemonTeamSet || data instanceof PokemonTeam || data instanceof Pokemon) {
398
- return data;
399
- }
400
- if (data instanceof ShowdownParser) {
401
- return data.parse();
402
- }
403
- return new ShowdownParser(data).parse();
404
- }
405
- static format(data) {
406
- return this.parse(data).toShowdown();
407
- }
408
- static toJson(data) {
409
- return this.parse(data).toJson();
410
- }
411
- static toShowdown(data) {
412
- if (data instanceof PokemonTeamSet || data instanceof PokemonTeam || data instanceof Pokemon) {
413
- return data.toShowdown();
414
- }
415
- if (data instanceof ShowdownParser) {
416
- return data.parse().toShowdown();
417
- }
418
- if (typeof data === "string") {
419
- data = JSON.parse(data);
420
- }
421
- return PokemonTeamSet.fromObject(data).toShowdown();
422
- }
423
- };
424
- // Annotate the CommonJS export names for ESM import in node:
425
- 0 && (module.exports = {
426
- Koffing,
427
- Pokemon,
428
- PokemonTeam,
429
- PokemonTeamSet,
430
- ShowdownParser
431
- });
18
+ const numericFields = /* @__PURE__ */ new Map([
19
+ ["level", "level"],
20
+ ["happiness", "happiness"],
21
+ ["friendship", "happiness"],
22
+ ["dynamax level", "dynamaxLevel"]
23
+ ]);
24
+ const stringFields = /* @__PURE__ */ new Map([
25
+ ["ability", "ability"],
26
+ ["trait", "ability"],
27
+ ["item", "item"],
28
+ ["nickname", "name"],
29
+ ["species", "species"],
30
+ ["tera type", "teraType"],
31
+ ["hidden power", "hpType"],
32
+ ["pokeball", "pokeball"],
33
+ ["ball", "pokeball"]
34
+ ]);
35
+ const natureStats = [
36
+ "atk",
37
+ "def",
38
+ "spe",
39
+ "spa",
40
+ "spd"
41
+ ];
42
+ const natures = [
43
+ [
44
+ "Hardy",
45
+ "Lonely",
46
+ "Brave",
47
+ "Adamant",
48
+ "Naughty"
49
+ ],
50
+ [
51
+ "Bold",
52
+ "Docile",
53
+ "Relaxed",
54
+ "Impish",
55
+ "Lax"
56
+ ],
57
+ [
58
+ "Timid",
59
+ "Hasty",
60
+ "Serious",
61
+ "Jolly",
62
+ "Naive"
63
+ ],
64
+ [
65
+ "Modest",
66
+ "Mild",
67
+ "Quiet",
68
+ "Bashful",
69
+ "Rash"
70
+ ],
71
+ [
72
+ "Calm",
73
+ "Gentle",
74
+ "Sassy",
75
+ "Careful",
76
+ "Quirky"
77
+ ]
78
+ ];
79
+ const controlCharacters = /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\u2028\u2029]/;
80
+ /** Parse Showdown text without silently clamping values or dropping extra moves. */
81
+ function parse(input, options = {}) {
82
+ const limits = resolveLimits(options);
83
+ checkInput(input, limits);
84
+ const hasControls = controlCharacters.test(input);
85
+ const hasTabs = input.includes(" ");
86
+ const hasPipes = input.includes("|");
87
+ const teams = [];
88
+ const diagnostics = [];
89
+ let team;
90
+ let pokemon;
91
+ let count = 0;
92
+ let seen = /* @__PURE__ */ new Set();
93
+ let explicitHpType = false;
94
+ let explicitHappiness = false;
95
+ let explicitNature = false;
96
+ let increased;
97
+ let decreased;
98
+ let lineNumber = 0;
99
+ const warn = (code, message) => {
100
+ report(diagnostics, {
101
+ code,
102
+ message,
103
+ severity: "warning",
104
+ line: lineNumber
105
+ }, limits);
106
+ };
107
+ const duplicate = (key) => {
108
+ if (seen.has(key)) warn("duplicate-field", `Repeated ${key}; the last value takes precedence`);
109
+ seen.add(key);
110
+ };
111
+ const addTeam = (value) => {
112
+ if (teams.length >= limits.maxTeams) fail("team-limit", "Too many teams", lineNumber);
113
+ teams.push(value);
114
+ team = value;
115
+ };
116
+ const move = (value) => {
117
+ if (!pokemon) return;
118
+ if (!value) {
119
+ warn("invalid-move", "Move name is empty");
120
+ return;
121
+ }
122
+ if (pokemon.moves.length >= limits.maxMoves) fail("move-limit", "Too many moves", lineNumber);
123
+ const hidden = /^Hidden Power\s*\[([^\]]+)\]$/i.exec(value);
124
+ if (hidden) {
125
+ value = `Hidden Power ${hidden[1].trim()}`;
126
+ if (!explicitHpType) pokemon.hpType = hidden[1].trim();
127
+ }
128
+ pokemon.moves.push(value);
129
+ if (pokemon.moves.length === 5) warn("move-count", "More than four moves; all moves were preserved");
130
+ if (/^Frustration$/i.test(value) && !explicitHappiness) pokemon.happiness = 0;
131
+ };
132
+ let nextCR = input.indexOf("\r");
133
+ let nextLF = input.indexOf("\n");
134
+ for (let start = 0; start <= input.length;) {
135
+ let end = nextLF < 0 ? input.length : nextLF;
136
+ if (nextCR >= 0 && nextCR < end) end = nextCR;
137
+ lineNumber++;
138
+ if (end - start > limits.maxLineLength) fail("line-limit", "Line is too long", lineNumber);
139
+ const rawLine = input.slice(start, end);
140
+ const line = (hasTabs ? rawLine.replace(/\t/g, " ") : rawLine).trim();
141
+ const separator = input[end];
142
+ start = end + (separator === "\r" && input[end + 1] === "\n" ? 2 : 1);
143
+ if (nextCR >= 0 && nextCR < start) nextCR = input.indexOf("\r", start);
144
+ if (nextLF >= 0 && nextLF < start) nextLF = input.indexOf("\n", start);
145
+ if (hasControls && controlCharacters.test(rawLine)) {
146
+ warn("invalid-character", "Control characters are not allowed");
147
+ continue;
148
+ }
149
+ if (!line || /^-{3,}$/.test(line)) {
150
+ pokemon = void 0;
151
+ continue;
152
+ }
153
+ if (hasPipes && line.includes("|")) fail("unsupported-packed-format", "Packed teams are not supported; import Showdown text instead", lineNumber);
154
+ if (pokemon && (line[0] === "-" || line[0] === "~")) {
155
+ move(line.slice(1).trimStart());
156
+ continue;
157
+ }
158
+ const header = line[0] === "=" ? /^===\s*(.*?)\s*===$/.exec(line) : null;
159
+ if (header) {
160
+ const value = { pokemon: [] };
161
+ let title = header[1].trim();
162
+ const format = /^\[([^\]]+)\]\s*/.exec(title);
163
+ if (format) {
164
+ value.format = format[1].trim();
165
+ title = title.slice(format[0].length);
166
+ }
167
+ const slash = title.lastIndexOf("/");
168
+ if (slash >= 0) {
169
+ value.folder = title.slice(0, slash).trim();
170
+ title = title.slice(slash + 1).trim();
171
+ }
172
+ if (title) value.name = title;
173
+ addTeam(value);
174
+ pokemon = void 0;
175
+ continue;
176
+ }
177
+ if (line.startsWith("===")) {
178
+ warn("invalid-header", "Malformed team header");
179
+ pokemon = void 0;
180
+ continue;
181
+ }
182
+ const colon = line.indexOf(":");
183
+ const hasDetail = colon > 0;
184
+ const nature = hasDetail || line.includes("@") || !/\sNature$/i.test(line) ? null : /^(.*?)\s+Nature$/i.exec(line);
185
+ const bare = !hasDetail && /^(Shiny|Gigantamax)$/i.test(line);
186
+ if (!pokemon) {
187
+ const typeNullHeader = hasDetail && /^(?:[^:]*\()?Type: Null\)?(?:\s+\([MFN]\))?(?:\s+\[[^\]]+\])?(?:\s+@\s+.+)?$/i.test(line);
188
+ if (line.startsWith("-") || hasDetail && !typeNullHeader || nature || bare) {
189
+ warn("orphan-detail", "Pokémon detail appears before a Pokémon header");
190
+ continue;
191
+ }
192
+ let identity = line;
193
+ let item;
194
+ const at = identity.lastIndexOf(" @ ");
195
+ if (at >= 0) {
196
+ item = identity.slice(at + 3).trim();
197
+ identity = identity.slice(0, at).trim();
198
+ }
199
+ let ability;
200
+ const bracket = /\s*\[([^\]]+)\]$/.exec(identity);
201
+ if (bracket) {
202
+ ability = bracket[1].trim();
203
+ identity = identity.slice(0, bracket.index).trim();
204
+ }
205
+ let gender;
206
+ const sex = /\s*\(([MFN])\)$/.exec(identity);
207
+ if (sex) {
208
+ gender = sex[1];
209
+ identity = identity.slice(0, sex.index).trim();
210
+ }
211
+ const named = /^(.*?)\s*\(([^()]+)\)$/.exec(identity);
212
+ const species = (named ? named[2] : identity).trim();
213
+ if (!species || /[\[\]@()=]/.test(species)) {
214
+ warn("invalid-header", "Malformed Pokémon header");
215
+ continue;
216
+ }
217
+ if (count >= limits.maxPokemon) fail("pokemon-limit", "Too many Pokémon", lineNumber);
218
+ count++;
219
+ pokemon = {
220
+ species,
221
+ moves: []
222
+ };
223
+ if (named?.[1]?.trim()) pokemon.name = named[1].trim();
224
+ if (item && !/^No Item$/i.test(item)) pokemon.item = item;
225
+ if (ability) pokemon.ability = ability;
226
+ if (gender) pokemon.gender = gender;
227
+ if (!team) addTeam({ pokemon: [] });
228
+ team.pokemon.push(pokemon);
229
+ seen.clear();
230
+ seen.add("species");
231
+ if (pokemon.name !== void 0) seen.add("name");
232
+ if (pokemon.item !== void 0) seen.add("item");
233
+ if (pokemon.ability !== void 0) seen.add("ability");
234
+ if (pokemon.gender !== void 0) seen.add("gender");
235
+ explicitHpType = false;
236
+ explicitHappiness = false;
237
+ explicitNature = false;
238
+ increased = void 0;
239
+ decreased = void 0;
240
+ continue;
241
+ }
242
+ const bracketDetail = line[0] === "[" ? /^\[([^\]]+)\](?:\s*@\s*(.+))?$/.exec(line) : null;
243
+ if (bracketDetail) {
244
+ duplicate("ability");
245
+ pokemon.ability = bracketDetail[1].trim();
246
+ if (bracketDetail[2]) {
247
+ duplicate("item");
248
+ if (/^No Item$/i.test(bracketDetail[2])) delete pokemon.item;
249
+ else pokemon.item = bracketDetail[2].trim();
250
+ }
251
+ continue;
252
+ }
253
+ if (nature) {
254
+ duplicate("nature");
255
+ pokemon.nature = nature[1].trim();
256
+ explicitNature = true;
257
+ continue;
258
+ }
259
+ const key = (hasDetail ? line.slice(0, colon).trim() : bare ? line : "").toLowerCase();
260
+ const value = hasDetail ? line.slice(colon + 1).trim() : "Yes";
261
+ if (key === "move") {
262
+ move(value);
263
+ continue;
264
+ }
265
+ if (key === "evs" || key === "ivs") {
266
+ duplicate(key);
267
+ const values = {};
268
+ let conflictingModifiers = false;
269
+ if (key === "evs") {
270
+ increased = void 0;
271
+ decreased = void 0;
272
+ if (!explicitNature) delete pokemon.nature;
273
+ }
274
+ for (const entry of value.split("/")) {
275
+ const match = /^\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)([+-]?)\s+([a-z .]+)\s*$/i.exec(entry);
276
+ const statName = match?.[3].toLowerCase().replace(/[ .]/g, "");
277
+ const stat = statName && Object.hasOwn(stats$1, statName) ? stats$1[statName] : void 0;
278
+ if (!match || !stat || !Number.isFinite(Number(match[1]))) {
279
+ warn("invalid-stat", `Invalid ${key} entry: ${entry.trim()}`);
280
+ continue;
281
+ }
282
+ if (values[stat] !== void 0) warn("duplicate-stat", `Repeated ${stat}; the last value takes precedence`);
283
+ values[stat] = Number(match[1]);
284
+ const max = key === "ivs" ? 31 : 255;
285
+ if (!Number.isInteger(values[stat]) || values[stat] < 0 || values[stat] > max) warn("stat-range", `${stat} is outside the traditional ${key} range; value preserved`);
286
+ if (match[2]) {
287
+ if (key !== "evs" || stat === "hp") warn("invalid-nature-modifier", "Nature modifiers apply only to non-HP EVs");
288
+ else if (match[2] === "+") {
289
+ if (increased) {
290
+ conflictingModifiers = true;
291
+ warn("invalid-nature-modifier", "More than one increased stat");
292
+ }
293
+ increased = stat;
294
+ } else {
295
+ if (decreased) {
296
+ conflictingModifiers = true;
297
+ warn("invalid-nature-modifier", "More than one decreased stat");
298
+ }
299
+ decreased = stat;
300
+ }
301
+ }
302
+ }
303
+ pokemon[key] = values;
304
+ if (key === "evs" && Object.values(values).reduce((sum, number) => sum + number, 0) > 510) warn("ev-total", "EV total exceeds 510; values preserved");
305
+ if (key === "evs" && increased && increased === decreased) {
306
+ conflictingModifiers = true;
307
+ warn("invalid-nature-modifier", "The same stat cannot be increased and decreased");
308
+ }
309
+ if (key === "evs" && !explicitNature && !conflictingModifiers && increased && decreased) pokemon.nature = natures[natureStats.indexOf(increased)][natureStats.indexOf(decreased)];
310
+ continue;
311
+ }
312
+ const numberKey = numericFields.get(key);
313
+ if (numberKey) {
314
+ duplicate(numberKey);
315
+ const number = Number(value);
316
+ if (!value || !Number.isFinite(number)) {
317
+ warn("invalid-number", `Invalid ${key}`);
318
+ continue;
319
+ }
320
+ pokemon[numberKey] = number;
321
+ if (numberKey === "happiness") explicitHappiness = true;
322
+ const min = numberKey === "level" ? 1 : 0;
323
+ const max = numberKey === "level" ? 100 : numberKey === "happiness" ? 255 : 10;
324
+ if (!Number.isInteger(number) || number < min || number > max) warn("number-range", `${key} is outside its traditional range; value preserved`);
325
+ continue;
326
+ }
327
+ if (key === "shiny" || key === "gigantamax") {
328
+ duplicate(key);
329
+ if (!/^(yes|no|true|false)$/i.test(value)) warn("invalid-boolean", `Invalid ${key}`);
330
+ else pokemon[key] = /^(yes|true)$/i.test(value);
331
+ continue;
332
+ }
333
+ if (key === "gender") {
334
+ duplicate("gender");
335
+ if (value === "M" || value === "F" || value === "N" || value === "") pokemon.gender = value;
336
+ else warn("invalid-gender", "Gender must be M, F, N or empty");
337
+ continue;
338
+ }
339
+ const stringKey = stringFields.get(key);
340
+ if (stringKey) {
341
+ duplicate(stringKey);
342
+ if (!value) {
343
+ warn("empty-field", `Empty ${key}`);
344
+ continue;
345
+ }
346
+ if (stringKey === "item" && /^No Item$/i.test(value)) delete pokemon.item;
347
+ else pokemon[stringKey] = value;
348
+ if (stringKey === "hpType") explicitHpType = true;
349
+ continue;
350
+ }
351
+ warn("unknown-line", `Unrecognized line: ${line}`);
352
+ }
353
+ finish(diagnostics, options);
354
+ return {
355
+ teams,
356
+ diagnostics
357
+ };
358
+ }
359
+ //#endregion
360
+ //#region src/serialize.ts
361
+ const stats = [
362
+ ["hp", "HP"],
363
+ ["atk", "Atk"],
364
+ ["def", "Def"],
365
+ ["spa", "SpA"],
366
+ ["spd", "SpD"],
367
+ ["spe", "Spe"]
368
+ ];
369
+ function decimal(value) {
370
+ const source = String(value);
371
+ if (!/[eE]/u.test(source)) return source;
372
+ const [mantissa, exponentText] = source.split(/[eE]/u);
373
+ const negative = mantissa.startsWith("-");
374
+ const [whole, fraction = ""] = (negative ? mantissa.slice(1) : mantissa).split(".");
375
+ const digits = whole + fraction;
376
+ const point = whole.length + Number(exponentText);
377
+ const expanded = point <= 0 ? `0.${"0".repeat(-point)}${digits}` : point >= digits.length ? `${digits}${"0".repeat(point - digits.length)}` : `${digits.slice(0, point)}.${digits.slice(point)}`;
378
+ return `${negative ? "-" : ""}${expanded}`;
379
+ }
380
+ function serializeSet(set) {
381
+ let title = set.name ? `${set.name} (${set.species})` : set.species;
382
+ if (set.gender === "M" || set.gender === "F") title += ` (${set.gender})`;
383
+ if (set.item) title += ` @ ${set.item}`;
384
+ let output = title;
385
+ const append = (label, value) => {
386
+ if (value !== void 0 && (typeof value !== "number" || Number.isFinite(value))) output += `\n${label}: ${value}`;
387
+ };
388
+ append("Ability", set.ability);
389
+ append("Level", set.level);
390
+ if (set.gender === "N" || set.gender === "") append("Gender", set.gender);
391
+ if (set.shiny !== void 0) append("Shiny", set.shiny ? "Yes" : "No");
392
+ append("Happiness", set.happiness);
393
+ append("Pokeball", set.pokeball);
394
+ append("Hidden Power", set.hpType);
395
+ append("Dynamax Level", set.dynamaxLevel);
396
+ if (set.gigantamax !== void 0) append("Gigantamax", set.gigantamax ? "Yes" : "No");
397
+ append("Tera Type", set.teraType);
398
+ const appendStats = (label, values) => {
399
+ if (values === void 0) return;
400
+ let entries = "";
401
+ for (const [key, name] of stats) {
402
+ const value = values[key];
403
+ if (value !== void 0 && Number.isFinite(value)) {
404
+ if (entries) entries += " / ";
405
+ entries += `${decimal(value)} ${name}`;
406
+ }
407
+ }
408
+ if (entries) append(label, entries);
409
+ };
410
+ appendStats("EVs", set.evs);
411
+ if (set.nature) output += `\n${set.nature} Nature`;
412
+ appendStats("IVs", set.ivs);
413
+ for (const move of set.moves) {
414
+ const hidden = /^Hidden Power ([a-z]+)$/iu.exec(move);
415
+ output += `\n- ${hidden ? `Hidden Power [${hidden[1]}]` : move}`;
416
+ }
417
+ return output;
418
+ }
419
+ /** Format trusted typed sets, like Showdown. Use parseJSON separately for untrusted data. */
420
+ function exportTeam(pokemon) {
421
+ return pokemon.map(serializeSet).join("\n\n");
422
+ }
423
+ /** Collection export always includes headers, preserving team boundaries. */
424
+ function exportTeams(teams) {
425
+ return teams.map((team) => {
426
+ const metadata = `${team.format ? `[${team.format}] ` : ""}${team.folder ? `${team.folder}/` : ""}${team.name ?? ""}`;
427
+ const body = team.pokemon.map(serializeSet).join("\n\n");
428
+ if (teams.length === 1 && !metadata && body) return body;
429
+ return `=== ${metadata} ===${body ? `\n\n${body}` : ""}`;
430
+ }).join("\n\n");
431
+ }
432
+ //#endregion
433
+ export { DEFAULT_LIMITS, KoffingError, exportTeam, exportTeams, parse, parseJSON, sanitizeTeam, validateTeam };