@gkucmierz/utils 1.20.0 → 1.21.1

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.
package/main.mjs CHANGED
@@ -32,6 +32,9 @@ import {
32
32
  import {
33
33
  lcm, lcmBI
34
34
  } from './src/lcm.mjs'
35
+ import {
36
+ ListNode
37
+ } from './src/list-node.mjs'
35
38
  import {
36
39
  memoize
37
40
  } from './src/memoize.mjs'
@@ -65,6 +68,7 @@ export * from './src/gpn.mjs';
65
68
  export * from './src/heap.mjs';
66
69
  export * from './src/herons-formula.mjs';
67
70
  export * from './src/lcm.mjs';
71
+ export * from './src/list-node.mjs';
68
72
  export * from './src/memoize.mjs';
69
73
  export * from './src/mod.mjs';
70
74
  export * from './src/phi.mjs';
@@ -74,5 +78,5 @@ export * from './src/square-root.mjs';
74
78
  export * from './src/tonelli-shanks.mjs';
75
79
 
76
80
  export default [
77
- SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
81
+ SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
78
82
  ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.20.0",
3
+ "version": "1.21.1",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.20.0",
9
+ "version": "1.21.1",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.20.0",
3
+ "version": "1.21.1",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,33 @@
1
+
2
+ import { ListNode } from '../src/list-node.mjs';
3
+
4
+ describe('ListNode', () => {
5
+ it('constructor', () => {
6
+ expect(new ListNode().val).toEqual(0);
7
+ expect(new ListNode().next).toEqual(null);
8
+ expect(new ListNode(123).val).toEqual(123);
9
+ expect(new ListNode(123).next).toEqual(null);
10
+ expect(new ListNode(123, 'next').next).toEqual('next');
11
+ });
12
+
13
+ it('toArr', () => {
14
+ expect(new ListNode().toArr()).toEqual([0]);
15
+ expect(new ListNode(123).toArr()).toEqual([123]);
16
+ expect(new ListNode(1, new ListNode(2)).toArr()).toEqual([1, 2]);
17
+ });
18
+
19
+ it('fromArr', () => {
20
+ expect(ListNode.fromArr([1, 2])).toEqual(new ListNode(1, new ListNode(2)));
21
+ });
22
+
23
+ it('both', () => {
24
+ const arr = [1, 2, 3, 4, 5];
25
+ expect(ListNode.fromArr(arr).toArr()).toEqual(arr);
26
+ });
27
+
28
+ it('cyclic reference', () => {
29
+ const node = ListNode.fromArr([1, 2]);
30
+ node.next.next = node;
31
+ expect(() => node.toArr()).toThrow(new Error('Cyclic reference detected'));
32
+ });
33
+ });
@@ -0,0 +1,31 @@
1
+
2
+
3
+ export class ListNode {
4
+ constructor(val = 0, next = null) {
5
+ this.val = val;
6
+ this.next = next;
7
+ }
8
+
9
+ toArr() {
10
+ const ret = [];
11
+ let node = this;
12
+ const set = new Set();
13
+ while (node) {
14
+ if (set.has(node)) throw Error('Cyclic reference detected');
15
+ set.add(node);
16
+ ret.push(node.val);
17
+ node = node.next;
18
+ }
19
+ return ret;
20
+ }
21
+
22
+ static fromArr(iter) {
23
+ const head = new ListNode;
24
+ let node = head;
25
+ for (let val of iter) {
26
+ node.next = new ListNode(val);
27
+ node = node.next;
28
+ }
29
+ return head.next;
30
+ }
31
+ };