aaavatars 2.1.0 → 3.0.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/dist/value.js DELETED
@@ -1,66 +0,0 @@
1
- import { ACCESSORIES, EYES, FILLS, MOUTHS, SHAPES, } from "./spec.js";
2
- export const AVATAR_VALUE_MAX_LENGTH = 200;
3
- const HEX_COLOR = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
4
- const ENUM_FIELDS = {
5
- shape: SHAPES,
6
- eyes: EYES,
7
- mouth: MOUTHS,
8
- accessory: ACCESSORIES,
9
- fill: FILLS,
10
- };
11
- const COLOR_FIELDS = ["body", "background"];
12
- const FIELD_ORDER = [
13
- "shape",
14
- "eyes",
15
- "mouth",
16
- "accessory",
17
- "body",
18
- "background",
19
- "fill",
20
- ];
21
- function isEnumField(key) {
22
- return key in ENUM_FIELDS;
23
- }
24
- function isColorField(key) {
25
- return COLOR_FIELDS.includes(key);
26
- }
27
- export function parseAvatarValue(raw) {
28
- if (raw.length > AVATAR_VALUE_MAX_LENGTH) {
29
- return {
30
- ok: false,
31
- error: `Avatar value is longer than ${AVATAR_VALUE_MAX_LENGTH} characters.`,
32
- };
33
- }
34
- const value = {};
35
- for (const entry of raw
36
- .split(";")
37
- .map((part) => part.trim())
38
- .filter(Boolean)) {
39
- const separator = entry.indexOf("=");
40
- const key = separator === -1 ? entry : entry.slice(0, separator).trim();
41
- const field = separator === -1 ? "" : entry.slice(separator + 1).trim();
42
- if (key in value)
43
- return { ok: false, error: `"${key}" is set more than once.` };
44
- if (isEnumField(key)) {
45
- const allowed = ENUM_FIELDS[key];
46
- if (!allowed.includes(field)) {
47
- return { ok: false, error: `"${key}" must be one of ${allowed.join(", ")}.` };
48
- }
49
- }
50
- else if (isColorField(key)) {
51
- if (!HEX_COLOR.test(field)) {
52
- return { ok: false, error: `"${key}" must be a hex color like #E76F51.` };
53
- }
54
- }
55
- else {
56
- return { ok: false, error: `Unknown avatar field "${key}". Use ${FIELD_ORDER.join(", ")}.` };
57
- }
58
- value[key] = field;
59
- }
60
- return { ok: true, value: value };
61
- }
62
- export function formatAvatarValue(value) {
63
- return FIELD_ORDER.filter((key) => value[key] !== undefined)
64
- .map((key) => `${key}=${value[key]}`)
65
- .join(";");
66
- }