Skip to content

Commit dd3995f

Browse files
committed
Update io methods
1 parent dd33222 commit dd3995f

File tree

11 files changed

+966
-125
lines changed

11 files changed

+966
-125
lines changed

danfojs-node/dist/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,42 @@ Object.defineProperty(exports, "toDateTime", {
9797
return _datetime.toDateTime;
9898
}
9999
});
100+
Object.defineProperty(exports, "read_csv", {
101+
enumerable: true,
102+
get: function () {
103+
return _io.readCSV;
104+
}
105+
});
106+
Object.defineProperty(exports, "to_csv", {
107+
enumerable: true,
108+
get: function () {
109+
return _io.toCSV;
110+
}
111+
});
112+
Object.defineProperty(exports, "read_json", {
113+
enumerable: true,
114+
get: function () {
115+
return _io.readJSON;
116+
}
117+
});
118+
Object.defineProperty(exports, "to_json", {
119+
enumerable: true,
120+
get: function () {
121+
return _io.toJSON;
122+
}
123+
});
124+
Object.defineProperty(exports, "read_excel", {
125+
enumerable: true,
126+
get: function () {
127+
return _io.readExcel;
128+
}
129+
});
130+
Object.defineProperty(exports, "to_excel", {
131+
enumerable: true,
132+
get: function () {
133+
return _io.toExcel;
134+
}
135+
});
100136
exports._version = void 0;
101137

102138
var _generic = _interopRequireDefault(require("./core/generic"));
@@ -123,5 +159,7 @@ var _strings = _interopRequireDefault(require("./core/strings"));
123159

124160
var _datetime = _interopRequireWildcard(require("./core/datetime"));
125161

162+
var _io = require("./io");
163+
126164
const _version = "0.2.8";
127165
exports._version = _version;

danfojs-node/dist/io/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
Object.defineProperty(exports, "readCSV", {
7+
enumerable: true,
8+
get: function () {
9+
return _io.$readCSV;
10+
}
11+
});
12+
Object.defineProperty(exports, "streamCSV", {
13+
enumerable: true,
14+
get: function () {
15+
return _io.$streamCSV;
16+
}
17+
});
18+
Object.defineProperty(exports, "toCSV", {
19+
enumerable: true,
20+
get: function () {
21+
return _io.$toCSV;
22+
}
23+
});
24+
Object.defineProperty(exports, "readJSON", {
25+
enumerable: true,
26+
get: function () {
27+
return _io2.$readJSON;
28+
}
29+
});
30+
Object.defineProperty(exports, "toJSON", {
31+
enumerable: true,
32+
get: function () {
33+
return _io2.$toJSON;
34+
}
35+
});
36+
Object.defineProperty(exports, "readExcel", {
37+
enumerable: true,
38+
get: function () {
39+
return _io3.$readExcel;
40+
}
41+
});
42+
Object.defineProperty(exports, "toExcel", {
43+
enumerable: true,
44+
get: function () {
45+
return _io3.$toExcel;
46+
}
47+
});
48+
49+
var _io = require("./io.csv");
50+
51+
var _io2 = require("./io.json");
52+
53+
var _io3 = require("./io.excel");

danfojs-node/dist/io/io.csv.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"use strict";
2+
3+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4+
5+
Object.defineProperty(exports, "__esModule", {
6+
value: true
7+
});
8+
exports.$toCSV = exports.$readCSV = void 0;
9+
10+
var _fs = _interopRequireDefault(require("fs"));
11+
12+
var _papaparse = _interopRequireDefault(require("papaparse"));
13+
14+
var _request = _interopRequireDefault(require("request"));
15+
16+
var _index = require("../index");
17+
18+
/**
19+
* @license
20+
* Copyright 2021, JsData. All rights reserved.
21+
*
22+
* This source code is licensed under the MIT license found in the
23+
* LICENSE file in the root directory of this source tree.
24+
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
* ==========================================================================
31+
*/
32+
const $readCSV = async (filePath, options) => {
33+
if (filePath.startsWith("http") || filePath.startsWith("https")) {
34+
return new Promise(resolve => {
35+
const dataStream = _request.default.get(filePath);
36+
37+
const parseStream = _papaparse.default.parse(_papaparse.default.NODE_STREAM_INPUT, options);
38+
39+
dataStream.pipe(parseStream);
40+
const data = [];
41+
parseStream.on("data", chunk => {
42+
data.push(chunk);
43+
});
44+
parseStream.on("finish", () => {
45+
resolve(new _index.DataFrame(data));
46+
});
47+
});
48+
} else {
49+
return new Promise(resolve => {
50+
const fileStream = _fs.default.createReadStream(filePath);
51+
52+
_papaparse.default.parse(fileStream, { ...options,
53+
complete: results => {
54+
const df = new _index.DataFrame(results.data);
55+
resolve(df);
56+
}
57+
});
58+
});
59+
}
60+
};
61+
62+
exports.$readCSV = $readCSV;
63+
64+
const $toCSV = (df, options) => {
65+
let {
66+
filePath,
67+
sep,
68+
header
69+
} = {
70+
sep: ",",
71+
header: true,
72+
filePath: undefined,
73+
...options
74+
};
75+
76+
if (df.$isSeries) {
77+
const csv = df.values.join(sep);
78+
79+
if (filePath !== undefined) {
80+
if (!filePath.endsWith(".csv")) {
81+
filePath = filePath + ".csv";
82+
}
83+
84+
_fs.default.writeFileSync(filePath, csv, "utf8");
85+
} else {
86+
return csv;
87+
}
88+
} else {
89+
const rows = df.values;
90+
let csvStr = header === true ? `${df.columns.join(sep)}\n` : "";
91+
92+
for (let i = 0; i < rows.length; i++) {
93+
const row = `${rows[i].join(sep)}\n`;
94+
csvStr += row;
95+
}
96+
97+
if (filePath !== undefined) {
98+
if (!filePath.endsWith(".csv")) {
99+
filePath = filePath + ".csv";
100+
}
101+
102+
_fs.default.writeFileSync(filePath, csvStr, "utf8");
103+
} else {
104+
return csvStr;
105+
}
106+
}
107+
};
108+
109+
exports.$toCSV = $toCSV;

danfojs-node/dist/io/io.excel.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"use strict";
2+
3+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4+
5+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
6+
7+
Object.defineProperty(exports, "__esModule", {
8+
value: true
9+
});
10+
exports.$toExcel = exports.$readExcel = void 0;
11+
12+
var _nodeFetch = _interopRequireWildcard(require("node-fetch"));
13+
14+
var _index = require("../index");
15+
16+
var _xlsx = _interopRequireDefault(require("xlsx"));
17+
18+
/**
19+
* @license
20+
* Copyright 2021, JsData. All rights reserved.
21+
*
22+
* This source code is licensed under the MIT license found in the
23+
* LICENSE file in the root directory of this source tree.
24+
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
* ==========================================================================
31+
*/
32+
const $readExcel = async (filePath, options) => {
33+
const {
34+
sheet,
35+
method,
36+
headers
37+
} = {
38+
sheet: 0,
39+
method: "GET",
40+
headers: {},
41+
...options
42+
};
43+
44+
if (filePath.startsWith("http") || filePath.startsWith("https")) {
45+
return new Promise(resolve => {
46+
(0, _nodeFetch.default)(filePath, {
47+
method,
48+
headers
49+
}).then(response => {
50+
if (response.status !== 200) {
51+
throw new Error(`Failed to load ${filePath}`);
52+
}
53+
54+
response.arrayBuffer().then(arrBuf => {
55+
const arrBufInt8 = new Uint8Array(arrBuf);
56+
57+
const workbook = _xlsx.default.read(arrBufInt8, {
58+
type: "array"
59+
});
60+
61+
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
62+
63+
const data = _xlsx.default.utils.sheet_to_json(worksheet);
64+
65+
const df = new _index.DataFrame(data);
66+
resolve(df);
67+
});
68+
}).catch(err => {
69+
throw new Error(err);
70+
});
71+
});
72+
} else {
73+
return new Promise(resolve => {
74+
const workbook = _xlsx.default.readFile(filePath);
75+
76+
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
77+
78+
const data = _xlsx.default.utils.sheet_to_json(worksheet);
79+
80+
const df = new _index.DataFrame(data);
81+
resolve(df);
82+
});
83+
}
84+
};
85+
86+
exports.$readExcel = $readExcel;
87+
88+
const $toExcel = (df, options) => {
89+
let {
90+
filePath,
91+
sheetName
92+
} = {
93+
filePath: "./output.xlsx",
94+
sheetName: "Sheet1",
95+
...options
96+
};
97+
98+
if (!filePath.endsWith(".xlsx")) {
99+
filePath = filePath + ".xlsx";
100+
}
101+
102+
let data;
103+
104+
if (df.$isSeries) {
105+
const row = df.values;
106+
const col = df.columns;
107+
data = [col, ...row.map(x => [x])];
108+
} else {
109+
const row = df.values;
110+
const cols = df.columns;
111+
data = [cols, ...row];
112+
}
113+
114+
const worksheet = _xlsx.default.utils.aoa_to_sheet(data);
115+
116+
const wb = _xlsx.default.utils.book_new();
117+
118+
_xlsx.default.utils.book_append_sheet(wb, worksheet, sheetName);
119+
120+
_xlsx.default.writeFile(wb, `${filePath}`);
121+
};
122+
123+
exports.$toExcel = $toExcel;

0 commit comments

Comments
 (0)