Rename deserialize()
to deserializeAndSanitize()
https://github.com/theatre-js/theatre/pull/118#discussion_r846631233
This commit is contained in:
parent
8aff8ba86d
commit
65f9f1c850
6 changed files with 20 additions and 20 deletions
|
@ -109,7 +109,7 @@ export const compound = <Props extends IShorthandCompoundProps>(
|
|||
[propTypeSymbol]: 'TheatrePropType',
|
||||
label: opts.label,
|
||||
default: mapValues(sanitizedProps, (p) => p.default) as $IntentionalAny,
|
||||
deserialize: (json: unknown) => {
|
||||
deserializeAndSanitize: (json: unknown) => {
|
||||
if (typeof json !== 'object' || !json) return undefined
|
||||
if (deserializationCache.has(json)) {
|
||||
return deserializationCache.get(json)
|
||||
|
@ -123,7 +123,7 @@ export const compound = <Props extends IShorthandCompoundProps>(
|
|||
let atLeastOnePropWasDeserialized = false
|
||||
for (const [key, propConfig] of Object.entries(sanitizedProps)) {
|
||||
if (Object.prototype.hasOwnProperty.call(json, key)) {
|
||||
const deserializedSub = propConfig.deserialize(
|
||||
const deserializedSub = propConfig.deserializeAndSanitize(
|
||||
(json as $IntentionalAny)[key] as unknown,
|
||||
)
|
||||
if (deserializedSub != null) {
|
||||
|
@ -262,7 +262,7 @@ export const number = (
|
|||
nudgeMultiplier:
|
||||
typeof opts.nudgeMultiplier === 'number' ? opts.nudgeMultiplier : 1,
|
||||
interpolate: _interpolateNumber,
|
||||
deserialize: numberDeserializer(opts.range),
|
||||
deserializeAndSanitize: numberDeserializer(opts.range),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -332,7 +332,7 @@ export const rgba = (
|
|||
[propTypeSymbol]: 'TheatrePropType',
|
||||
label: opts.label,
|
||||
interpolate: _interpolateRgba,
|
||||
deserialize: _sanitizeRgba,
|
||||
deserializeAndSanitize: _sanitizeRgba,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -426,7 +426,7 @@ export const boolean = (
|
|||
[propTypeSymbol]: 'TheatrePropType',
|
||||
label: opts.label,
|
||||
interpolate: opts.interpolate ?? leftInterpolate,
|
||||
deserialize: _ensureBoolean,
|
||||
deserializeAndSanitize: _ensureBoolean,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -483,7 +483,7 @@ export const string = (
|
|||
[propTypeSymbol]: 'TheatrePropType',
|
||||
label: opts.label,
|
||||
interpolate: opts.interpolate ?? leftInterpolate,
|
||||
deserialize: _ensureString,
|
||||
deserializeAndSanitize: _ensureString,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -540,7 +540,9 @@ export function stringLiteral<Opts extends {[key in string]: string}>(
|
|||
as: opts.as ?? 'menu',
|
||||
label: opts.label,
|
||||
interpolate: opts.interpolate ?? leftInterpolate,
|
||||
deserialize(json: unknown): undefined | Extract<keyof Opts, string> {
|
||||
deserializeAndSanitize(
|
||||
json: unknown,
|
||||
): undefined | Extract<keyof Opts, string> {
|
||||
if (typeof json !== 'string') return undefined
|
||||
if (Object.prototype.hasOwnProperty.call(options, json)) {
|
||||
return json as $IntentionalAny
|
||||
|
@ -584,13 +586,13 @@ export interface IBasePropType<
|
|||
label: string | undefined
|
||||
default: ValueType
|
||||
/**
|
||||
* Each prop config has a `deserialize()` function that deserializes
|
||||
* Each prop config has a `deserializeAndSanitize()` function that deserializes and sanitizes
|
||||
* any js value into one that is acceptable by this prop config, or `undefined`.
|
||||
*
|
||||
* The `DeserializeType` is usually equal to `ValueType`. That is the case with
|
||||
* all simple prop configs, such as `number`, `string`, or `rgba`. However, composite
|
||||
* configs such as `compound` or `enum` may deserialize into a partial value. For example,
|
||||
* a prop config of `t.compound({x: t.number(0), y: t.number(0)})` may deserialize into `{x: 10}`.
|
||||
* configs such as `compound` or `enum` may deserialize+sanitize into a partial value. For example,
|
||||
* a prop config of `t.compound({x: t.number(0), y: t.number(0)})` may deserialize+sanitize into `{x: 10}`.
|
||||
* This behavior is used by {@link SheetObject.getValues} to replace the missing sub-props
|
||||
* with their default value.
|
||||
*
|
||||
|
@ -601,7 +603,7 @@ export interface IBasePropType<
|
|||
* Additionally, returning an `undefined` allows {@link SheetObject.getValues} to
|
||||
* replace the `undefined` with the default value of that prop.
|
||||
*/
|
||||
deserialize: (json: unknown) => undefined | DeserializeType
|
||||
deserializeAndSanitize: (json: unknown) => undefined | DeserializeType
|
||||
}
|
||||
|
||||
interface ISimplePropType<LiteralIdentifier extends string, ValueType>
|
||||
|
|
|
@ -19,7 +19,7 @@ export type InterpolationTriple = {
|
|||
// low-hanging fruit for perf optimization.
|
||||
// It can be improved by:
|
||||
// 1. Not creating a new InterpolationTriple object on every change
|
||||
// 2. Caching propConfig.deserialize(value)
|
||||
// 2. Caching propConfig.deserializeAndSanitize(value)
|
||||
|
||||
export default function interpolationTripleAtPosition(
|
||||
trackP: Pointer<TrackData | undefined>,
|
||||
|
|
|
@ -152,7 +152,7 @@ export default class SheetObject implements IdentityDerivationProvider {
|
|||
pathToProp,
|
||||
)! as Extract<PropTypeConfig, {interpolate: $IntentionalAny}>
|
||||
|
||||
const deserialize = propConfig.deserialize
|
||||
const deserializeAndSanitize = propConfig.deserializeAndSanitize
|
||||
const interpolate =
|
||||
propConfig.interpolate! as Interpolator<$IntentionalAny>
|
||||
|
||||
|
@ -161,7 +161,7 @@ export default class SheetObject implements IdentityDerivationProvider {
|
|||
|
||||
if (!triple) return valsAtom.setIn(pathToProp, undefined)
|
||||
|
||||
const leftDeserialized = deserialize(triple.left)
|
||||
const leftDeserialized = deserializeAndSanitize(triple.left)
|
||||
|
||||
const left =
|
||||
typeof leftDeserialized === 'undefined'
|
||||
|
@ -171,7 +171,7 @@ export default class SheetObject implements IdentityDerivationProvider {
|
|||
if (triple.right === undefined)
|
||||
return valsAtom.setIn(pathToProp, left)
|
||||
|
||||
const rightDeserialized = deserialize(triple.right)
|
||||
const rightDeserialized = deserializeAndSanitize(triple.right)
|
||||
const right =
|
||||
typeof rightDeserialized === 'undefined'
|
||||
? propConfig.default
|
||||
|
|
|
@ -102,7 +102,7 @@ export default class SheetObjectTemplate {
|
|||
) || {}
|
||||
|
||||
const config = val(this._config.pointer)
|
||||
const deserialized = config.deserialize(json) || {}
|
||||
const deserialized = config.deserializeAndSanitize(json) || {}
|
||||
return deserialized
|
||||
}),
|
||||
)
|
||||
|
|
|
@ -43,9 +43,7 @@ export function valueInProp<PropConfig extends PropTypeConfig_AllNonCompounds>(
|
|||
): PropConfig extends IBasePropType<$IntentionalAny, $IntentionalAny, infer T>
|
||||
? T
|
||||
: never {
|
||||
const deserialize = propConfig.deserialize
|
||||
|
||||
const sanitizedVal = deserialize(value)
|
||||
const sanitizedVal = propConfig.deserializeAndSanitize(value)
|
||||
if (typeof sanitizedVal === 'undefined') {
|
||||
return propConfig.default
|
||||
} else {
|
||||
|
|
|
@ -105,7 +105,7 @@ export default function createTransactionPrivateApi(
|
|||
}
|
||||
|
||||
const deserialized = cloneDeepSerializable(
|
||||
propConfig.deserialize(value),
|
||||
propConfig.deserializeAndSanitize(value),
|
||||
)
|
||||
if (typeof deserialized === 'undefined') {
|
||||
throw new Error(
|
||||
|
|
Loading…
Reference in a new issue