@dicebear/core 5.0.0-alpha.27 → 5.0.0-alpha.28

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/lib/core.js CHANGED
@@ -31,7 +31,9 @@ export function createAvatar(style, options = {}) {
31
31
  if (options.radius || options.clip) {
32
32
  result.body = svgUtils.addViewboxMask(result, (_b = options.radius) !== null && _b !== void 0 ? _b : 0);
33
33
  }
34
- const attributes = svgUtils.createAttrString(result.attributes);
34
+ // Reduces the occurrence of ID collisions when rendering multiple avatars on one HTML page.
35
+ result.body = svgUtils.randomizeIds(result, prng.seed);
36
+ const attributes = svgUtils.createAttrString(result);
35
37
  const description = '<desc>Created with dicebear.com</desc>';
36
38
  const metadata = license.xml(style);
37
39
  const exif = license.exif(style);
package/lib/index.d.ts CHANGED
@@ -7,4 +7,5 @@
7
7
  export * from './core.js';
8
8
  export * from './schema.js';
9
9
  export * as license from './utils/license.js';
10
+ export * as escape from './utils/escape.js';
10
11
  export * from './types.js';
package/lib/index.js CHANGED
@@ -7,4 +7,5 @@
7
7
  export * from './core.js';
8
8
  export * from './schema.js';
9
9
  export * as license from './utils/license.js';
10
+ export * as escape from './utils/escape.js';
10
11
  export * from './types.js';
package/lib/types.d.ts CHANGED
@@ -29,6 +29,7 @@ export interface Prng {
29
29
  bool(likelihood?: number): boolean;
30
30
  integer(min: number, max: number): number;
31
31
  pick<T>(arr: T[]): T;
32
+ string(length: number, characters?: string): string;
32
33
  }
33
34
  export declare type StyleSchema = JSONSchema7;
34
35
  export declare type StyleOptions<O extends {}> = Partial<O & Options>;
@@ -1,2 +1 @@
1
1
  export declare function xml(content: string): string;
2
- export declare function md(content: string): string;
@@ -6,7 +6,3 @@ export function xml(content) {
6
6
  .replace(/</g, '&lt;')
7
7
  .replace(/>/g, '&gt;');
8
8
  }
9
- export function md(content) {
10
- const result = content.replace(/([\\`*_{}[\]()#+-.!])/g, '\\$1');
11
- return xml(result);
12
- }
@@ -14,7 +14,6 @@ export function xml(style) {
14
14
  ? `<cc:license rdf:resource="${_.xml(style.meta.license.url)}" />`
15
15
  : '';
16
16
  return ('<metadata' +
17
- ' id="license"' +
18
17
  ' xmlns:dc="http://purl.org/dc/elements/1.1/"' +
19
18
  ' xmlns:cc="http://creativecommons.org/ns#"' +
20
19
  ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' +
package/lib/utils/prng.js CHANGED
@@ -35,5 +35,12 @@ export function create(seed) {
35
35
  pick(arr) {
36
36
  return arr[integer(0, arr.length - 1)];
37
37
  },
38
+ string(length, characters = 'abcdefghijklmnopqrstuvwxyz1234567890') {
39
+ let str = '';
40
+ for (let i = 0; i < length; i++) {
41
+ str += characters[integer(0, characters.length - 1)];
42
+ }
43
+ return str;
44
+ },
38
45
  };
39
46
  }
@@ -1,10 +1,4 @@
1
- import type { StyleCreateResult, StyleCreateResultAttributes } from '../types.js';
2
- declare type CreateGroupProps = {
3
- children: string;
4
- x: number;
5
- y: number;
6
- };
7
- export declare function createGroup({ children, x, y }: CreateGroupProps): string;
1
+ import type { StyleCreateResult } from '../types.js';
8
2
  export declare function getViewBox(result: StyleCreateResult): {
9
3
  x: number;
10
4
  y: number;
@@ -17,5 +11,5 @@ export declare function addTranslate(result: StyleCreateResult, x?: number, y?:
17
11
  export declare function addRotate(result: StyleCreateResult, rotate: number): string;
18
12
  export declare function addFlip(result: StyleCreateResult): string;
19
13
  export declare function addViewboxMask(result: StyleCreateResult, radius: number): string;
20
- export declare function createAttrString(attributes: StyleCreateResultAttributes): string;
21
- export {};
14
+ export declare function createAttrString(result: StyleCreateResult): string;
15
+ export declare function randomizeIds(result: StyleCreateResult, seed: string): string;
package/lib/utils/svg.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import * as escape from './escape.js';
2
- export function createGroup({ children, x, y }) {
3
- return `<g transform="translate(${x}, ${y})">${children}</g>`;
4
- }
2
+ import { create as createPrng } from './prng.js';
5
3
  export function getViewBox(result) {
6
4
  let viewBox = result.attributes['viewBox'].split(' ');
7
5
  let x = parseInt(viewBox[0]);
@@ -49,9 +47,23 @@ export function addViewboxMask(result, radius) {
49
47
  `</mask>` +
50
48
  `<g mask="url(#viewboxMask)">${result.body}</g>`);
51
49
  }
52
- export function createAttrString(attributes) {
53
- attributes = { xmlns: 'http://www.w3.org/2000/svg', ...attributes };
50
+ export function createAttrString(result) {
51
+ const attributes = {
52
+ xmlns: 'http://www.w3.org/2000/svg',
53
+ ...result.attributes,
54
+ };
54
55
  return Object.keys(attributes)
55
56
  .map((attr) => `${escape.xml(attr)}="${escape.xml(attributes[attr])}"`)
56
57
  .join(' ');
57
58
  }
59
+ export function randomizeIds(result, seed) {
60
+ const prng = createPrng(JSON.stringify({
61
+ attributes: result.attributes,
62
+ seed,
63
+ }));
64
+ const ids = {};
65
+ return result.body.replace(/(id="|url\(#)([a-z0-9-_]+)([")])/gi, (match, m1, m2, m3) => {
66
+ ids[m2] = ids[m2] || prng.string(8);
67
+ return `${m1}${ids[m2]}${m3}`;
68
+ });
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/core",
3
- "version": "5.0.0-alpha.27",
3
+ "version": "5.0.0-alpha.28",
4
4
  "description": "An avatar library for designers and developers.",
5
5
  "keywords": [
6
6
  "avatar",
@@ -31,7 +31,7 @@
31
31
  "test": "uvu tests"
32
32
  },
33
33
  "dependencies": {
34
- "@dicebear/converter": "^5.0.0-alpha.27",
34
+ "@dicebear/converter": "^5.0.0-alpha.28",
35
35
  "@types/json-schema": "^7.0.7"
36
36
  },
37
37
  "devDependencies": {
@@ -46,5 +46,5 @@
46
46
  "publishConfig": {
47
47
  "access": "public"
48
48
  },
49
- "gitHead": "10fb6bbb9cb153fff52e32758ba65b4c6c9c7f3a"
49
+ "gitHead": "de2e082b64c80896ef63459d18063cf149973ab7"
50
50
  }