-
Notifications
You must be signed in to change notification settings - Fork 0
API: CDIF
API › "@mkacct/cdif" › CDIF
class CDIFThe CDIF class contains all of this library's main functionality, both for parsing and serializing cDIF data.
public constructor(
cdifVersion?: number
)Creates an instance of the cDIF parser/serializer. The one optional argument is the major version of the cDIF specification by which the instance should abide. (If it is omitted, the latest version will be assumed.)
Parameters:
-
cdifVersion:number(Optional) – integer major version of cDIF to use (defaults to latest)
Throws:
-
RangeError– ifcdifVersionis invalid
public static readonly Symbol: {...}An object containing "protocol" symbols used by the cDIF API, comparable to JavaScript's built-in "well-known Symbols". These symbols are as follows:
-
CDIF.Symbol.preprocess– Key denoting a method that will be called as a serializer preprocessor function for an object when usingusePreprocessMethods(). (Note: The serializer does not look for this method by default. This symbol is only meaningful when usingusePreprocessMethods(). See that preprocessor's own documentation for more information.)
public static readonly OMIT_PROPERTY: unique symbolWhen returned from a postprocessor or preprocessor function, denotes that the property should be omitted. (See "Postprocessors and preprocessors" for more information.)
public static getCdifVersion(
cdifText: string
): number | undefinedDetermines the major version of cDIF declared by a cDIF file. This method can be used before instantiating a CDIF object, and may be useful if you're parsing a cDIF file of unknown version.
Parameters:
-
cdifText:string– a valid cDIF string (read from a file or otherwise)
Returns:
-
number | undefined– the cDIF major version, orundefinedif no "cDIF" directive is present
Throws:
-
CDIFDirectiveError– if an initial "cDIF" directive is present but has an invalid version string
public parse(
cdifText: string,
parserOptions?: CDIFParserOptions
): unknownConverts a cDIF string to a JavaScript value. The cDIF string may (or may not) include # directives and other file format elements. You can pass a CDIFParserOptions object (see "Configuration: Parser" for details) to customize the parser behavior.
Parameters:
-
cdifText:string– a valid cDIF string (read from a file or otherwise) -
parserOptions:CDIFParserOptions(Optional) – customize the behavior of the parser
Returns:
-
unknown–cdifTextconverted to a JS value (usually an object or array)
Throws:
-
CDIFError– if a postprocessor function tries to omit the root value -
CDIFSyntaxError– if the input has invalid syntax -
CDIFDirectiveError– if an unknown directive is encountered, or a directive is used incorrectly -
CDIFDirectiveError– ifallowUnexpectedVersionStringis false and the "cDIF" directive is used with an unexpected version string -
CDIFReferenceError– if a component reference is not defined -
CDIFReferenceError– if a circular component reference is encountered -
CDIFTypeError– if a spread expression is used with a component of the wrong type
public serialize(
value: unknown,
serializerOptions?: CDIFSerializerOptions
): stringConverts a JavaScript value to a cDIF data string. It will not contain the # cDIF directive or any other file format elements. You can pass a CDIFSerializerOptions object (see "Configuration: Serializer" for details) to customize the serializer behavior.
Parameters:
-
value:unknown– a JS value (usually an object or array) to be converted -
serializerOptions:CDIFSerializerOptions(Optional) – customize the behavior of the serializer
Returns:
-
string–valueconverted to a cDIF data string
Throws:
-
CDIFError– if any value is aCDIFPrimitiveValuecreated with the wrong cDIF version -
CDIFError– if a preprocessor function tries to omit the root value, or, in strict mode, a collection value -
CDIFSyntaxError– if an object property name is not a valid cDIF name -
CDIFSyntaxError– if a preprocessor function returns a type name that is not a valid cDIF type name -
CDIFTypeError– if a circular reference is encountered -
CDIFTypeError– in strict mode, if any value is of a disallowed type
public serializeFile(
value: unknown,
serializerOptions?: CDIFSerializerOptions
fileOptions?: CDIFFileOptions
): stringConverts a JavaScript value to a cDIF file string (suitable for writing to a file). It may contain various file format elements (such as the # cDIF directive) depending on a provided CDIFFileOptions object (see "Configuration: File formatter" for details). As with CDIF.serialize(), you can also pass a CDIFSerializerOptions object (see "Configuration: Serializer" for details) to customize the serializer behavior.
Parameters:
-
value:unknown– a JS value (usually an object or array) to be converted -
serializerOptions:CDIFSerializerOptions(Optional) – customize the behavior of the serializer -
fileOptions:CDIFFileOptions(Optional) – customize the behavior of the file formatter
Returns:
-
string–valueconverted to a cDIF file string
Throws:
-
Error– iffileOptionsis invalid -
CDIFError– if any value is aCDIFPrimitiveValuecreated with the wrong cDIF version -
CDIFError– if a preprocessor function tries to omit the root value, or, in strict mode, a collection value -
CDIFSyntaxError– if an object property name is not a valid cDIF name -
CDIFSyntaxError– if a preprocessor function returns a type name that is not a valid cDIF type name -
CDIFTypeError– if a circular reference is encountered -
CDIFTypeError– in strict mode, if any value is of a disallowed type
public createPrimitiveValue(
cdifText: string
): CDIFPrimitiveValueCreates a CDIFPrimitiveValue instance for the primitive value represented by the provided cDIF text. (See that class's documentation for more information.)
Parameters:
-
cdifText:string– valid cDIF text representing a primitive value
Returns:
-
CDIFPrimitiveValue– a cDIF primitive value object representation of the primitive value
Throws:
-
CDIFSyntaxError– ifcdifTextis not a valid cDIF primitive value