@dicebear/core 9.4.2 → 10.0.0-rc.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.
Files changed (82) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +12 -0
  3. package/lib/Avatar.d.ts +15 -0
  4. package/lib/Avatar.js +38 -0
  5. package/lib/Error/CircularColorReferenceError.d.ts +4 -0
  6. package/lib/Error/CircularColorReferenceError.js +8 -0
  7. package/lib/Error/OptionsValidationError.d.ts +5 -0
  8. package/lib/Error/OptionsValidationError.js +7 -0
  9. package/lib/Error/StyleValidationError.d.ts +5 -0
  10. package/lib/Error/StyleValidationError.js +7 -0
  11. package/lib/Error/ValidationError.d.ts +8 -0
  12. package/lib/Error/ValidationError.js +18 -0
  13. package/lib/Options.d.ts +23 -0
  14. package/lib/Options.js +217 -0
  15. package/lib/OptionsDescriptor.d.ts +37 -0
  16. package/lib/OptionsDescriptor.js +70 -0
  17. package/lib/Prng/Fnv1a.d.ts +4 -0
  18. package/lib/Prng/Fnv1a.js +16 -0
  19. package/lib/Prng/Mulberry32.d.ts +7 -0
  20. package/lib/Prng/Mulberry32.js +40 -0
  21. package/lib/Prng.d.ts +11 -0
  22. package/lib/Prng.js +101 -0
  23. package/lib/Renderer.d.ts +7 -0
  24. package/lib/Renderer.js +293 -0
  25. package/lib/Style/Canvas.d.ts +9 -0
  26. package/lib/Style/Canvas.js +31 -0
  27. package/lib/Style/Color.d.ts +8 -0
  28. package/lib/Style/Color.js +28 -0
  29. package/lib/Style/Component.d.ts +13 -0
  30. package/lib/Style/Component.js +46 -0
  31. package/lib/Style/ComponentTranslate.d.ts +7 -0
  32. package/lib/Style/ComponentTranslate.js +25 -0
  33. package/lib/Style/ComponentVariant.d.ts +8 -0
  34. package/lib/Style/ComponentVariant.js +28 -0
  35. package/lib/Style/Element.d.ts +10 -0
  36. package/lib/Style/Element.js +36 -0
  37. package/lib/Style/Meta.d.ts +11 -0
  38. package/lib/Style/Meta.js +37 -0
  39. package/lib/Style/MetaCreator.d.ts +7 -0
  40. package/lib/Style/MetaCreator.js +25 -0
  41. package/lib/Style/MetaLicense.d.ts +8 -0
  42. package/lib/Style/MetaLicense.js +28 -0
  43. package/lib/Style/MetaSource.d.ts +7 -0
  44. package/lib/Style/MetaSource.js +25 -0
  45. package/lib/Style.d.ts +18 -0
  46. package/lib/Style.js +63 -0
  47. package/lib/StyleDefinition.d.ts +83 -0
  48. package/lib/StyleOptions.d.ts +54 -0
  49. package/lib/StyleOptions.js +1 -0
  50. package/lib/Utils/Color.d.ts +9 -0
  51. package/lib/Utils/Color.js +67 -0
  52. package/lib/Utils/Initials.d.ts +3 -0
  53. package/lib/Utils/Initials.js +26 -0
  54. package/lib/Utils/License.d.ts +5 -0
  55. package/lib/Utils/License.js +64 -0
  56. package/lib/Utils/Xml.d.ts +4 -0
  57. package/lib/Utils/Xml.js +20 -0
  58. package/lib/Validator/OptionsValidator.d.ts +3 -0
  59. package/lib/Validator/OptionsValidator.js +2203 -0
  60. package/lib/Validator/StyleValidator.d.ts +3 -0
  61. package/lib/Validator/StyleValidator.js +4986 -0
  62. package/lib/index.d.ts +4 -12
  63. package/lib/index.js +3 -12
  64. package/package.json +13 -8
  65. package/lib/core.d.ts +0 -2
  66. package/lib/core.js +0 -67
  67. package/lib/schema.d.ts +0 -2
  68. package/lib/schema.js +0 -79
  69. package/lib/types.d.ts +0 -73
  70. package/lib/utils/color.d.ts +0 -6
  71. package/lib/utils/color.js +0 -35
  72. package/lib/utils/escape.d.ts +0 -1
  73. package/lib/utils/escape.js +0 -8
  74. package/lib/utils/license.d.ts +0 -3
  75. package/lib/utils/license.js +0 -54
  76. package/lib/utils/options.d.ts +0 -3
  77. package/lib/utils/options.js +0 -32
  78. package/lib/utils/prng.d.ts +0 -2
  79. package/lib/utils/prng.js +0 -68
  80. package/lib/utils/svg.d.ts +0 -15
  81. package/lib/utils/svg.js +0 -79
  82. /package/lib/{types.js → StyleDefinition.js} +0 -0
package/lib/index.d.ts CHANGED
@@ -1,12 +1,4 @@
1
- /*!
2
- * DiceBear (@dicebear/core)
3
- *
4
- * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
- * Copyright (c) 2024 Florian Körner
6
- */
7
- import * as license from './utils/license.js';
8
- import * as escape from './utils/escape.js';
9
- export * from './core.js';
10
- export * from './schema.js';
11
- export * from './types.js';
12
- export { license, escape };
1
+ export { Style, type StyleDefinition } from './Style.js';
2
+ export { Avatar } from './Avatar.js';
3
+ export { OptionsDescriptor } from './OptionsDescriptor.js';
4
+ export type { StyleOptions } from './StyleOptions.js';
package/lib/index.js CHANGED
@@ -1,12 +1,3 @@
1
- /*!
2
- * DiceBear (@dicebear/core)
3
- *
4
- * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
- * Copyright (c) 2024 Florian Körner
6
- */
7
- import * as license from './utils/license.js';
8
- import * as escape from './utils/escape.js';
9
- export * from './core.js';
10
- export * from './schema.js';
11
- export * from './types.js';
12
- export { license, escape };
1
+ export { Style } from './Style.js';
2
+ export { Avatar } from './Avatar.js';
3
+ export { OptionsDescriptor } from './OptionsDescriptor.js';
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@dicebear/core",
3
- "version": "9.4.2",
4
- "description": "An avatar library for designers and developers.",
3
+ "version": "10.0.0-rc.1",
4
+ "description": "Unique avatars from dozens of styles — deterministic, customizable, vector-based.",
5
5
  "keywords": [
6
6
  "avatar",
7
- "identicon"
7
+ "identicon",
8
+ "avataaars",
9
+ "bottts",
10
+ "dicebear"
8
11
  ],
9
12
  "homepage": "https://www.dicebear.com",
10
13
  "bugs": {
@@ -20,27 +23,29 @@
20
23
  "main": "./lib/index.js",
21
24
  "exports": "./lib/index.js",
22
25
  "types": "./lib/index.d.ts",
26
+ "sideEffects": false,
23
27
  "files": [
24
28
  "LICENSE",
25
29
  "lib",
26
30
  "README.md"
27
31
  ],
28
32
  "scripts": {
29
- "prebuild": "del-cli lib",
33
+ "prebuild": "del-cli lib && node scripts/compile-schema.mjs",
30
34
  "build": "tsc",
31
35
  "prepublishOnly": "npm run build",
36
+ "prepack": "node scripts/extend-license.mjs",
37
+ "postpack": "git checkout LICENSE",
32
38
  "test": "node --test"
33
39
  },
34
- "dependencies": {
35
- "@types/json-schema": "^7.0.15"
36
- },
37
40
  "devDependencies": {
41
+ "@dicebear/schema": "^0.9.0",
38
42
  "@tsconfig/recommended": "^1.0.13",
43
+ "ajv": "^8.17.1",
39
44
  "del-cli": "^7.0.0",
40
45
  "typescript": "^5.9.3"
41
46
  },
42
47
  "engines": {
43
- "node": ">=18.0.0"
48
+ "node": ">=22.0.0"
44
49
  },
45
50
  "publishConfig": {
46
51
  "access": "public"
package/lib/core.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { Result, Style, StyleOptions } from './types.js';
2
- export declare function createAvatar<O extends {}>(style: Style<O>, options?: StyleOptions<O>): Result;
package/lib/core.js DELETED
@@ -1,67 +0,0 @@
1
- import * as svgUtils from './utils/svg.js';
2
- import { merge as mergeOptions } from './utils/options.js';
3
- import { create as createPrng } from './utils/prng.js';
4
- import * as license from './utils/license.js';
5
- import { getBackgroundColors } from './utils/color.js';
6
- export function createAvatar(style, options = {}) {
7
- var _a, _b, _c, _d, _e;
8
- options = mergeOptions(style, options);
9
- const prng = createPrng(options.seed);
10
- const result = style.create({ prng: prng, options });
11
- const backgroundType = prng.pick((_a = options.backgroundType) !== null && _a !== void 0 ? _a : [], 'solid');
12
- const { primary: primaryBackgroundColor, secondary: secondaryBackgroundColor, } = getBackgroundColors(prng, (_b = options.backgroundColor) !== null && _b !== void 0 ? _b : [], backgroundType);
13
- const backgroundRotation = prng.integer(((_c = options.backgroundRotation) === null || _c === void 0 ? void 0 : _c.length)
14
- ? Math.min(...options.backgroundRotation)
15
- : 0, ((_d = options.backgroundRotation) === null || _d === void 0 ? void 0 : _d.length)
16
- ? Math.max(...options.backgroundRotation)
17
- : 0);
18
- if (options.size) {
19
- result.attributes.width = options.size.toString();
20
- result.attributes.height = options.size.toString();
21
- }
22
- if (options.scale !== undefined && options.scale !== 100) {
23
- result.body = svgUtils.addScale(result, options.scale);
24
- }
25
- if (options.flip) {
26
- result.body = svgUtils.addFlip(result);
27
- }
28
- if (options.rotate) {
29
- result.body = svgUtils.addRotate(result, options.rotate);
30
- }
31
- if (options.translateX || options.translateY) {
32
- result.body = svgUtils.addTranslate(result, options.translateX, options.translateY);
33
- }
34
- if (primaryBackgroundColor !== 'transparent' &&
35
- secondaryBackgroundColor !== 'transparent') {
36
- result.body = svgUtils.addBackground(result, primaryBackgroundColor, secondaryBackgroundColor, backgroundType, backgroundRotation);
37
- }
38
- if (options.radius || options.clip) {
39
- result.body = svgUtils.addViewboxMask(result, (_e = options.radius) !== null && _e !== void 0 ? _e : 0);
40
- }
41
- if (options.randomizeIds) {
42
- // Reduces the occurrence of ID collisions when rendering multiple avatars on one HTML page.
43
- result.body = svgUtils.randomizeIds(result);
44
- }
45
- const attributes = svgUtils.createAttrString(result);
46
- const metadata = license.xml(style);
47
- const svg = `<svg ${attributes}>${metadata}${result.body}</svg>`;
48
- return {
49
- toString: () => svg,
50
- toJson: () => {
51
- var _a;
52
- return ({
53
- svg: svg,
54
- extra: {
55
- primaryBackgroundColor,
56
- secondaryBackgroundColor,
57
- backgroundType,
58
- backgroundRotation,
59
- ...(_a = result.extra) === null || _a === void 0 ? void 0 : _a.call(result),
60
- },
61
- });
62
- },
63
- toDataUri: () => {
64
- return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;
65
- },
66
- };
67
- }
package/lib/schema.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { JSONSchema7 } from 'json-schema';
2
- export declare const schema: JSONSchema7;
package/lib/schema.js DELETED
@@ -1,79 +0,0 @@
1
- export const schema = {
2
- type: 'object',
3
- $schema: 'http://json-schema.org/draft-07/schema#',
4
- properties: {
5
- seed: {
6
- type: 'string',
7
- },
8
- flip: {
9
- type: 'boolean',
10
- default: false,
11
- },
12
- rotate: {
13
- type: 'integer',
14
- minimum: 0,
15
- maximum: 360,
16
- default: 0,
17
- },
18
- scale: {
19
- type: 'integer',
20
- minimum: 0,
21
- maximum: 200,
22
- default: 100,
23
- },
24
- radius: {
25
- type: 'integer',
26
- minimum: 0,
27
- maximum: 50,
28
- default: 0,
29
- },
30
- size: {
31
- type: 'integer',
32
- minimum: 1,
33
- },
34
- backgroundColor: {
35
- type: 'array',
36
- items: {
37
- type: 'string',
38
- pattern: '^(transparent|[a-fA-F0-9]{6})$',
39
- },
40
- },
41
- backgroundType: {
42
- type: 'array',
43
- items: {
44
- type: 'string',
45
- enum: ['solid', 'gradientLinear'],
46
- },
47
- default: ['solid'],
48
- },
49
- backgroundRotation: {
50
- type: 'array',
51
- items: {
52
- type: 'integer',
53
- minimum: -360,
54
- maximum: 360,
55
- },
56
- default: [0, 360],
57
- },
58
- translateX: {
59
- type: 'integer',
60
- minimum: -100,
61
- maximum: 100,
62
- default: 0,
63
- },
64
- translateY: {
65
- type: 'integer',
66
- minimum: -100,
67
- maximum: 100,
68
- default: 0,
69
- },
70
- clip: {
71
- type: 'boolean',
72
- default: true,
73
- },
74
- randomizeIds: {
75
- type: 'boolean',
76
- default: false,
77
- },
78
- },
79
- };
package/lib/types.d.ts DELETED
@@ -1,73 +0,0 @@
1
- import type { JSONSchema7 } from 'json-schema';
2
- export interface ResultConvertOptions {
3
- includeExif?: boolean;
4
- }
5
- export interface Result {
6
- toString(): string;
7
- toJson(): {
8
- svg: string;
9
- extra: Record<string, unknown>;
10
- };
11
- toDataUri(): string;
12
- }
13
- export type BackgroundType = 'solid' | 'gradientLinear';
14
- export interface Options {
15
- seed?: string;
16
- flip?: boolean;
17
- rotate?: number;
18
- scale?: number;
19
- radius?: number;
20
- size?: number;
21
- backgroundColor?: string[];
22
- backgroundType?: BackgroundType[];
23
- backgroundRotation?: number[];
24
- translateX?: number;
25
- translateY?: number;
26
- clip?: boolean;
27
- randomizeIds?: boolean;
28
- }
29
- export interface Exif {
30
- [key: string]: string;
31
- }
32
- export type SchemaDefaults = Record<string, unknown>;
33
- export interface Prng {
34
- seed: string;
35
- next(): void;
36
- bool(likelihood?: number): boolean;
37
- integer(min: number, max: number): number;
38
- pick<T>(arr: T[], fallback: T): T;
39
- pick<T>(arr: T[]): T | undefined;
40
- shuffle<T>(arr: T[]): T[];
41
- string(length: number, characters?: string): string;
42
- }
43
- export type StyleSchema = JSONSchema7;
44
- export type StyleOptions<O extends {}> = Partial<O & Options>;
45
- export interface StyleCreateProps<O extends {}> {
46
- prng: Prng;
47
- options: StyleOptions<O>;
48
- }
49
- export type StyleCreate<O extends {}> = (props: StyleCreateProps<O>) => StyleCreateResult;
50
- export interface StyleCreateResultAttributes {
51
- viewBox: string;
52
- [key: string]: string;
53
- }
54
- export interface StyleCreateResult {
55
- attributes: StyleCreateResultAttributes;
56
- body: string;
57
- extra?: () => Record<string, unknown>;
58
- }
59
- export interface StyleMeta {
60
- title?: string;
61
- source?: string;
62
- creator?: string;
63
- homepage?: string;
64
- license?: {
65
- name: string;
66
- url: string;
67
- };
68
- }
69
- export interface Style<O extends {}> {
70
- meta?: StyleMeta;
71
- schema?: StyleSchema;
72
- create: StyleCreate<O>;
73
- }
@@ -1,6 +0,0 @@
1
- import { Prng } from '../types.js';
2
- export declare function convertColor(color: string): string;
3
- export declare function getBackgroundColors(prng: Prng, backgroundColor: string[], backgroundType: 'solid' | 'gradientLinear'): {
4
- primary: string;
5
- secondary: string;
6
- };
@@ -1,35 +0,0 @@
1
- export function convertColor(color) {
2
- return 'transparent' === color ? color : `#${color}`;
3
- }
4
- export function getBackgroundColors(prng, backgroundColor, backgroundType) {
5
- var _a;
6
- let shuffledBackgroundColors = prng.shuffle(backgroundColor);
7
- if (shuffledBackgroundColors.length <= 1) {
8
- // If no background colour or only one background colour has been selected,
9
- // the random sorting logic can be omitted.
10
- shuffledBackgroundColors = backgroundColor;
11
- // A function call should in any case make an identical number of calls to the PRNG.
12
- prng.next();
13
- }
14
- else if (backgroundColor.length == 2 &&
15
- backgroundType == 'gradientLinear') {
16
- // If the background is to be a colour gradient and exactly two background
17
- // colours have been specified, do not sort them randomly. In this case, we
18
- // assume that the order of the background colours was chosen on purpose.
19
- shuffledBackgroundColors = backgroundColor;
20
- // A function call should in any case make an identical number of calls to the PRNG.
21
- prng.next();
22
- }
23
- else {
24
- shuffledBackgroundColors = prng.shuffle(backgroundColor);
25
- }
26
- if (shuffledBackgroundColors.length === 0) {
27
- shuffledBackgroundColors = ['transparent'];
28
- }
29
- const primary = shuffledBackgroundColors[0];
30
- const secondary = (_a = shuffledBackgroundColors[1]) !== null && _a !== void 0 ? _a : shuffledBackgroundColors[0];
31
- return {
32
- primary: convertColor(primary),
33
- secondary: convertColor(secondary),
34
- };
35
- }
@@ -1 +0,0 @@
1
- export declare function xml(content: string): string;
@@ -1,8 +0,0 @@
1
- export function xml(content) {
2
- return content
3
- .replace(/&/g, '&amp;')
4
- .replace(/'/g, '&apos;')
5
- .replace(/"/g, '&quot;')
6
- .replace(/</g, '&lt;')
7
- .replace(/>/g, '&gt;');
8
- }
@@ -1,3 +0,0 @@
1
- import { Style } from '../types.js';
2
- export declare function xml(style: Style<any>): string;
3
- export declare function text(style: Style<any>): string;
@@ -1,54 +0,0 @@
1
- import * as _ from './escape.js';
2
- export function xml(style) {
3
- var _a, _b, _c, _d, _e, _f, _g;
4
- const title = (_a = style.meta) === null || _a === void 0 ? void 0 : _a.title;
5
- const creator = (_b = style.meta) === null || _b === void 0 ? void 0 : _b.creator;
6
- const source = (_c = style.meta) === null || _c === void 0 ? void 0 : _c.source;
7
- const license = (_e = (_d = style.meta) === null || _d === void 0 ? void 0 : _d.license) === null || _e === void 0 ? void 0 : _e.url;
8
- const rights = text(style);
9
- if (!title && !creator && !source && !license && !rights) {
10
- return '';
11
- }
12
- // https://nsteffel.github.io/dublin_core_generator/generator.html
13
- return ('<metadata' +
14
- ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"' +
15
- ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
16
- ' xmlns:dc="http://purl.org/dc/elements/1.1/"' +
17
- ' xmlns:dcterms="http://purl.org/dc/terms/">' +
18
- '<rdf:RDF>' +
19
- '<rdf:Description>' +
20
- (title ? `<dc:title>${_.xml(title)}</dc:title>` : '') +
21
- (creator ? `<dc:creator>${_.xml(creator)}</dc:creator>` : '') +
22
- (source
23
- ? `<dc:source xsi:type="dcterms:URI">${_.xml((_g = (_f = style.meta) === null || _f === void 0 ? void 0 : _f.source) !== null && _g !== void 0 ? _g : '')}</dc:source>`
24
- : '') +
25
- (license
26
- ? `<dcterms:license xsi:type="dcterms:URI">${_.xml(license)}</dcterms:license>`
27
- : '') +
28
- (rights ? `<dc:rights>${_.xml(rights)}</dc:rights>` : '') +
29
- '</rdf:Description>' +
30
- '</rdf:RDF>' +
31
- '</metadata>');
32
- }
33
- export function text(style) {
34
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
35
- let title = ((_a = style.meta) === null || _a === void 0 ? void 0 : _a.title) ? `„${(_b = style.meta) === null || _b === void 0 ? void 0 : _b.title}”` : 'Design';
36
- let creator = `„${(_d = (_c = style.meta) === null || _c === void 0 ? void 0 : _c.creator) !== null && _d !== void 0 ? _d : 'Unknown'}”`;
37
- if ((_e = style.meta) === null || _e === void 0 ? void 0 : _e.source) {
38
- title += ` (${style.meta.source})`;
39
- }
40
- let result = '';
41
- if (((_g = (_f = style.meta) === null || _f === void 0 ? void 0 : _f.license) === null || _g === void 0 ? void 0 : _g.name) !== 'MIT' &&
42
- ((_h = style.meta) === null || _h === void 0 ? void 0 : _h.creator) !== 'DiceBear' &&
43
- ((_j = style.meta) === null || _j === void 0 ? void 0 : _j.title)) {
44
- result += 'Remix of ';
45
- }
46
- result += `${title} by ${creator}`;
47
- if ((_l = (_k = style.meta) === null || _k === void 0 ? void 0 : _k.license) === null || _l === void 0 ? void 0 : _l.name) {
48
- result += `, licensed under „${(_o = (_m = style.meta) === null || _m === void 0 ? void 0 : _m.license) === null || _o === void 0 ? void 0 : _o.name}”`;
49
- if ((_q = (_p = style.meta) === null || _p === void 0 ? void 0 : _p.license) === null || _q === void 0 ? void 0 : _q.url) {
50
- result += ` (${style.meta.license.url})`;
51
- }
52
- }
53
- return result;
54
- }
@@ -1,3 +0,0 @@
1
- import type { SchemaDefaults, Style, StyleOptions, StyleSchema } from '../types.js';
2
- export declare function defaults(schema: StyleSchema): SchemaDefaults;
3
- export declare function merge<O extends {}>(style: Style<O>, options: StyleOptions<O>): StyleOptions<O>;
@@ -1,32 +0,0 @@
1
- import { schema } from '../schema.js';
2
- export function defaults(schema) {
3
- var _a;
4
- let result = {};
5
- let props = (_a = schema.properties) !== null && _a !== void 0 ? _a : {};
6
- Object.keys(props).forEach((key) => {
7
- let val = props[key];
8
- if (typeof val === 'object' && undefined !== val.default) {
9
- if (Array.isArray(val.default)) {
10
- result[key] = [...val.default];
11
- }
12
- else if (typeof val.default === 'object') {
13
- result[key] = { ...val.default };
14
- }
15
- else {
16
- result[key] = val.default;
17
- }
18
- }
19
- });
20
- return result;
21
- }
22
- export function merge(style, options) {
23
- var _a;
24
- let result = {
25
- ...defaults(schema),
26
- ...defaults((_a = style.schema) !== null && _a !== void 0 ? _a : {}),
27
- ...options,
28
- };
29
- // Return a complete copy because the styles could partially customize the
30
- // options and thus modify nested objects and arrays.
31
- return JSON.parse(JSON.stringify(result));
32
- }
@@ -1,2 +0,0 @@
1
- import type { Prng } from '../types.js';
2
- export declare function create(seed?: string): Prng;
package/lib/utils/prng.js DELETED
@@ -1,68 +0,0 @@
1
- const MIN = -2147483648;
2
- const MAX = 2147483647;
3
- const MAX_SEED_LENGTH = 1024;
4
- function xorshift(value) {
5
- value ^= value << 13;
6
- value ^= value >> 17;
7
- value ^= value << 5;
8
- return value;
9
- }
10
- function hashSeed(seed) {
11
- let hash = 0;
12
- for (let i = 0; i < seed.length; i++) {
13
- hash = ((hash << 5) - hash + seed.charCodeAt(i)) | 0;
14
- hash = xorshift(hash);
15
- }
16
- return hash;
17
- }
18
- export function create(seed = '') {
19
- // Ensure that seed is a string and limit length to prevent CPU exhaustion
20
- seed = seed.toString().slice(0, MAX_SEED_LENGTH);
21
- let value = hashSeed(seed) || 1;
22
- const next = () => (value = xorshift(value));
23
- const integer = (min, max) => {
24
- return Math.floor(((next() - MIN) / (MAX - MIN)) * (max + 1 - min) + min);
25
- };
26
- return {
27
- seed,
28
- next,
29
- bool(likelihood = 50) {
30
- return integer(1, 100) <= likelihood;
31
- },
32
- integer(min, max) {
33
- return integer(min, max);
34
- },
35
- pick(arr, fallback) {
36
- var _a;
37
- if (arr.length === 0) {
38
- next();
39
- return fallback;
40
- }
41
- return (_a = arr[integer(0, arr.length - 1)]) !== null && _a !== void 0 ? _a : fallback;
42
- },
43
- shuffle(arr) {
44
- // Each method call should call the `next` function only once.
45
- // Therefore, we use a separate instance of the PRNG here.
46
- const internalPrng = create(next().toString());
47
- // Fisher-Yates shuffle algorithm - We do not use the Array.sort method
48
- // because it is not stable and produces different results when used in
49
- // different browsers. See: https://github.com/dicebear/dicebear/issues/394
50
- const workingArray = [...arr];
51
- for (let i = workingArray.length - 1; i > 0; i--) {
52
- const j = internalPrng.integer(0, i);
53
- [workingArray[i], workingArray[j]] = [workingArray[j], workingArray[i]];
54
- }
55
- return workingArray;
56
- },
57
- string(length, characters = 'abcdefghijklmnopqrstuvwxyz1234567890') {
58
- // Each method call should call the `next` function only once.
59
- // Therefore, we use a separate instance of the PRNG here.
60
- const internalPrng = create(next().toString());
61
- let str = '';
62
- for (let i = 0; i < length; i++) {
63
- str += characters[internalPrng.integer(0, characters.length - 1)];
64
- }
65
- return str;
66
- },
67
- };
68
- }
@@ -1,15 +0,0 @@
1
- import type { BackgroundType, StyleCreateResult } from '../types.js';
2
- export declare function getViewBox(result: StyleCreateResult): {
3
- x: number;
4
- y: number;
5
- width: number;
6
- height: number;
7
- };
8
- export declare function addBackground(result: StyleCreateResult, primaryColor: string, secondaryColor: string, type: BackgroundType, rotation: number): string;
9
- export declare function addScale(result: StyleCreateResult, scale: number): string;
10
- export declare function addTranslate(result: StyleCreateResult, x?: number, y?: number): string;
11
- export declare function addRotate(result: StyleCreateResult, rotate: number): string;
12
- export declare function addFlip(result: StyleCreateResult): string;
13
- export declare function addViewboxMask(result: StyleCreateResult, radius: number): string;
14
- export declare function createAttrString(result: StyleCreateResult): string;
15
- export declare function randomizeIds(result: StyleCreateResult): string;
package/lib/utils/svg.js DELETED
@@ -1,79 +0,0 @@
1
- import * as escape from './escape.js';
2
- import { create as createPrng } from './prng.js';
3
- export function getViewBox(result) {
4
- let viewBox = result.attributes['viewBox'].split(' ');
5
- let x = parseInt(viewBox[0]);
6
- let y = parseInt(viewBox[1]);
7
- let width = parseInt(viewBox[2]);
8
- let height = parseInt(viewBox[3]);
9
- return {
10
- x,
11
- y,
12
- width,
13
- height,
14
- };
15
- }
16
- export function addBackground(result, primaryColor, secondaryColor, type, rotation) {
17
- let { width, height, x, y } = getViewBox(result);
18
- const solidBackground = `<rect fill="${escape.xml(primaryColor)}" width="${width}" height="${height}" x="${x}" y="${y}" />`;
19
- switch (type) {
20
- case 'solid':
21
- return solidBackground + result.body;
22
- case 'gradientLinear':
23
- return (`<rect fill="url(#backgroundLinear)" width="${width}" height="${height}" x="${x}" y="${y}" />` +
24
- `<defs>` +
25
- `<linearGradient id="backgroundLinear" gradientTransform="rotate(${rotation} 0.5 0.5)">` +
26
- `<stop stop-color="${escape.xml(primaryColor)}"/>` +
27
- `<stop offset="1" stop-color="${escape.xml(secondaryColor)}"/>` +
28
- `</linearGradient>` +
29
- `</defs>` +
30
- result.body);
31
- }
32
- }
33
- export function addScale(result, scale) {
34
- let { width, height, x, y } = getViewBox(result);
35
- let percent = scale ? (scale - 100) / 100 : 0;
36
- let translateX = (width / 2 + x) * percent * -1;
37
- let translateY = (height / 2 + y) * percent * -1;
38
- return `<g transform="translate(${translateX} ${translateY}) scale(${scale / 100})">${result.body}</g>`;
39
- }
40
- export function addTranslate(result, x, y) {
41
- let viewBox = getViewBox(result);
42
- let translateX = (viewBox.width + viewBox.x * 2) * ((x !== null && x !== void 0 ? x : 0) / 100);
43
- let translateY = (viewBox.height + viewBox.y * 2) * ((y !== null && y !== void 0 ? y : 0) / 100);
44
- return `<g transform="translate(${translateX} ${translateY})">${result.body}</g>`;
45
- }
46
- export function addRotate(result, rotate) {
47
- let { width, height, x, y } = getViewBox(result);
48
- return `<g transform="rotate(${rotate}, ${width / 2 + x}, ${height / 2 + y})">${result.body}</g>`;
49
- }
50
- export function addFlip(result) {
51
- let { width, x } = getViewBox(result);
52
- return `<g transform="scale(-1 1) translate(${width * -1 - x * 2} 0)">${result.body}</g>`;
53
- }
54
- export function addViewboxMask(result, radius) {
55
- let { width, height, x, y } = getViewBox(result);
56
- let rx = radius ? (width * radius) / 100 : 0;
57
- let ry = radius ? (height * radius) / 100 : 0;
58
- return (`<mask id="viewboxMask">` +
59
- `<rect width="${width}" height="${height}" rx="${rx}" ry="${ry}" x="${x}" y="${y}" fill="#fff" />` +
60
- `</mask>` +
61
- `<g mask="url(#viewboxMask)">${result.body}</g>`);
62
- }
63
- export function createAttrString(result) {
64
- const attributes = {
65
- xmlns: 'http://www.w3.org/2000/svg',
66
- ...result.attributes,
67
- };
68
- return Object.keys(attributes)
69
- .map((attr) => `${escape.xml(attr)}="${escape.xml(attributes[attr])}"`)
70
- .join(' ');
71
- }
72
- export function randomizeIds(result) {
73
- const prng = createPrng(Math.random().toString());
74
- const ids = {};
75
- return result.body.replace(/(id="|url\(#)([a-z0-9-_]+)([")])/gi, (match, m1, m2, m3) => {
76
- ids[m2] = ids[m2] || prng.string(8);
77
- return `${m1}${ids[m2]}${m3}`;
78
- });
79
- }
File without changes