js-fundamentals 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * fundamentals
3
+ * Main entry point for the library
4
+ */
5
+ import Queue from "./queue/index.js";
6
+ import PriorityQueue from "./priorityqueue/index.js";
7
+ export { Queue, PriorityQueue };
@@ -2,8 +2,6 @@
2
2
  * fundamentals
3
3
  * Main entry point for the library
4
4
  */
5
-
6
- import Queue from "./queue";
7
- import PriorityQueue from "./priorityqueue";
8
-
5
+ import Queue from "./queue/index.js";
6
+ import PriorityQueue from "./priorityqueue/index.js";
9
7
  export { Queue, PriorityQueue };
@@ -0,0 +1,16 @@
1
+ interface PriorityQueueOptions {
2
+ descending?: boolean;
3
+ }
4
+ declare class PriorityQueue<T> {
5
+ private items;
6
+ private descending;
7
+ constructor(options?: PriorityQueueOptions);
8
+ enqueue(item: T, priority: number): void;
9
+ dequeue(): T | undefined;
10
+ peek(): T | undefined;
11
+ isEmpty(): boolean;
12
+ size(): number;
13
+ toArray(): T[];
14
+ changePriority(item: T, newPriority: number): void;
15
+ }
16
+ export default PriorityQueue;
@@ -0,0 +1,47 @@
1
+ class PriorityQueue {
2
+ constructor(options = {}) {
3
+ this.items = [];
4
+ const { descending = false } = options;
5
+ this.descending = descending;
6
+ }
7
+ enqueue(item, priority) {
8
+ this.items.push({ item, priority });
9
+ this.items.sort((a, b) => this.descending ? b.priority - a.priority : a.priority - b.priority);
10
+ }
11
+ dequeue() {
12
+ const entry = this.items.shift();
13
+ return entry?.item;
14
+ }
15
+ peek() {
16
+ return this.items[0]?.item;
17
+ }
18
+ isEmpty() {
19
+ return this.items.length === 0;
20
+ }
21
+ size() {
22
+ return this.items.length;
23
+ }
24
+ toArray() {
25
+ return this.items.map((entry) => entry.item);
26
+ }
27
+ changePriority(item, newPriority) {
28
+ const index = this.items.findIndex((i) => {
29
+ if (i.item === item) {
30
+ return true;
31
+ }
32
+ if (typeof i.item === "object" &&
33
+ typeof item === "object" &&
34
+ i.item !== null &&
35
+ item !== null) {
36
+ return JSON.stringify(i.item) === JSON.stringify(item);
37
+ }
38
+ return false;
39
+ });
40
+ if (index === -1) {
41
+ throw new Error("Item not found");
42
+ }
43
+ this.items[index].priority = newPriority;
44
+ this.items.sort((a, b) => this.descending ? b.priority - a.priority : a.priority - b.priority);
45
+ }
46
+ }
47
+ export default PriorityQueue;
@@ -0,0 +1,11 @@
1
+ declare class Queue<T> {
2
+ protected items: T[];
3
+ constructor();
4
+ enqueue(item: T): void;
5
+ dequeue(): T | undefined;
6
+ peek(): T | undefined;
7
+ isEmpty(): boolean;
8
+ size(): number;
9
+ toArray(): T[];
10
+ }
11
+ export default Queue;
@@ -0,0 +1,24 @@
1
+ class Queue {
2
+ constructor() {
3
+ this.items = [];
4
+ }
5
+ enqueue(item) {
6
+ this.items.push(item);
7
+ }
8
+ dequeue() {
9
+ return this.items.shift();
10
+ }
11
+ peek() {
12
+ return this.items[0];
13
+ }
14
+ isEmpty() {
15
+ return this.items.length === 0;
16
+ }
17
+ size() {
18
+ return this.items.length;
19
+ }
20
+ toArray() {
21
+ return this.items;
22
+ }
23
+ }
24
+ export default Queue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-fundamentals",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/eetujukka1/fundamentals#readme",
6
6
  "bugs": {
@@ -13,8 +13,19 @@
13
13
  "license": "ISC",
14
14
  "author": "",
15
15
  "type": "module",
16
- "main": "index.js",
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
17
27
  "scripts": {
28
+ "build": "tsc",
18
29
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
19
30
  "lint": "eslint",
20
31
  "format": "npx prettier --write .",
@@ -22,9 +33,13 @@
22
33
  },
23
34
  "devDependencies": {
24
35
  "@eslint/js": "^9.39.2",
36
+ "@types/jest": "^30.0.0",
25
37
  "eslint": "^9.39.2",
26
38
  "globals": "^17.0.0",
27
39
  "jest": "^30.2.0",
28
- "prettier": "^3.8.0"
40
+ "prettier": "^3.8.0",
41
+ "ts-jest": "^29.4.6",
42
+ "typescript": "^5.9.3",
43
+ "typescript-eslint": "^8.54.0"
29
44
  }
30
45
  }
@@ -1,24 +0,0 @@
1
- name: Pipeline
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- pull_request:
8
- branches: [main]
9
- types: [opened, synchronize]
10
-
11
- jobs:
12
- simple_deployment_pipeline:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - uses: actions/checkout@v4
16
- - uses: actions/setup-node@v4
17
- with:
18
- node-version: "20"
19
- - name: Install dependencies
20
- run: npm install
21
- - name: Check style
22
- run: npm run lint
23
- - name: Check format
24
- run: npm run checkformat
package/eslint.config.js DELETED
@@ -1,12 +0,0 @@
1
- import js from "@eslint/js";
2
- import globals from "globals";
3
- import { defineConfig } from "eslint/config";
4
-
5
- export default defineConfig([
6
- {
7
- files: ["**/*.{js,mjs,cjs}"],
8
- plugins: { js },
9
- extends: ["js/recommended"],
10
- languageOptions: { globals: globals.browser },
11
- },
12
- ]);
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- /**
2
- * fundamentals
3
- * Main entry point - exports from src/
4
- */
5
-
6
- export * from "./src";
package/jest.config.js DELETED
@@ -1,7 +0,0 @@
1
- export default {
2
- testEnvironment: "node",
3
- transform: {},
4
- moduleNameMapper: {
5
- "^(\\.{1,2}/.*)\\.js$": "$1",
6
- },
7
- };
@@ -1,63 +0,0 @@
1
- import Queue from "../queue";
2
-
3
- class PriorityQueue extends Queue {
4
- constructor(options = {}) {
5
- super();
6
- const { descending = false } = options;
7
- this.descending = descending;
8
- }
9
-
10
- enqueue(item, priority) {
11
- this.items.push({ item, priority });
12
- this.items.sort((a, b) =>
13
- this.descending ? b.priority - a.priority : a.priority - b.priority,
14
- );
15
- }
16
-
17
- dequeue() {
18
- return this.items.shift().item;
19
- }
20
-
21
- peek() {
22
- return this.items[0].item;
23
- }
24
-
25
- isEmpty() {
26
- return this.items.length === 0;
27
- }
28
-
29
- size() {
30
- return this.items.length;
31
- }
32
-
33
- toArray() {
34
- return this.items.map((item) => item.item);
35
- }
36
-
37
- changePriority(item, newPriority) {
38
- const index = this.items.findIndex((i) => {
39
- if (i.item === item) {
40
- return true;
41
- }
42
-
43
- if (
44
- typeof i.item === "object" &&
45
- typeof item === "object" &&
46
- i.item !== null &&
47
- item !== null
48
- ) {
49
- return JSON.stringify(i.item) === JSON.stringify(item);
50
- }
51
- return false;
52
- });
53
- if (index === -1) {
54
- throw new Error("Item not found");
55
- }
56
- this.items[index].priority = newPriority;
57
- this.items.sort((a, b) =>
58
- this.descending ? b.priority - a.priority : a.priority - b.priority,
59
- );
60
- }
61
- }
62
-
63
- export default PriorityQueue;
@@ -1,31 +0,0 @@
1
- class Queue {
2
- constructor() {
3
- this.items = [];
4
- }
5
-
6
- enqueue(item) {
7
- this.items.push(item);
8
- }
9
-
10
- dequeue() {
11
- return this.items.shift();
12
- }
13
-
14
- peek() {
15
- return this.items[0];
16
- }
17
-
18
- isEmpty() {
19
- return this.items.length === 0;
20
- }
21
-
22
- size() {
23
- return this.items.length;
24
- }
25
-
26
- toArray() {
27
- return this.items;
28
- }
29
- }
30
-
31
- export default Queue;
@@ -1,76 +0,0 @@
1
- import { PriorityQueue } from "../src/index";
2
- import { describe, it, expect } from "@jest/globals";
3
-
4
- describe("PriorityQueue", () => {
5
- it("should create a priority queue", () => {
6
- const queue = new PriorityQueue();
7
- expect(queue).toBeDefined();
8
- });
9
-
10
- it("should enqueue an item", () => {
11
- const queue = new PriorityQueue();
12
- queue.enqueue(1, 1);
13
- expect(queue.size()).toBe(1);
14
- expect(queue.isEmpty()).toBe(false);
15
- });
16
-
17
- it("should dequeue an item", () => {
18
- const queue = new PriorityQueue();
19
- queue.enqueue(3, 3);
20
- queue.enqueue(2, 2);
21
- queue.enqueue(1, 1);
22
- expect(queue.dequeue()).toBe(1);
23
- expect(queue.isEmpty()).toBe(false);
24
- });
25
-
26
- it("should dequeue an item with descending priority", () => {
27
- const queue = new PriorityQueue({ descending: true });
28
- queue.enqueue(1, 1);
29
- queue.enqueue(2, 2);
30
- queue.enqueue(3, 3);
31
- expect(queue.dequeue()).toBe(3);
32
- expect(queue.isEmpty()).toBe(false);
33
- });
34
-
35
- it("should peek at the front item", () => {
36
- const queue = new PriorityQueue();
37
- queue.enqueue(3, 3);
38
- queue.enqueue(2, 2);
39
- queue.enqueue(1, 1);
40
- expect(queue.peek()).toBe(1);
41
- });
42
-
43
- it("should check if queue is empty", () => {
44
- const queue = new PriorityQueue();
45
- expect(queue.isEmpty()).toBe(true);
46
- });
47
-
48
- it("should check if queue is not empty", () => {
49
- const queue = new PriorityQueue();
50
- queue.enqueue(1, 1);
51
- expect(queue.isEmpty()).toBe(false);
52
- });
53
-
54
- it("should return the size of the queue", () => {
55
- const queue = new PriorityQueue();
56
- queue.enqueue(1, 1);
57
- expect(queue.size()).toBe(1);
58
- });
59
-
60
- it("should return an array of items", () => {
61
- const queue = new PriorityQueue();
62
- queue.enqueue(3, 3);
63
- queue.enqueue(2, 2);
64
- queue.enqueue(1, 1);
65
- expect(queue.toArray()).toEqual([1, 2, 3]);
66
- });
67
-
68
- it("should change the priority of an item", () => {
69
- const queue = new PriorityQueue();
70
- queue.enqueue({ id: 1 }, 1);
71
- queue.enqueue({ id: 2 }, 2);
72
- queue.enqueue({ id: 3 }, 3);
73
- queue.changePriority({ id: 2 }, 4);
74
- expect(queue.toArray()).toEqual([{ id: 1 }, { id: 3 }, { id: 2 }]);
75
- });
76
- });
@@ -1,56 +0,0 @@
1
- import Queue from "../src/queue";
2
- import { describe, it, expect } from "@jest/globals";
3
-
4
- describe("Queue", () => {
5
- it("should create a queue", () => {
6
- const queue = new Queue();
7
- expect(queue).toBeDefined();
8
- });
9
-
10
- it("should enqueue an item", () => {
11
- const queue = new Queue();
12
- queue.enqueue(1);
13
- expect(queue.size()).toBe(1);
14
- expect(queue.isEmpty()).toBe(false);
15
- });
16
-
17
- it("should dequeue an item", () => {
18
- const queue = new Queue();
19
- queue.enqueue(1);
20
- expect(queue.dequeue()).toBe(1);
21
- expect(queue.isEmpty()).toBe(true);
22
- });
23
-
24
- it("should peek at the front item", () => {
25
- const queue = new Queue();
26
- queue.enqueue(1);
27
- queue.enqueue(2);
28
- expect(queue.peek()).toBe(1);
29
- });
30
-
31
- it("should check if queue is empty", () => {
32
- const queue = new Queue();
33
- expect(queue.isEmpty()).toBe(true);
34
- });
35
-
36
- it("should check if queue is not empty", () => {
37
- const queue = new Queue();
38
- queue.enqueue(1);
39
- expect(queue.isEmpty()).toBe(false);
40
- });
41
-
42
- it("should return the size of the queue", () => {
43
- const queue = new Queue();
44
- queue.enqueue(1);
45
- queue.enqueue(2);
46
- expect(queue.size()).toBe(2);
47
- });
48
-
49
- it("should return an array of items", () => {
50
- const queue = new Queue();
51
- queue.enqueue(1);
52
- queue.enqueue(2);
53
- queue.enqueue(3);
54
- expect(queue.toArray()).toEqual([1, 2, 3]);
55
- });
56
- });