UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.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;
13const util_1 = require("@antv/util");
14function identity(x) {
15 return x;
16}
17exports.identity = identity;
18/**
19 * Composes functions from left to right.
20 */
21function compose(fns) {
22 return fns.reduce((composed, fn) => (x, ...args) => fn(composed(x, ...args), ...args), identity);
23}
24exports.compose = compose;
25/**
26 * Composes single-argument async functions from left to right.
27 */
28function 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}
34exports.composeAsync = composeAsync;
35function capitalizeFirst(str) {
36 return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
37}
38exports.capitalizeFirst = capitalizeFirst;
39function error(message = '') {
40 throw new Error(message);
41}
42exports.error = error;
43function 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}
52exports.copyAttributes = copyAttributes;
53function defined(x) {
54 return x !== undefined && x !== null && !Number.isNaN(x);
55}
56exports.defined = defined;
57function random(a, b) {
58 return a + (b - a) * Math.random();
59}
60exports.random = random;
61function 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}
71exports.useMemo = useMemo;
72function 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}
78exports.appendTransform = appendTransform;
79function subObject(obj, prefix) {
80 return maybeSubObject(obj, prefix) || {};
81}
82exports.subObject = subObject;
83function 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}
90exports.maybeSubObject = maybeSubObject;
91function prefixObject(obj, prefix) {
92 return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
93 return [`${prefix}${(0, util_1.upperFirst)(key)}`, value];
94 }));
95}
96exports.prefixObject = prefixObject;
97function filterPrefixObject(obj, prefix) {
98 return Object.fromEntries(Object.entries(obj).filter(([key]) => prefix.find((p) => key.startsWith(p))));
99}
100exports.filterPrefixObject = filterPrefixObject;
101function omitPrefixObject(obj, ...prefixes) {
102 return Object.fromEntries(Object.entries(obj).filter(([key]) => prefixes.every((prefix) => !key.startsWith(prefix))));
103}
104exports.omitPrefixObject = omitPrefixObject;
105function 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}
113exports.maybePercentage = maybePercentage;
114function isStrictObject(d) {
115 return (typeof d === 'object' &&
116 !(d instanceof Date) &&
117 d !== null &&
118 !Array.isArray(d));
119}
120exports.isStrictObject = isStrictObject;
121function isUnset(value) {
122 return value === null || value === false;
123}
124exports.isUnset = isUnset;
125function 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}
139exports.deepAssign = deepAssign;
140//# sourceMappingURL=helper.js.map
\No newline at end of file