collection-of-algos
v1.0.71
Published
A collection of mostly common algorithms, data structures with visualizations and explanations.
Downloads
20
Maintainers
Readme
A continuing collection of good to know algorithms mainly in JS
It's also a library that can be used in your projects. It contains algos that are both common and un-common.
To use this package in your project
- npm package here
Installation Open your terminal or command prompt, navigate to your project directory and run the following command:
# using npm
npm install collection-of-algos
# using yarn
yarn add collection-of-algos
This will install version latest
version of the package collection-of-algos
.
Import For CommonJS (Node.js):
const algos = require('collection-of-algos');
For ES6 syntax:
import algos from 'collection-of-algos';
Usage
algos.nativeSort();
Import specific functions
import { bubbleSort, nativeSort } from 'collection-of-algos';
Usage
let exampleArr = [5, 3, 1, 4, 2];
// create shallow copy of the array
const unsortedArr = [...exampleArr];
const obj1 = { arr: unsortedArr };
// using a promise chain
bubbleSort(obj1).then(({ arr }) => {
console.log(arr); // [1, 2, 3, 4, 5]
});
// or using async/await
const { arr: arrOfNums } = await bubbleSort(obj1);
console.log(arrOfNums); // [1, 2, 3, 4, 5]
const obj2 = {
arr: [
{ name: 'John', age: 23 },
{ name: 'Mike', age: 32 },
{ name: 'Chris', age: 11 },
{ name: 'Nick', age: 19 },
],
key: 'age',
};
const { arr: arrOfObjs } = await nativeSort(obj2);
console.log(arrOfObjs);
// [
// { name: 'Chris', age: 11 },
// { name: 'Nick', age: 19 },
// { name: 'John', age: 23 },
// { name: 'Mike', age: 32 },
// ]
In this code, bubbleSort
and nativeSort
are specific functions imported from the collection-of-algos
package. bubbleSort
is a sorting algorithm that's being applied to an array of numbers, while nativeSort
is used to sort an array of objects by a specific key.
Remember, before using await
, you should ensure that your code is inside an async
function. The await
operator is used to wait for a Promise. It can only be used inside an async
function.
Work with this repo locally
In your terminal
# install dependencies
yarn
# serve the app
yarn dev
Running unit tests
# run all tests
yarn test
# for coverage report
yarn coverage
local server to test functions locally (Ctrl-C to stop)
Connect with me
/docs-dist (The visualizer codebase)
- Currently contains the build dist for the documentation folder which is currently hidden.
- Once it's in a good place I will makde it public.
Todos
- [ ] Think about separating each algo into its own page
- [ ] Add notes and drawings for each algo
- [ ] Unit the helper functions
- [ ] Docker support
Using Docker (WIP parcel watch is not working)
# build the docker image
docker-compose build
# serve the app
docker-compose up
# Stop & remove the container
docker-compose down