@dicebear/core 11.0.0-rc.4 → 11.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <h1><img src="https://www.dicebear.com/logo-readme.svg" width="28" /> DiceBear Core (JavaScript)</h1>
2
2
 
3
3
  [![stars](https://www.dicebear.com/badges/stars.svg)](https://github.com/dicebear/dicebear/stargazers)
4
- [![license](https://www.dicebear.com/badges/license.svg)](https://github.com/dicebear/dicebear/blob/10.x/LICENSE)
4
+ [![license](https://www.dicebear.com/badges/license.svg)](https://github.com/dicebear/dicebear/blob/11.x/LICENSE)
5
5
 
6
6
  JavaScript implementation of the DiceBear avatar library. Generates
7
7
  deterministic SVG avatars from style definitions and a seed string.
@@ -16,9 +16,12 @@ style, and options, regardless of the language used.
16
16
  ## Installation
17
17
 
18
18
  ```sh
19
- npm install @dicebear/core
19
+ npm install @dicebear/core @dicebear/styles
20
20
  ```
21
21
 
22
+ `@dicebear/styles` holds the definitions of the avatar styles. Leave it out when
23
+ you bring your own definitions.
24
+
22
25
  Requires Node.js 22+ or any modern browser.
23
26
 
24
27
  ## Usage
package/lib/Avatar.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Style } from './Style.js';
2
- import type { StyleOptions } from './StyleOptions.js';
2
+ import type { NoInferStyle, StyleOptions } from './StyleOptions.js';
3
3
  interface AvatarJson<D = unknown> {
4
4
  readonly svg: string;
5
5
  readonly options: StyleOptions<D>;
@@ -16,7 +16,7 @@ export declare class Avatar<D = unknown> {
16
16
  * Pass a {@link Style} instance and reuse it across avatars. The parsed
17
17
  * style carries the validation, so nothing is re-checked per render.
18
18
  */
19
- constructor(style: Style<D>, optionsInput?: StyleOptions<D>);
19
+ constructor(style: Style<D>, optionsInput?: StyleOptions<NoInferStyle<D>>);
20
20
  /**
21
21
  * Returns the rendered SVG markup.
22
22
  */
package/lib/Options.js CHANGED
@@ -26,7 +26,8 @@ export class Options {
26
26
  _Options_instances.add(this);
27
27
  _Options_data.set(this, void 0);
28
28
  _Options_tags.set(this, void 0);
29
- __classPrivateFieldSet(this, _Options_data, structuredClone(data), "f");
29
+ // `null` renders the default avatar, as in the other ports.
30
+ __classPrivateFieldSet(this, _Options_data, structuredClone(data ?? {}), "f");
30
31
  }
31
32
  /**
32
33
  * The seed, with every lone surrogate replaced by U+FFFD: ill-formed text
package/lib/Style.d.ts CHANGED
@@ -4,6 +4,8 @@ import { Canvas } from './Style/Canvas.js';
4
4
  import { Component } from './Style/Component.js';
5
5
  import { Color } from './Style/Color.js';
6
6
  export type { StyleDefinition } from './StyleDefinition.js';
7
+ /** Key of the type-only member that carries the definition type. */
8
+ declare const definitionType: unique symbol;
7
9
  /**
8
10
  * Validated, lazily-decomposed wrapper around a style definition. Construction
9
11
  * runs the JSON Schema validator and stores a deep clone of the input so that
@@ -11,6 +13,12 @@ export type { StyleDefinition } from './StyleDefinition.js';
11
13
  */
12
14
  export declare class Style<D = unknown> {
13
15
  #private;
16
+ /**
17
+ * Carries `D` in the shape of the class, so the definition type survives
18
+ * when a style is passed around, and `new Avatar(style, options)` checks the
19
+ * options against it. It exists for the type checker only and is never set.
20
+ */
21
+ readonly [definitionType]?: D;
14
22
  constructor(data: D);
15
23
  /**
16
24
  * Returns the definition's `$id`, or `undefined` when not set.
@@ -13,6 +13,15 @@ export interface TagFilterToken {
13
13
  readonly value?: string;
14
14
  readonly negated: boolean;
15
15
  }
16
+ /**
17
+ * Keeps TypeScript from inferring `T` at this position, so the options of an
18
+ * avatar are checked against the style instead of widening the style's type
19
+ * to fit them. The same as the built-in `NoInfer` of TypeScript 5.4, written
20
+ * out so that the declarations also work with older compilers.
21
+ *
22
+ * @internal
23
+ */
24
+ export type NoInferStyle<T> = [T][T extends unknown ? 0 : never];
16
25
  type ComponentNames<D> = D extends {
17
26
  components: Record<infer K extends string, unknown>;
18
27
  } ? string extends K ? never : K : never;
@@ -71,9 +80,9 @@ type ColorOptions<C extends string> = [C] extends [never] ? unknown : {
71
80
  readonly [K in C as `${K}ColorOrder`]?: StyleOptionsColorOrderValue;
72
81
  };
73
82
  type AnimationOptions = {
74
- readonly [key: `${string}Animation`]: boolean | undefined;
75
- readonly [key: `${string}AnimationSpeed`]: number | readonly [number, number] | undefined;
76
- readonly [key: `${string}AnimationDelay`]: number | readonly [number, number] | undefined;
83
+ readonly [key: `${string}Animation`]: unknown;
84
+ readonly [key: `${string}AnimationSpeed`]: unknown;
85
+ readonly [key: `${string}AnimationDelay`]: unknown;
77
86
  };
78
87
  export type StyleOptions<D = unknown> = StyleOptionsBase & ComponentOptions<D, ComponentNames<D>> & ColorOptions<AllColorNames<D>> & (HasSpecificKeys<D> extends true ? AnimationOptions : {
79
88
  readonly [key: string]: unknown;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Finds a number in the options that is not finite.
3
+ *
4
+ * JSON cannot carry Infinity or NaN, so every other port turns such a value
5
+ * down before or during validation. The compiled JS validator lets one
6
+ * through where the schema sets no maximum, a variant weight for example, and
7
+ * the avatar then rendered here but nowhere else.
8
+ */
9
+ export declare class Finite {
10
+ static readonly MESSAGE = "must be a finite number";
11
+ /**
12
+ * Returns the JSON pointer of the first non-finite number in `value`, or
13
+ * `undefined` if there is none. Runs after the depth check, so the value
14
+ * nests at most a few levels.
15
+ */
16
+ static find(value: unknown, path?: string): string | undefined;
17
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Finds a number in the options that is not finite.
3
+ *
4
+ * JSON cannot carry Infinity or NaN, so every other port turns such a value
5
+ * down before or during validation. The compiled JS validator lets one
6
+ * through where the schema sets no maximum, a variant weight for example, and
7
+ * the avatar then rendered here but nowhere else.
8
+ */
9
+ export class Finite {
10
+ /**
11
+ * Returns the JSON pointer of the first non-finite number in `value`, or
12
+ * `undefined` if there is none. Runs after the depth check, so the value
13
+ * nests at most a few levels.
14
+ */
15
+ static find(value, path = '') {
16
+ if (typeof value === 'number') {
17
+ return Number.isFinite(value) ? undefined : path;
18
+ }
19
+ if (value === null || typeof value !== 'object') {
20
+ return undefined;
21
+ }
22
+ for (const [key, inner] of Object.entries(value)) {
23
+ const pointer = `${path}/${key.replace(/~/g, '~0').replace(/\//g, '~1')}`;
24
+ const found = Finite.find(inner, pointer);
25
+ if (found !== undefined) {
26
+ return found;
27
+ }
28
+ }
29
+ return undefined;
30
+ }
31
+ }
32
+ Finite.MESSAGE = 'must be a finite number';
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Style as StyleBase, type StyleDefinition } from './Style.js';
2
2
  import { Avatar as AvatarBase } from './Avatar.js';
3
- import type { StyleOptions } from './StyleOptions.js';
3
+ import type { NoInferStyle, StyleOptions } from './StyleOptions.js';
4
4
  /**
5
5
  * A style whose definition is checked against the definition schema before
6
6
  * it is parsed. `@dicebear/core/lite` exports the same class without the
@@ -15,8 +15,13 @@ export declare class Style<D = unknown> extends StyleBase<D> {
15
15
  * for options that come from your own code.
16
16
  */
17
17
  export declare class Avatar<D = unknown> extends AvatarBase<D> {
18
- constructor(style: StyleBase<D>, optionsInput?: StyleOptions<D>);
18
+ constructor(style: StyleBase<D>, optionsInput?: StyleOptions<NoInferStyle<D>>);
19
19
  }
20
20
  export type { StyleDefinition };
21
21
  export { OptionsDescriptor } from './OptionsDescriptor.js';
22
22
  export type { StyleOptions } from './StyleOptions.js';
23
+ export { ValidationError } from './Error/ValidationError.js';
24
+ export type { ValidationErrorDetail } from './Error/ValidationError.js';
25
+ export { StyleValidationError } from './Error/StyleValidationError.js';
26
+ export { OptionsValidationError } from './Error/OptionsValidationError.js';
27
+ export { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ import { OptionsValidator } from './Validator/OptionsValidator.js';
5
5
  import { StyleValidationError } from './Error/StyleValidationError.js';
6
6
  import { OptionsValidationError } from './Error/OptionsValidationError.js';
7
7
  import { Depth } from './Utils/Depth.js';
8
+ import { Finite } from './Utils/Finite.js';
8
9
  /**
9
10
  * A style whose definition is checked against the definition schema before
10
11
  * it is parsed. `@dicebear/core/lite` exports the same class without the
@@ -30,7 +31,17 @@ export class Avatar extends AvatarBase {
30
31
  throw new OptionsValidationError([{ message: Depth.MESSAGE }]);
31
32
  }
32
33
  OptionsValidator.validate(optionsInput ?? {});
34
+ const nonFinite = Finite.find(optionsInput);
35
+ if (nonFinite !== undefined) {
36
+ throw new OptionsValidationError([
37
+ { instancePath: nonFinite, keyword: 'type', message: Finite.MESSAGE },
38
+ ]);
39
+ }
33
40
  super(style, optionsInput);
34
41
  }
35
42
  }
36
43
  export { OptionsDescriptor } from './OptionsDescriptor.js';
44
+ export { ValidationError } from './Error/ValidationError.js';
45
+ export { StyleValidationError } from './Error/StyleValidationError.js';
46
+ export { OptionsValidationError } from './Error/OptionsValidationError.js';
47
+ export { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
package/lib/lite.d.ts CHANGED
@@ -2,3 +2,8 @@ export { Style, type StyleDefinition } from './Style.js';
2
2
  export { Avatar } from './Avatar.js';
3
3
  export { OptionsDescriptor } from './OptionsDescriptor.js';
4
4
  export type { StyleOptions } from './StyleOptions.js';
5
+ export { ValidationError } from './Error/ValidationError.js';
6
+ export type { ValidationErrorDetail } from './Error/ValidationError.js';
7
+ export { StyleValidationError } from './Error/StyleValidationError.js';
8
+ export { OptionsValidationError } from './Error/OptionsValidationError.js';
9
+ export { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
package/lib/lite.js CHANGED
@@ -6,3 +6,9 @@
6
6
  export { Style } from './Style.js';
7
7
  export { Avatar } from './Avatar.js';
8
8
  export { OptionsDescriptor } from './OptionsDescriptor.js';
9
+ // The lite entry validates nothing, but the circular color check still runs,
10
+ // and code shared with the root entry may catch the validation errors.
11
+ export { ValidationError } from './Error/ValidationError.js';
12
+ export { StyleValidationError } from './Error/StyleValidationError.js';
13
+ export { OptionsValidationError } from './Error/OptionsValidationError.js';
14
+ export { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/core",
3
- "version": "11.0.0-rc.4",
3
+ "version": "11.0.0",
4
4
  "description": "Unique avatars from dozens of styles — deterministic, customizable, vector-based.",
5
5
  "keywords": [
6
6
  "avatar",
@@ -27,6 +27,13 @@
27
27
  "./lite": "./lib/lite.js"
28
28
  },
29
29
  "types": "./lib/index.d.ts",
30
+ "typesVersions": {
31
+ "*": {
32
+ "lite": [
33
+ "./lib/lite.d.ts"
34
+ ]
35
+ }
36
+ },
30
37
  "sideEffects": false,
31
38
  "files": [
32
39
  "LICENSE",
@@ -42,7 +49,7 @@
42
49
  "test": "node --test"
43
50
  },
44
51
  "devDependencies": {
45
- "@dicebear/schema": "^3.0.0-rc.1",
52
+ "@dicebear/schema": "^3.0.0",
46
53
  "@exodus/schemasafe": "^1.3.0",
47
54
  "@tsconfig/recommended": "^1.0.13",
48
55
  "del-cli": "^7.0.0",