Skip to content

Commit

Permalink
Add Prettier (#29)
Browse files Browse the repository at this point in the history
* Add Prettier

* Fix commands
  • Loading branch information
sibeitokgong authored and yangshun committed Oct 22, 2017
1 parent 79ae07e commit 3a01648
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 30 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.js
@@ -1,13 +1,21 @@
module.exports = {
extends: 'airbnb-base',
extends: [
'airbnb-base',
'prettier',
],
env: {
browser: true,
jest: true,
node: true,
},
plugins: [
'prettier',
],
rules: {
'no-param-reassign': 'off', // Needed for swapping elements within an array.
'no-plusplus': 'off',
'no-underscore-dangle': 'off', // Indicate private methods.
'prettier/prettier': 'error',
},
};

4 changes: 4 additions & 0 deletions .prettierrc
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": 'all'
}
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -38,5 +38,11 @@ Data Structures and Algorithms library for JavaScript. Pretty much still WIP but

```sh
$ yarn install
$ yarn run test --watch
$ yarn test --watch
```

Before pushing/submitting PR

```sh
$ yarn check-all
```
6 changes: 3 additions & 3 deletions lib/algorithms/heapSort.js
Expand Up @@ -6,8 +6,8 @@
*/
function _heapify(arr, size, index) {
let largest = index;
const left = (2 * index) + 1;
const right = (2 * index) + 2;
const left = 2 * index + 1;
const right = 2 * index + 2;
if (left < size && arr[left] > arr[largest]) {
largest = left;
}
Expand All @@ -29,7 +29,7 @@ function _heapify(arr, size, index) {
function heapSort(arr) {
const result = arr.slice(0);
const size = arr.length;
for (let i = Math.floor((size / 2) - 1); i >= 0; i--) {
for (let i = Math.floor(size / 2 - 1); i >= 0; i--) {
_heapify(result, size, i);
}
for (let i = size - 1; i >= 0; i--) {
Expand Down
23 changes: 12 additions & 11 deletions lib/data-structures/BloomFilter.js
Expand Up @@ -5,11 +5,11 @@
*/
// (ax + b) % p
const defaultHashFunctions = [
x => ((25 * x) + 13) % 31,
x => ((109 * x) + 71) % 139,
x => ((677 * x) + 241) % 859,
x => ((547 * x) + 383) % 997,
x => ((173 * x) + 149) % 499,
x => (25 * x + 13) % 31,
x => (109 * x + 71) % 139,
x => (677 * x + 241) % 859,
x => (547 * x + 383) % 997,
x => (173 * x + 149) % 499,
];

class BloomFilter {
Expand All @@ -34,11 +34,10 @@ class BloomFilter {
* @param {*} item The item to be added.
*/
add(item) {
this._hashFunctions
.forEach((hashFunction) => {
// the mod _size is needed to ensure we do not go out of bounds
this._bits[hashFunction(item) % this._size] = true;
});
this._hashFunctions.forEach(hashFunction => {
// the mod _size is needed to ensure we do not go out of bounds
this._bits[hashFunction(item) % this._size] = true;
});
}

/**
Expand All @@ -49,7 +48,9 @@ class BloomFilter {
*/
contains(item) {
// check if every bit at the location indicated by the hash function returns true
return this._hashFunctions.every(hashFunction => this._bits[hashFunction(item) % this._size]);
return this._hashFunctions.every(
hashFunction => this._bits[hashFunction(item) % this._size],
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/data-structures/List.js
Expand Up @@ -23,7 +23,7 @@ export function traverseNodes(list, index, allowExceed = false) {
}
} else {
node = list._dummyTail.prev;
for (let i = 0; i < (Math.abs(index) - 1) && i < (list.length - 1); i++) {
for (let i = 0; i < Math.abs(index) - 1 && i < list.length - 1; i++) {
node = node.prev;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/data-structures/Trie.js
Expand Up @@ -149,7 +149,7 @@ class Trie {
curr = curr[char];
}
function traverse(node, chars) {
Object.keys(node).forEach((char) => {
Object.keys(node).forEach(char => {
if (char === TERMINATING_CHAR) {
results[chars] = node[char];
return;
Expand Down
10 changes: 5 additions & 5 deletions lib/data-structures/__tests__/BloomFilter.test.js
@@ -1,11 +1,11 @@
import BloomFilter from '../BloomFilter';

const objectHashFunctions = [
x => ((25 * x.value) + 13) % 31,
x => ((109 * x.value) + 71) % 139,
x => ((677 * x.value) + 241) % 859,
x => ((547 * x.value) + 383) % 997,
x => ((173 * x.value) + 149) % 499,
x => (25 * x.value + 13) % 31,
x => (109 * x.value + 71) % 139,
x => (677 * x.value + 241) % 859,
x => (547 * x.value + 383) % 997,
x => (173 * x.value + 149) % 499,
];

describe('BloomFilter', () => {
Expand Down
13 changes: 12 additions & 1 deletion lib/data-structures/__tests__/List.test.js
Expand Up @@ -139,7 +139,18 @@ describe('List', () => {
test('get()', () => {
// TODO: Test extreme indices on empty list
const l = listFactory(10);
expect(l.toArray()).toEqual([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]);
expect(l.toArray()).toEqual([
100,
200,
300,
400,
500,
600,
700,
800,
900,
1000,
]);
expect(l.get(-100)).toBe(undefined);
expect(l.get(-11)).toBe(undefined);
expect(l.get(-10)).toBe(100);
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Expand Up @@ -17,7 +17,6 @@ export {
heapSort,
mergeSort,
quickSort,

Deque,
List,
Node,
Expand Down
36 changes: 32 additions & 4 deletions lib/utils/sortingInputs.js
Expand Up @@ -13,10 +13,38 @@ function sortingInputs(algorithm) {
expect(algorithm([10, 2, 4])).toEqual([2, 4, 10]);
expect(algorithm([4, 5, 6, 1, 2, 3])).toEqual([1, 2, 3, 4, 5, 6]);
expect(algorithm([1, 2, 3, 4, 5, 0])).toEqual([0, 1, 2, 3, 4, 5]);
expect(algorithm([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(algorithm([5, 4, 3, 2, 1, 10, 9, 8, 7, 6])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(algorithm([98322, 3242, 876, -234, 34, 12331]))
.toEqual([-234, 34, 876, 3242, 12331, 98322]);
expect(algorithm([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
]);
expect(algorithm([5, 4, 3, 2, 1, 10, 9, 8, 7, 6])).toEqual([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
]);
expect(algorithm([98322, 3242, 876, -234, 34, 12331])).toEqual([
-234,
34,
876,
3242,
12331,
98322,
]);
});

test('duplicate elements', () => {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,9 @@
"description": "Data structures and algorithms library for JavaScript.",
"main": "lib/index.js",
"scripts": {
"check-all": "yarn prettier && yarn lint && yarn test",
"lint": "eslint .",
"prettier": "prettier --write 'lib/**/*.js'",
"test": "jest --coverage"
},
"repository": "git+https://github.com/yangshun/lago.git",
Expand All @@ -22,8 +24,11 @@
"devDependencies": {
"eslint": "^4.9.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-config-prettier": "^2.6.0",
"eslint-plugin-import": "^2.8.0",
"jest": "^20.0.4"
"eslint-plugin-prettier": "^2.3.1",
"jest": "^20.0.4",
"prettier": "^1.7.4"
},
"dependencies": {
"babel-jest": "^20.0.3",
Expand Down
29 changes: 29 additions & 0 deletions yarn.lock
Expand Up @@ -1013,6 +1013,12 @@ eslint-config-airbnb-base@^12.1.0:
dependencies:
eslint-restricted-globals "^0.1.1"

eslint-config-prettier@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-2.6.0.tgz#f21db0ebb438ad678fb98946097c4bb198befccc"
dependencies:
get-stdin "^5.0.1"

eslint-import-resolver-node@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.1.tgz#4422574cde66a9a7b099938ee4d508a199e0e3cc"
Expand Down Expand Up @@ -1042,6 +1048,13 @@ eslint-plugin-import@^2.8.0:
minimatch "^3.0.3"
read-pkg-up "^2.0.0"

eslint-plugin-prettier@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-2.3.1.tgz#e7a746c67e716f335274b88295a9ead9f544e44d"
dependencies:
fast-diff "^1.1.1"
jest-docblock "^21.0.0"

eslint-restricted-globals@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/eslint-restricted-globals/-/eslint-restricted-globals-0.1.1.tgz#35f0d5cbc64c2e3ed62e93b4b1a7af05ba7ed4d7"
Expand Down Expand Up @@ -1179,6 +1192,10 @@ fast-deep-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"

fast-diff@^1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.1.2.tgz#4b62c42b8e03de3f848460b639079920695d0154"

fast-levenshtein@~2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
Expand Down Expand Up @@ -1289,6 +1306,10 @@ get-caller-file@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"

get-stdin@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-5.0.1.tgz#122e161591e21ff4c52530305693f20e6393a398"

getpass@^0.1.1:
version "0.1.7"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"
Expand Down Expand Up @@ -1739,6 +1760,10 @@ jest-docblock@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.0.3.tgz#17bea984342cc33d83c50fbe1545ea0efaa44712"

jest-docblock@^21.0.0:
version "21.2.0"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414"

jest-environment-jsdom@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-environment-jsdom/-/jest-environment-jsdom-20.0.3.tgz#048a8ac12ee225f7190417713834bb999787de99"
Expand Down Expand Up @@ -2334,6 +2359,10 @@ preserve@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"

prettier@^1.7.4:
version "1.7.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.4.tgz#5e8624ae9363c80f95ec644584ecdf55d74f93fa"

pretty-format@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.0.3.tgz#020e350a560a1fe1a98dc3beb6ccffb386de8b14"
Expand Down

0 comments on commit 3a01648

Please sign in to comment.