Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
3
Indexable
Never
const { type } = require("os"); // what is that?

/**
 typeof value === "number" ||
      typeof value === "string" ||
      value === false ||
      value === null ||
      value === undefined ||

This is used more than once. How do you avoid repetition?
*/

// typeof value === "object" -> used more than once - avoid repetition
// create methods that handle checking if it is a primitive or not. Then use them in the class.
// Array.isArray(input) -> extract as a method

class TypesHelper {
  constructor() {}

  isPrimitive(value) {
    if (
      typeof value === "number" ||
      typeof value === "string" ||
      value === false ||
      value === null ||
      value === undefined ||
      typeof value === "symbol"
    ) {
      return true;
    }
    if (typeof value === "object") {
      return false;
    }
  }

  isReference(value) {
    if (
      typeof value === "number" ||
      typeof value === "string" ||
      value === false ||
      value === null ||
      value === undefined
    ) {
      return false;
    }
    if (typeof value === "object" || typeof value === "function") {
      return true;
    }

    if (value instanceof Date) {
      return true;
    }
  }

  cloneValue(input) {
    if (Array.isArray(input)) {
      return [...input];
    }

    if (typeof input === "object") {
      return { ...input };
    }

    let obj = {};

    for (let keys in input) {
      obj[keys] = this.cloneValue(input[keys]); // ASK IOSIF - let's skip this for now. It is more advanced.
    }
    return obj;
  }

  areValuesEqual(x, y) {
    if (Number.isNaN(x) && Number.isNaN(y)) {
      return true;
    }

    return x === y;
  }
}

module.exports = TypesHelper;