Selaa lähdekoodia

:recycle: add tests | break-up functions

master
j 2 vuotta sitten
vanhempi
commit
c31702cc3a
10 muutettua tiedostoa jossa 1717 lisäystä ja 146 poistoa
  1. 0
    134
      index.js
  2. 1475
    0
      package-lock.json
  3. 16
    12
      package.json
  4. 30
    0
      src/container.js
  5. 58
    0
      src/index.js
  6. 47
    0
      src/system.js
  7. 30
    0
      src/utils.js
  8. 25
    0
      test/container.spec.js
  9. 19
    0
      test/system.spec.js
  10. 17
    0
      test/utils.spec.js

+ 0
- 134
index.js Näytä tiedosto

@@ -1,134 +0,0 @@
1
-const { nanoid } = require('nanoid')
2
-
3
-const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
4
-const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
5
-
6
-const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
7
-const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
8
-
9
-const makeContainerT = (inputs, outputs, container) => [
10
-    inputs,
11
-    outputs,
12
-    container,
13
-]
14
-
15
-const fromConfig = {
16
-    tank_1: {
17
-        inputs: [1],
18
-        outputs: ['foo'],
19
-        dims: { l: 24, w: 12, h: 12 },
20
-    },
21
-    sump_1: {
22
-        inputs: [],
23
-        outputs: [],
24
-        dims: { l: 18, w: 12, h: 12 },
25
-    },
26
-    ato_1: {
27
-        inputs: [],
28
-        outputs: [],
29
-        dims: { l: 6, w: 12, h: 12 },
30
-    },
31
-}
32
-const digestConfig = config => {
33
-    const sys = new System()
34
-    for (const [containerName, inputsOutputsDims] of Object.entries(config)) {
35
-        const { inputs, outputs, dims } = inputsOutputsDims
36
-        const containerT = makeContainerT(
37
-            inputs,
38
-            outputs,
39
-            new Container(dims, 0, containerName)
40
-        )
41
-        sys.add(containerT[2].id, containerT)
42
-    }
43
-    return sys
44
-}
45
-
46
-class InOut {
47
-    constructor() {
48
-        this.value = null
49
-    }
50
-}
51
-
52
-/** Holds systems or containers */
53
-class Controller {}
54
-
55
-/** Holds systems or containers */
56
-class System {
57
-    constructor() {
58
-        this.inventory = {}
59
-    }
60
-    get inputs() {
61
-        return Object.values(this.inventory).map(containerT => containerT[0])
62
-    }
63
-    get outputs() {
64
-        return Object.values(this.inventory).map(containerT => containerT[1])
65
-    }
66
-    get containers() {
67
-        return Object.values(this.inventory).map(containerT => containerT[2])
68
-    }
69
-    add(id, containerT) {
70
-        this.inventory[id] = containerT
71
-    }
72
-    inputsFor({ id }) {
73
-        return Object.values(this.inventory).find(
74
-            containerT => containerT[2].id == id
75
-        )[0]
76
-    }
77
-    outputsFor({ id }) {
78
-        return Object.values(this.inventory).find(
79
-            containerT => containerT[2].id == id
80
-        )[1]
81
-    }
82
-}
83
-
84
-class Container {
85
-    constructor({ l, w, h }, level = 0, label) {
86
-        this._id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
87
-        this._label = label ? label : ''
88
-        this.l = l
89
-        this.w = w
90
-        this.h = h
91
-        this.level = level
92
-    }
93
-    get id() {
94
-        return this._id
95
-    }
96
-    get label() {
97
-        return this._label
98
-    }
99
-    get liquidVolumeCapacity() {
100
-        return toLiquid(this.l * this.w * this.h)
101
-    }
102
-    get liquidVolumeFilled() {
103
-        return toLiquid(this.l * this.w * this.level)
104
-    }
105
-    get liquidVolumeRemaining() {
106
-        return this.liquidVolumeCapacity - this.liquidVolumeFilled
107
-    }
108
-    setLevel(updatedLevel) {
109
-        this.level = updatedLevel
110
-    }
111
-}
112
-
113
-const _changeLevel = (amount, container) => {
114
-    const updatedLevel =
115
-        fromLiquid(container.liquidVolumeFilled + amount) /
116
-        container.l /
117
-        container.w
118
-    container.setLevel(updatedLevel)
119
-}
120
-const fill = ({ container, amount }) => _changeLevel(amount, container)
121
-const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
122
-
123
-mySystem = digestConfig(fromConfig)
124
-const tanks = mySystem.containers
125
-console.log('mySystem :>> ', mySystem.inventory)
126
-console.log('tank :>> ', tanks[0].liquidVolumeCapacity)
127
-console.log('tank :>> ', tanks[0].liquidVolumeFilled)
128
-fill({ container: tanks[0], amount: 10 })
129
-console.log('tank :>> ', tanks[0].liquidVolumeFilled)
130
-console.log('refs intact?', mySystem.containers[0] === tanks[0])
131
-drain({ container: tanks[0], amount: 3 })
132
-console.log('tank :>> ', tanks[0].liquidVolumeFilled)
133
-console.log('inputs :>> ', mySystem.inputsFor(tanks[0]))
134
-console.log('outputs :>> ', mySystem.outputsFor(tanks[0]))

+ 1475
- 0
package-lock.json
File diff suppressed because it is too large
Näytä tiedosto


+ 16
- 12
package.json Näytä tiedosto

@@ -1,14 +1,18 @@
1 1
 {
2
-  "name": "level",
3
-  "version": "1.0.0",
4
-  "description": "liquid measurement",
5
-  "main": "index.js",
6
-  "scripts": {
7
-    "test": "echo \"Error: no test specified\" && exit 1"
8
-  },
9
-  "author": "TOJ",
10
-  "license": "UNLICENSED",
11
-  "dependencies": {
12
-    "nanoid": "^3.3.7"
13
-  }
2
+    "name": "level",
3
+    "version": "1.0.0",
4
+    "description": "liquid measurement",
5
+    "main": "index.js",
6
+    "scripts": {
7
+        "test": "echo \"Error: no test specified\" && exit 1"
8
+    },
9
+    "type": "module",
10
+    "author": "TOJ",
11
+    "license": "UNLICENSED",
12
+    "dependencies": {
13
+        "nanoid": "^3.3.7"
14
+    },
15
+    "devDependencies": {
16
+        "vitest": "^1.3.1"
17
+    }
14 18
 }

+ 30
- 0
src/container.js Näytä tiedosto

@@ -0,0 +1,30 @@
1
+import { nanoid } from 'nanoid'
2
+import { toLiquid } from './utils.js'
3
+
4
+class Container {
5
+    constructor({ l, w, h }, level = 0, label, id = nanoid()) {
6
+        this._id = id //=> "V1StGXR8_Z5jdHi6B-myT"
7
+        this._label = label ? label : ''
8
+        this.l = l
9
+        this.w = w
10
+        this.h = h
11
+        this.level = level
12
+    }
13
+    get id() {
14
+        return this._id
15
+    }
16
+    get label() {
17
+        return this._label
18
+    }
19
+    get liquidVolumeCapacity() {
20
+        return toLiquid(this.l * this.w * this.h)
21
+    }
22
+    get liquidVolumeFilled() {
23
+        return toLiquid(this.l * this.w * this.level)
24
+    }
25
+    get liquidVolumeRemaining() {
26
+        return this.liquidVolumeCapacity - this.liquidVolumeFilled
27
+    }
28
+}
29
+
30
+export { Container }

+ 58
- 0
src/index.js Näytä tiedosto

@@ -0,0 +1,58 @@
1
+const { makeContainerT } = require('./system.js')
2
+const { Container } = require('./container.js')
3
+const { fill, drain } = require('./utils.js')
4
+
5
+const fromConfig = {
6
+    tank_1: {
7
+        inputs: [1],
8
+        outputs: ['foo'],
9
+        dims: { l: 24, w: 12, h: 12 },
10
+    },
11
+    sump_1: {
12
+        inputs: [],
13
+        outputs: [],
14
+        dims: { l: 18, w: 12, h: 12 },
15
+    },
16
+    ato_1: {
17
+        inputs: [],
18
+        outputs: [],
19
+        dims: { l: 6, w: 12, h: 12 },
20
+    },
21
+}
22
+const digestConfig = config => {
23
+    const sys = new System()
24
+    for (const [containerName, inputsOutputsDims] of Object.entries(config)) {
25
+        const { inputs, outputs, dims } = inputsOutputsDims
26
+        const containerT = makeContainerT(
27
+            inputs,
28
+            outputs,
29
+            new Container(dims, 0, containerName)
30
+        )
31
+        sys.add(containerT[2].id, containerT)
32
+    }
33
+    return sys
34
+}
35
+
36
+/** Holds systems or containers */
37
+class Controller {}
38
+
39
+mySystem = digestConfig(fromConfig)
40
+
41
+console.log('mySystem :>> ', mySystem.inventory)
42
+console.log('\ntank capacity:>> ', mySystem.containers[0].liquidVolumeCapacity)
43
+console.log('tank level:>> ', mySystem.containers[0].liquidVolumeFilled)
44
+console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 0)
45
+
46
+const postFill = fill({ container: mySystem.containers[0], amount: 10 })
47
+mySystem.replaceContainer(postFill)
48
+console.log('\nfilled tank :>> ', mySystem.containers[0].liquidVolumeFilled)
49
+console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 10)
50
+
51
+const postDrain = drain({ container: mySystem.containers[0], amount: 3 })
52
+mySystem.replaceContainer(postDrain)
53
+console.log('\ndrained tank :>> ', mySystem.containers[0].liquidVolumeFilled)
54
+console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 7)
55
+
56
+console.log('\ninputs + outputs')
57
+console.log('inputs :>> ', mySystem.inputsFor(mySystem.containers[0]))
58
+console.log('outputs :>> ', mySystem.outputsFor(mySystem.containers[0]))

+ 47
- 0
src/system.js Näytä tiedosto

@@ -0,0 +1,47 @@
1
+/** Holds systems or containers */
2
+class System {
3
+    constructor() {
4
+        this.inventory = {}
5
+    }
6
+    get inputs() {
7
+        return Object.values(this.inventory).map(containerT => containerT[0])
8
+    }
9
+    get outputs() {
10
+        return Object.values(this.inventory).map(containerT => containerT[1])
11
+    }
12
+    get containers() {
13
+        return Object.values(this.inventory).map(containerT => containerT[2])
14
+    }
15
+    replaceContainer(container) {
16
+        const [inputs, outputs] = this.inventory[container.id]
17
+        this.inventory[container.id] = makeContainerT(
18
+            inputs,
19
+            outputs,
20
+            container
21
+        )
22
+    }
23
+    add(id, containerT) {
24
+        this.inventory[id] = containerT
25
+    }
26
+    remove(id) {
27
+        delete this.inventory[id]
28
+    }
29
+    inputsFor({ id }) {
30
+        return Object.values(this.inventory).find(
31
+            containerT => containerT[2].id == id
32
+        )[0]
33
+    }
34
+    outputsFor({ id }) {
35
+        return Object.values(this.inventory).find(
36
+            containerT => containerT[2].id == id
37
+        )[1]
38
+    }
39
+}
40
+
41
+const makeContainerT = (inputs, outputs, container) => [
42
+    inputs,
43
+    outputs,
44
+    container,
45
+]
46
+
47
+export { System, makeContainerT }

+ 30
- 0
src/utils.js Näytä tiedosto

@@ -0,0 +1,30 @@
1
+import { Container } from './container.js'
2
+
3
+const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
4
+const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
5
+
6
+const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
7
+const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
8
+
9
+const _changeLevel = (amount, container) => {
10
+    const updatedLevel =
11
+        fromLiquid(container.liquidVolumeFilled + amount) /
12
+        container.l /
13
+        container.w
14
+    return new Container(
15
+        {
16
+            l: container.l,
17
+            w: container.w,
18
+            h: container.h,
19
+        },
20
+        updatedLevel,
21
+        container.label,
22
+        container.id
23
+    )
24
+}
25
+
26
+// Human API
27
+const fill = ({ container, amount }) => _changeLevel(amount, container)
28
+const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
29
+
30
+export { toLiquid, fromLiquid, fill, drain }

+ 25
- 0
test/container.spec.js Näytä tiedosto

@@ -0,0 +1,25 @@
1
+import { expect, test } from 'vitest'
2
+import { Container } from '../src/container.js'
3
+
4
+const dims = { l: 10, w: 10, h: 10 } // inches
5
+const capacity = 4.329004329004329 // gallons
6
+
7
+test('container - calculates liquid volume correctly', () => {
8
+    let testContainer = new Container(dims)
9
+    expect(testContainer.level).toBe(0)
10
+    expect(testContainer.liquidVolumeCapacity).toBe(capacity)
11
+    expect(testContainer.liquidVolumeFilled).toBe(0)
12
+    expect(testContainer.liquidVolumeRemaining).toBe(capacity)
13
+
14
+    testContainer = new Container(dims, dims.h / 2)
15
+    expect(testContainer.level).toBe(dims.h / 2)
16
+    expect(testContainer.liquidVolumeCapacity).toBe(capacity)
17
+    expect(testContainer.liquidVolumeFilled).toBe(capacity / 2)
18
+    expect(testContainer.liquidVolumeRemaining).toBe(capacity - capacity / 2)
19
+
20
+    testContainer = new Container(dims, dims.h)
21
+    expect(testContainer.level).toBe(dims.h)
22
+    expect(testContainer.liquidVolumeCapacity).toBe(capacity)
23
+    expect(testContainer.liquidVolumeFilled).toBe(capacity)
24
+    expect(testContainer.liquidVolumeRemaining).toBe(0)
25
+})

+ 19
- 0
test/system.spec.js Näytä tiedosto

@@ -0,0 +1,19 @@
1
+import { expect, test } from 'vitest'
2
+import { System } from '../src/system.js'
3
+
4
+test('system - instantiates and stores inventory correctly', () => {
5
+    let testSystem = new System()
6
+    testSystem.add('aaa', ['input_test', 'output_test', { id: 'aaa' }])
7
+    expect(testSystem.inputs).toStrictEqual(['input_test'])
8
+    expect(testSystem.outputs).toStrictEqual(['output_test'])
9
+    expect(testSystem.containers[0].id).toStrictEqual('aaa')
10
+
11
+    // Make sure inputs and outputs get reassigned
12
+    testSystem.replaceContainer({ id: 'aaa' })
13
+    expect(testSystem.inputs).toStrictEqual(['input_test'])
14
+    expect(testSystem.outputs).toStrictEqual(['output_test'])
15
+    expect(testSystem.containers[0].id).toStrictEqual('aaa')
16
+
17
+    testSystem.remove('aaa')
18
+    expect(testSystem.inventory).toStrictEqual({})
19
+})

+ 17
- 0
test/utils.spec.js Näytä tiedosto

@@ -0,0 +1,17 @@
1
+import { expect, test } from 'vitest'
2
+import { Container } from '../src/container.js'
3
+import { fill, drain } from '../src/utils.js'
4
+
5
+const dims = { l: 10, w: 10, h: 10 } // inches
6
+const capacity = 4.329004329004329 // gallons
7
+
8
+test('utils - fill and drain work correctly', () => {
9
+    let testContainer = new Container(dims)
10
+    testContainer = fill({ container: testContainer, amount: capacity })
11
+    expect(testContainer.liquidVolumeFilled).toBe(capacity)
12
+    expect(testContainer.liquidVolumeRemaining).toBe(0)
13
+
14
+    testContainer = drain({ container: testContainer, amount: capacity })
15
+    expect(testContainer.liquidVolumeFilled).toBe(0)
16
+    expect(testContainer.liquidVolumeRemaining).toBe(capacity)
17
+})

Loading…
Peruuta
Tallenna