-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
122 lines (96 loc) · 3.03 KB
/
script.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
"use strict";
const form = document.querySelector("#form");
console.log("v6");
// model.createInvArray();
let header = "";
// --- Functions ---
// Convert text string to array
const textToArray = function (string) {
header = string.slice(0, string.indexOf("\n")).split(",");
const rows = string.slice(string.indexOf("\n") + 1).split("\n");
const arr = rows.map((row) => {
// split data rows into arrays of strings
const splitRows = row.split(",");
// split each string element into [entry, values] pairs
const objArr = splitRows.map((el, i) => {
const obj = [header[i], el];
return obj;
});
// convert [entry, value] pairs to {'entry': 'value'} objects
const array = Object.fromEntries(objArr);
return array;
});
return arr;
};
const arrayToCSVString = function (LTOs, header) {
const string = [
header,
...LTOs.map((item) => [
item["Product Sub-Category"],
item["Product Class"],
item["SKU"],
item["Product Name"],
item["Wholesale Price"],
item["Price Promotion Amt"],
item["Final Wholesale Price"],
item["Packaging"],
item[""],
item["Country"],
item["Agent Name\r"],
]),
]
.map((e) => e.join(","))
.join("\n");
return string;
};
// Controlled event
form.addEventListener("submit", function (e) {
e.preventDefault();
console.log("CSV file submitted");
const input = invFile.files[0];
const input2 = ltoFile.files[0];
console.log(input);
console.log(input2);
// FileReader is a JS Object that asynchronously reads the contents of files
const reader = new FileReader();
// Reads content of input
reader.readAsText(input);
// function uses reader to read CSV file to text and convert text to array once file has been loaded.
reader.addEventListener("load", function (e) {
// The function takes the submit event target (the csv file) and stores the result to variable 'text'
console.log(e);
const textInv = e.target.result;
// convert text string to array of objects
const dataInv = textToArray(textInv);
// console.log(data);
// write data to browswer screen
// document.write(JSON.stringify(data));
// Search data array for desired value and create final data array
// const LTOs = [];
const readerInv = new FileReader();
readerInv.readAsText(input2);
readerInv.addEventListener("load", function (e) {
console.log(e);
const textLTO = e.target.result;
const data = textToArray(textLTO);
data.pop();
console.log(dataInv);
console.log(data);
const LTOinInv = [];
data.forEach((obj) => {
dataInv.forEach((inv) => {
if (obj["SKU"] === inv["SKU"]) {
LTOinInv.push(obj);
}
});
});
console.log(LTOinInv);
// create a csv String
const csvFile = arrayToCSVString(LTOinInv, header);
const myFile = new File([csvFile], "LTOs_in_inventory.txt", {
type: "text/plain;charset=utf-8",
});
saveAs(myFile);
});
});
});