@antti5/ship-tool-common 1.0.10 → 1.1.0

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/eslint.config.js CHANGED
@@ -1,44 +1,65 @@
1
1
  import { defineConfig } from 'eslint/config';
2
2
  import js from '@eslint/js';
3
3
  import globals from 'globals';
4
+ import tseslint from 'typescript-eslint';
4
5
 
5
- export default defineConfig([{
6
- files: ['**/*.{js,mjs,cjs}'],
7
- plugins: { js },
8
- extends: ['js/recommended'],
9
- languageOptions: {
10
- globals: {
11
- ...globals.browser, // window object is used in Util
12
- ...globals.node
6
+ export default defineConfig([
7
+ {
8
+ files: ['**/*.{js,ts,mjs,cjs}'],
9
+ plugins: { js },
10
+ extends: ['js/recommended'],
11
+ languageOptions: {
12
+ globals: {
13
+ ...globals.browser, // window object is used in Util
14
+ ...globals.node
15
+ },
16
+ ecmaVersion: 2022,
17
+ sourceType: 'module'
18
+ },
19
+ rules: {
20
+ indent: ['error', 3, {
21
+ ignoreComments: true,
22
+ SwitchCase: 1
23
+ }],
24
+ 'linebreak-style': ['error', 'unix'],
25
+ quotes: ['error', 'single'],
26
+ semi: ['error', 'always'],
27
+ eqeqeq: 'error',
28
+ 'no-trailing-spaces': ['warn', {
29
+ ignoreComments: true
30
+ }],
31
+ 'arrow-spacing': ['error', {
32
+ before: true,
33
+ after: true
34
+ }],
35
+ 'no-unused-vars': ['warn', {
36
+ varsIgnorePattern: '^_',
37
+ argsIgnorePattern: '^_',
38
+ caughtErrorsIgnorePattern: '^_'
39
+ }],
40
+ 'object-curly-spacing': ['error', 'always'],
41
+ 'no-constant-condition': ['warn', {
42
+ checkLoops: false
43
+ }],
13
44
  },
14
- ecmaVersion: 2022,
15
- sourceType: 'module'
16
45
  },
17
- rules: {
18
- indent: ['error', 3, {
19
- ignoreComments: true,
20
- SwitchCase: 1
21
- }],
22
- 'linebreak-style': ['error', 'unix'],
23
- quotes: ['error', 'single'],
24
- semi: ['error', 'always'],
25
- eqeqeq: 'error',
26
- 'no-trailing-spaces': ['warn', {
27
- ignoreComments: true
28
- }],
29
- 'arrow-spacing': ['error', {
30
- before: true,
31
- after: true
32
- }],
33
- 'no-unused-vars': ['warn', {
34
- varsIgnorePattern: '^_',
35
- argsIgnorePattern: '^_',
36
- caughtErrorsIgnorePattern: '^_'
37
- }],
38
- 'object-curly-spacing': ['error', 'always'],
39
- 'no-constant-condition': ['warn', {
40
- checkLoops: false
41
- }],
46
+
47
+ {
48
+ files: ['**/*.{ts,tsx}'],
49
+ extends: [
50
+ ...tseslint.configs.recommendedTypeChecked,
51
+ ],
52
+ languageOptions: {
53
+ parserOptions: {
54
+ projectService: true,
55
+ tsconfigRootDir: import.meta.dirname,
56
+ },
57
+ },
58
+ rules: {
59
+ '@typescript-eslint/no-unused-vars': ['warn', {
60
+ argsIgnorePattern: '^_',
61
+ }]
62
+ }
42
63
  },
43
- }]);
64
+ ]);
44
65
 
@@ -1,20 +1,31 @@
1
+ interface Waiter {
2
+ resolve: (value?: unknown) => void,
3
+ reject: (reason: unknown) => void
4
+ }
5
+
6
+ interface QueueItem extends Waiter {
7
+ jobClass?: string,
8
+ }
9
+
10
+
1
11
  class Semaphore {
2
12
 
3
13
  #value = 0;
4
14
  #max;
5
- #queue = [];
15
+ #queue: QueueItem[] = [];
16
+
17
+ #rateLimit: number = 0;
18
+ #rateLimitInterval: number | null = null;
6
19
 
7
- #rateLimit;
8
- #rateLimitInterval;
20
+ #waiters: Waiter[] = [];
9
21
 
10
- #waiters = [];
11
22
 
12
- constructor(max = 1, { rateLimit = null } = {}) {
23
+ constructor(max = 1, { rateLimit = 0 }: { rateLimit?: number } = {}) {
13
24
  this.#max = max;
14
25
  this.#rateLimit = rateLimit;
15
26
  }
16
27
 
17
- acquire({ jobClass = null } = {}) {
28
+ acquire({ jobClass }: { jobClass?: string } = {}) {
18
29
 
19
30
  /* If the semaphore is not at maximum value and no rate limit interval
20
31
  has been set, return immediately.
@@ -23,7 +34,7 @@ class Semaphore {
23
34
  acquisition will be timed. */
24
35
 
25
36
  if (this.#value < this.#max && !this.#rateLimitInterval) {
26
- if (this.#rateLimit)
37
+ if (this.#rateLimit > 0)
27
38
  this.#rateLimitInterval = setInterval(
28
39
  () => this.#resolveNext(),
29
40
  1000 / this.#rateLimit
@@ -42,7 +53,11 @@ class Semaphore {
42
53
 
43
54
  if (this.#value === 0 && this.#queue.length === 0)
44
55
  while (this.#waiters.length > 0)
45
- this.#waiters.pop().resolve();
56
+
57
+ /* Note: Here non-null assertion is required because, regardless of the length check,
58
+ TypeScript believes that pop() may return undefined. */
59
+
60
+ this.#waiters.pop()!.resolve();
46
61
 
47
62
  if (!this.#rateLimit)
48
63
  this.#resolveNext();
@@ -51,7 +66,7 @@ class Semaphore {
51
66
  #resolveNext() {
52
67
  if (this.#queue.length > 0 && this.#value < this.#max) {
53
68
  this.#value++;
54
- this.#queue.shift().resolve();
69
+ this.#queue.shift()!.resolve();
55
70
  }
56
71
 
57
72
  if (this.#queue.length === 0 && this.#rateLimitInterval) {
@@ -62,23 +77,23 @@ class Semaphore {
62
77
 
63
78
  purge() {
64
79
  while (this.#queue.length > 0)
65
- this.#queue.pop().reject('Semaphore was purged');
80
+ this.#queue.pop()!.reject('Semaphore was purged');
66
81
 
67
82
  while (this.#waiters.length > 0)
68
- this.#waiters.pop().reject('Semaphore was purged');
83
+ this.#waiters.pop()!.reject('Semaphore was purged');
69
84
 
70
85
  this.#queue = [];
71
86
  this.#value = 0;
72
87
  }
73
88
 
74
- raiseJobClass(jobClass) {
89
+ raiseJobClass(jobClass: string) {
75
90
  this.#queue = [
76
91
  ...this.#queue.filter(job => job.jobClass === jobClass),
77
92
  ...this.#queue.filter(job => job?.jobClass !== jobClass)
78
93
  ];
79
94
  }
80
95
 
81
- lowerJobClass(jobClass) {
96
+ lowerJobClass(jobClass: string) {
82
97
  this.#queue = [
83
98
  ...this.#queue.filter(job => job?.jobClass !== jobClass),
84
99
  ...this.#queue.filter(job => job.jobClass === jobClass)
@@ -93,7 +108,7 @@ class Semaphore {
93
108
  return this.#max;
94
109
  }
95
110
 
96
- setMax(max) {
111
+ setMax(max: number) {
97
112
  this.#max = max;
98
113
  }
99
114
 
@@ -101,11 +116,14 @@ class Semaphore {
101
116
  return this.#rateLimit;
102
117
  }
103
118
 
104
- setRateLimit(rateLimit) {
119
+ setRateLimit(rateLimit: number) {
105
120
  this.#rateLimit = rateLimit;
106
121
  }
107
122
 
108
- async run(func, { jobClass = null } = {}) {
123
+ async run(
124
+ func: () => Promise<void>,
125
+ { jobClass }: { jobClass?: string } = {}
126
+ ) {
109
127
  try {
110
128
  await this.acquire({ jobClass });
111
129
  return await func();
@@ -119,6 +137,7 @@ class Semaphore {
119
137
  return new Promise((resolve, reject) => {
120
138
  this.#waiters.push({ resolve, reject });
121
139
  });
140
+ return;
122
141
  }
123
142
  }
124
143
 
@@ -1,5 +1,8 @@
1
1
  class UID {
2
2
 
3
+ static #uidOldTimestamp: number;
4
+ static #uidCounter: number;
5
+
3
6
  /* Return an UID with the following properties:
4
7
 
5
8
  1) Length is fixed at 10 characters.
@@ -9,9 +12,6 @@ class UID {
9
12
  5) If UIDs are created in two different processes within a one-second time
10
13
  window, the probability of a collision is less than one in billion. */
11
14
 
12
- static #uidOldTimestamp = null;
13
- static #uidCounter = null;
14
-
15
15
  static create() {
16
16
 
17
17
  const DIGITS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
@@ -22,7 +22,7 @@ class UID {
22
22
 
23
23
  /* Return a positive integer in a different base. */
24
24
 
25
- const ntoaBase = n => {
25
+ const ntoaBase = (n: number) => {
26
26
  let result = '';
27
27
  while (n >= BASE) {
28
28
  result = DIGITS[n % BASE] + result;
@@ -1,4 +1,4 @@
1
- import Vector3 from './Vector3.js';
1
+ import Vector3 from './Vector3.ts';
2
2
 
3
3
  const G = 9.8;
4
4
  const G_VEC = new Vector3(0, -G, 0);
@@ -10,12 +10,20 @@ const TEMPERATURE_LAPSE_RATE_LVL0 = -0.0065; // K / m
10
10
  const PRESSURE_CST_H0 = G * AIR_MOLAR_MASS / (GAS_CST_R * TEMPERATURE_LAPSE_RATE_LVL0);
11
11
 
12
12
  class BulletSim {
13
- constructor(mass, diam, dragCoeff, vel, pos, dir) {
13
+
14
+ mass: number;
15
+ dragCoeff: number;
16
+ area: number;
17
+ time: number;
18
+ pos: Vector3;
19
+ vel: Vector3;
20
+
21
+ constructor(mass: number, diam: number, dragCoeff: number, vel: number, pos: Vector3, dir: Vector3) {
14
22
  this.mass = mass;
15
23
  this.dragCoeff = dragCoeff;
16
24
  this.pos = pos;
17
25
  this.vel = dir.normalized().mul(vel);
18
- this.area = 0.25 * diam * diam * Math.PI;
26
+ this.area = diam ** 2 * Math.PI / 4;
19
27
  this.time = 0.0;
20
28
  }
21
29
 
@@ -0,0 +1,70 @@
1
+ import Vector3 from './Vector3.ts';
2
+ import BulletSim from './BulletSim.ts';
3
+ import TrajectoryPoint from './TrajectoryPoint.ts';
4
+
5
+ class Trajectory {
6
+
7
+ #elevation: number;
8
+ #points: TrajectoryPoint[];
9
+
10
+ constructor(
11
+ projectile: {
12
+ bulletMass: number,
13
+ bulletDiametr: number,
14
+ bulletAirDrag: number,
15
+ bulletSpeed: number // Initial velocity
16
+ },
17
+ elevation: number,
18
+ { flatten = false } = {}
19
+ ) {
20
+
21
+ this.#elevation = elevation;
22
+
23
+ const pos = new Vector3(0.0, 10.0, 0.0);
24
+ const dir = new Vector3(1.0, Math.tan(elevation), 0.0);
25
+ const vel = new Vector3(
26
+ Math.cos(elevation) * projectile.bulletSpeed,
27
+ Math.sin(elevation) * projectile.bulletSpeed,
28
+ 0.0
29
+ );
30
+
31
+ this.#points = [
32
+ new TrajectoryPoint(pos, vel, 0, flatten)
33
+ ];
34
+
35
+ const bullet = new BulletSim(
36
+ projectile.bulletMass,
37
+ projectile.bulletDiametr,
38
+ projectile.bulletAirDrag,
39
+ projectile.bulletSpeed,
40
+ pos,
41
+ dir
42
+ );
43
+
44
+ while (bullet.pos.y > 0) {
45
+ bullet.step();
46
+ this.#points.push(new TrajectoryPoint(pos, bullet.vel, bullet.time, flatten));
47
+ }
48
+ }
49
+
50
+ getPoints() {
51
+ return this.#points;
52
+ }
53
+
54
+ getParameters() {
55
+ const lastPoint = this.getPoints().at(-1);
56
+
57
+ if (!lastPoint)
58
+ throw new Error('Unable to get last trajectory point');
59
+
60
+ return {
61
+ elevation: this.#elevation,
62
+ range: lastPoint.pos.x,
63
+ flightTime: lastPoint.time / 2.75,
64
+ impactAngle: Math.abs(Math.atan(lastPoint.vel.y / lastPoint.vel.x)),
65
+ impactVelocity: lastPoint.vel.len()
66
+ };
67
+ }
68
+ }
69
+
70
+ export default Trajectory;
@@ -0,0 +1,23 @@
1
+ import Vector3 from './Vector3.ts';
2
+
3
+ const BALLISTIC_FLATTENING_ENABLED = true;
4
+ const BALLISTIC_FLATTENING = 1.0;
5
+
6
+ class TrajectoryPoint {
7
+
8
+ pos: Vector3;
9
+ vel: Vector3;
10
+ time: number;
11
+
12
+ constructor(pos: Vector3, vel: Vector3, time: number, flatten = false) {
13
+ this.pos = Vector3.copy(pos);
14
+ this.vel = Vector3.copy(vel);
15
+ this.time = time;
16
+
17
+ if (flatten)
18
+ if (BALLISTIC_FLATTENING_ENABLED && this.pos.y > 0)
19
+ this.pos.y = Math.log(BALLISTIC_FLATTENING * this.pos.y + 1.0) / BALLISTIC_FLATTENING;
20
+ }
21
+ }
22
+
23
+ export default TrajectoryPoint;
@@ -1,11 +1,16 @@
1
1
  class Vector3 {
2
+
3
+ x: number;
4
+ y: number;
5
+ z: number;
6
+
2
7
  constructor(x = 0.0, y = 0.0, z = 0.0) {
3
8
  this.x = x;
4
9
  this.y = y;
5
10
  this.z = z;
6
11
  }
7
12
 
8
- static copy(other) {
13
+ static copy(other: Vector3) {
9
14
  return new Vector3(
10
15
  other.x,
11
16
  other.y,
@@ -13,13 +18,13 @@ class Vector3 {
13
18
  );
14
19
  }
15
20
 
16
- iadd(other) {
21
+ iadd(other: Vector3) {
17
22
  this.x += other.x;
18
23
  this.y += other.y;
19
24
  this.z += other.z;
20
25
  }
21
26
 
22
- add(other) {
27
+ add(other: Vector3) {
23
28
  return new Vector3(
24
29
  this.x + other.x,
25
30
  this.y + other.y,
@@ -27,13 +32,13 @@ class Vector3 {
27
32
  );
28
33
  }
29
34
 
30
- isub(other) {
35
+ isub(other: Vector3) {
31
36
  this.x -= other.x;
32
37
  this.y -= other.y;
33
38
  this.z -= other.z;
34
39
  }
35
40
 
36
- sub(other) {
41
+ sub(other: Vector3) {
37
42
  return new Vector3(
38
43
  this.x - other.x,
39
44
  this.y - other.y,
@@ -41,7 +46,7 @@ class Vector3 {
41
46
  );
42
47
  }
43
48
 
44
- imul(other) {
49
+ imul(other: Vector3 | number) {
45
50
  if (other instanceof Vector3) {
46
51
  this.x *= other.x;
47
52
  this.y *= other.y;
@@ -53,7 +58,7 @@ class Vector3 {
53
58
  }
54
59
  }
55
60
 
56
- mul(other) {
61
+ mul(other: Vector3 | number) {
57
62
  if (other instanceof Vector3)
58
63
  return new Vector3(
59
64
  this.x * other.x,
@@ -68,7 +73,7 @@ class Vector3 {
68
73
  );
69
74
  }
70
75
 
71
- idiv(other) {
76
+ idiv(other: Vector3 | number) {
72
77
  if (other instanceof Vector3) {
73
78
  this.x /= other.x;
74
79
  this.y /= other.y;
@@ -80,7 +85,7 @@ class Vector3 {
80
85
  }
81
86
  }
82
87
 
83
- div(other) {
88
+ div(other: Vector3 | number) {
84
89
  if (other instanceof Vector3)
85
90
  return new Vector3(
86
91
  this.x / other.x,
@@ -95,7 +100,7 @@ class Vector3 {
95
100
  );
96
101
  }
97
102
 
98
- dot(other) {
103
+ dot(other: Vector3) {
99
104
  return this.x * other.x + this.y * other.y + this.z * other.z;
100
105
  }
101
106
 
@@ -0,0 +1,9 @@
1
+ import Vector3 from './Vector3.ts';
2
+ import BulletSim from './BulletSim.ts';
3
+
4
+ const pos = new Vector3(0, 0, 0);
5
+ const dir = new Vector3(1, 1, 1);
6
+ const sim = new BulletSim(1, 1, 1, 1, pos, dir);
7
+ sim.step();
8
+ sim.step();
9
+ sim.step();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antti5/ship-tool-common",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "Ship Tool common utils and library code",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,9 +17,9 @@
17
17
  },
18
18
  "exports": {
19
19
  "./Util": "./lib/Util.js",
20
- "./Semaphore": "./lib/Semaphore.js",
21
- "./UID": "./lib/UID.js",
22
- "./Trajectory": "./lib/trajectory/Trajectory.js",
20
+ "./Semaphore": "./lib/Semaphore.ts",
21
+ "./UID": "./lib/UID.ts",
22
+ "./Trajectory": "./lib/trajectory/Trajectory.ts",
23
23
  "./node/Util": "./lib/UtilNode.js"
24
24
  },
25
25
  "scripts": {
@@ -28,7 +28,8 @@
28
28
  "devDependencies": {
29
29
  "@eslint/js": "9.39.2",
30
30
  "eslint": "9.36.0",
31
- "globals": "16.4.0"
31
+ "globals": "16.4.0",
32
+ "typescript-eslint": "8.73.0"
32
33
  },
33
34
  "dependencies": {
34
35
  "dotenv": "17.4.2",
package/tsconfig.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "compilerOptions": {
3
+ // "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
4
+ "target": "es2022",
5
+ // "useDefineForClassFields": true,
6
+ // "lib": [
7
+ // "es2022",
8
+ // "DOM",
9
+ // "DOM.Iterable"
10
+ // ],
11
+ "module": "nodenext",
12
+ // "types": ["vite/client"],
13
+ // "skipLibCheck": true,
14
+
15
+ /* Bundler mode */
16
+ "moduleResolution": "nodenext",
17
+ "allowImportingTsExtensions": true,
18
+ // "verbatimModuleSyntax": true,
19
+ // "moduleDetection": "force",
20
+ "noEmit": true,
21
+ // "jsx": "react-jsx",
22
+
23
+ /* Linting */
24
+ "strict": true,
25
+ "esModuleInterop": true,
26
+ "noFallthroughCasesInSwitch": true,
27
+ "noImplicitReturns": true,
28
+ "noUncheckedSideEffectImports": true,
29
+ "noUnusedLocals": true,
30
+ "noUnusedParameters": true,
31
+
32
+ /* Allows JSON file import, like vanilla JavaScript would do with import attribute:
33
+ import './foo.json' with { type: 'json '} */
34
+ "resolveJsonModule": true
35
+ },
36
+ "include": ["lib"]
37
+ }
@@ -1,51 +0,0 @@
1
- import Vector3 from './Vector3.js';
2
- import BulletSim from './BulletSim.js';
3
- import TrajectoryPoint from './TrajectoryPoint.js';
4
-
5
- class Trajectory {
6
-
7
- constructor(
8
- {
9
- bulletMass: mass,
10
- bulletDiametr: diameter,
11
- bulletAirDrag: dragCoeff,
12
- bulletSpeed: initialVelocity
13
- },
14
- elevation,
15
- { flatten = false } = {}
16
- ) {
17
-
18
- this._elevation = elevation;
19
-
20
- const pos = new Vector3(0.0, 10.0, 0.0);
21
- const dir = new Vector3(1.0, Math.tan(elevation), 0.0);
22
- const vel = new Vector3(Math.cos(elevation) * initialVelocity, Math.sin(elevation) * initialVelocity, 0.0);
23
-
24
- this.points = [
25
- new TrajectoryPoint(pos, vel, 0, flatten)
26
- ];
27
-
28
- const bullet = new BulletSim(mass, diameter, dragCoeff, initialVelocity, pos, dir);
29
- while (bullet.pos.y > 0) {
30
- bullet.step();
31
- this.points.push(new TrajectoryPoint(pos, bullet.vel, bullet.time, flatten));
32
- }
33
- }
34
-
35
- getPoints() {
36
- return this.points;
37
- }
38
-
39
- getParameters() {
40
- const lastPoint = this.getPoints().at(-1);
41
- return {
42
- elevation: this._elevation,
43
- range: lastPoint.pos.x,
44
- flightTime: lastPoint.time / 2.75,
45
- impactAngle: Math.abs(Math.atan(lastPoint.vel.y / lastPoint.vel.x)),
46
- impactVelocity: lastPoint.vel.len()
47
- };
48
- }
49
- }
50
-
51
- export default Trajectory;
@@ -1,17 +0,0 @@
1
- import Vector3 from './Vector3.js';
2
-
3
- const BALLISTIC_FLATTENING = 1.0;
4
-
5
- class TrajectoryPoint {
6
- constructor(pos, vel, time, flatten = false) {
7
- this.pos = Vector3.copy(pos);
8
- this.vel = Vector3.copy(vel);
9
- this.time = time;
10
-
11
- if (flatten)
12
- if (BALLISTIC_FLATTENING !== 0.0 && this.pos.y > 0)
13
- this.pos.y = Math.log(BALLISTIC_FLATTENING * this.pos.y + 1.0) / BALLISTIC_FLATTENING;
14
- }
15
- }
16
-
17
- export default TrajectoryPoint;