Giter VIP home page Giter VIP logo

arrow's Introduction

arrow's People

Contributors

boussadjra avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tmhao2005 jkzilla

arrow's Issues

Import functions directly or wrap them inside a module ?

@boussadjra , I need your opinion about exporting each function like in the example provided in README.

Or export an Object that lets the developers call the functions inside it.

In React we have the two options :

import React from "react"
React.createElement()

and

import {useEffect} from "react"
const effect = useEffect();

JS has a functional and OOP ( recently ) paradigm so I could get exactly the decision behind each architecture.

In Java, I was doing this by defining the static functions, factory functions, and instantiated functions... The concept was clean but JS behavior confuses me.

What's your opinion?

How to avoid duplicated methods unit tests ?

Reproduction

Source Category Suggestion Description
Duplicated snippets Refactoring passString() & passNumber() are duplicated in many unit tests

Description

In null.spec.js & and number.spec.js there are duplicated methods in many positions :

it("should throw error if the parameter is not an array", () => {
        function passNumber() {
            ArrayNull.hasPairNull(0);
        }
        function passString() {
            ArrayNull.hasPairNull("item");
        }
        expect(passNumber).toThrowError("The parameter should be an array");
        expect(passString).toThrowError("The parameter should be an array");
    }); 

How can I refactor the code to have only one reusable function or code snippet by many unit tests scripts ?

How to find symetric differencebetween several array using `filter` and `reduce`?

Steps to reproduce:

Targeted file: difference. js

Description:

The goal of this function is to find the asymmetric difference between two or more arrays. However, I found a silent bug when I was adding more unit tests.

Current implementation

export const difference = (...arrays) => {
    if (arrays.length === 1) return arrays[0]
    const args = Array.from([...arrays])
    let initArray = args[0];
    let differentElements = new Set();
    args.forEach(array => args.indexOf(array) !== 0 && isArray(array) &&
        array.forEach(value => !initArray.includes(value) && differentElements.add(value)))
    return Array.from(differentElements)
}

New implementation

export const diff = (arr1, arr2) => {
	return [
		...arr1.filter((item) => !arr2.includes(item)),
		...arr2.filter((item) => !arr1.includes(item)),
	];
};

Expected failed tests:

	it('should return an array with the uncommon elements between arrays ', () => {

               expect(diff([1, 2], [1, 2, 5])).toStrictEqual([5]); // passed

		expect(diff([1, 2], [1, 2, 3], [1, 2, 3, 4])).toStrictEqual([4]); // failed

		expect(diff(['a', 'b'], ['b', 'a'], ['a', 'b', 'c'])).toStrictEqual(['c']); // failed

		expect(diff([null], [undefined, null], [undefined, null, 'Ala'])).toStrictEqual(['Ala']); //failed

		expect(diff(['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],['diorite', 'andesite', 'grass', 'dirt', 'dead shrub'])
		).toStrictEqual(['pink wool']); // failed
	});

	it('should return an empty array ', () => {
		expect(diff([1, 2], [1, 2], [1, 2])).toStrictEqual([]); //failed
	});

	it('should return the original array ', () => {
		expect(diff([1, 2])).toStrictEqual([1, 2]); //failed
	});

How to remove duplicated if in __throwError function ?

Reproduction

Source Category Suggestion Description
Duplicated snippets Refactoring if(expression){ duplicated code } are duplicated in __throwError() function

Description

In null.spec.js there are duplicated if with the same functions : passString() , passNumber() ,passEmptyArray(),passUndefinedOrNull() :

function __throwError(func) {
    if (func.length === 1) {

        function passNumber() {
            func(0);
        }

        function passString() {
            func("item");
        }

        function passEmptyArray() {
            func([]);
        }

        function passUndefinedOrNull() {
            func(undefined || null);
        }

        expect(passNumber).toThrowError("The parameter should be an array");
        expect(passString).toThrowError("The parameter should be an array");
        expect(passEmptyArray).toThrowError("The array is empty");
        expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");

    }

    if (func.length === 2) {

        function passNumber() {
            func(0, 1);
        }

        function passString() {
            func("item", 1);
        }

        function passEmptyArray() {
            func([], 1);
        }

        function passUndefinedOrNull() {
            func(undefined || null, 1);
        }

        expect(passNumber).toThrowError("The parameter should be an array");
        expect(passString).toThrowError("The parameter should be an array");
        expect(passEmptyArray).toThrowError("The array is empty");
        expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");

    }
}

How to export module to one index.js file ?

@boussadjra , I'm struggling to export the modules to one global file index.js then create a build setup from it.

please check create-build-setup-branch and see how I created index.js for each folder ( has, math..) then create one index.js that import module from these indexes then run npm run build to build the setup.

The problem is I could not import the functions when I want to use the module for production.

Need to implement groupBy function

Need to implement a function that looks like the sql group by statement in which we indicate the field to group by it and an aggregate function such as sum, avg, count, min and max , lets suppose that we have the following array :

let scores=[
  {
    "date": "2020-08-04",
    "time": "8:32",
    "score": 2
  },
  {
    "date": "2020-08-04",
    "time": "8:45",
    "score": 1
  },
  {
    "date": "2020-08-04",
    "time": "11:15",
    "score": 4
  },
  {
    "date": "2020-08-06",
    "time": "9:52",
    "score": 7
  },
  {
    "date": "2020-08-06",
    "time": "15:42",
    "score": 3
  },
  {
    "date": "2020-08-09",
    "time": "8:57",
    "score": 1
  },
  {
    "date": "2020-08-09",
    "time": "18:26",
    "score": 5
  }
]

expected result after applying the function groupBy('date','sum','score') :

 [
    {
        date:'2020-08-04',
        score :7
    },
    {
        date:'2020-08-06',
        score:10
    },
    {
        date:'2020-08-09',
        score:6
    }]

or the other result by applying groupBy('date','count','score') or groupBy('date','count','*') :

 [
    {
        date:'2020-08-04',
        count:3
    },
    {
        date:'2020-08-06',
        count:2
    },
    {
        date:'2020-08-09',
        count:2
    }]

How to structure Node | JavaScript projects ?

Reproduction

Source Category Description
Architecture I need to create a project structure that follows the Javascript Projects Standards

Description

  • assets/ For images

  • doc/ For documentation files

  • dist/ For compiled scripts

  • coverage For testing coverage ( Auto-generated )

  • examples For code examples

  • specs For unit testing

  • src For manipulated code

  • .babelrc For Babel Configuration

  • .eslintignore For Eslint Configuration

  • .eslintrc.json For Eslint Configuration

  • gitignore For git Configuration

  • npmignore For npm Configuration

  • .jestconfig For Jest Configuration

  • .webpack.dev.config For Webpack Configuration

  • .webpack.prod.config For Webpack Configuration

Did I miss a directory or a configuration ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.