@dicebear/core 5.0.0-alpha.2 → 5.0.0-alpha.20

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.es.js DELETED
@@ -1,523 +0,0 @@
1
- var type = "object";
2
- var $schema = "http://json-schema.org/draft-07/schema#";
3
- var title = "Options";
4
- var properties$1 = {
5
- seed: {
6
- type: "string"
7
- },
8
- dataUri: {
9
- type: "boolean",
10
- "default": false
11
- },
12
- flip: {
13
- type: "boolean",
14
- "default": false
15
- },
16
- rotate: {
17
- type: "integer",
18
- minimum: 0,
19
- maximum: 360,
20
- "default": 0
21
- },
22
- scale: {
23
- type: "integer",
24
- minimum: 0,
25
- maximum: 200,
26
- "default": 100
27
- },
28
- radius: {
29
- type: "integer",
30
- minimum: 0,
31
- maximum: 50,
32
- "default": 0
33
- },
34
- size: {
35
- type: "integer",
36
- minimum: 1
37
- },
38
- backgroundColor: {
39
- type: "array",
40
- items: {
41
- anyOf: [
42
- {
43
- type: "string",
44
- pattern: "^#([a-fA-F0-9]{3}|[a-fA-F0-9]{4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$"
45
- },
46
- {
47
- type: "string",
48
- pattern: "^[0-9a-zA-Z]+$"
49
- }
50
- ]
51
- }
52
- },
53
- translateX: {
54
- type: "integer",
55
- minimum: -100,
56
- maximum: 100,
57
- "default": 0
58
- },
59
- translateY: {
60
- type: "integer",
61
- minimum: -100,
62
- maximum: 100,
63
- "default": 0
64
- },
65
- clip: {
66
- type: "boolean",
67
- "default": true
68
- }
69
- };
70
- var additionalProperties = false;
71
- var untypedSchema = {
72
- type: type,
73
- $schema: $schema,
74
- title: title,
75
- properties: properties$1,
76
- additionalProperties: additionalProperties
77
- };
78
-
79
- function xml(attr) {
80
- return attr.replace(/&/g, '&amp;').replace(/'/g, '&apos;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
81
- }
82
-
83
- var _escape = /*#__PURE__*/Object.freeze({
84
- __proto__: null,
85
- xml: xml
86
- });
87
-
88
- function properties(schema) {
89
- var _schema$properties;
90
-
91
- return (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
92
- }
93
- function defaults(schema) {
94
- let result = {};
95
- let props = properties(schema);
96
- Object.keys(props).forEach(key => {
97
- let val = props[key];
98
-
99
- if (typeof val === 'object' && undefined !== val.default) {
100
- result[key] = val.default;
101
- }
102
- });
103
- return result;
104
- }
105
-
106
- var schema$1 = /*#__PURE__*/Object.freeze({
107
- __proto__: null,
108
- properties: properties,
109
- defaults: defaults
110
- });
111
-
112
- function merge(style, options) {
113
- let result = { ...{
114
- seed: Math.random().toString()
115
- },
116
- ...defaults(untypedSchema),
117
- ...defaults(style.schema),
118
- ...options
119
- };
120
- return result;
121
- }
122
-
123
- var options = /*#__PURE__*/Object.freeze({
124
- __proto__: null,
125
- merge: merge
126
- });
127
-
128
- const MIN = -2147483648;
129
- const MAX = 2147483647;
130
-
131
- function xorshift(value) {
132
- value ^= value << 13;
133
- value ^= value >> 17;
134
- value ^= value << 5;
135
- return value;
136
- }
137
-
138
- function hashSeed(seed) {
139
- let hash = 0;
140
-
141
- for (let i = 0; i < seed.length; i++) {
142
- hash = (hash << 5) - hash + seed.charCodeAt(i) | 0;
143
- hash = xorshift(hash);
144
- }
145
-
146
- return hash;
147
- }
148
-
149
- function randomSeed() {
150
- return MIN + Math.floor((MAX - MIN) * Math.random()).toString();
151
- }
152
-
153
- function create(seed) {
154
- var _seed;
155
-
156
- seed = (_seed = seed) !== null && _seed !== void 0 ? _seed : randomSeed();
157
- let value = hashSeed(seed) || 1;
158
-
159
- const next = () => value = xorshift(value);
160
-
161
- const integer = (min, max) => {
162
- return Math.floor((next() - MIN) / (MAX - MIN) * (max + 1 - min) + min);
163
- };
164
-
165
- return {
166
- seed,
167
-
168
- bool() {
169
- let likelihood = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 50;
170
- return integer(0, 100) < likelihood;
171
- },
172
-
173
- integer(min, max) {
174
- return integer(min, max);
175
- },
176
-
177
- pick(arr) {
178
- return arr[integer(0, arr.length - 1)];
179
- }
180
-
181
- };
182
- }
183
-
184
- var prng = /*#__PURE__*/Object.freeze({
185
- __proto__: null,
186
- create: create
187
- });
188
-
189
- const ccLicenses = {
190
- by: {
191
- permits: ['Reproduction', 'Distribution', 'DerivativeWorks'],
192
- requires: ['Notice', 'Attribution'],
193
- prohibits: []
194
- },
195
- 'by-sa': {
196
- permits: ['Reproduction', 'Distribution', 'DerivativeWorks'],
197
- requires: ['Notice', 'Attribution', 'ShareAlike'],
198
- prohibits: []
199
- },
200
- 'by-nd': {
201
- permits: ['Reproduction', 'Distribution'],
202
- requires: ['Notice', 'Attribution'],
203
- prohibits: []
204
- },
205
- 'by-nc': {
206
- permits: ['Reproduction', 'Distribution', 'DerivativeWorks'],
207
- requires: ['Notice', 'Attribution'],
208
- prohibits: ['CommercialUse']
209
- },
210
- 'by-nc-sa': {
211
- permits: ['Reproduction', 'Distribution', 'DerivativeWorks'],
212
- requires: ['Notice', 'Attribution', 'ShareAlike'],
213
- prohibits: ['CommercialUse']
214
- },
215
- 'by-nc-nd': {
216
- permits: ['Reproduction', 'Distribution'],
217
- requires: ['Notice', 'Attribution'],
218
- prohibits: ['CommercialUse']
219
- },
220
- zero: {
221
- permits: ['Reproduction', 'Distribution', 'DerivativeWorks'],
222
- requires: [],
223
- prohibits: []
224
- }
225
- };
226
- function createGroup(_ref) {
227
- let {
228
- children,
229
- x,
230
- y
231
- } = _ref;
232
- return "<g transform=\"translate(".concat(x, ", ").concat(y, ")\">").concat(children, "</g>");
233
- }
234
- function getXmlnsAttributes() {
235
- return {
236
- 'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
237
- 'xmlns:cc': 'http://creativecommons.org/ns#',
238
- 'xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
239
- 'xmlns:svg': 'http://www.w3.org/2000/svg',
240
- xmlns: 'http://www.w3.org/2000/svg'
241
- };
242
- }
243
- function getMetadata(style) {
244
- return "\n<metadata>\n<rdf:RDF>\n<cc:Work>\n<dc:format>image/svg+xml</dc:format>\n<dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\n".concat(getMetadataWorkTitle(style), "\n").concat(getMetadataWorkCreator(style), "\n").concat(getMetadataWorkSource(style), "\n").concat(getMetadataWorkLicense(style), "\n").concat(getMetadataWorkContributor(style), "\n</cc:Work>\n").concat(getMetadataLicense(style), "\n</rdf:RDF>\n</metadata>\n");
245
- }
246
- function getMetadataWorkTitle(style) {
247
- if (style.meta.title) {
248
- return "<dc:title>".concat(style.meta.title, "</dc:title>");
249
- }
250
-
251
- return '';
252
- }
253
- function getMetadataWorkCreator(style) {
254
- if (style.meta.creator) {
255
- let creators = Array.isArray(style.meta.creator) ? style.meta.creator : [style.meta.creator];
256
- return "\n<dc:creator>\n".concat(getMetadataWorkAgents(creators), "\n</dc:creator>\n");
257
- }
258
-
259
- return '';
260
- }
261
- function getMetadataWorkSource(style) {
262
- if (style.meta.source) {
263
- return "<dc:source>".concat(style.meta.source, "</dc:source>");
264
- }
265
-
266
- return '';
267
- }
268
- function getMetadataWorkLicense(style) {
269
- if (style.meta.license) {
270
- return "<cc:license rdf:resource=\"".concat(style.meta.license.url, "\" />");
271
- }
272
-
273
- return '';
274
- }
275
- function getMetadataWorkContributor(style) {
276
- if (style.meta.contributor) {
277
- let contributors = Array.isArray(style.meta.contributor) ? style.meta.contributor : [style.meta.contributor];
278
- return "\n<dc:contributor>\n".concat(getMetadataWorkAgents(contributors), "\n</dc:contributor>\n");
279
- }
280
-
281
- return '';
282
- }
283
- function getMetadataWorkAgents(agents) {
284
- return agents.map(agent => "\n<cc:Agent>\n<dc:title>".concat(agent, "</dc:title>\n</cc:Agent>\n"));
285
- }
286
- function getMetadataLicense(style) {
287
- var _style$meta$license;
288
-
289
- let match = (_style$meta$license = style.meta.license) === null || _style$meta$license === void 0 ? void 0 : _style$meta$license.url.match(/^https?:\/\/creativecommons.org\/(?:licenses|publicdomain)\/([a-z\-]+)\/\d.\d\//);
290
-
291
- if (match) {
292
- let license = ccLicenses[match[1]];
293
-
294
- if (license) {
295
- var _style$meta$license2;
296
-
297
- let result = "";
298
- license.permits.forEach(permits => {
299
- result += "<cc:permits rdf:resource=\"https://creativecommons.org/ns#".concat(permits, "\" />");
300
- });
301
- license.requires.forEach(requires => {
302
- result += "<cc:requires rdf:resource=\"https://creativecommons.org/ns#".concat(requires, "\" />");
303
- });
304
- license.prohibits.forEach(prohibits => {
305
- result += "<cc:prohibits rdf:resource=\"https://creativecommons.org/ns#".concat(prohibits, "\" />");
306
- });
307
- return "\n<cc:License rdf:about=\"".concat((_style$meta$license2 = style.meta.license) === null || _style$meta$license2 === void 0 ? void 0 : _style$meta$license2.url, "\">\n").concat(result, "\n</cc:License>\n");
308
- }
309
- }
310
-
311
- return '';
312
- }
313
- function getViewBox(result) {
314
- let viewBox = result.attributes['viewBox'].split(' ');
315
- let x = parseInt(viewBox[0]);
316
- let y = parseInt(viewBox[1]);
317
- let width = parseInt(viewBox[2]);
318
- let height = parseInt(viewBox[3]);
319
- return {
320
- x,
321
- y,
322
- width,
323
- height
324
- };
325
- }
326
- function addBackgroundColor(result, backgroundColor) {
327
- let {
328
- width,
329
- height,
330
- x,
331
- y
332
- } = getViewBox(result);
333
- return "\n<rect fill=\"".concat(backgroundColor, "\" width=\"").concat(width, "\" height=\"").concat(height, "\" x=\"").concat(x, "\" y=\"").concat(y, "\" />\n").concat(result.body, "\n");
334
- }
335
- function addScale(result, scale) {
336
- let {
337
- width,
338
- height,
339
- x,
340
- y
341
- } = getViewBox(result);
342
- let percent = scale ? (scale - 100) / 100 : 0;
343
- let translateX = (width / 2 + x) * percent * -1;
344
- let translateY = (height / 2 + y) * percent * -1;
345
- return "\n<g transform=\"translate(".concat(translateX, " ").concat(translateY, ") scale(").concat(scale / 100, ")\">\n").concat(result.body, "\n</g>\n");
346
- }
347
- function addTranslate(result, x, y) {
348
- let viewBox = getViewBox(result);
349
- let translateX = (viewBox.width + viewBox.x * 2) * ((x !== null && x !== void 0 ? x : 0) / 100);
350
- let translateY = (viewBox.height + viewBox.y * 2) * ((y !== null && y !== void 0 ? y : 0) / 100);
351
- return "\n<g transform=\"translate(".concat(translateX, " ").concat(translateY, ")\">\n").concat(result.body, "\n</g>\n");
352
- }
353
- function addRotate(result, rotate) {
354
- let {
355
- width,
356
- height,
357
- x,
358
- y
359
- } = getViewBox(result);
360
- return "\n<g transform=\"rotate(".concat(rotate, ", ").concat(width / 2 + x, ", ").concat(height / 2 + y, ")\">\n").concat(result.body, "\n</g>\n");
361
- }
362
- function addFlip(result) {
363
- let {
364
- width,
365
- x
366
- } = getViewBox(result);
367
- return "\n<g transform=\"scale(-1 1) translate(".concat(width * -1 - x * 2, " 0)\">\n").concat(result.body, "\n</g>\n");
368
- }
369
- function addViewboxMask(result, radius) {
370
- let {
371
- width,
372
- height,
373
- x,
374
- y
375
- } = getViewBox(result);
376
- let rx = radius ? width * radius / 100 : 0;
377
- let ry = radius ? height * radius / 100 : 0;
378
- return "\n<mask id=\"viewboxMask\">\n<rect width=\"".concat(width, "\" height=\"").concat(height, "\" rx=\"").concat(rx, "\" ry=\"").concat(ry, "\" x=\"").concat(x, "\" y=\"").concat(y, "\" fill=\"#fff\" />\n</mask>\n<g mask=\"url(#viewboxMask)\">").concat(result.body, "</g>\n");
379
- }
380
- function createAttrString(attributes) {
381
- attributes = { ...getXmlnsAttributes(),
382
- ...attributes
383
- };
384
- return Object.keys(attributes).map(attr => "".concat(xml(attr), "=\"").concat(xml(attributes[attr]), "\"")).join(' ');
385
- }
386
- function removeWhitespace(svg) {
387
- return svg // Remove spaces at both ends of the string
388
- .trim() // Remove breaking lines
389
- .replace(/\n/g, ' ') // Remove space between tags
390
- .replace(/>\s+</g, '><') // Reduce whitespace
391
- .replace(/\s{2,}/g, ' ') // Create self closing tags
392
- .replace(/<([^\/>]+)><\/[^>]+>/gi, '<$1/>') // Remove whitespace before tag close
393
- .replace(/\s(\/?>)/g, '$1');
394
- }
395
- function convertToDataUri(svg) {
396
- return "data:image/svg+xml;utf8,".concat(encodeURIComponent(svg));
397
- }
398
-
399
- var svg = /*#__PURE__*/Object.freeze({
400
- __proto__: null,
401
- createGroup: createGroup,
402
- getXmlnsAttributes: getXmlnsAttributes,
403
- getMetadata: getMetadata,
404
- getMetadataWorkTitle: getMetadataWorkTitle,
405
- getMetadataWorkCreator: getMetadataWorkCreator,
406
- getMetadataWorkSource: getMetadataWorkSource,
407
- getMetadataWorkLicense: getMetadataWorkLicense,
408
- getMetadataWorkContributor: getMetadataWorkContributor,
409
- getMetadataWorkAgents: getMetadataWorkAgents,
410
- getMetadataLicense: getMetadataLicense,
411
- getViewBox: getViewBox,
412
- addBackgroundColor: addBackgroundColor,
413
- addScale: addScale,
414
- addTranslate: addTranslate,
415
- addRotate: addRotate,
416
- addFlip: addFlip,
417
- addViewboxMask: addViewboxMask,
418
- createAttrString: createAttrString,
419
- removeWhitespace: removeWhitespace,
420
- convertToDataUri: convertToDataUri
421
- });
422
-
423
- var index = /*#__PURE__*/Object.freeze({
424
- __proto__: null,
425
- escape: _escape,
426
- options: options,
427
- prng: prng,
428
- svg: svg,
429
- schema: schema$1
430
- });
431
-
432
- function createAvatar(style) {
433
- var _options$backgroundCo;
434
-
435
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
436
- options = merge(style, options);
437
- const prng = create(options.seed);
438
- const result = style.create({
439
- prng: prng,
440
- options
441
- });
442
- const backgroundColor = prng.pick((_options$backgroundCo = options.backgroundColor) !== null && _options$backgroundCo !== void 0 ? _options$backgroundCo : []);
443
-
444
- if (options.size) {
445
- result.attributes.width = options.size.toString();
446
- result.attributes.height = options.size.toString();
447
- }
448
-
449
- if (options.scale !== undefined && options.scale !== 100) {
450
- result.body = addScale(result, options.scale);
451
- }
452
-
453
- if (options.flip) {
454
- result.body = addFlip(result);
455
- }
456
-
457
- if (options.rotate) {
458
- result.body = addRotate(result, options.rotate);
459
- }
460
-
461
- if (options.translateX || options.translateY) {
462
- result.body = addTranslate(result, options.translateX, options.translateY);
463
- }
464
-
465
- if (backgroundColor && backgroundColor !== 'transparent') {
466
- result.body = addBackgroundColor(result, backgroundColor);
467
- }
468
-
469
- if (options.radius || options.clip) {
470
- var _options$radius;
471
-
472
- result.body = addViewboxMask(result, (_options$radius = options.radius) !== null && _options$radius !== void 0 ? _options$radius : 0);
473
- }
474
-
475
- let avatar = removeWhitespace("\n<svg ".concat(createAttrString(result.attributes), ">\n").concat(getMetadata(style), "\n").concat(result.body, "\n</svg>\n"));
476
- return options.dataUri ? convertToDataUri(avatar) : avatar;
477
- }
478
- function createPreview(style, options, property) {
479
- var _style$preview, _options$backgroundCo2;
480
-
481
- options = merge(style, options);
482
- const prng = create(options.seed);
483
- let result = (_style$preview = style.preview) === null || _style$preview === void 0 ? void 0 : _style$preview.call(style, {
484
- prng,
485
- options,
486
- property
487
- });
488
- const backgroundColor = prng.pick((_options$backgroundCo2 = options.backgroundColor) !== null && _options$backgroundCo2 !== void 0 ? _options$backgroundCo2 : []);
489
- const hasBackgroundColor = backgroundColor && backgroundColor !== 'transparent';
490
- const isBackgroundVisible = property === 'backgroundColor';
491
-
492
- if (undefined === result) {
493
- if (hasBackgroundColor && isBackgroundVisible) {
494
- result = {
495
- attributes: {
496
- viewBox: "0 0 1 1",
497
- fill: 'none',
498
- 'shape-rendering': 'auto'
499
- },
500
- body: ""
501
- };
502
- } else {
503
- return undefined;
504
- }
505
- }
506
-
507
- if (hasBackgroundColor && isBackgroundVisible) {
508
- result.body = addBackgroundColor(result, backgroundColor);
509
- }
510
-
511
- let avatar = removeWhitespace("\n<svg ".concat(createAttrString(result.attributes), ">\n").concat(getMetadata(style), "\n").concat(result.body, "\n</svg>\n"));
512
- return options.dataUri ? convertToDataUri(avatar) : avatar;
513
- }
514
-
515
- /*!
516
- * DiceBear (@dicebear/core)
517
- *
518
- * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
519
- * Copyright (c) 2021 Florian Körner
520
- */
521
- const schema = untypedSchema;
522
-
523
- export { createAvatar, createPreview, schema, index as utils };