-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support interpolation expression
- Loading branch information
Showing
8 changed files
with
151 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ export type ErrorMessage = { | |
name: string; | ||
error: string[]; | ||
}; | ||
|
||
export type Deps = Record<string, any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { FormData, Deps, Schema } from '../types'; | ||
import { isJsonSchema, isSegment, isObject } from './validate'; | ||
|
||
/* eslint-disable no-new-func */ | ||
export const safeEval = (code: string) => { | ||
return Function(`"use strict"; ${code}`)(); | ||
}; | ||
|
||
/** | ||
* 解析表达式计算结果 | ||
*/ | ||
export const evaluateSegment = ( | ||
segment: string, | ||
selfValue: unknown, | ||
formData: FormData, | ||
deps: Deps | ||
) => { | ||
try { | ||
return safeEval(` | ||
const $selfValue =${JSON.stringify(selfValue)}; | ||
const $values = ${JSON.stringify(formData)}; | ||
const $deps = ${JSON.stringify(deps)}; | ||
return (${segment}) | ||
`); | ||
} catch (error) { | ||
console.error(error, 'expression:', segment, 'formData:', formData); | ||
return null; | ||
} | ||
}; | ||
|
||
export const resolvePropertiesSegment = ( | ||
properties: Record<string, any>, | ||
formData: FormData, | ||
deps: Deps | ||
): Record<string, any> => { | ||
const resolvedProperties = {}; | ||
|
||
Object.keys(properties).forEach((key) => { | ||
const val = properties[key]; | ||
if (Array.isArray(val)) { | ||
resolvedProperties[key] = val; | ||
} else if (isObject(val)) { | ||
resolvedProperties[key] = resolvePropertiesSegment(val, formData, deps); | ||
} else if (isSegment(val)) { | ||
resolvedProperties[key] = evaluateSegment(val.slice(2, -2), formData[key], formData, deps); | ||
} else { | ||
resolvedProperties[key] = val; | ||
} | ||
}); | ||
|
||
return resolvedProperties; | ||
}; | ||
|
||
export const resolveSchemaWithSegments = ( | ||
schema: Schema, | ||
formData: FormData, | ||
deps: Deps | ||
): Schema => { | ||
if (!isJsonSchema(schema)) { | ||
return schema; | ||
} | ||
|
||
const { properties = {}, ...rest } = schema; | ||
|
||
const resolvedProperties = Object.fromEntries( | ||
Object.entries(properties).map(([field, property]) => [ | ||
field, | ||
resolvePropertiesSegment(property, formData, deps), | ||
]) | ||
); | ||
|
||
return { | ||
...rest, | ||
properties: resolvedProperties, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters