@dicelette/core 1.19.0 → 1.20.1

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Engine } from 'random-js';
1
+ import { Engine, Random } from 'random-js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  /**
@@ -9,7 +9,7 @@ import { z } from 'zod';
9
9
  * Random stats = 6
10
10
  * result = "1d20>3"
11
11
  */
12
- declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate): string;
12
+ declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate, engine?: Engine | null): string;
13
13
  /**
14
14
  * Parse the string provided and turn it as a readable dice for dice parser
15
15
  * @param dice {string}
@@ -24,6 +24,41 @@ declare function roll(dice: string, engine?: Engine | null): Resultat | undefine
24
24
  */
25
25
  declare function calculator(sign: Sign, value: number, total: number): number;
26
26
 
27
+ declare class DiceTypeError extends Error {
28
+ readonly dice: string;
29
+ readonly cause: string | undefined;
30
+ readonly method: unknown;
31
+ constructor(dice: string, cause?: string, method?: unknown);
32
+ }
33
+ declare class FormulaError extends Error {
34
+ readonly formula: string;
35
+ readonly cause: string | undefined;
36
+ readonly method: unknown;
37
+ constructor(formula: string, cause?: string, method?: unknown);
38
+ }
39
+ declare class MaxGreater extends Error {
40
+ readonly name: string;
41
+ readonly value: number;
42
+ readonly max: number;
43
+ constructor(value: number, max: number);
44
+ }
45
+ declare class EmptyObjectError extends Error {
46
+ readonly name: string;
47
+ constructor();
48
+ }
49
+ declare class TooManyDice extends Error {
50
+ readonly name: string;
51
+ constructor();
52
+ }
53
+ declare class TooManyStats extends Error {
54
+ readonly name: string;
55
+ constructor();
56
+ }
57
+ declare class NoStatisticsError extends Error {
58
+ readonly name: string;
59
+ constructor();
60
+ }
61
+
27
62
  interface Resultat {
28
63
  /**
29
64
  * Original dice throw
@@ -198,6 +233,61 @@ interface CustomCritical {
198
233
  affectSkill?: boolean;
199
234
  }
200
235
 
236
+ declare const COMMENT_REGEX: RegExp;
237
+ declare const SIGN_REGEX: RegExp;
238
+ declare const SIGN_REGEX_SPACE: RegExp;
239
+ declare const SYMBOL_DICE = "&";
240
+ declare const DETECT_CRITICAL: RegExp;
241
+
242
+ interface StatisticalSchema extends StatisticalTemplate {
243
+ /**
244
+ * Specifies the URL for the schema definition
245
+ * This property is optional and should contain a valid URI.
246
+ */
247
+ $schema?: string;
248
+ }
249
+
250
+ /**
251
+ * Definition of the Zod schema for template data
252
+ */
253
+
254
+ declare const templateSchema: z.ZodObject<{
255
+ charName: z.ZodOptional<z.ZodBoolean>;
256
+ statistics: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
257
+ max: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
258
+ min: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
259
+ combinaison: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string | undefined, string>>>;
260
+ exclude: z.ZodOptional<z.ZodBoolean>;
261
+ }, z.core.$strip>>>;
262
+ total: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
263
+ forceDistrib: z.ZodOptional<z.ZodBoolean>;
264
+ diceType: z.ZodOptional<z.ZodString>;
265
+ critical: z.ZodOptional<z.ZodPipe<z.ZodObject<{
266
+ success: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
267
+ failure: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
268
+ }, z.core.$strip>, z.ZodTransform<{
269
+ success?: string | number | undefined;
270
+ failure?: string | number | undefined;
271
+ }, {
272
+ success?: string | number | undefined;
273
+ failure?: string | number | undefined;
274
+ }>>>;
275
+ customCritical: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
276
+ sign: z.ZodEnum<{
277
+ "<": "<";
278
+ ">": ">";
279
+ ">=": ">=";
280
+ "<=": "<=";
281
+ "!=": "!=";
282
+ "==": "==";
283
+ }>;
284
+ value: z.ZodString;
285
+ onNaturalDice: z.ZodOptional<z.ZodBoolean>;
286
+ affectSkill: z.ZodOptional<z.ZodBoolean>;
287
+ }, z.core.$strip>>>;
288
+ damage: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
289
+ }, z.core.$strip>;
290
+
201
291
  /**
202
292
  * Escape regex string
203
293
  * @param string {string}
@@ -232,16 +322,40 @@ declare function isNumber(value: unknown): boolean;
232
322
  * Replace the `{exp}` in the dice.
233
323
  * If the `{exp}` has a default value in the form of `{exp || defaultValue}`, it will be replaced by the default value.
234
324
  * @param {string} dice
325
+ * @param engine
235
326
  * @returns {string} the dice with the {exp} replaced by a random value
236
327
  */
237
- declare function replaceExpByRandom(dice: string): string;
328
+ declare function replaceExpByRandom(dice: string, engine?: Engine | null): string;
329
+ /**
330
+ * Utility function to get a random integer between min and max and using the specified engine
331
+ * @param min {number}
332
+ * @param max {number}
333
+ * @param engine {Engine | null} Engine to use, default to nodeCrypto
334
+ * @param rng {Random | undefined} Random instance to use, see https://www.npmjs.com/package/random-js#usage
335
+ * @returns {number} Random integer between min and max
336
+ */
337
+ declare function randomInt(min: number, max: number, engine?: Engine | null, rng?: Random): number;
338
+ /**
339
+ * Utility function that allow to get the id of an engine
340
+ * @param engine {unknown} Engine to identify
341
+ * @returns {string} Id of the engine or "unknown"
342
+ * @private
343
+ */
344
+ declare function getEngineId(engine: unknown): string;
345
+ /**
346
+ * Utility function to get the engine from its name
347
+ * @param engine {"nativeMath" | "browserCrypto" | "nodeCrypto"} The engine name
348
+ * @returns {Engine} The engine
349
+ * @public
350
+ */
351
+ declare function getEngine(engine: "nativeMath" | "browserCrypto" | "nodeCrypto"): Engine;
238
352
 
239
353
  /**
240
354
  * Verify if the provided dice work with random value
241
355
  * @param testDice {string}
242
356
  * @param allStats {Record<string,number>}
243
357
  */
244
- declare function evalStatsDice(testDice: string, allStats?: Record<string, number>): string;
358
+ declare function evalStatsDice(testDice: string, allStats?: Record<string, number>, engine?: Engine | null): string;
245
359
  /**
246
360
  * Generate a random dice and remove the formula (+ evaluate it)
247
361
  * Used for diceDamage only
@@ -249,13 +363,13 @@ declare function evalStatsDice(testDice: string, allStats?: Record<string, numbe
249
363
  * @param template {StatisticalTemplate}
250
364
  * @returns
251
365
  */
252
- declare function diceRandomParse(value: string, template: StatisticalTemplate): string;
366
+ declare function diceRandomParse(value: string, template: StatisticalTemplate, engine?: Engine | null): string;
253
367
  /**
254
368
  * Same as damageDice but for DiceType
255
369
  * @param dice {string}
256
370
  * @param template {StatisticalTemplate}
257
371
  */
258
- declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate): string;
372
+ declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate, engine?: Engine | null): string;
259
373
  /**
260
374
  * Random the combinaison and evaluate it to check if everything is valid
261
375
  * @param combinaison {Record<string,string>}
@@ -274,17 +388,17 @@ declare function evalOneCombinaison(combinaison: string, stats: Record<string, n
274
388
  * @param {boolean} verify - If true, will roll the dices to check if everything is valid
275
389
  * @returns {StatisticalTemplate}
276
390
  */
277
- declare function verifyTemplateValue(template: unknown, verify?: boolean): StatisticalTemplate;
391
+ declare function verifyTemplateValue(template: unknown, verify?: boolean, engine?: Engine | null): StatisticalTemplate;
278
392
  /**
279
393
  * Test each damage roll from the template.damage
280
394
  * @param {StatisticalTemplate} template
281
395
  */
282
- declare function testDiceRegistered(template: StatisticalTemplate): void;
396
+ declare function testDiceRegistered(template: StatisticalTemplate, engine?: Engine | null): void;
283
397
  /**
284
398
  * Test all combinaison with generated random value
285
399
  * @param {StatisticalTemplate} template
286
400
  */
287
- declare function testStatCombinaison(template: StatisticalTemplate): void;
401
+ declare function testStatCombinaison(template: StatisticalTemplate, engine?: Engine | null): void;
288
402
  /**
289
403
  * Generate a random stat based on the template and the statistical min and max
290
404
  * @param {number|undefined} total
@@ -292,96 +406,6 @@ declare function testStatCombinaison(template: StatisticalTemplate): void;
292
406
  * @param {number | undefined} min
293
407
  * @returns
294
408
  */
295
- declare function generateRandomStat(total?: number | undefined, max?: number, min?: number): number;
296
-
297
- declare class DiceTypeError extends Error {
298
- readonly dice: string;
299
- readonly cause: string | undefined;
300
- readonly method: unknown;
301
- constructor(dice: string, cause?: string, method?: unknown);
302
- }
303
- declare class FormulaError extends Error {
304
- readonly formula: string;
305
- readonly cause: string | undefined;
306
- readonly method: unknown;
307
- constructor(formula: string, cause?: string, method?: unknown);
308
- }
309
- declare class MaxGreater extends Error {
310
- readonly name: string;
311
- readonly value: number;
312
- readonly max: number;
313
- constructor(value: number, max: number);
314
- }
315
- declare class EmptyObjectError extends Error {
316
- readonly name: string;
317
- constructor();
318
- }
319
- declare class TooManyDice extends Error {
320
- readonly name: string;
321
- constructor();
322
- }
323
- declare class TooManyStats extends Error {
324
- readonly name: string;
325
- constructor();
326
- }
327
- declare class NoStatisticsError extends Error {
328
- readonly name: string;
329
- constructor();
330
- }
331
-
332
- declare const COMMENT_REGEX: RegExp;
333
- declare const SIGN_REGEX: RegExp;
334
- declare const SIGN_REGEX_SPACE: RegExp;
335
- declare const SYMBOL_DICE = "&";
336
- declare const DETECT_CRITICAL: RegExp;
337
-
338
- /**
339
- * Definition of the Zod schema for template data
340
- */
341
-
342
- declare const templateSchema: z.ZodObject<{
343
- charName: z.ZodOptional<z.ZodBoolean>;
344
- statistics: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
345
- max: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
346
- min: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
347
- combinaison: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string | undefined, string>>>;
348
- exclude: z.ZodOptional<z.ZodBoolean>;
349
- }, z.core.$strip>>>;
350
- total: z.ZodOptional<z.ZodPipe<z.ZodNumber, z.ZodTransform<number | undefined, number>>>;
351
- forceDistrib: z.ZodOptional<z.ZodBoolean>;
352
- diceType: z.ZodOptional<z.ZodString>;
353
- critical: z.ZodOptional<z.ZodPipe<z.ZodObject<{
354
- success: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
355
- failure: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
356
- }, z.core.$strip>, z.ZodTransform<{
357
- success?: string | number | undefined;
358
- failure?: string | number | undefined;
359
- }, {
360
- success?: string | number | undefined;
361
- failure?: string | number | undefined;
362
- }>>>;
363
- customCritical: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
364
- sign: z.ZodEnum<{
365
- ">=": ">=";
366
- "<=": "<=";
367
- "<": "<";
368
- ">": ">";
369
- "!=": "!=";
370
- "==": "==";
371
- }>;
372
- value: z.ZodString;
373
- onNaturalDice: z.ZodOptional<z.ZodBoolean>;
374
- affectSkill: z.ZodOptional<z.ZodBoolean>;
375
- }, z.core.$strip>>>;
376
- damage: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
377
- }, z.core.$strip>;
378
-
379
- interface StatisticalSchema extends StatisticalTemplate {
380
- /**
381
- * Specifies the URL for the schema definition
382
- * This property is optional and should contain a valid URI.
383
- */
384
- $schema?: string;
385
- }
409
+ declare function generateRandomStat(total?: number | undefined, max?: number, min?: number, engine?: Engine | null): number;
386
410
 
387
- export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, isNumber, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
411
+ export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, getEngine, getEngineId, isNumber, randomInt, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };