amountFormatter.ts
11 months ago
convertLocalDateTimeToISOString.ts
10 months ago
formatTimestamp.ts
10 months ago
formatToDateLocalInput.ts
10 months ago
formatToDateTimeLocalInput.ts
10 months ago
getRelativeTimeString.ts
10 months ago
index.ts
10 months ago
prepareDefaultValuesFromSchema.ts
11 months ago
prepareDefaultValuesFromSchema.ts
49 lines
| 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 |