DEV Community

Cover image for Show me your unnecessary code
Itachi Uchiha
Itachi Uchiha

Posted on

Show me your unnecessary code

Hi. Somehow, I wrote this code piece in the past. Today, I don't use this code but this still exists in our project.

function isNumber(data) {
    return typeof data == 'number' && !isNaN(data)
}

function isBoolean(data) {
    return typeof data === "boolean"
}

function isFloat(data){
    return isNumber(data) && data % 1 !== 0
}

function isNull(data){
    return !data && Object.is(data, null) && data === null
}

function isUndefined(data) {
    return !data && Object.is(data, undefined) && data === undefined
}

function isEmpty(data) {
    return data === "" && data.length === 0
}

const I = {
    sware: {
        this(data) {
            return {
                is: {
                    number: () => isNumber(data),
                    float: () => isFloat(data),
                    bool: () => isBoolean(data),
                    null: () => isNull(data),
                    undefined: () => isUndefined(data),
                    empty: () => isEmpty(data)
                }
            }
        }
    }
}

const aNumericValue = 341
const aFloatValue = 3.14
const aBoolValue = false
const aNullValue = null
Enter fullscreen mode Exit fullscreen mode

You can use this like that;

const aNumericValue = 341
I.sware.this(aNumericValue).is.number()
Enter fullscreen mode Exit fullscreen mode

So, what is yours?

Top comments (5)

Collapse
 
madza profile image
Madza

Static values in pre-ES6:

Object.defineProperty(typeof global === "object" ? global : window, "static", {
    value:        10,
    enumerable:   true,
    writable:     false,
    configurable: false
})

instead now:

const static = 10;
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
 
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 :)

Collapse
 
nombrekeff profile image
Keff

Kinda weird, looking at it now :P