DEV Community

Discussion on: Show me your unnecessary code

Collapse
 
nombrekeff profile image
Keff

Nice, I'll add a bit. It's a little experiment I did back in the day and now I think is not very useful:

const ffor = {
    _baseFor: () => ({
        _pipes: [],
        _coupled: [],
        pipe(...fns) {
            this._pipes.push(...fns);
            return this;
        },
        merge(fforInstance) {
            this._pipes.push(...fforInstance._pipes);
            return this;
        }
    }),
    get of() {
        return {
            ...this._baseFor(),
            with(values = []) {
                let newValues = [];
                for (let val of values) {
                    for (let pipe of this._pipes) {
                        val = pipe(val, values) || val;
                    }
                    if (val) newValues.push(val);
                }

                return newValues;
            }
        }
    },
    get in() {
        return {
            ...this._baseFor(),
            with(values = []) {
                let newValues = [];
                for (let key in values) {
                    let val = values[key];
                    for (let pipe of this._pipes) {
                        val = pipe(key, values[key], values) || val;
                    }
                    if (val) newValues.push(val);
                }

                return newValues;
            }
        }
    },
};

You can use it as follows:

// For of
let logItems =
    ffor.of
        .pipe((item) => console.log('item is: ' + item));

let forOf =
    ffor.of
        .pipe((item) => item + 1);

let result = forOf.merge(logItems).with([1, 2, 3, 4]);
console.log(result);


// For in
let forIn =
    ffor.in
        .pipe((key, value) => {
            console.log(key + ' is: ' + value)
            return { key: key, value: value };
        });

let result2 = forIn.with({ name: 'Manolo', lastname: 'Edge' });
console.log(result2);

This outputs the following:

Collapse
 
nombrekeff profile image
Keff

Kinda weird, looking at it now :P

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Omg. I like that :P Should I use this :P

Collapse
 
nombrekeff profile image
Keff

Feel free, I was just messing around, although it might have some cool usage. Let me know if you end up using it :)