M
M
Max Zhukov2018-03-14 22:47:41
JavaScript
Max Zhukov, 2018-03-14 22:47:41

How to make chaining for your class?

There is a customArrayFunctions class,
the chain method works as it should, but the functions are repeated, so after calling chain, I need to take functions from the main class like customArrayFunctions.chain([1,2,3]).take(1) take function to take from customArrayFunctions and an array to be substituted immediately, and you need to specify only n

"use strict";
var customArrayFunctions = (function() {
    function isArrayNumber(arr) {
        for (var i = 0; i < arr.length; i++) {
            if (typeof arr[i] != "number") {
                return false;
            }
        }
        return true;
    }

    function take(arr, n) {
        if (Array.isArray(arr)) {
            return arr.slice(0, n);
        }
    }

    function skip(arr, n) {
        if (Array.isArray(arr)) {
            return arr.slice(n, arr.length);
        }
    }

    function map(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length,
                newarr = [];
            for (var i = 0; i < length; i = i + 1) {
                newarr.push(callback(i, arr[i]));
            }
            return newarr;
        }
    }

    function reduce(arr, callback, val) {
        if (Array.isArray(arr) && isArrayNumber(arr)) {
            var length = arr.length,
                newarr = [];
            for (var i = 0; i < length; i = i + 1) {
                newarr.push(callback(i, arr[i]) - val);
            }
            return newarr;
        }
    }

    function filter(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length,
                newarr = [];
            for (var i = 0; i < length; i = i + 1) {
                if (callback(i, arr[i])) {
                    newarr.push(arr[i]);
                }
            }
            return newarr;
        }
    }

    function foreach(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length;
            for (var i = 0; i < length; i = i + 1) {
                arr[i] = callback(i, arr[i]) ? callback(i, arr[i]) : arr[i];
            }
        }
    }
    function chain(arr) {
        return {
            take: function(n) {
                arr = arr.slice(0, n);
                return this;
            },
            skip: function(n) {
                arr = arr.slice(n, arr.length);
                return this;
            },
            map: function(callback) {
                var length = arr.length,
                    newarr = [];
                for (var i = 0; i < length; i = i + 1) {
                    newarr.push(callback(i, arr[i]));
                }
                arr = newarr;
                return this;

            },
            reduce: function(callback, val) {
                var length = arr.length,
                    newarr = [];
                for (var i = 0; i < length; i = i + 1) {
                    newarr.push(callback(i, arr[i]) - val);
                }
                arr = newarr;
                return this;

            },
            filter: function(callback) {
                var length = arr.length,
                    newarr = [];
                for (var i = 0; i < length; i = i + 1) {
                    if (callback(i, arr[i])) {
                        newarr.push(arr[i]);
                    }
                }
                arr = newarr;
                return this;
            },
            value: function() {
                return arr;
            }
        };
    }
    return {
        take: take,
        skip: skip,
        map: map,
        reduce: reduce,
        filter: filter,
        foreach: foreach,
        chain: chain
    };
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-15
@MaksZhukov

There is such an option: chain will return a Proxy with a get-trap in which we check the name of the requested property, and if this is one of the methods, then we return this method wrapped in another Proxy, where there will be an apply-trap with a method call and wrapping the result in a chain . Like so .
You can also look into the sources of lodash, for example, and see how chaining is implemented there (in a different way, yes).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question