Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prettier tooling #2

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "airbnb-base",
"plugins": [
"import"
]
}
"extends": [
"prettier"
],
"parserOptions": {
"sourceType": "module"
}
}
21 changes: 21 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node.js Tests

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
- run: npm install
- run: npm run prettier
- run: npm test
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# practical-algorithms

Old Possum's Repo of Practical Algorithms & Data Structures
4 changes: 4 additions & 0 deletions lists-and-trees/bst-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default class Node {
this.right = null;
}

setData(d) {
this.data = d;
}

getData() {
return this.data;
}
Expand Down
2 changes: 1 addition & 1 deletion lists-and-trees/linked-list-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export default class Node {
getNext() {
return this.next;
}
};
}
6 changes: 4 additions & 2 deletions lists-and-trees/linked-list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Node from './linked-list-node';
import Node from "./linked-list-node";

export default class LinkedList {
constructor(n) {
Expand All @@ -12,7 +12,9 @@ export default class LinkedList {
} else if (n instanceof Node) {
this.root = n;
} else {
throw new Error('LinkedList constructor was not called with a Node or Array as its argument.');
throw new Error(
"LinkedList constructor was not called with a Node or Array as its argument.",
);
}
}

Expand Down
26 changes: 13 additions & 13 deletions lists-and-trees/test/linked-list-node.spec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/* eslint-disable prefer-arrow-callback */

import { describe, it } from 'mocha';
import { describe, it } from "mocha";

import { expect } from 'chai';
import Node from '../linked-list-node';
import { expect } from "chai";
import Node from "../linked-list-node";

describe('Linked list Node class', function () {
it('should construct with passed data', function () {
const a = new Node('a');
describe("Linked list Node class", function () {
it("should construct with passed data", function () {
const a = new Node("a");

expect(a.getData()).to.equal('a');
expect(a.getData()).to.equal("a");
});

it('should allow to set next', function () {
const a = new Node('a');
const b = new Node('b');
it("should allow to set next", function () {
const a = new Node("a");
const b = new Node("b");

a.setNext(b);
expect(a.getNext().getData()).to.equal('b');
expect(a.getNext().getData()).to.equal("b");
});

it('should set next to null by default', function () {
const a = new Node('a');
it("should set next to null by default", function () {
const a = new Node("a");

a.setNext();
expect(a.getNext()).to.equal(null);
Expand Down
68 changes: 35 additions & 33 deletions lists-and-trees/test/linked-list.spec.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
/* global beforeEach */
/* eslint-disable prefer-arrow-callback */

import { describe, it } from 'mocha';
import { describe, it } from "mocha";

import { expect } from 'chai';
import LinkedList from '../linked-list';
import Node from '../linked-list-node';
import { expect } from "chai";
import LinkedList from "../linked-list";
import Node from "../linked-list-node";

describe('Linked list class', function () {
describe('constructor', function () {
it('Should throw if constructor called w/wrong argument', function () {
describe("Linked list class", function () {
describe("constructor", function () {
it("Should throw if constructor called w/wrong argument", function () {
expect(function () {
const foo = new LinkedList('foo'); // eslint-disable-line
}).to.throw('LinkedList constructor was not called with a Node or Array as its argument.');
const foo = new LinkedList("foo"); // eslint-disable-line
}).to.throw(
"LinkedList constructor was not called with a Node or Array as its argument.",
);
});

it('Should assign root if constructor called w/argument', function () {
const a = new Node('a');
it("Should assign root if constructor called w/argument", function () {
const a = new Node("a");
const foo = new LinkedList(a);
expect(foo.root).to.equal(a);

const b = new Node('b');
const b = new Node("b");
const bar = new LinkedList([b]);
expect(bar.root).to.equal(b);
});
});

describe('methods', function () {
describe("methods", function () {
let a;
let b;
let c;

beforeEach(function () {
a = new Node('a');
b = new Node('b');
c = new Node('c');
a = new Node("a");
b = new Node("b");
c = new Node("c");
});

it('should, provided with data, delete node from list', function () {
it("should, provided with data, delete node from list", function () {
const foo = new LinkedList([a, b, c]);
foo.delete('b');
expect(foo.walk()).to.deep.equal(['a', 'c']);
foo.delete("b");
expect(foo.walk()).to.deep.equal(["a", "c"]);
});

it('should, provided with data, get node from list', function () {
it("should, provided with data, get node from list", function () {
const foo = new LinkedList([a, b, c]);
expect(foo.get('b')).to.equal(b);
expect(foo.get("b")).to.equal(b);
});

it('should walk list, and return an array of data', function () {
it("should walk list, and return an array of data", function () {
const foo = new LinkedList([a, b, c]);
expect(foo.walk()).to.deep.equal(['a', 'b', 'c']);
expect(foo.walk()).to.deep.equal(["a", "b", "c"]);
});

it('should, provided with prior data, insert a node', function () {
it("should, provided with prior data, insert a node", function () {
const foo = new LinkedList([a, b, c]);
const x = new Node('x');
foo.insert('b', x);
expect(foo.walk()).to.deep.equal(['a', 'b', 'x', 'c']);
const x = new Node("x");
foo.insert("b", x);
expect(foo.walk()).to.deep.equal(["a", "b", "x", "c"]);
});

it('should push to the end of the list', function () {
it("should push to the end of the list", function () {
const foo = new LinkedList([a, b, c]);
const x = new Node('x');
const x = new Node("x");
foo.push(x);
expect(foo.walk()).to.deep.equal(['a', 'b', 'c', 'x']);
expect(foo.walk()).to.deep.equal(["a", "b", "c", "x"]);
});

it('should unshift to the front of the list', function () {
it("should unshift to the front of the list", function () {
const foo = new LinkedList([a, b, c]);
const x = new Node('x');
const x = new Node("x");
foo.unshift(x);
expect(foo.walk()).to.deep.equal(['x', 'a', 'b', 'c']);
expect(foo.walk()).to.deep.equal(["x", "a", "b", "c"]);
});
});
});
2 changes: 1 addition & 1 deletion misc/binary-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export default function binarySearch(arr, item) {
}
}
return -1;
};
}
13 changes: 8 additions & 5 deletions misc/luhn.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@
export default function luhn(number) {
let isValid = false;
// coercion
const arr = number.toString().split('').reverse();
const arr = number.toString().split("").reverse();
const mapped = arr.map((item, index) => {
item = parseInt(item, 10); // eslint-disable-line
if ((index + 1) % 2 === 0) {
item *= 2; // eslint-disable-line
}
if (item >= 10) {
// coercion
item = parseInt(item.toString().split('')[0], 10) + parseInt(item.toString().split('')[1], 10); // eslint-disable-line
item =
parseInt(item.toString().split("")[0], 10) +
parseInt(item.toString().split("")[1], 10); // eslint-disable-line
}

return item;
});

let sum = 0;
for (let i = 1; i < mapped.length; i++) { // eslint-disable-line
for (let i = 1; i < mapped.length; i++) {
// eslint-disable-line
// coercion
sum += parseInt(mapped[i], 10);
}

// coercion
const sumArr = sum.toString().split('');
const sumArr = sum.toString().split("");
// coercion
const unitsDigit = parseInt(sumArr[sumArr.length - 1], 10);
// coercion
Expand All @@ -51,4 +54,4 @@ export default function luhn(number) {
}

return isValid;
};
}
5 changes: 2 additions & 3 deletions misc/primes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
export default function primes(limit) {
const primesArray = [];

outerLoop:
for (let i = 2; i <= limit; i++) {
outerLoop: for (let i = 2; i <= limit; i++) {
if (i !== 2 && i % 2 === 0) {
continue;
}
Expand All @@ -20,5 +19,5 @@ export default function primes(limit) {
}

return primesArray;
};
}
/* eslint-enable */
14 changes: 7 additions & 7 deletions misc/test/binary-search.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable prefer-arrow-callback */

import { describe, it } from 'mocha';
import { describe, it } from "mocha";

import { expect } from 'chai';
import binarySearch from '../binary-search';
import primes from '../primes';
import { expect } from "chai";
import binarySearch from "../binary-search";
import primes from "../primes";

describe('Binary search', function () {
it('should return index of the correct guess', function () {
describe("Binary search", function () {
it("should return index of the correct guess", function () {
expect(binarySearch(primes(25), 17)).to.equal(6);
});

it('should return -1 if no feasible correct guess', function () {
it("should return -1 if no feasible correct guess", function () {
expect(binarySearch(primes(25), 15)).to.equal(-1);
});
});
12 changes: 6 additions & 6 deletions misc/test/luhn.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable prefer-arrow-callback */

import { describe, it } from 'mocha';
import { describe, it } from "mocha";

import { expect } from 'chai';
import luhn from '../luhn';
import { expect } from "chai";
import luhn from "../luhn";

describe('Luhn algorithm', function () {
it('should return true for valid numbers', function () {
describe("Luhn algorithm", function () {
it("should return true for valid numbers", function () {
expect(luhn(79927398713)).to.equal(true);
expect(luhn(347136731693028)).to.equal(true);
});

it('should return false for invalid numbers', function () {
it("should return false for invalid numbers", function () {
expect(luhn(79927398710)).to.equal(false);
expect(luhn(79927398711)).to.equal(false);
expect(luhn(79927398712)).to.equal(false);
Expand Down
10 changes: 5 additions & 5 deletions misc/test/primes.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable prefer-arrow-callback */

import { describe, it } from 'mocha';
import { describe, it } from "mocha";

import { expect } from 'chai';
import primes from '../primes';
import { expect } from "chai";
import primes from "../primes";

describe('Primes up to limit', function () {
it('should return array of primes up to limit', function () {
describe("Primes up to limit", function () {
it("should return array of primes up to limit", function () {
expect(primes(25)).to.deep.equal([2, 3, 5, 7, 11, 13, 17, 19, 23]);
});
});
Loading
Loading