All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.shared.clonable.ts Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
export abstract class Clonable {
    abstract clone(): T;
}

// Unlike classes, interfaces exist only at compile-time. It is not possible to do a common instanceof check
export function instanceOfClonable(obj: any): obj is Clonable {
    return 'clone' in obj;
}

export function cloneAsPossible(val: any): any {
    if (val == null) return val;

    if (typeof val === 'object') {
        if (val instanceof Array) {
            const result = [];
            val.forEach((child) => {
                result.push(cloneAsPossible(child));
            });
            return result;
        }
        if (val instanceof Map) {
            const result = new Map();
            val.forEach((value, key) => {
                result.set(cloneAsPossible(key), cloneAsPossible(value));
            });
            return result;
        }

        if (instanceOfClonable(val)) return val.clone();
    }

    return val;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy