Rename deserialize() to deserializeAndSanitize()

https://github.com/theatre-js/theatre/pull/118#discussion_r846631233
This commit is contained in:
Aria Minaei 2022-04-10 19:47:59 +02:00
parent 8aff8ba86d
commit 65f9f1c850
6 changed files with 20 additions and 20 deletions

View file

@ -109,7 +109,7 @@ export const compound = <Props extends IShorthandCompoundProps>(
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts.label, label: opts.label,
default: mapValues(sanitizedProps, (p) => p.default) as $IntentionalAny, default: mapValues(sanitizedProps, (p) => p.default) as $IntentionalAny,
deserialize: (json: unknown) => { deserializeAndSanitize: (json: unknown) => {
if (typeof json !== 'object' || !json) return undefined if (typeof json !== 'object' || !json) return undefined
if (deserializationCache.has(json)) { if (deserializationCache.has(json)) {
return deserializationCache.get(json) return deserializationCache.get(json)
@ -123,7 +123,7 @@ export const compound = <Props extends IShorthandCompoundProps>(
let atLeastOnePropWasDeserialized = false let atLeastOnePropWasDeserialized = false
for (const [key, propConfig] of Object.entries(sanitizedProps)) { for (const [key, propConfig] of Object.entries(sanitizedProps)) {
if (Object.prototype.hasOwnProperty.call(json, key)) { if (Object.prototype.hasOwnProperty.call(json, key)) {
const deserializedSub = propConfig.deserialize( const deserializedSub = propConfig.deserializeAndSanitize(
(json as $IntentionalAny)[key] as unknown, (json as $IntentionalAny)[key] as unknown,
) )
if (deserializedSub != null) { if (deserializedSub != null) {
@ -262,7 +262,7 @@ export const number = (
nudgeMultiplier: nudgeMultiplier:
typeof opts.nudgeMultiplier === 'number' ? opts.nudgeMultiplier : 1, typeof opts.nudgeMultiplier === 'number' ? opts.nudgeMultiplier : 1,
interpolate: _interpolateNumber, interpolate: _interpolateNumber,
deserialize: numberDeserializer(opts.range), deserializeAndSanitize: numberDeserializer(opts.range),
} }
} }
@ -332,7 +332,7 @@ export const rgba = (
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts.label, label: opts.label,
interpolate: _interpolateRgba, interpolate: _interpolateRgba,
deserialize: _sanitizeRgba, deserializeAndSanitize: _sanitizeRgba,
} }
} }
@ -426,7 +426,7 @@ export const boolean = (
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts.label, label: opts.label,
interpolate: opts.interpolate ?? leftInterpolate, interpolate: opts.interpolate ?? leftInterpolate,
deserialize: _ensureBoolean, deserializeAndSanitize: _ensureBoolean,
} }
} }
@ -483,7 +483,7 @@ export const string = (
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
label: opts.label, label: opts.label,
interpolate: opts.interpolate ?? leftInterpolate, 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', as: opts.as ?? 'menu',
label: opts.label, label: opts.label,
interpolate: opts.interpolate ?? leftInterpolate, 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 (typeof json !== 'string') return undefined
if (Object.prototype.hasOwnProperty.call(options, json)) { if (Object.prototype.hasOwnProperty.call(options, json)) {
return json as $IntentionalAny return json as $IntentionalAny
@ -584,13 +586,13 @@ export interface IBasePropType<
label: string | undefined label: string | undefined
default: ValueType 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`. * 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 * The `DeserializeType` is usually equal to `ValueType`. That is the case with
* all simple prop configs, such as `number`, `string`, or `rgba`. However, composite * 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, * 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 into `{x: 10}`. * 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 * This behavior is used by {@link SheetObject.getValues} to replace the missing sub-props
* with their default value. * with their default value.
* *
@ -601,7 +603,7 @@ export interface IBasePropType<
* Additionally, returning an `undefined` allows {@link SheetObject.getValues} to * Additionally, returning an `undefined` allows {@link SheetObject.getValues} to
* replace the `undefined` with the default value of that prop. * 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> interface ISimplePropType<LiteralIdentifier extends string, ValueType>

View file

@ -19,7 +19,7 @@ export type InterpolationTriple = {
// low-hanging fruit for perf optimization. // low-hanging fruit for perf optimization.
// It can be improved by: // It can be improved by:
// 1. Not creating a new InterpolationTriple object on every change // 1. Not creating a new InterpolationTriple object on every change
// 2. Caching propConfig.deserialize(value) // 2. Caching propConfig.deserializeAndSanitize(value)
export default function interpolationTripleAtPosition( export default function interpolationTripleAtPosition(
trackP: Pointer<TrackData | undefined>, trackP: Pointer<TrackData | undefined>,

View file

@ -152,7 +152,7 @@ export default class SheetObject implements IdentityDerivationProvider {
pathToProp, pathToProp,
)! as Extract<PropTypeConfig, {interpolate: $IntentionalAny}> )! as Extract<PropTypeConfig, {interpolate: $IntentionalAny}>
const deserialize = propConfig.deserialize const deserializeAndSanitize = propConfig.deserializeAndSanitize
const interpolate = const interpolate =
propConfig.interpolate! as Interpolator<$IntentionalAny> propConfig.interpolate! as Interpolator<$IntentionalAny>
@ -161,7 +161,7 @@ export default class SheetObject implements IdentityDerivationProvider {
if (!triple) return valsAtom.setIn(pathToProp, undefined) if (!triple) return valsAtom.setIn(pathToProp, undefined)
const leftDeserialized = deserialize(triple.left) const leftDeserialized = deserializeAndSanitize(triple.left)
const left = const left =
typeof leftDeserialized === 'undefined' typeof leftDeserialized === 'undefined'
@ -171,7 +171,7 @@ export default class SheetObject implements IdentityDerivationProvider {
if (triple.right === undefined) if (triple.right === undefined)
return valsAtom.setIn(pathToProp, left) return valsAtom.setIn(pathToProp, left)
const rightDeserialized = deserialize(triple.right) const rightDeserialized = deserializeAndSanitize(triple.right)
const right = const right =
typeof rightDeserialized === 'undefined' typeof rightDeserialized === 'undefined'
? propConfig.default ? propConfig.default

View file

@ -102,7 +102,7 @@ export default class SheetObjectTemplate {
) || {} ) || {}
const config = val(this._config.pointer) const config = val(this._config.pointer)
const deserialized = config.deserialize(json) || {} const deserialized = config.deserializeAndSanitize(json) || {}
return deserialized return deserialized
}), }),
) )

View file

@ -43,9 +43,7 @@ export function valueInProp<PropConfig extends PropTypeConfig_AllNonCompounds>(
): PropConfig extends IBasePropType<$IntentionalAny, $IntentionalAny, infer T> ): PropConfig extends IBasePropType<$IntentionalAny, $IntentionalAny, infer T>
? T ? T
: never { : never {
const deserialize = propConfig.deserialize const sanitizedVal = propConfig.deserializeAndSanitize(value)
const sanitizedVal = deserialize(value)
if (typeof sanitizedVal === 'undefined') { if (typeof sanitizedVal === 'undefined') {
return propConfig.default return propConfig.default
} else { } else {

View file

@ -105,7 +105,7 @@ export default function createTransactionPrivateApi(
} }
const deserialized = cloneDeepSerializable( const deserialized = cloneDeepSerializable(
propConfig.deserialize(value), propConfig.deserializeAndSanitize(value),
) )
if (typeof deserialized === 'undefined') { if (typeof deserialized === 'undefined') {
throw new Error( throw new Error(