@dicebear/core 7.0.1 → 8.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.
- package/lib/core.js +2 -2
- package/lib/utils/color.d.ts +1 -1
- package/lib/utils/color.js +13 -3
- package/lib/utils/prng.js +9 -1
- package/package.json +3 -3
package/lib/core.js
CHANGED
|
@@ -9,8 +9,8 @@ export function createAvatar(style, options = {}) {
|
|
|
9
9
|
options = mergeOptions(style, options);
|
|
10
10
|
const prng = createPrng(options.seed);
|
|
11
11
|
const result = style.create({ prng: prng, options });
|
|
12
|
-
const
|
|
13
|
-
const
|
|
12
|
+
const backgroundType = prng.pick((_a = options.backgroundType) !== null && _a !== void 0 ? _a : [], 'solid');
|
|
13
|
+
const { primary: primaryBackgroundColor, secondary: secondaryBackgroundColor, } = getBackgroundColors(prng, (_b = options.backgroundColor) !== null && _b !== void 0 ? _b : [], backgroundType);
|
|
14
14
|
const backgroundRotation = prng.integer(((_c = options.backgroundRotation) === null || _c === void 0 ? void 0 : _c.length) ? Math.min(...options.backgroundRotation) : 0, ((_d = options.backgroundRotation) === null || _d === void 0 ? void 0 : _d.length) ? Math.max(...options.backgroundRotation) : 0);
|
|
15
15
|
if (options.size) {
|
|
16
16
|
result.attributes.width = options.size.toString();
|
package/lib/utils/color.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Prng } from '../types.js';
|
|
2
2
|
export declare function convertColor(color: string): string;
|
|
3
|
-
export declare function getBackgroundColors(prng: Prng, backgroundColor: string[]): {
|
|
3
|
+
export declare function getBackgroundColors(prng: Prng, backgroundColor: string[], backgroundType: 'solid' | 'gradientLinear'): {
|
|
4
4
|
primary: string;
|
|
5
5
|
secondary: string;
|
|
6
6
|
};
|
package/lib/utils/color.js
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
export function convertColor(color) {
|
|
2
2
|
return 'transparent' === color ? color : `#${color}`;
|
|
3
3
|
}
|
|
4
|
-
export function getBackgroundColors(prng, backgroundColor) {
|
|
4
|
+
export function getBackgroundColors(prng, backgroundColor, backgroundType) {
|
|
5
5
|
var _a;
|
|
6
|
-
let shuffledBackgroundColors;
|
|
7
|
-
if (
|
|
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;
|
|
8
11
|
// A function call should in any case make an identical number of calls to the PRNG.
|
|
9
12
|
prng.next();
|
|
13
|
+
}
|
|
14
|
+
else if (backgroundColor.length == 2 && backgroundType == 'gradientLinear') {
|
|
15
|
+
// If the background is to be a colour gradient and exactly two background
|
|
16
|
+
// colours have been specified, do not sort them randomly. In this case, we
|
|
17
|
+
// assume that the order of the background colours was chosen on purpose.
|
|
10
18
|
shuffledBackgroundColors = backgroundColor;
|
|
19
|
+
// A function call should in any case make an identical number of calls to the PRNG.
|
|
20
|
+
prng.next();
|
|
11
21
|
}
|
|
12
22
|
else {
|
|
13
23
|
shuffledBackgroundColors = prng.shuffle(backgroundColor);
|
package/lib/utils/prng.js
CHANGED
|
@@ -43,7 +43,15 @@ export function create(seed = '') {
|
|
|
43
43
|
// Each method call should call the `next` function only once.
|
|
44
44
|
// Therefore, we use a separate instance of the PRNG here.
|
|
45
45
|
const internalPrng = create(next().toString());
|
|
46
|
-
|
|
46
|
+
// Fisher-Yates shuffle algorithm - We do not use the Array.sort method
|
|
47
|
+
// because it is not stable and produces different results when used in
|
|
48
|
+
// different browsers. See: https://github.com/dicebear/dicebear/issues/394
|
|
49
|
+
const workingArray = [...arr];
|
|
50
|
+
for (let i = workingArray.length - 1; i > 0; i--) {
|
|
51
|
+
const j = internalPrng.integer(0, i);
|
|
52
|
+
[workingArray[i], workingArray[j]] = [workingArray[j], workingArray[i]];
|
|
53
|
+
}
|
|
54
|
+
return workingArray;
|
|
47
55
|
},
|
|
48
56
|
string(length, characters = 'abcdefghijklmnopqrstuvwxyz1234567890') {
|
|
49
57
|
// Each method call should call the `next` function only once.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dicebear/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-rc.1",
|
|
4
4
|
"description": "An avatar library for designers and developers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"avatar",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"test": "uvu tests"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@dicebear/converter": "
|
|
35
|
+
"@dicebear/converter": "8.0.0-rc.1",
|
|
36
36
|
"@types/json-schema": "^7.0.11"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "d0e1e6de2c38449636f03090226d5a8989e29d5d"
|
|
51
51
|
}
|