@freckleface/golembase-js-transformations
Version:
A small library of JavaScript object transformations for golem-base
69 lines (68 loc) • 3.09 kB
TypeScript
import { Annotation } from "golem-base-sdk";
/**
* An interface representing a collection of annotations, separated by type.
*/
export interface Annotations {
stringAnnotations: Annotation<string>[];
numericAnnotations: Annotation<number>[];
}
/**
* Transforms a plain JavaScript object (POJO) into an Annotations object.
* It separates properties into numeric types and converts all other types
* into string annotations.
* @param myobj The plain JavaScript object to transform.
* @returns An Annotations object.
*/
export declare const transformPOJOToAnnotations: (myobj: Omit<any, "entityKey">) => Annotations;
/**
* Transforms an Annotations object back into a single plain JavaScript object.
* @param annotations An object containing arrays of string and numeric annotations.
* @param convertBools A flag to determine if string 'true'/'false' should be converted to booleans.
* @returns A plain JavaScript object.
*/
export declare const transformAnnotationsToPOJO: (annotations: Annotations, convertBools?: boolean) => {
[key: string]: string | number | boolean;
};
/**
* Transforms a POJO with array values into an Annotations object.
* Each array is converted into a comma-separated string, with optional sorting.
* @param listObj The object with array values.
* @param sort A flag to determine if the arrays should be sorted before joining.
* @returns An Annotations object.
*/
export declare const transformListPOJOToAnnotations: (listObj: {
[key: string]: any[];
}, sort?: boolean) => Annotations;
/**
* Transforms an Annotations object with comma-separated strings back into a POJO with arrays.
* It dynamically detects if the array items are numeric or boolean.
* @param annotations The Annotations object.
* @param convertBools A flag to determine if string 'true'/'false' should be converted to booleans.
* @param sort A flag to determine if the resulting arrays should be sorted.
* @returns A POJO with array values.
*/
export declare const transformAnnotationsToListPOJO: (annotations: Annotations, convertBools?: boolean, sort?: boolean) => {
[key: string]: (string | number | boolean)[];
};
/**
* Prepares a JavaScript object for storage by separating it into a main data payload
* consisting of the stringified form of the JavaScript object
* and a set of searchable index annotations.
* @param sourceObject The object to transform.
* @param indexes An optional array of top-level keys from the sourceObject to create as separate annotations for indexing.
* @returns An object containing the stringified data and the generated annotations.
*/
export declare const transformJSONToGolemNoSQL: (sourceObject: {
[key: string]: any;
}, indexes?: string[]) => {
dataAsString: string;
annotations: Annotations;
};
/**
* Reconstructs a full JavaScript object from its stringified data representation.
* @param dataAsString The stringified JSON data payload.
* @returns The reconstructed JavaScript object.
*/
export declare const transformGolemNoSQLToJSON: (dataAsString: string) => {
[key: string]: any;
};