@elliemae/ds-props-helpers 2.2.0-next.4 → 2.3.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/defaultProps/index.js +28 -9
- package/cjs/defaultProps/index.js.map +7 -0
- package/cjs/defaultProps/useMemoMergePropsWithDefault.js +44 -44
- package/cjs/defaultProps/useMemoMergePropsWithDefault.js.map +7 -0
- package/cjs/getProps/index.js +37 -20
- package/cjs/getProps/index.js.map +7 -0
- package/cjs/index.js +30 -24
- package/cjs/index.js.map +7 -0
- package/cjs/tests/test.schema.js +67 -0
- package/cjs/tests/test.schema.js.map +7 -0
- package/cjs/validation/errorTemplates.js +44 -12
- package/cjs/validation/errorTemplates.js.map +7 -0
- package/cjs/validation/index.js +30 -15
- package/cjs/validation/index.js.map +7 -0
- package/cjs/validation/typescriptGuards.js +60 -31
- package/cjs/validation/typescriptGuards.js.map +7 -0
- package/cjs/validation/typescriptParsers.js +65 -34
- package/cjs/validation/typescriptParsers.js.map +7 -0
- package/cjs/validation/typescriptValidator.js +99 -135
- package/cjs/validation/typescriptValidator.js.map +7 -0
- package/cjs/validation/validator.js +43 -24
- package/cjs/validation/validator.js.map +7 -0
- package/esm/defaultProps/index.js +3 -1
- package/esm/defaultProps/index.js.map +7 -0
- package/esm/defaultProps/useMemoMergePropsWithDefault.js +14 -34
- package/esm/defaultProps/useMemoMergePropsWithDefault.js.map +7 -0
- package/esm/getProps/index.js +8 -15
- package/esm/getProps/index.js.map +7 -0
- package/esm/index.js +5 -7
- package/esm/index.js.map +7 -0
- package/esm/tests/test.schema.js +38 -0
- package/esm/tests/test.schema.js.map +7 -0
- package/esm/validation/errorTemplates.js +15 -7
- package/esm/validation/errorTemplates.js.map +7 -0
- package/esm/validation/index.js +5 -3
- package/esm/validation/index.js.map +7 -0
- package/esm/validation/typescriptGuards.js +31 -18
- package/esm/validation/typescriptGuards.js.map +7 -0
- package/esm/validation/typescriptParsers.js +36 -30
- package/esm/validation/typescriptParsers.js.map +7 -0
- package/esm/validation/typescriptValidator.js +57 -106
- package/esm/validation/typescriptValidator.js.map +7 -0
- package/esm/validation/validator.js +12 -18
- package/esm/validation/validator.js.map +7 -0
- package/package.json +1 -17
- package/types/index.d.ts +0 -1
- package/types/validation/typescriptValidator.d.ts +2 -2
- package/cjs/globalProps/constants.js +0 -15
- package/cjs/globalProps/globalAttributesPropTypes.js +0 -372
- package/cjs/globalProps/index.js +0 -11
- package/cjs/globalProps/useGetGlobalAttributes.js +0 -36
- package/esm/globalProps/constants.js +0 -11
- package/esm/globalProps/globalAttributesPropTypes.js +0 -368
- package/esm/globalProps/index.js +0 -2
- package/esm/globalProps/useGetGlobalAttributes.js +0 -32
- package/types/globalProps/constants.d.ts +0 -3
- package/types/globalProps/globalAttributesPropTypes.d.ts +0 -2169
- package/types/globalProps/index.d.ts +0 -2
- package/types/globalProps/useGetGlobalAttributes.d.ts +0 -5
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/defaultProps/useMemoMergePropsWithDefault.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useRef } from 'react';\nimport deepequal from 'fast-deep-equal/react';\n\n// Hook\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst useMemoCompare = <T = any>(next: T, compare: (prevToCompare: T, nextToCompare: T) => boolean): T => {\n // Ref for storing previous value\n const previousRef = useRef(next);\n const previous = previousRef.current;\n // Pass previous and next value to compare function\n // to determine whether to consider them equal.\n const isEqual = compare(previous, next);\n // If not equal update previousRef to next value.\n // We only update if not equal so that this hook continues to return\n // the same old value if compare keeps returning true.\n if (!isEqual) previousRef.current = next;\n // Finally, if equal then return the previous value\n return isEqual ? previous : next;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const useMemoMergePropsWithDefault = <T = Record<string, any>>(\n props: Partial<T>,\n defaultProps: Partial<T>,\n compare = deepequal,\n): T => {\n const mergedProps = { ...defaultProps, ...props } as T;\n\n return useMemoCompare<T>(mergedProps, compare);\n};\n"],
|
|
5
|
+
"mappings": "AAAA;ACAA;AACA;AAIA,MAAM,iBAAiB,CAAU,MAAS,YAAgE;AAExG,QAAM,cAAc,OAAO;AAC3B,QAAM,WAAW,YAAY;AAG7B,QAAM,UAAU,QAAQ,UAAU;AAIlC,MAAI,CAAC;AAAS,gBAAY,UAAU;AAEpC,SAAO,UAAU,WAAW;AAAA;AAIvB,MAAM,+BAA+B,CAC1C,OACA,cACA,UAAU,cACJ;AACN,QAAM,cAAc,KAAK,iBAAiB;AAE1C,SAAO,eAAkB,aAAa;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/esm/getProps/index.js
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}));
|
|
10
|
-
const getDataProps = props => Object.fromEntries(Object.entries(props).filter(_ref2 => {
|
|
11
|
-
let [key] = _ref2;
|
|
12
|
-
return key.includes('data-');
|
|
13
|
-
}));
|
|
14
|
-
|
|
15
|
-
export { getAriaProps, getDataProps };
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
const getAriaProps = (props) => Object.fromEntries(Object.entries(props).filter(([key]) => key.includes("aria-")));
|
|
3
|
+
const getDataProps = (props) => Object.fromEntries(Object.entries(props).filter(([key]) => key.includes("data-")));
|
|
4
|
+
export {
|
|
5
|
+
getAriaProps,
|
|
6
|
+
getDataProps
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/getProps/index.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export const getAriaProps = (props: Record<string, unknown>): Record<string, unknown> =>\n Object.fromEntries(Object.entries(props).filter(([key]) => key.includes('aria-')));\n\nexport const getDataProps = (props: Record<string, unknown>): Record<string, unknown> =>\n Object.fromEntries(Object.entries(props).filter(([key]) => key.includes('data-')));\n"],
|
|
5
|
+
"mappings": "AAAA;ACAO,MAAM,eAAe,CAAC,UAC3B,OAAO,YAAY,OAAO,QAAQ,OAAO,OAAO,CAAC,CAAC,SAAS,IAAI,SAAS;AAEnE,MAAM,eAAe,CAAC,UAC3B,OAAO,YAAY,OAAO,QAAQ,OAAO,OAAO,CAAC,CAAC,SAAS,IAAI,SAAS;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/esm/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
export { useGetGlobalAttributes } from './globalProps/useGetGlobalAttributes.js';
|
|
7
|
-
export { globalAttributesPropTypes } from './globalProps/globalAttributesPropTypes.js';
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export * from "./defaultProps";
|
|
3
|
+
export * from "./validation";
|
|
4
|
+
export * from "./getProps";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export * from './defaultProps';\nexport * from './validation';\nexport * from './getProps';\n"],
|
|
5
|
+
"mappings": "AAAA;ACAA;AACA;AACA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { PropTypes, describe } from "react-desc";
|
|
3
|
+
const TestSchema = {
|
|
4
|
+
string: PropTypes.string.description("String"),
|
|
5
|
+
number: PropTypes.number.description("Number"),
|
|
6
|
+
boolean: PropTypes.bool.description("Boolean"),
|
|
7
|
+
any: PropTypes.any.description("Any"),
|
|
8
|
+
union: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).description("Union"),
|
|
9
|
+
union2: PropTypes.oneOf(["A", "B"]).description("Union 2"),
|
|
10
|
+
union3: PropTypes.oneOfType([
|
|
11
|
+
PropTypes.string,
|
|
12
|
+
PropTypes.arrayOf(PropTypes.string),
|
|
13
|
+
PropTypes.shape({ string: PropTypes.string })
|
|
14
|
+
]).description("Union 3"),
|
|
15
|
+
array: PropTypes.arrayOf(PropTypes.string).description("Array"),
|
|
16
|
+
object: PropTypes.object.description("Object"),
|
|
17
|
+
shape: PropTypes.shape({
|
|
18
|
+
string: PropTypes.string.description("String").isRequired,
|
|
19
|
+
number: PropTypes.number.description("Number"),
|
|
20
|
+
boolean: PropTypes.bool.description("Boolean"),
|
|
21
|
+
anotherShape: PropTypes.shape({
|
|
22
|
+
string: PropTypes.string.description("String").isRequired,
|
|
23
|
+
number: PropTypes.number.description("Number"),
|
|
24
|
+
boolean: PropTypes.bool.description("Boolean")
|
|
25
|
+
}).description("Another shape")
|
|
26
|
+
}).description("Shape"),
|
|
27
|
+
function: PropTypes.func.description("Function"),
|
|
28
|
+
jsx: PropTypes.element.description("JSX"),
|
|
29
|
+
node: PropTypes.node.description("Node")
|
|
30
|
+
};
|
|
31
|
+
const TestComponent = () => null;
|
|
32
|
+
const TestComponentWithSchema = describe(TestComponent).description("Test Component");
|
|
33
|
+
TestComponentWithSchema.propTypes = TestSchema;
|
|
34
|
+
const TypescriptSchema = TestComponentWithSchema.toTypescript();
|
|
35
|
+
export {
|
|
36
|
+
TypescriptSchema
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=test.schema.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/tests/test.schema.js"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { PropTypes, describe } from 'react-desc';\n\nconst TestSchema = {\n string: PropTypes.string.description('String'),\n number: PropTypes.number.description('Number'),\n boolean: PropTypes.bool.description('Boolean'),\n any: PropTypes.any.description('Any'),\n union: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).description('Union'),\n union2: PropTypes.oneOf(['A', 'B']).description('Union 2'),\n union3: PropTypes.oneOfType([\n PropTypes.string,\n PropTypes.arrayOf(PropTypes.string),\n PropTypes.shape({ string: PropTypes.string }),\n ]).description('Union 3'),\n array: PropTypes.arrayOf(PropTypes.string).description('Array'),\n object: PropTypes.object.description('Object'),\n shape: PropTypes.shape({\n string: PropTypes.string.description('String').isRequired,\n number: PropTypes.number.description('Number'),\n boolean: PropTypes.bool.description('Boolean'),\n anotherShape: PropTypes.shape({\n string: PropTypes.string.description('String').isRequired,\n number: PropTypes.number.description('Number'),\n boolean: PropTypes.bool.description('Boolean'),\n }).description('Another shape'),\n }).description('Shape'),\n function: PropTypes.func.description('Function'),\n jsx: PropTypes.element.description('JSX'),\n node: PropTypes.node.description('Node'),\n};\n\nconst TestComponent = () => null;\n\nconst TestComponentWithSchema = describe(TestComponent).description('Test Component');\n\nTestComponentWithSchema.propTypes = TestSchema;\n\nexport const TypescriptSchema = TestComponentWithSchema.toTypescript();\n"],
|
|
5
|
+
"mappings": "AAAA;ACAA;AAEA,MAAM,aAAa;AAAA,EACjB,QAAQ,UAAU,OAAO,YAAY;AAAA,EACrC,QAAQ,UAAU,OAAO,YAAY;AAAA,EACrC,SAAS,UAAU,KAAK,YAAY;AAAA,EACpC,KAAK,UAAU,IAAI,YAAY;AAAA,EAC/B,OAAO,UAAU,UAAU,CAAC,UAAU,QAAQ,UAAU,SAAS,YAAY;AAAA,EAC7E,QAAQ,UAAU,MAAM,CAAC,KAAK,MAAM,YAAY;AAAA,EAChD,QAAQ,UAAU,UAAU;AAAA,IAC1B,UAAU;AAAA,IACV,UAAU,QAAQ,UAAU;AAAA,IAC5B,UAAU,MAAM,EAAE,QAAQ,UAAU;AAAA,KACnC,YAAY;AAAA,EACf,OAAO,UAAU,QAAQ,UAAU,QAAQ,YAAY;AAAA,EACvD,QAAQ,UAAU,OAAO,YAAY;AAAA,EACrC,OAAO,UAAU,MAAM;AAAA,IACrB,QAAQ,UAAU,OAAO,YAAY,UAAU;AAAA,IAC/C,QAAQ,UAAU,OAAO,YAAY;AAAA,IACrC,SAAS,UAAU,KAAK,YAAY;AAAA,IACpC,cAAc,UAAU,MAAM;AAAA,MAC5B,QAAQ,UAAU,OAAO,YAAY,UAAU;AAAA,MAC/C,QAAQ,UAAU,OAAO,YAAY;AAAA,MACrC,SAAS,UAAU,KAAK,YAAY;AAAA,OACnC,YAAY;AAAA,KACd,YAAY;AAAA,EACf,UAAU,UAAU,KAAK,YAAY;AAAA,EACrC,KAAK,UAAU,QAAQ,YAAY;AAAA,EACnC,MAAM,UAAU,KAAK,YAAY;AAAA;AAGnC,MAAM,gBAAgB,MAAM;AAE5B,MAAM,0BAA0B,SAAS,eAAe,YAAY;AAEpE,wBAAwB,YAAY;AAE7B,MAAM,mBAAmB,wBAAwB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/* eslint-disable max-params */
|
|
1
|
+
import * as React from "react";
|
|
4
2
|
const throwTypeError = (componentName, validPropKey, invalidProp, validFormat) => {
|
|
5
|
-
throw new Error(
|
|
3
|
+
throw new Error(`${componentName}:: You are trying to pass a not valid "${validPropKey}" property,
|
|
4
|
+
please provide a valid type.
|
|
5
|
+
|
|
6
|
+
Received: ${invalidProp} (${typeof invalidProp})
|
|
7
|
+
Expected: (${validFormat.replace("\n", " or ")})
|
|
8
|
+
`);
|
|
6
9
|
};
|
|
7
10
|
const throwRequiredError = (componentName, validPropKey) => {
|
|
8
|
-
throw new Error(
|
|
11
|
+
throw new Error(`${componentName}:: Please provide a/an "${validPropKey}" property to use this component.
|
|
12
|
+
This property is required.
|
|
13
|
+
`);
|
|
9
14
|
};
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
export {
|
|
16
|
+
throwRequiredError,
|
|
17
|
+
throwTypeError
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=errorTemplates.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/validation/errorTemplates.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable max-params */\nexport const throwTypeError = (\n componentName: string,\n validPropKey: string,\n invalidProp: unknown,\n validFormat: string,\n): void => {\n throw new Error(\n `${componentName}:: You are trying to pass a not valid \"${validPropKey}\" property, \n please provide a valid type.\n\n Received: ${invalidProp} (${typeof invalidProp})\n Expected: (${validFormat.replace('\\n', ' or ')})\n `,\n );\n};\n\nexport const throwRequiredError = (componentName: string, validPropKey: string): void => {\n throw new Error(\n `${componentName}:: Please provide a/an \"${validPropKey}\" property to use this component. \n This property is required.\n `,\n );\n};\n"],
|
|
5
|
+
"mappings": "AAAA;ACCO,MAAM,iBAAiB,CAC5B,eACA,cACA,aACA,gBACS;AACT,QAAM,IAAI,MACR,GAAG,uDAAuD;AAAA;AAAA;AAAA,cAGhD,gBAAgB,OAAO;AAAA,eACtB,YAAY,QAAQ,MAAM;AAAA;AAAA;AAKlC,MAAM,qBAAqB,CAAC,eAAuB,iBAA+B;AACvF,QAAM,IAAI,MACR,GAAG,wCAAwC;AAAA;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/esm/validation/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export * from "./validator";
|
|
3
|
+
export * from "./errorTemplates";
|
|
4
|
+
export * from "./typescriptValidator";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/validation/index.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export * from './validator';\nexport * from './errorTemplates';\nexport * from './typescriptValidator';\n"],
|
|
5
|
+
"mappings": "AAAA;ACAA;AACA;AACA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,23 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const isUndefined = format => format === '"undefined"';
|
|
7
|
-
const isNull = format => format === '"null"';
|
|
8
|
-
const isUnion = format => {
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
const isPrimitiveType = (format) => ["string", "number", "boolean"].includes(format);
|
|
3
|
+
const isUndefined = (format) => format === '"undefined"';
|
|
4
|
+
const isNull = (format) => format === '"null"';
|
|
5
|
+
const isUnion = (format) => {
|
|
9
6
|
let depth = 0;
|
|
10
7
|
let satisfies = false;
|
|
11
|
-
format.split(
|
|
12
|
-
if ([
|
|
8
|
+
format.split("").forEach((char) => {
|
|
9
|
+
if (["{", "("].includes(char))
|
|
10
|
+
depth += 1;
|
|
11
|
+
else if (["}", ")"].includes(char))
|
|
12
|
+
depth -= 1;
|
|
13
|
+
else if (char === "|" && depth === 0)
|
|
14
|
+
satisfies = true;
|
|
13
15
|
});
|
|
14
16
|
return satisfies;
|
|
15
17
|
};
|
|
16
|
-
const isString = format => !isUnion(format) && format[0] === '"' && format.slice(-1) === '"';
|
|
17
|
-
const isArray = format => !isUnion(format) && format.slice(-2) ===
|
|
18
|
-
const isObject = format => format ===
|
|
19
|
-
const isFunction = format => !isUnion(format) && format ===
|
|
20
|
-
const isJSXorNode = format => !isUnion(format) && [
|
|
21
|
-
const isSomethingWithParenthesis = format => !isUnion(format) && format[0] ===
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const isString = (format) => !isUnion(format) && format[0] === '"' && format.slice(-1) === '"';
|
|
19
|
+
const isArray = (format) => !isUnion(format) && format.slice(-2) === "[]";
|
|
20
|
+
const isObject = (format) => format === "object" || !isUnion(format) && format[0] === "{" && format.slice(-1) === "}";
|
|
21
|
+
const isFunction = (format) => !isUnion(format) && format === "((...args: any[]) => any)";
|
|
22
|
+
const isJSXorNode = (format) => !isUnion(format) && ["React.ReactNode", "JSX.Element"].includes(format);
|
|
23
|
+
const isSomethingWithParenthesis = (format) => !isUnion(format) && format[0] === "(" && format.slice(-1) === ")";
|
|
24
|
+
export {
|
|
25
|
+
isArray,
|
|
26
|
+
isFunction,
|
|
27
|
+
isJSXorNode,
|
|
28
|
+
isNull,
|
|
29
|
+
isObject,
|
|
30
|
+
isPrimitiveType,
|
|
31
|
+
isSomethingWithParenthesis,
|
|
32
|
+
isString,
|
|
33
|
+
isUndefined,
|
|
34
|
+
isUnion
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=typescriptGuards.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/validation/typescriptGuards.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "type GuardFn = (format: string) => boolean;\n\nexport const isPrimitiveType: GuardFn = (format) => ['string', 'number', 'boolean'].includes(format);\nexport const isUndefined: GuardFn = (format) => format === '\"undefined\"';\nexport const isNull: GuardFn = (format) => format === '\"null\"';\nexport const isUnion: GuardFn = (format) => {\n let depth = 0;\n let satisfies = false;\n format.split('').forEach((char) => {\n if (['{', '('].includes(char)) depth += 1;\n else if (['}', ')'].includes(char)) depth -= 1;\n else if (char === '|' && depth === 0) satisfies = true;\n });\n return satisfies;\n};\n\nexport const isString: GuardFn = (format) => !isUnion(format) && format[0] === '\"' && format.slice(-1) === '\"';\n\nexport const isArray: GuardFn = (format) => !isUnion(format) && format.slice(-2) === '[]';\n\nexport const isObject: GuardFn = (format) =>\n format === 'object' || (!isUnion(format) && format[0] === '{' && format.slice(-1) === '}');\n\nexport const isFunction: GuardFn = (format) => !isUnion(format) && format === '((...args: any[]) => any)';\n\nexport const isJSXorNode: GuardFn = (format) => !isUnion(format) && ['React.ReactNode', 'JSX.Element'].includes(format);\n\nexport const isSomethingWithParenthesis: GuardFn = (format) =>\n !isUnion(format) && format[0] === '(' && format.slice(-1) === ')';\n"],
|
|
5
|
+
"mappings": "AAAA;ACEO,MAAM,kBAA2B,CAAC,WAAW,CAAC,UAAU,UAAU,WAAW,SAAS;AACtF,MAAM,cAAuB,CAAC,WAAW,WAAW;AACpD,MAAM,SAAkB,CAAC,WAAW,WAAW;AAC/C,MAAM,UAAmB,CAAC,WAAW;AAC1C,MAAI,QAAQ;AACZ,MAAI,YAAY;AAChB,SAAO,MAAM,IAAI,QAAQ,CAAC,SAAS;AACjC,QAAI,CAAC,KAAK,KAAK,SAAS;AAAO,eAAS;AAAA,aAC/B,CAAC,KAAK,KAAK,SAAS;AAAO,eAAS;AAAA,aACpC,SAAS,OAAO,UAAU;AAAG,kBAAY;AAAA;AAEpD,SAAO;AAAA;AAGF,MAAM,WAAoB,CAAC,WAAW,CAAC,QAAQ,WAAW,OAAO,OAAO,OAAO,OAAO,MAAM,QAAQ;AAEpG,MAAM,UAAmB,CAAC,WAAW,CAAC,QAAQ,WAAW,OAAO,MAAM,QAAQ;AAE9E,MAAM,WAAoB,CAAC,WAChC,WAAW,YAAa,CAAC,QAAQ,WAAW,OAAO,OAAO,OAAO,OAAO,MAAM,QAAQ;AAEjF,MAAM,aAAsB,CAAC,WAAW,CAAC,QAAQ,WAAW,WAAW;AAEvE,MAAM,cAAuB,CAAC,WAAW,CAAC,QAAQ,WAAW,CAAC,mBAAmB,eAAe,SAAS;AAEzG,MAAM,6BAAsC,CAAC,WAClD,CAAC,QAAQ,WAAW,OAAO,OAAO,OAAO,OAAO,MAAM,QAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,41 +1,47 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const typescriptObjectParser = format => {
|
|
7
|
-
const keyValuePairs = []; // State of the algorithm
|
|
8
|
-
|
|
9
|
-
let lastKey = '';
|
|
10
|
-
let lastValue = '';
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
const typescriptObjectParser = (format) => {
|
|
3
|
+
const keyValuePairs = [];
|
|
4
|
+
let lastKey = "";
|
|
5
|
+
let lastValue = "";
|
|
11
6
|
let shouldAppendToKey = true;
|
|
12
|
-
|
|
13
7
|
const pushPair = () => {
|
|
14
|
-
if (lastKey)
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
if (lastKey)
|
|
9
|
+
keyValuePairs.push([lastKey, lastValue]);
|
|
10
|
+
lastKey = "";
|
|
11
|
+
lastValue = "";
|
|
17
12
|
shouldAppendToKey = true;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
};
|
|
21
14
|
let depth = 0;
|
|
22
|
-
format.split(
|
|
23
|
-
if (char ===
|
|
15
|
+
format.split("").forEach((char) => {
|
|
16
|
+
if (char === "{") {
|
|
24
17
|
depth += 1;
|
|
25
|
-
if (depth > 1)
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
if (depth > 1)
|
|
19
|
+
lastValue += char;
|
|
20
|
+
} else if (char === "}") {
|
|
21
|
+
if (depth > 1)
|
|
22
|
+
lastValue += char;
|
|
28
23
|
depth -= 1;
|
|
29
|
-
if (depth === 1)
|
|
30
|
-
|
|
24
|
+
if (depth === 1)
|
|
25
|
+
pushPair();
|
|
26
|
+
} else if (char === ":") {
|
|
31
27
|
shouldAppendToKey = false;
|
|
32
|
-
if (depth > 1)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
if (depth > 1)
|
|
29
|
+
lastValue += char;
|
|
30
|
+
} else if (char === ",") {
|
|
31
|
+
if (depth === 1)
|
|
32
|
+
pushPair();
|
|
33
|
+
else
|
|
34
|
+
lastValue += char;
|
|
35
|
+
} else if (char === " ") {
|
|
36
|
+
} else if (shouldAppendToKey)
|
|
37
|
+
lastKey += char;
|
|
38
|
+
else
|
|
39
|
+
lastValue += char;
|
|
36
40
|
});
|
|
37
41
|
pushPair();
|
|
38
42
|
return keyValuePairs;
|
|
39
43
|
};
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
export {
|
|
45
|
+
typescriptObjectParser
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=typescriptParsers.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../../src/validation/typescriptParsers.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable complexity */\nexport const typescriptObjectParser = (format: string): [string, string][] => {\n const keyValuePairs: [string, string][] = [];\n\n // State of the algorithm\n let lastKey = '';\n let lastValue = '';\n let shouldAppendToKey = true;\n\n const pushPair = () => {\n if (lastKey) keyValuePairs.push([lastKey, lastValue]);\n lastKey = '';\n lastValue = '';\n shouldAppendToKey = true;\n };\n\n // Complex -- but working -- logic\n let depth = 0;\n format.split('').forEach((char) => {\n if (char === '{') {\n depth += 1;\n if (depth > 1) lastValue += char;\n } else if (char === '}') {\n if (depth > 1) lastValue += char;\n depth -= 1;\n if (depth === 1) pushPair();\n } else if (char === ':') {\n shouldAppendToKey = false;\n if (depth > 1) lastValue += char;\n } else if (char === ',') {\n if (depth === 1) pushPair();\n else lastValue += char;\n } else if (char === ' ') {\n // Do nothing\n } else if (shouldAppendToKey) lastKey += char;\n else lastValue += char;\n });\n pushPair();\n\n return keyValuePairs;\n};\n"],
|
|
5
|
+
"mappings": "AAAA;ACCO,MAAM,yBAAyB,CAAC,WAAuC;AAC5E,QAAM,gBAAoC;AAG1C,MAAI,UAAU;AACd,MAAI,YAAY;AAChB,MAAI,oBAAoB;AAExB,QAAM,WAAW,MAAM;AACrB,QAAI;AAAS,oBAAc,KAAK,CAAC,SAAS;AAC1C,cAAU;AACV,gBAAY;AACZ,wBAAoB;AAAA;AAItB,MAAI,QAAQ;AACZ,SAAO,MAAM,IAAI,QAAQ,CAAC,SAAS;AACjC,QAAI,SAAS,KAAK;AAChB,eAAS;AACT,UAAI,QAAQ;AAAG,qBAAa;AAAA,eACnB,SAAS,KAAK;AACvB,UAAI,QAAQ;AAAG,qBAAa;AAC5B,eAAS;AACT,UAAI,UAAU;AAAG;AAAA,eACR,SAAS,KAAK;AACvB,0BAAoB;AACpB,UAAI,QAAQ;AAAG,qBAAa;AAAA,eACnB,SAAS,KAAK;AACvB,UAAI,UAAU;AAAG;AAAA;AACZ,qBAAa;AAAA,eACT,SAAS,KAAK;AAAA,eAEd;AAAmB,iBAAW;AAAA;AACpC,mBAAa;AAAA;AAEpB;AAEA,SAAO;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,202 +1,153 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { describe } from "react-desc";
|
|
3
|
+
import { useMemo, useState } from "react";
|
|
4
|
+
import { throwRequiredError, throwTypeError } from "./errorTemplates";
|
|
5
|
+
import {
|
|
6
|
+
isArray,
|
|
7
|
+
isFunction,
|
|
8
|
+
isJSXorNode,
|
|
9
|
+
isObject,
|
|
10
|
+
isPrimitiveType,
|
|
11
|
+
isSomethingWithParenthesis,
|
|
12
|
+
isString,
|
|
13
|
+
isUnion,
|
|
14
|
+
isUndefined,
|
|
15
|
+
isNull
|
|
16
|
+
} from "./typescriptGuards";
|
|
17
|
+
import { typescriptObjectParser } from "./typescriptParsers";
|
|
17
18
|
const validateUndefined = (schemaName, key, value, format) => {
|
|
18
|
-
if (value !==
|
|
19
|
+
if (value !== void 0 || value === "undefined") {
|
|
19
20
|
throwTypeError(schemaName, key, value, format);
|
|
20
21
|
}
|
|
21
22
|
};
|
|
22
|
-
|
|
23
23
|
const validateNull = (schemaName, key, value, format) => {
|
|
24
|
-
if (value !== null || value ===
|
|
24
|
+
if (value !== null || value === "null") {
|
|
25
25
|
throwTypeError(schemaName, key, value, format);
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
|
-
|
|
29
28
|
const validatePrimitiveType = (schemaName, key, value, format) => {
|
|
30
29
|
if (typeof value !== format) {
|
|
31
30
|
throwTypeError(schemaName, key, value, format);
|
|
32
31
|
}
|
|
33
32
|
};
|
|
34
|
-
|
|
35
33
|
const validateString = (schemaName, key, value, format) => {
|
|
36
34
|
if (value !== format.slice(1, -1)) {
|
|
37
35
|
throwTypeError(schemaName, key, value, format);
|
|
38
36
|
}
|
|
39
37
|
};
|
|
40
|
-
|
|
41
38
|
const validateArray = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
42
|
-
// Check that we have an array
|
|
43
39
|
if (!Array.isArray(value)) {
|
|
44
40
|
throwTypeError(schemaName, key, value, format);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
}
|
|
48
42
|
value.forEach((val, index) => {
|
|
49
|
-
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
51
|
-
validateValueWithFormat(schemaName, "".concat(key, "[").concat(index, "]"), val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
|
|
43
|
+
validateValueWithFormat(schemaName, `${key}[${index}]`, val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
|
|
52
44
|
});
|
|
53
45
|
};
|
|
54
|
-
|
|
55
46
|
function isObjectType(value) {
|
|
56
|
-
return !(typeof value !==
|
|
47
|
+
return !(typeof value !== "object" || Array.isArray(value));
|
|
57
48
|
}
|
|
58
|
-
|
|
59
49
|
const validateObject = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
60
|
-
const valuesIsObject = isObjectType(value);
|
|
61
|
-
|
|
50
|
+
const valuesIsObject = isObjectType(value);
|
|
62
51
|
if (!valuesIsObject) {
|
|
63
52
|
throwTypeError(schemaName, key, value, format);
|
|
64
53
|
return;
|
|
65
54
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const keyValuePairs = typescriptObjectParser(format);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
keyValuePairs.forEach(_ref => {
|
|
73
|
-
let [objectKey, objectValue] = _ref;
|
|
74
|
-
const trueKey = objectKey.slice(-1) === '?' ? objectKey.slice(0, -1) : objectKey;
|
|
75
|
-
|
|
55
|
+
if (format === "object")
|
|
56
|
+
return;
|
|
57
|
+
const keyValuePairs = typescriptObjectParser(format);
|
|
58
|
+
keyValuePairs.forEach(([objectKey, objectValue]) => {
|
|
59
|
+
const trueKey = objectKey.slice(-1) === "?" ? objectKey.slice(0, -1) : objectKey;
|
|
76
60
|
if (trueKey === objectKey && !(trueKey in value)) {
|
|
77
61
|
throwRequiredError(schemaName, key);
|
|
78
62
|
}
|
|
79
|
-
|
|
80
63
|
if (trueKey in value) {
|
|
81
|
-
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
83
|
-
validateValueWithFormat(schemaName, "".concat(key, "[").concat(trueKey, "]"), value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
|
|
64
|
+
validateValueWithFormat(schemaName, `${key}[${trueKey}]`, value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
|
|
84
65
|
}
|
|
85
66
|
});
|
|
86
67
|
};
|
|
87
|
-
|
|
88
68
|
const validateUnion = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
89
69
|
const possibilities = format.split(/\s?\|\s?/);
|
|
90
70
|
const errors = [];
|
|
91
|
-
possibilities.forEach(possibility => {
|
|
71
|
+
possibilities.forEach((possibility) => {
|
|
92
72
|
try {
|
|
93
|
-
// this is a recursive func, we need to invoke it before it's defined.
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
95
73
|
validateValueWithFormat(schemaName, key, value, possibility, validationsMemo, nextValidationsMemo);
|
|
96
74
|
} catch (e) {
|
|
97
75
|
errors.push(e);
|
|
98
76
|
}
|
|
99
77
|
});
|
|
100
|
-
|
|
101
78
|
if (errors.length === possibilities.length) {
|
|
102
79
|
throwTypeError(schemaName, key, value, format);
|
|
103
80
|
}
|
|
104
81
|
};
|
|
105
|
-
|
|
106
82
|
const validateFunction = (schemaName, key, value, format) => {
|
|
107
|
-
|
|
108
|
-
if (typeof value !== 'function') {
|
|
83
|
+
if (typeof value !== "function") {
|
|
109
84
|
throwTypeError(schemaName, key, value, format);
|
|
110
85
|
}
|
|
111
86
|
};
|
|
112
|
-
|
|
113
87
|
function isJSXElement(value) {
|
|
114
|
-
return value === null || typeof value ===
|
|
88
|
+
return value === null || typeof value === "object" && value !== null && "$$typeof" in value;
|
|
115
89
|
}
|
|
116
|
-
|
|
117
90
|
const validateJSXorNode = (schemaName, key, value, format) => {
|
|
118
91
|
const valueIsJSX = isJSXElement(value);
|
|
119
|
-
|
|
120
|
-
if (format === 'JSX.Element' && !valueIsJSX) {
|
|
92
|
+
if (format === "JSX.Element" && !valueIsJSX) {
|
|
121
93
|
throwTypeError(schemaName, key, value, format);
|
|
122
94
|
}
|
|
123
|
-
};
|
|
124
|
-
// Schema validator
|
|
125
|
-
// =============================================================================
|
|
126
|
-
|
|
127
|
-
|
|
95
|
+
};
|
|
128
96
|
const validateValueWithFormat = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
129
97
|
nextValidationsMemo[value] = format;
|
|
130
|
-
|
|
131
98
|
if (value in validationsMemo) {
|
|
132
|
-
// We already validated this value on this format
|
|
133
99
|
return;
|
|
134
100
|
}
|
|
135
|
-
|
|
136
101
|
if (isUndefined(format)) {
|
|
137
|
-
validateUndefined(schemaName, key, value, format);
|
|
102
|
+
validateUndefined(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
138
103
|
} else if (isNull(format)) {
|
|
139
|
-
validateNull(schemaName, key, value, format);
|
|
104
|
+
validateNull(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
140
105
|
} else if (isPrimitiveType(format)) {
|
|
141
|
-
validatePrimitiveType(schemaName, key, value, format);
|
|
106
|
+
validatePrimitiveType(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
142
107
|
} else if (isUnion(format)) {
|
|
143
108
|
validateUnion(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
144
109
|
} else if (isString(format)) {
|
|
145
|
-
validateString(schemaName, key, value, format);
|
|
110
|
+
validateString(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
146
111
|
} else if (isArray(format)) {
|
|
147
112
|
validateArray(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
148
113
|
} else if (isObject(format)) {
|
|
149
114
|
validateObject(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
150
115
|
} else if (isFunction(format)) {
|
|
151
|
-
validateFunction(schemaName, key, value, format);
|
|
116
|
+
validateFunction(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
152
117
|
} else if (isJSXorNode(format)) {
|
|
153
|
-
validateJSXorNode(schemaName, key, value, format);
|
|
118
|
+
validateJSXorNode(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
154
119
|
} else if (isSomethingWithParenthesis(format)) {
|
|
155
120
|
validateValueWithFormat(schemaName, key, value, format.slice(1, -1), validationsMemo, nextValidationsMemo);
|
|
156
121
|
}
|
|
157
122
|
};
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const {
|
|
163
|
-
properties,
|
|
164
|
-
name: schemaName
|
|
165
|
-
} = schema;
|
|
166
|
-
properties.forEach(property => {
|
|
167
|
-
const {
|
|
168
|
-
name,
|
|
169
|
-
format,
|
|
170
|
-
required
|
|
171
|
-
} = property;
|
|
172
|
-
|
|
123
|
+
const validateTypescriptPropTypesImplementation = (props, schema, validationsMemo = {}, nextValidationsMemo = {}) => {
|
|
124
|
+
const { properties, name: schemaName } = schema;
|
|
125
|
+
properties.forEach((property) => {
|
|
126
|
+
const { name, format, required } = property;
|
|
173
127
|
if (required && !(name in props)) {
|
|
174
128
|
throwRequiredError(schema.name, name);
|
|
175
129
|
}
|
|
176
|
-
|
|
177
|
-
if (name in props && (props[name] !== undefined || required)) {
|
|
130
|
+
if (name in props && (props[name] !== void 0 || required)) {
|
|
178
131
|
validateValueWithFormat(schemaName, name, props[name], format, validationsMemo, nextValidationsMemo);
|
|
179
132
|
}
|
|
180
133
|
});
|
|
181
|
-
};
|
|
182
|
-
|
|
134
|
+
};
|
|
183
135
|
const useValidateTypescriptPropTypes = (props, propTypes) => {
|
|
184
|
-
const [validationsMemo, setValidationsMemo] = useState({});
|
|
185
|
-
|
|
136
|
+
const [validationsMemo, setValidationsMemo] = useState({});
|
|
186
137
|
const ComponentWithSchema = useMemo(() => {
|
|
187
|
-
const Component = () => {
|
|
188
|
-
|
|
189
|
-
|
|
138
|
+
const Component = () => {
|
|
139
|
+
};
|
|
190
140
|
return describe(Component);
|
|
191
|
-
}, []);
|
|
192
|
-
|
|
141
|
+
}, []);
|
|
193
142
|
ComponentWithSchema.propTypes = propTypes;
|
|
194
143
|
useMemo(() => {
|
|
195
144
|
const nextValidationsMemo = {};
|
|
196
|
-
validateTypescriptPropTypesImplementation(props,
|
|
197
|
-
|
|
198
|
-
setValidationsMemo(nextValidationsMemo); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
145
|
+
validateTypescriptPropTypesImplementation(props, ComponentWithSchema.toTypescript(), validationsMemo, nextValidationsMemo);
|
|
146
|
+
setValidationsMemo(nextValidationsMemo);
|
|
199
147
|
}, [props]);
|
|
200
148
|
};
|
|
201
|
-
|
|
202
|
-
|
|
149
|
+
export {
|
|
150
|
+
useValidateTypescriptPropTypes,
|
|
151
|
+
validateTypescriptPropTypesImplementation
|
|
152
|
+
};
|
|
153
|
+
//# sourceMappingURL=typescriptValidator.js.map
|