| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.svgPropertiesAndEvents = svgPropertiesAndEvents;
|
|---|
| 7 | exports.svgPropertiesAndEventsFromUnknown = svgPropertiesAndEventsFromUnknown;
|
|---|
| 8 | var _react = require("react");
|
|---|
| 9 | var _excludeEventProps = require("./excludeEventProps");
|
|---|
| 10 | var _svgPropertiesNoEvents = require("./svgPropertiesNoEvents");
|
|---|
| 11 | /**
|
|---|
| 12 | * Filters an object to only include SVG properties, data attributes, and event handlers.
|
|---|
| 13 | * @param obj - The object to filter.
|
|---|
| 14 | * @returns A new object containing only valid SVG properties, data attributes, and event handlers.
|
|---|
| 15 | */
|
|---|
| 16 | function svgPropertiesAndEvents(obj) {
|
|---|
| 17 | var result = {};
|
|---|
| 18 | // for ... in loop is 10x faster than Object.entries + filter + Object.fromEntries in Chrome
|
|---|
| 19 |
|
|---|
| 20 | for (var key in obj) {
|
|---|
| 21 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|---|
| 22 | if ((0, _svgPropertiesNoEvents.isSvgElementPropKey)(key) || (0, _svgPropertiesNoEvents.isDataAttribute)(key) || (0, _excludeEventProps.isEventKey)(key)) {
|
|---|
| 23 | result[key] = obj[key];
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 | return result;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Function to filter SVG properties from various input types.
|
|---|
| 32 | * The input types can be:
|
|---|
| 33 | * - A record of string keys to any values, in which case it returns a record of only SVG properties
|
|---|
| 34 | * - A React element, in which case it returns the props of the element filtered to only SVG properties
|
|---|
| 35 | * - Anything else, in which case it returns null
|
|---|
| 36 | *
|
|---|
| 37 | * This function has a wide-open return type, because it will read and filter the props of an arbitrary React element.
|
|---|
| 38 | * This can be SVG, HTML, whatnot, with arbitrary values, so we can't type it more specifically.
|
|---|
| 39 | *
|
|---|
| 40 | * If you wish to have a type-safe version, use svgPropertiesNoEvents directly with a typed object.
|
|---|
| 41 | *
|
|---|
| 42 | * @param input - The input to filter, which can be a record, a React element, or other types.
|
|---|
| 43 | * @returns A record of SVG properties if the input is a record or React element, otherwise null.
|
|---|
| 44 | */
|
|---|
| 45 | function svgPropertiesAndEventsFromUnknown(input) {
|
|---|
| 46 | if (input == null) {
|
|---|
| 47 | return null;
|
|---|
| 48 | }
|
|---|
| 49 | if (/*#__PURE__*/(0, _react.isValidElement)(input)) {
|
|---|
| 50 | // @ts-expect-error we can't type this better because input can be any React element
|
|---|
| 51 | return svgPropertiesAndEvents(input.props);
|
|---|
| 52 | }
|
|---|
| 53 | if (typeof input === 'object' && !Array.isArray(input)) {
|
|---|
| 54 | return svgPropertiesAndEvents(input);
|
|---|
| 55 | }
|
|---|
| 56 | return null;
|
|---|
| 57 | } |
|---|