Procházet zdrojové kódy

:recycle: use an id for each system

master
j před 2 roky
rodič
revize
a0624735bb
1 změnil soubory, kde provedl 103 přidání a 28 odebrání
  1. 103
    28
      index.js

+ 103
- 28
index.js Zobrazit soubor

1
+const { nanoid } = require('nanoid')
2
+
1
 const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
3
 const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
2
 const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
4
 const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
3
 
5
 
4
 const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
6
 const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
5
 const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
7
 const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
6
 
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
+
7
 class Container {
84
 class Container {
8
-    constructor({ l, w, h }, level = 0) {
85
+    constructor({ l, w, h }, level = 0, label) {
86
+        this._id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
87
+        this._label = label ? label : ''
9
         this.l = l
88
         this.l = l
10
         this.w = w
89
         this.w = w
11
         this.h = h
90
         this.h = h
12
         this.level = level
91
         this.level = level
13
     }
92
     }
93
+    get id() {
94
+        return this._id
95
+    }
96
+    get label() {
97
+        return this._label
98
+    }
14
     get liquidVolumeCapacity() {
99
     get liquidVolumeCapacity() {
15
         return toLiquid(this.l * this.w * this.h)
100
         return toLiquid(this.l * this.w * this.h)
16
     }
101
     }
20
     get liquidVolumeRemaining() {
105
     get liquidVolumeRemaining() {
21
         return this.liquidVolumeCapacity - this.liquidVolumeFilled
106
         return this.liquidVolumeCapacity - this.liquidVolumeFilled
22
     }
107
     }
108
+    setLevel(updatedLevel) {
109
+        this.level = updatedLevel
110
+    }
23
 }
111
 }
24
 
112
 
25
-/** Functional Core */
26
 const _changeLevel = (amount, container) => {
113
 const _changeLevel = (amount, container) => {
27
-    const alteredLevel =
114
+    const updatedLevel =
28
         fromLiquid(container.liquidVolumeFilled + amount) /
115
         fromLiquid(container.liquidVolumeFilled + amount) /
29
         container.l /
116
         container.l /
30
         container.w
117
         container.w
31
-    return createContainer(container, alteredLevel)
32
-}
33
-const createContainer = ({ l, w, h }, level) => {
34
-    if (Math.sign(l) + Math.sign(w) + Math.sign(h) !== 3)
35
-        return console.error(
36
-            `[Error] All dimensions must be positive and greater than zero!`
37
-        )
38
-    return new Container({ l, w, h }, level)
118
+    container.setLevel(updatedLevel)
39
 }
119
 }
40
 const fill = ({ container, amount }) => _changeLevel(amount, container)
120
 const fill = ({ container, amount }) => _changeLevel(amount, container)
41
 const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
121
 const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
42
 
122
 
43
-myTank = createContainer({ l: 12, w: 12, h: 12 })
44
-console.log('capacity:', myTank.liquidVolumeCapacity)
45
-console.log('filled:', myTank.liquidVolumeFilled)
46
-
47
-myTank = fill({ amount: myTank.liquidVolumeCapacity, container: myTank })
48
-console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
49
-console.log('remain h:', myTank.h - myTank.level)
50
-
51
-myTank = drain({ amount: 4, container: myTank })
52
-console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
53
-console.log('remain h:', myTank.h - myTank.level)
54
-console.log('filled:', myTank.liquidVolumeFilled)
55
-
56
-myTank = fill({ amount: 3, container: myTank })
57
-console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
58
-console.log('remain h:', myTank.h - myTank.level)
59
-console.log('filled:', myTank.liquidVolumeFilled)
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]))

Načítá se…
Zrušit
Uložit