Skip to content

API: Preprocessors

Madeline Kahn edited this page Dec 30, 2025 · 6 revisions

API"@mkacct/cdif/preprocessors"

"@mkacct/cdif/preprocessors" contains included serializer preprocessors to make advanced configuration of the serializer more convenient. Note that the exported functions are not preprocessor functions themselves; rather, they return preprocessor functions. (See the documentation on preprocessors for more information.)

filterObjectProperties()

function filterObjectProperties(
    names: ReadonlyArray<string>
): CDIFSerializerPreprocessorFunction

Returns a preprocessor that omits all properties in an object (and any nested objects) other than those specified by names. It only omits string-keyed properties, so arrays are unaffected.

Parameters:

  • names: ReadonlyArray<string> – object properties to include

Returns:

  • CDIFSerializerPreprocessorFunction – the preprocessor function

useIntegers()

function useIntegers(): CDIFSerializerPreprocessorFunction

Returns a preprocessor that makes integer numbers (those that are evenly divisible by 1) be serialized as cDIF integers. (They won't have trailing decimal points in the resulting cDIF text.) Not recommended if input will contain numbers that may not be integers.

Returns:

  • CDIFSerializerPreprocessorFunction – the preprocessor function

assignType()

function assignType(
    type: string,
    condition: (data: {
        key: null | string | number;
        value: object;
    }) => boolean
): CDIFSerializerPreprocessorFunction

Returns a preprocessor that assigns the given type (type) to an object if the given condition (condition) is met. (If the condition is not met, it returns void, continuing to the next preprocessor as usual.)

The actual object value is unchanged. If you need to modify the value, write your own preprocessor instead of using assignType().

Parameters:

  • type: string – the type identifier to assign
  • condition: (data: {...}) => boolean – a function that takes {key, value} and returns true if and only if value should be assigned type type

Returns:

  • CDIFSerializerPreprocessorFunction – the preprocessor function

usePreprocessMethods()

function usePreprocessMethods(): CDIFSerializerPreprocessorFunction

Returns a preprocessor that delegates to the [CDIF.Symbol.preprocess] method of an object if it exists. (If not, it returns void, continuing to the next preprocessor as usual.) This allows preprocessors to be defined on objects themselves for better cohesion.

This preprocessor takes effect when the value being preprocessed is an object with a symbol-keyed property [CDIF.Symbol.preprocess] (see the documentation on CDIF.Symbol for details on symbols such as this one). The value of this property must be a valid preprocessor function (not a function that returns one; see the documentation on preprocessors for details on CDIFSerializerPreprocessorFunction). If so, the value of [CDIF.Symbol.preprocess] will be called as a preprocessor at this time.

Returns:

  • CDIFSerializerPreprocessorFunction – the preprocessor function

Clone this wiki locally