forked from mdisibio/dex-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (108 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
var fs = require('fs')
class DexData {
constructor() {
this.products = [];
}
startItem(name) {
this.currItem = {}
this.products.push(this.currItem)
}
ensureItem() {
if(this.currItem == undefined) {
this.startItem()
}
}
complete() {
delete this.currItem
}
}
function assign(parts, fieldNumber, object, property, lambda) {
var fieldValue = parts[fieldNumber]
if(typeof(fieldValue) == 'string') {
fieldValue = fieldValue.trim()
if(lambda != undefined) {
object[property] = lambda(fieldValue)
} else {
object[property] = fieldValue
}
}
}
exports.DexData = DexData;
var parseID1 = function(line, context) {
var parts = line.split("*")
context.machine = context.machine || {}
assign(parts, 1, context.machine, "serialNumber")
assign(parts, 2, context.machine, "modelNumber")
assign(parts, 3, context.machine, "buildStandard")
assign(parts, 6, context.machine, "assetNumber")
}
var parseCB1 = function(line, context) {
var parts = line.split("*")
context.machine = context.machine || {}
context.machine.controlBoard = context.machine.controlBoard || {}
assign(parts, 1, context.machine.controlBoard, "serialNumber")
assign(parts, 2, context.machine.controlBoard, "modelNumber")
assign(parts, 3, context.machine.controlBoard, "softwareRevision")
}
var parsePA1 = function(line, context) {
context.startItem()
var parts = line.split("*")
assign(parts, 1, context.currItem, "name")
assign(parts, 2, context.currItem, "price", (i) => i / 100)
}
var parsePA2 = function(line, context) {
context.ensureItem()
var parts = line.split("*")
assign(parts, 1, context.currItem, "sold", (i) => Number(i))
assign(parts, 2, context.currItem, "revenue", (i) => i / 100)
}
var parsePA3 = function(line, context) {
context.ensureItem()
var parts = line.split("*")
assign(parts, 1, context.currItem, "testVendCount")
}
var parsePA5 = function(line, context) {
context.ensureItem()
var parts = line.split("*")
assign(parts, 1, context.currItem, "soldOutDate")
assign(parts, 2, context.currItem, "soldOutTime")
assign(parts, 3, context.currItem, "soldOutCount")
}
var defaultHandlers = {
"ID1" : parseID1,
"CB1" : parseCB1,
"PA1" : parsePA1,
"PA2" : parsePA2,
"PA3" : parsePA3,
"PA5" : parsePA5
};
exports.readText = function(text, cb) {
var handlers = defaultHandlers
text = text.toString()
if(text.length <= 0) {
cb(new Error('file or text was empty'));
return
}
var lines = text.toString().split('\n')
var dexdata = new DexData()
lines.forEach(function(line) {
line = line.replace("\r", "")
var prefix2 = line.substring(0, 2)
var prefix3 = line.substring(0, 3)
var handler = handlers[prefix3] || handlers[prefix2]
if(handler) {
handler(line, dexdata)
}
})
dexdata.complete()
cb(undefined, dexdata);
};
exports.readFile = function(path, cb) {
fs.readFile(path, (err, data) => {
if(err) {
cb(err);
};
exports.readText(data, cb);
})
};