| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.extendDomain = extendDomain;
|
|---|
| 7 | exports.isWellFormedNumberDomain = isWellFormedNumberDomain;
|
|---|
| 8 | exports.numericalDomainSpecifiedWithoutRequiringData = numericalDomainSpecifiedWithoutRequiringData;
|
|---|
| 9 | exports.parseNumericalUserDomain = parseNumericalUserDomain;
|
|---|
| 10 | var _ChartUtils = require("./ChartUtils");
|
|---|
| 11 | var _DataUtils = require("./DataUtils");
|
|---|
| 12 | var _isWellBehavedNumber = require("./isWellBehavedNumber");
|
|---|
| 13 | function isWellFormedNumberDomain(v) {
|
|---|
| 14 | if (Array.isArray(v) && v.length === 2) {
|
|---|
| 15 | var [min, max] = v;
|
|---|
| 16 | if ((0, _isWellBehavedNumber.isWellBehavedNumber)(min) && (0, _isWellBehavedNumber.isWellBehavedNumber)(max)) {
|
|---|
| 17 | return true;
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 | return false;
|
|---|
| 21 | }
|
|---|
| 22 | function extendDomain(providedDomain, boundaryDomain, allowDataOverflow) {
|
|---|
| 23 | if (allowDataOverflow) {
|
|---|
| 24 | // If the data are allowed to overflow - we're fine with whatever user provided
|
|---|
| 25 | return providedDomain;
|
|---|
| 26 | }
|
|---|
| 27 | /*
|
|---|
| 28 | * If the data are not allowed to overflow - we need to extend the domain.
|
|---|
| 29 | * Means that effectively the user is allowed to make the domain larger
|
|---|
| 30 | * but not smaller.
|
|---|
| 31 | */
|
|---|
| 32 | return [Math.min(providedDomain[0], boundaryDomain[0]), Math.max(providedDomain[1], boundaryDomain[1])];
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * So Recharts allows users to provide their own domains,
|
|---|
| 37 | * but it also places some expectations on what the domain is.
|
|---|
| 38 | * We can improve on the typescript typing, but we also need a runtime test
|
|---|
| 39 | to observe that the user-provided domain is well-formed,
|
|---|
| 40 | * that is: an array with exactly two numbers.
|
|---|
| 41 | *
|
|---|
| 42 | * This function does not accept data as an argument.
|
|---|
| 43 | * This is to enable a performance optimization - if the domain is there,
|
|---|
| 44 | * and we know what it is without traversing all the data,
|
|---|
| 45 | * then we don't have to traverse all the data!
|
|---|
| 46 | *
|
|---|
| 47 | * If the user-provided domain is not well-formed,
|
|---|
| 48 | * this function will return undefined - in which case we should traverse the data to calculate the real domain.
|
|---|
| 49 | *
|
|---|
| 50 | * This function is for parsing the numerical domain only.
|
|---|
| 51 | *
|
|---|
| 52 | * @param userDomain external prop, user provided, before validation. Can have various shapes: array, function, special magical strings inside too.
|
|---|
| 53 | * @param allowDataOverflow boolean, provided by users. If true then the data domain wins
|
|---|
| 54 | *
|
|---|
| 55 | * @return [min, max] domain if it's well-formed; undefined if the domain is invalid
|
|---|
| 56 | */
|
|---|
| 57 | function numericalDomainSpecifiedWithoutRequiringData(userDomain, allowDataOverflow) {
|
|---|
| 58 | if (!allowDataOverflow) {
|
|---|
| 59 | // Cannot compute data overflow if the data is not provided
|
|---|
| 60 | return undefined;
|
|---|
| 61 | }
|
|---|
| 62 | if (typeof userDomain === 'function') {
|
|---|
| 63 | // The user function expects the data to be provided as an argument
|
|---|
| 64 | return undefined;
|
|---|
| 65 | }
|
|---|
| 66 | if (Array.isArray(userDomain) && userDomain.length === 2) {
|
|---|
| 67 | var [providedMin, providedMax] = userDomain;
|
|---|
| 68 | var finalMin, finalMax;
|
|---|
| 69 | if ((0, _isWellBehavedNumber.isWellBehavedNumber)(providedMin)) {
|
|---|
| 70 | finalMin = providedMin;
|
|---|
| 71 | } else if (typeof providedMin === 'function') {
|
|---|
| 72 | // The user function expects the data to be provided as an argument
|
|---|
| 73 | return undefined;
|
|---|
| 74 | }
|
|---|
| 75 | if ((0, _isWellBehavedNumber.isWellBehavedNumber)(providedMax)) {
|
|---|
| 76 | finalMax = providedMax;
|
|---|
| 77 | } else if (typeof providedMax === 'function') {
|
|---|
| 78 | // The user function expects the data to be provided as an argument
|
|---|
| 79 | return undefined;
|
|---|
| 80 | }
|
|---|
| 81 | var candidate = [finalMin, finalMax];
|
|---|
| 82 | if (isWellFormedNumberDomain(candidate)) {
|
|---|
| 83 | return candidate;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | return undefined;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * So Recharts allows users to provide their own domains,
|
|---|
| 91 | * but it also places some expectations on what the domain is.
|
|---|
| 92 | * We can improve on the typescript typing, but we also need a runtime test
|
|---|
| 93 | * to observe that the user-provided domain is well-formed,
|
|---|
| 94 | * that is: an array with exactly two numbers.
|
|---|
| 95 | * If the user-provided domain is not well-formed,
|
|---|
| 96 | * this function will return undefined - in which case we should traverse the data to calculate the real domain.
|
|---|
| 97 | *
|
|---|
| 98 | * This function is for parsing the numerical domain only.
|
|---|
| 99 | *
|
|---|
| 100 | * You are probably thinking, why does domain need tick count?
|
|---|
| 101 | * Well it adjusts the domain based on where the "nice ticks" land, and nice ticks depend on the tick count.
|
|---|
| 102 | *
|
|---|
| 103 | * @param userDomain external prop, user provided, before validation. Can have various shapes: array, function, special magical strings inside too.
|
|---|
| 104 | * @param dataDomain calculated from data. Can be undefined, as an option for performance optimization
|
|---|
| 105 | * @param allowDataOverflow provided by users. If true then the data domain wins
|
|---|
| 106 | *
|
|---|
| 107 | * @return [min, max] domain if it's well-formed; undefined if the domain is invalid
|
|---|
| 108 | */
|
|---|
| 109 | function parseNumericalUserDomain(userDomain, dataDomain, allowDataOverflow) {
|
|---|
| 110 | if (!allowDataOverflow && dataDomain == null) {
|
|---|
| 111 | // Cannot compute data overflow if the data is not provided
|
|---|
| 112 | return undefined;
|
|---|
| 113 | }
|
|---|
| 114 | if (typeof userDomain === 'function' && dataDomain != null) {
|
|---|
| 115 | try {
|
|---|
| 116 | var result = userDomain(dataDomain, allowDataOverflow);
|
|---|
| 117 | if (isWellFormedNumberDomain(result)) {
|
|---|
| 118 | return extendDomain(result, dataDomain, allowDataOverflow);
|
|---|
| 119 | }
|
|---|
| 120 | } catch (_unused) {
|
|---|
| 121 | /* ignore the exception and compute domain from data later */
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | if (Array.isArray(userDomain) && userDomain.length === 2) {
|
|---|
| 125 | var [providedMin, providedMax] = userDomain;
|
|---|
| 126 | var finalMin, finalMax;
|
|---|
| 127 | if (providedMin === 'auto') {
|
|---|
| 128 | if (dataDomain != null) {
|
|---|
| 129 | finalMin = Math.min(...dataDomain);
|
|---|
| 130 | }
|
|---|
| 131 | } else if ((0, _DataUtils.isNumber)(providedMin)) {
|
|---|
| 132 | finalMin = providedMin;
|
|---|
| 133 | } else if (typeof providedMin === 'function') {
|
|---|
| 134 | try {
|
|---|
| 135 | if (dataDomain != null) {
|
|---|
| 136 | finalMin = providedMin(dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[0]);
|
|---|
| 137 | }
|
|---|
| 138 | } catch (_unused2) {
|
|---|
| 139 | /* ignore the exception and compute domain from data later */
|
|---|
| 140 | }
|
|---|
| 141 | } else if (typeof providedMin === 'string' && _ChartUtils.MIN_VALUE_REG.test(providedMin)) {
|
|---|
| 142 | var match = _ChartUtils.MIN_VALUE_REG.exec(providedMin);
|
|---|
| 143 | if (match == null || match[1] == null || dataDomain == null) {
|
|---|
| 144 | finalMin = undefined;
|
|---|
| 145 | } else {
|
|---|
| 146 | var value = +match[1];
|
|---|
| 147 | finalMin = dataDomain[0] - value;
|
|---|
| 148 | }
|
|---|
| 149 | } else {
|
|---|
| 150 | finalMin = dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[0];
|
|---|
| 151 | }
|
|---|
| 152 | if (providedMax === 'auto') {
|
|---|
| 153 | if (dataDomain != null) {
|
|---|
| 154 | finalMax = Math.max(...dataDomain);
|
|---|
| 155 | }
|
|---|
| 156 | } else if ((0, _DataUtils.isNumber)(providedMax)) {
|
|---|
| 157 | finalMax = providedMax;
|
|---|
| 158 | } else if (typeof providedMax === 'function') {
|
|---|
| 159 | try {
|
|---|
| 160 | if (dataDomain != null) {
|
|---|
| 161 | finalMax = providedMax(dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[1]);
|
|---|
| 162 | }
|
|---|
| 163 | } catch (_unused3) {
|
|---|
| 164 | /* ignore the exception and compute domain from data later */
|
|---|
| 165 | }
|
|---|
| 166 | } else if (typeof providedMax === 'string' && _ChartUtils.MAX_VALUE_REG.test(providedMax)) {
|
|---|
| 167 | var _match = _ChartUtils.MAX_VALUE_REG.exec(providedMax);
|
|---|
| 168 | if (_match == null || _match[1] == null || dataDomain == null) {
|
|---|
| 169 | finalMax = undefined;
|
|---|
| 170 | } else {
|
|---|
| 171 | var _value = +_match[1];
|
|---|
| 172 | finalMax = dataDomain[1] + _value;
|
|---|
| 173 | }
|
|---|
| 174 | } else {
|
|---|
| 175 | finalMax = dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[1];
|
|---|
| 176 | }
|
|---|
| 177 | var candidate = [finalMin, finalMax];
|
|---|
| 178 | if (isWellFormedNumberDomain(candidate)) {
|
|---|
| 179 | if (dataDomain == null) {
|
|---|
| 180 | return candidate;
|
|---|
| 181 | }
|
|---|
| 182 | return extendDomain(candidate, dataDomain, allowDataOverflow);
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | return undefined;
|
|---|
| 186 | } |
|---|