me-ventures / microservice-toolkit

MIT License
3 stars 1 forks source link

add utility class #20

Closed EvaLok closed 6 years ago

EvaLok commented 6 years ago

starting simple..

export namespace Utility {
    export function extend<T, U>(first: T, second: U): T & U {
        const result = {} as T & U;

        Object.keys(first).forEach((id: string) => {
            (result as any)[id] = (first as any)[id];
        });

        for (const id in second) {
            if (!result.hasOwnProperty(id)) {
                (result as any)[id] = (second as any)[id];
            }
        }
        return result;
    }
}
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Utility;
    (function (Utility) {
        function extend(first, second) {
            var result = {};
            Object.keys(first).forEach(function (id) {
                result[id] = first[id];
            });
            for (var id in second) {
                if (!result.hasOwnProperty(id)) {
                    result[id] = second[id];
                }
            }
            return result;
        }
        Utility.extend = extend;
    })(Utility = exports.Utility || (exports.Utility = {}));
EvaLok commented 6 years ago

@MaikelH thinking about this again, a namespace is better than a class with static methods i think, similar to how there is isNullOrUndefined and so exported as functions in node's util

let me know if this looks OK to you

EvaLok commented 6 years ago

@MaikelH also, this by default has the second object not override preexisting properties of the first object; this seems unconventional (i would expect the second object's properties to override those of the first). maybe we could add an optional override or noOverride parameter to the extend function in order to address this

MaikelH commented 6 years ago

I like the idea of an namespace, makes a lot of sense. We can then use it the same way as isNullOrUndefined like you say.

Adding an extra property is good I think, by default we can set it to false.

EvaLok commented 6 years ago

so.. i actually started doing this (tried out yarn and it's great btw, so it wasn't at total waste of time), and got to thinking.. "maybe i'll have one last look around for something pre-existing and decent".. and as it turns out.. there is a good solution, that has @types with generics.. so.. think we're good :)

not only that, it's deep as opposed to shallow

https://www.npmjs.com/package/deep-extend

// Type definitions for open 0.4.1
// Project: https://github.com/unclechu/node-deep-extend
// Definitions by: rhysd <https://github.com/rhysd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/*
 * Recursive object extending.
 */
declare function deepExtend<T, U>(target: T, source: U): T & U;
declare function deepExtend<T, U, V>(target: T, source1: U, source2: V): T & U & V;
declare function deepExtend<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
declare function deepExtend(target: any, ...sources: any[]): any;
declare namespace deepExtend {}
export = deepExtend;
MaikelH commented 6 years ago

Nice, it had 9+ million downloads last month so it is probably decent 😏

EvaLok commented 6 years ago

also apparently yarn's lockfile is more reliable than npm's.. nice how everything sort of came together hehehe