helper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. import { lowerFirst, upperFirst } from '@antv/util';
  11. export function identity(x) {
  12. return x;
  13. }
  14. /**
  15. * Composes functions from left to right.
  16. */
  17. export function compose(fns) {
  18. return fns.reduce((composed, fn) => (x, ...args) => fn(composed(x, ...args), ...args), identity);
  19. }
  20. /**
  21. * Composes single-argument async functions from left to right.
  22. */
  23. export function composeAsync(fns) {
  24. return fns.reduce((composed, fn) => (x) => __awaiter(this, void 0, void 0, function* () {
  25. const value = yield composed(x);
  26. return fn(value);
  27. }), identity);
  28. }
  29. export function capitalizeFirst(str) {
  30. return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
  31. }
  32. export function error(message = '') {
  33. throw new Error(message);
  34. }
  35. export function copyAttributes(target, source) {
  36. const { attributes } = source;
  37. const exclude = new Set(['id', 'className']);
  38. for (const [key, value] of Object.entries(attributes)) {
  39. if (!exclude.has(key))
  40. target.attr(key, value);
  41. }
  42. }
  43. export function defined(x) {
  44. return x !== undefined && x !== null && !Number.isNaN(x);
  45. }
  46. export function random(a, b) {
  47. return a + (b - a) * Math.random();
  48. }
  49. export function useMemo(compute) {
  50. const map = new Map();
  51. return (key) => {
  52. if (map.has(key))
  53. return map.get(key);
  54. const value = compute(key);
  55. map.set(key, value);
  56. return value;
  57. };
  58. }
  59. export function appendTransform(node, transform) {
  60. const { transform: preTransform } = node.style;
  61. const unset = (d) => d === 'none' || d === undefined;
  62. const prefix = unset(preTransform) ? '' : preTransform;
  63. node.style.transform = `${prefix} ${transform}`.trimStart();
  64. }
  65. export function subObject(obj, prefix) {
  66. return maybeSubObject(obj, prefix) || {};
  67. }
  68. export function maybeSubObject(obj, prefix) {
  69. const entries = Object.entries(obj || {})
  70. .filter(([key]) => key.startsWith(prefix))
  71. .map(([key, value]) => [lowerFirst(key.replace(prefix, '').trim()), value])
  72. .filter(([key]) => !!key);
  73. return entries.length === 0 ? null : Object.fromEntries(entries);
  74. }
  75. export function prefixObject(obj, prefix) {
  76. return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
  77. return [`${prefix}${upperFirst(key)}`, value];
  78. }));
  79. }
  80. export function filterPrefixObject(obj, prefix) {
  81. return Object.fromEntries(Object.entries(obj).filter(([key]) => prefix.find((p) => key.startsWith(p))));
  82. }
  83. export function omitPrefixObject(obj, ...prefixes) {
  84. return Object.fromEntries(Object.entries(obj).filter(([key]) => prefixes.every((prefix) => !key.startsWith(prefix))));
  85. }
  86. export function maybePercentage(x, size) {
  87. if (x === undefined)
  88. return null;
  89. if (typeof x === 'number')
  90. return x;
  91. const px = +x.replace('%', '');
  92. return Number.isNaN(px) ? null : (px / 100) * size;
  93. }
  94. export function isStrictObject(d) {
  95. return (typeof d === 'object' &&
  96. !(d instanceof Date) &&
  97. d !== null &&
  98. !Array.isArray(d));
  99. }
  100. export function isUnset(value) {
  101. return value === null || value === false;
  102. }
  103. //# sourceMappingURL=helper.js.map