All files / src/Content Types.ts

100% Statements 12/12
100% Branches 23/23
100% Functions 6/6
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60            1x                                   1x 14458x             1x 9312x             1x 42x             1x 5141x             1x 36x    
/**
 * @module Content
 */ /** */
 
import { DeferredObject } from '../ComplexTypes';
import { IContent } from './IContent';
import { ContentInternal } from './index';
import { ISavedContent } from './ISavedContent';
 
/**
 * Generic type alias for a Content representation. It's possible that a content of this type has not been saved yet to the Repository.
 */
export type Content<T extends IContent = any> = ContentInternal<T> & T;
 
/**
 * Generic type alias for a saved Content representation. Saved contents *have* an Id, Path and a Name property
 */
 
export type SavedContent<T extends IContent = any> = ContentInternal<T> & T & ISavedContent;
 
/**
 * Typeguard that determines if the specified Object is a DeferredObject
 * @param fieldObject The object that needs to be checked
 */
export const isDeferred = (fieldObject: any): fieldObject is DeferredObject => {
    return fieldObject && fieldObject.__deferred && fieldObject.__deferred.uri && fieldObject.__deferred.uri.length > 0 || false;
};
 
/**
 * Typeguard that determines if the specified Object is an IContentOptions instance
 * @param object The object that needs to be checked
 */
export const isIContent = (object: any): object is IContent => {
    return object && object.Id && object.Path && object.Type && object.Type.length > 0 || false;
};
 
/**
 * Typeguard that determines if the specified Object is a Content instance
 * @param object The object that needs to be checked
 */
export const isContent = <T extends IContent = IContent>(object: any): object is ContentInternal<T> => {
    return object instanceof ContentInternal;
};
 
/**
 * Typeguard that determines if the specified Object is an IContentOptions array
 * @param {any[]} objectList The object that needs to be checked
 */
export const isIContentList = (objectList: any[]): objectList is IContent[] => {
    return objectList && objectList.length !== undefined && objectList.find((o) => !isIContent(o)) === undefined || false;
};
 
/**
 * Typeguard that determines if the specified Content instance is saved.
 * @param { ContentInternal<T> } c The content that needs to be checked
 */
export const isSavedContent = <T extends IContent>(c: ContentInternal<T>): c is SavedContent<T> => {
    return c && isContent(c) && c.Id && c.Path && c.Path.length && c.Name && c.Name.length > 0 || false;
};