1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.deepAssign = exports.isUnset = exports.isStrictObject = exports.maybePercentage = exports.omitPrefixObject = exports.filterPrefixObject = exports.prefixObject = exports.maybeSubObject = exports.subObject = exports.appendTransform = exports.useMemo = exports.random = exports.defined = exports.copyAttributes = exports.error = exports.capitalizeFirst = exports.composeAsync = exports.compose = exports.identity = void 0;
|
13 | const util_1 = require("@antv/util");
|
14 | function identity(x) {
|
15 | return x;
|
16 | }
|
17 | exports.identity = identity;
|
18 |
|
19 |
|
20 |
|
21 | function compose(fns) {
|
22 | return fns.reduce((composed, fn) => (x, ...args) => fn(composed(x, ...args), ...args), identity);
|
23 | }
|
24 | exports.compose = compose;
|
25 |
|
26 |
|
27 |
|
28 | function composeAsync(fns) {
|
29 | return fns.reduce((composed, fn) => (x) => __awaiter(this, void 0, void 0, function* () {
|
30 | const value = yield composed(x);
|
31 | return fn(value);
|
32 | }), identity);
|
33 | }
|
34 | exports.composeAsync = composeAsync;
|
35 | function capitalizeFirst(str) {
|
36 | return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
|
37 | }
|
38 | exports.capitalizeFirst = capitalizeFirst;
|
39 | function error(message = '') {
|
40 | throw new Error(message);
|
41 | }
|
42 | exports.error = error;
|
43 | function copyAttributes(target, source) {
|
44 | const { attributes } = source;
|
45 | const exclude = new Set(['id', 'className']);
|
46 | for (const [key, value] of Object.entries(attributes)) {
|
47 | if (!exclude.has(key)) {
|
48 | target.attr(key, value);
|
49 | }
|
50 | }
|
51 | }
|
52 | exports.copyAttributes = copyAttributes;
|
53 | function defined(x) {
|
54 | return x !== undefined && x !== null && !Number.isNaN(x);
|
55 | }
|
56 | exports.defined = defined;
|
57 | function random(a, b) {
|
58 | return a + (b - a) * Math.random();
|
59 | }
|
60 | exports.random = random;
|
61 | function useMemo(compute) {
|
62 | const map = new Map();
|
63 | return (key) => {
|
64 | if (map.has(key))
|
65 | return map.get(key);
|
66 | const value = compute(key);
|
67 | map.set(key, value);
|
68 | return value;
|
69 | };
|
70 | }
|
71 | exports.useMemo = useMemo;
|
72 | function appendTransform(node, transform) {
|
73 | const { transform: preTransform } = node.style;
|
74 | const unset = (d) => d === 'none' || d === undefined;
|
75 | const prefix = unset(preTransform) ? '' : preTransform;
|
76 | node.style.transform = `${prefix} ${transform}`.trimStart();
|
77 | }
|
78 | exports.appendTransform = appendTransform;
|
79 | function subObject(obj, prefix) {
|
80 | return maybeSubObject(obj, prefix) || {};
|
81 | }
|
82 | exports.subObject = subObject;
|
83 | function maybeSubObject(obj, prefix) {
|
84 | const entries = Object.entries(obj || {})
|
85 | .filter(([key]) => key.startsWith(prefix))
|
86 | .map(([key, value]) => [(0, util_1.lowerFirst)(key.replace(prefix, '').trim()), value])
|
87 | .filter(([key]) => !!key);
|
88 | return entries.length === 0 ? null : Object.fromEntries(entries);
|
89 | }
|
90 | exports.maybeSubObject = maybeSubObject;
|
91 | function prefixObject(obj, prefix) {
|
92 | return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
|
93 | return [`${prefix}${(0, util_1.upperFirst)(key)}`, value];
|
94 | }));
|
95 | }
|
96 | exports.prefixObject = prefixObject;
|
97 | function filterPrefixObject(obj, prefix) {
|
98 | return Object.fromEntries(Object.entries(obj).filter(([key]) => prefix.find((p) => key.startsWith(p))));
|
99 | }
|
100 | exports.filterPrefixObject = filterPrefixObject;
|
101 | function omitPrefixObject(obj, ...prefixes) {
|
102 | return Object.fromEntries(Object.entries(obj).filter(([key]) => prefixes.every((prefix) => !key.startsWith(prefix))));
|
103 | }
|
104 | exports.omitPrefixObject = omitPrefixObject;
|
105 | function maybePercentage(x, size) {
|
106 | if (x === undefined)
|
107 | return null;
|
108 | if (typeof x === 'number')
|
109 | return x;
|
110 | const px = +x.replace('%', '');
|
111 | return Number.isNaN(px) ? null : (px / 100) * size;
|
112 | }
|
113 | exports.maybePercentage = maybePercentage;
|
114 | function isStrictObject(d) {
|
115 | return (typeof d === 'object' &&
|
116 | !(d instanceof Date) &&
|
117 | d !== null &&
|
118 | !Array.isArray(d));
|
119 | }
|
120 | exports.isStrictObject = isStrictObject;
|
121 | function isUnset(value) {
|
122 | return value === null || value === false;
|
123 | }
|
124 | exports.isUnset = isUnset;
|
125 | function deepAssign(dist, src, maxLevel = 5, level = 0) {
|
126 | if (level >= maxLevel)
|
127 | return;
|
128 | for (const key of Object.keys(src)) {
|
129 | const value = src[key];
|
130 | if (!(0, util_1.isPlainObject)(value) || !(0, util_1.isPlainObject)(dist[key])) {
|
131 | dist[key] = value;
|
132 | }
|
133 | else {
|
134 | deepAssign(dist[key], value, maxLevel, level + 1);
|
135 | }
|
136 | }
|
137 | return dist;
|
138 | }
|
139 | exports.deepAssign = deepAssign;
|
140 |
|
\ | No newline at end of file |