PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Admin / common / prepareDefaultValuesFromSchema.ts

prepareDefaultValuesFromSchema.ts in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Admin/common/prepareDefaultValuesFromSchema.ts

49 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { SchemaProperty } from '../types';
2
3 /**
4 * @since 4.10.0
5 */
6 export function prepareDefaultValuesFromSchema(
7 record: Record<string, any>,
8 schemaProperties: Record<string, SchemaProperty>
9 ): Record<string, any> {
10 const isReadOnly = (schema: SchemaProperty): boolean => {
11 return schema?.readOnly || schema?.readonly;
12 };
13
14 const isObject = (value: any, schema: SchemaProperty): boolean => {
15 return schema?.properties &&typeof value === "object" && value !== null && !Array.isArray(value);
16 };
17
18 const isArray = (value: any, schema: SchemaProperty): boolean => {
19 return schema?.type === "array" && schema?.items && Array.isArray(value);
20 };
21
22 const processValue = (value: any, schema: SchemaProperty): any => {
23 if (isObject(value, schema)) {
24 return prepareDefaultValuesFromSchema(value, schema.properties as Record<string, SchemaProperty>);
25 }
26
27 if (isArray(value, schema)) {
28 return value.map(item =>
29 isObject(item, schema.items as SchemaProperty)
30 ? prepareDefaultValuesFromSchema(item, (schema.items as any).properties ?? {})
31 : item
32 );
33 }
34
35 return value;
36 };
37
38 return Object.fromEntries(
39 Object.entries(record)
40 .filter(([key]) => !isReadOnly(schemaProperties[key]))
41 .map(([key, value]) => [
42 key,
43 schemaProperties[key]
44 ? processValue(value, schemaProperties[key])
45 : value
46 ])
47 );
48 }
49