Skip to content

Commit 37004ea

Browse files
author
Nejc
committed
refactor: refactored some code
1 parent 6311daa commit 37004ea

File tree

74 files changed

+1647
-253
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1647
-253
lines changed

.all-contributorsrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"contributors": [
1111
{
1212
"login": "nejcm",
13-
"name": "Nejc Mursic",
14-
"avatar_url": "https://avatars3.githubusercontent.com/u/1865210?v=3",
13+
"name": "Nejc Muršič",
14+
"avatar_url": "https://avatars3.githubusercontent.com/u/1865210?v=4",
1515
"profile": "https://nejcmursic.com",
1616
"contributions": [
1717
"code",

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"no-eq-null": "off",
5858
"react/jsx-indent": "off",
5959
"jsx-a11y/label-has-for": "off",
60-
"jsx-a11y/label-has-associated-control": "off"
60+
"jsx-a11y/label-has-associated-control": "off",
61+
"@typescript-eslint/no-non-null-assertion": "off"
6162
}
6263
}

.github/workflows/node.js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: CI/CD
22

33
on:
44
push:
@@ -7,7 +7,7 @@ on:
77
branches: [master, beta, next, develop]
88

99
jobs:
10-
test:
10+
test-deploy:
1111
name: Running tests...
1212
runs-on: ubuntu-latest
1313

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ branches:
1919
jobs:
2020
include:
2121
- stage: deploy
22-
if: branch = master
22+
if: branch = master AND type != pull_request
2323
node_js: lts/*
2424
env:
2525
- PRODUCTION=production

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ ones refactored to improve code quality.
1818
![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)
1919

2020
[![JavaScript Style Guide][style-guide-badge]][style-guide]
21-
[![All Contributors][all-contributors-badge]](#contributors)
21+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
22+
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg)](#contributors)
23+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
2224
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
2325

2426
[![Watch on GitHub][github-watch-badge]][github-watch]
@@ -100,20 +102,9 @@ filing an issue on GitHub.
100102
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
101103
<!-- prettier-ignore-start -->
102104
<!-- markdownlint-disable -->
103-
104105
<table>
105106
<tr>
106-
<td align="center">
107-
<a href="https://github.com/nejcm">
108-
<img src="https://avatars3.githubusercontent.com/u/1865210?v=3" width="100px" alt="Nejc"/>
109-
<br />
110-
<sub><b>Nejc Muršič</b></sub>
111-
</a>
112-
<br />
113-
<a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Code">💻</a>
114-
<a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Documentation">📖</a>
115-
<a href="#infra" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Tests">⚠️</a>
116-
</td>
107+
<td align="center"><a href="https://github.com/nejcm"><img src="https://avatars3.githubusercontent.com/u/1865210?v=4" width="100px" alt="Nejc"/><br /><sub><b>Nejc Muršič</b></sub></a><br /><a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Code">💻</a> <a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Documentation">📖</a> <a href="#infra" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/nejcm/js-algorithms/commits?author=nejcm" title="Tests">⚠️</a></td>
117108
</tr>
118109
</table>
119110

@@ -138,7 +129,6 @@ Reach out to us at one of the following places:
138129

139130
<!-- prettier-ignore-start -->
140131

141-
[all-contributors-badge]: https://img.shields.io/badge/all_contributors-1-orange.svg
142132
[all-contributors]: https://github.com/all-contributors/all-contributors
143133
[bugs]: https://github.com/nejcm/js-algorithms/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
144134
[build-badge]: https://img.shields.io/travis/com/nejcm/js-algorithms.svg

src/algorithms/cryptography/polynomial-hash/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ export interface PolynomialHash {
88
roll: (prevHash: number, prevWindow: string, newWindow: string) => number;
99
}
1010

11+
// get char number value
12+
function char2Number(char: string): number {
13+
let code = char.codePointAt(0) as number;
14+
const surrogate = char.codePointAt(1);
15+
// surrogate pair
16+
if (surrogate !== undefined) {
17+
const surrogateShift = 2 ** 16;
18+
code += surrogate * surrogateShift;
19+
}
20+
return code;
21+
}
22+
1123
const PolynomialHash = ({
1224
base = 37,
1325
modulus = 101,
1426
}: PolynomialHashProps = {}): PolynomialHash => {
15-
// get char number value
16-
const charToNumber = (char: string): number => {
17-
let code = char.codePointAt(0) as number;
18-
const surrogate = char.codePointAt(1);
19-
// surrogate pair
20-
if (surrogate !== undefined) {
21-
const surrogateShift = 2 ** 16;
22-
code += surrogate * surrogateShift;
23-
}
24-
return code;
25-
};
26-
2727
return {
2828
// calculate hash for a string
2929
hash: (window: string) => {
3030
// all character codes
31-
const codes = Array.from(window).map((char) => charToNumber(char));
31+
const codes = Array.from(window).map((char) => char2Number(char));
3232

3333
let hash = 0;
3434
// calculate hash
@@ -43,8 +43,8 @@ const PolynomialHash = ({
4343
roll(prevHash: number, prevWindow: string, newWindow: string): number {
4444
let hash = prevHash;
4545

46-
const prevValue = charToNumber(prevWindow[0]);
47-
const newValue = charToNumber(newWindow[newWindow.length - 1]);
46+
const prevValue = char2Number(prevWindow[0]);
47+
const newValue = char2Number(newWindow[newWindow.length - 1]);
4848

4949
let multiplier = 1;
5050
for (let i = 1; i < prevWindow.length; i += 1) {

src/algorithms/pattern/Algorithm.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import {CompareType} from '../../helpers/comparator';
2+
13
export interface Options {
2-
visitingCallback?: (val: string) => void;
3-
compareFunction?: (val1: string, val2: string) => boolean;
4+
visitingCallback?: (val?: CompareType) => void;
5+
compareFunction?: (val1: CompareType, val2: CompareType) => boolean;
46
[key: string]: unknown;
57
}
68

src/algorithms/pattern/boyer-moore/index.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
1-
/* eslint-disable no-unused-vars */
2-
/* eslint-disable @typescript-eslint/no-unused-vars */
1+
import {max} from '../../../helpers';
32
import algorithm, {Algorithm, AlgorithmProps, Options} from '../Algorithm';
43

5-
export interface boyerMooreOptions extends Options {
6-
chars?: number;
4+
export interface BoyerMooreOptions extends Options {
5+
chars: number;
76
}
87

9-
const boyerMoore = (options?: boyerMooreOptions): Algorithm => {
10-
const algoOptions: Options & {chars: number} = {
8+
const boyerMoore = (options?: Partial<BoyerMooreOptions>): Algorithm => {
9+
const algoOptions: BoyerMooreOptions = {
1110
chars: 256,
1211
...options,
1312
};
1413

15-
const max = (a: number, b: number): number => (a > b ? a : b);
16-
1714
const buildBadCharHeuristic = (seek: string, m: number): number[] => {
1815
const badChar = [];
1916
// initialize all occurrences as -1
20-
// Array(algoOptions.chars).fill(-1);
2117
for (let i = 0; i < algoOptions.chars; i++) badChar.push(-1);
2218
// fill the actual value of last occurrence of a character
2319
for (let i = 0; i < m; i++) badChar[seek.charCodeAt(i)] = i;
24-
2520
return badChar;
2621
};
2722

2823
const search = (text: string, seek: string): number | null => {
29-
if (!text || !seek) {
30-
return -1;
31-
}
32-
24+
if (!text || !seek) return -1;
3325
const n = text.length;
3426
const m = seek.length;
35-
if (n === 0 || m === 0 || m > n) {
36-
return -1;
37-
}
27+
if (n === 0 || m === 0 || m > n) return -1;
3828

3929
const badChar = buildBadCharHeuristic(seek, m);
40-
4130
let s = 0;
4231
while (s <= n - m) {
4332
let j = m - 1;
@@ -50,7 +39,6 @@ const boyerMoore = (options?: boyerMooreOptions): Algorithm => {
5039
s += max(1, j - badChar[text.charCodeAt(s + j)]);
5140
}
5241
}
53-
5442
return -1;
5543
};
5644

src/algorithms/pattern/knuth-morris-pratt/index.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {run} from '../../../helpers';
21
import {equalThan} from '../../../helpers/comparator';
32
import algorithm, {Algorithm, AlgorithmProps, Options} from '../Algorithm';
43

@@ -26,27 +25,20 @@ const knuthMorrisPratt = (options?: Options): Algorithm => {
2625
prefixIndex = table[prefixIndex - 1];
2726
}
2827
}
29-
3028
return table;
3129
};
3230

3331
const search = (text: string, seek: string): number | null => {
34-
if (!text || !seek) {
35-
return -1;
36-
}
37-
32+
if (!text || !seek) return -1;
3833
const n = text.length;
3934
const m = seek.length;
40-
if (n === 0 || m === 0 || m > n) {
41-
return -1;
42-
}
35+
if (n === 0 || m === 0 || m > n) return -1;
4336

4437
let textIndex = 0;
4538
let seekIndex = 0;
4639
const patternTable = buildPatternTable(seek);
47-
4840
while (textIndex < n) {
49-
if (run(algoOptions.compareFunction, [text[textIndex], seek[seekIndex]])) {
41+
if (algoOptions.compareFunction!(text[textIndex], seek[seekIndex])) {
5042
if (seekIndex === m - 1) {
5143
// match found
5244
return textIndex - m + 1;
@@ -61,7 +53,6 @@ const knuthMorrisPratt = (options?: Options): Algorithm => {
6153
textIndex++;
6254
}
6355
}
64-
6556
return -1;
6657
};
6758

src/algorithms/pattern/naive/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {run} from '../../../helpers';
21
import {equalThan} from '../../../helpers/comparator';
32
import algorithm, {Algorithm, AlgorithmProps, Options} from '../Algorithm';
43

@@ -9,23 +8,16 @@ const naiveAlgorithm = (options?: Options): Algorithm => {
98
};
109

1110
const search = (text: string, seek: string): number | null => {
12-
if (!text || !seek) {
13-
return -1;
14-
}
11+
if (!text || !seek) return -1;
1512
const n = text.length;
1613
const m = seek.length;
17-
if (n === 0 || m === 0 || m > n) {
18-
return -1;
19-
}
14+
if (n === 0 || m === 0 || m > n) return -1;
2015

2116
for (let i = 0; i <= n - m; i++) {
2217
let j;
2318
for (j = 0; j < m; j++) {
24-
if (!run(algoOptions.compareFunction, [text[i + j], seek[j]])) {
25-
break;
26-
}
19+
if (!algoOptions.compareFunction!(text[i + j], seek[j])) break;
2720
}
28-
2921
if (j === m) {
3022
// match found
3123
return i;

0 commit comments

Comments
 (0)