-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
253 lines (241 loc) · 6.12 KB
/
mod.ts
File metadata and controls
253 lines (241 loc) · 6.12 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import originalPl from "polars";
import { readExcel } from "./src/read_excel.ts";
import { writeExcel } from "./src/write_excel.ts";
import type {
ExtendedDataFrame,
ExtendedPolars,
ReadExcelOptions,
WriteExcelOptions,
} from "./types/mod.ts";
export type * from "./types/mod.ts";
/**
* A wrapper function for the original DataFrame constructor from the `nodejs-polars` library.
* This function ensures that the `writeExcel` method is available on the DataFrame instance.
*
* @param {...Parameters<typeof originalPl.DataFrame>} args - The arguments to be passed to the original DataFrame constructor.
* @returns {ExtendedDataFrame} - The extended DataFrame instance with `writeExcel` method.
*
* @remarks
* This function dynamically adds the `writeExcel` method to the DataFrame instance if it doesn't already exist.
* It also extends various DataFrame methods to ensure that any new DataFrame returned by these methods
* also includes the `writeExcel` method.
*
* The methods extended include:
* - clone
* - describe
* - drop
* - dropNulls
* - explode
* - extend
* - fillNull
* - filter
* - frameEqual
* - head
* - hstack
* - interpolate
* - join
* - joinAsof
* - limit
* - max
* - mean
* - median
* - unpivot
* - min
* - nullCount
* - partitionBy
* - pivot
* - quantile
* - rechunk
* - rename
* - select
* - shift
* - shiftAndFill
* - shrinkToFit
* - slice
* - sort
* - std
* - sum
* - tail
* - transpose
* - unique
* - unnest
* - var
* - vstack
* - withColumn
* - withColumns
* - withColumnRenamed
* - withRowCount
* - where
* - upsample
*
* @example
* ```typescript
* const df = WrappedDataFrame(data);
* await df.writeExcel('output.xlsx');
* ```
*/
const WrappedDataFrame = function <
T extends Record<string, originalPl.Series<any, string>>,
>(
...args: Parameters<typeof originalPl.DataFrame>
): ExtendedDataFrame<T> {
const instance = originalPl.DataFrame(
...args,
) as unknown as ExtendedDataFrame<T>;
// Add the `writeExcel` method if it doesn't exist
if (!instance.writeExcel) {
instance.writeExcel = async function (
filePath: string,
options?: WriteExcelOptions,
): Promise<void> {
await writeExcel(this, filePath, options);
};
}
// Extend the methods that return a new DataFrame
([
"clone",
"describe",
"drop",
"dropNulls",
"explode",
"extend",
"fillNull",
"filter",
"frameEqual",
"head",
"hstack",
"interpolate",
"join",
"joinAsof",
// "lazy",
"limit",
"max",
"mean",
"median",
"unpivot",
"min",
"nullCount",
"partitionBy",
"pivot",
"quantile",
"rechunk",
"rename",
"select",
"shift",
"shiftAndFill",
"shrinkToFit",
"slice",
"sort",
"std",
"sum",
"tail",
"transpose",
"unique",
"unnest",
"var",
"vstack",
"withColumn",
"withColumns",
"withColumnRenamed",
"withRowCount",
"where",
"upsample",
] as Array<keyof ExtendedDataFrame<any>>).forEach((method) => {
const originalMethod = instance[method].bind(instance);
Object.defineProperty(instance, method, {
value: function (
...args: Parameters<typeof originalMethod>
): ExtendedDataFrame<any> {
const newDf = originalMethod(...args);
return WrappedDataFrame(
newDf as unknown as Parameters<typeof originalPl.DataFrame>[0],
);
},
writable: true,
configurable: true,
});
});
return instance;
};
// Replace the DataFrame factory with the wrapped one
const extendedPl = {
...originalPl,
DataFrame: WrappedDataFrame as <
T extends Record<string, originalPl.Series<any, string>>,
>(
...args: Parameters<typeof originalPl.DataFrame>
) => ExtendedDataFrame<T>,
readExcel: async function (
filePath: string,
options?: ReadExcelOptions,
): Promise<ExtendedDataFrame<any>> {
const df = await readExcel(filePath, options);
// Wrap the returned DataFrame to add the writeExcel method
return WrappedDataFrame(
df as unknown as Parameters<typeof originalPl.DataFrame>[0],
);
},
writeExcel,
ExtendedDataFrame: null as unknown as ExtendedDataFrame<any>,
} as ExtendedPolars;
// Override the top-level `concat` function to return an ExtendedDataFrame
extendedPl.concat = function (
items: any[],
options?: {
rechunk?: boolean;
how?:
| "vertical"
| "diagonal"
| "horizontal"
| "verticalRelaxed"
| "diagonalRelaxed"
| "align"
| "alignInner"
| "alignFull"
| "alignLeft"
| "alignRight";
},
): any {
const result = originalPl.concat(items, options);
// Check if result is a DataFrame and wrap it, else return raw
if (
originalPl.DataFrame.prototype.constructor &&
result instanceof originalPl.DataFrame
) {
return WrappedDataFrame(result);
}
// Series or LazyDataFrame — return as-is
return result;
};
/**
* Extended version of the `polars` library that provides:
* - the original `polars` object with all its features,
* - a wrapped `DataFrame` class (called `WrappedDataFrame`) with an added `writeExcel` method,
* - `readExcel` and `writeExcel` functions for reading and writing Excel files,
* - and the `ExtendedDataFrame` type for the extended DataFrame.
*
* Main enhancements compared to the original library:
* - the `writeExcel` method is available directly on DataFrame instances,
* - all DataFrame methods returning new DataFrames return the wrapped ExtendedDataFrame with `writeExcel`,
* - the `concat` function returns an ExtendedDataFrame,
* - the `readExcel` and `writeExcel` functions are exposed directly on the export.
*
* @remarks
* This export allows seamless use of Excel-related functionality without manually wrapping DataFrame objects.
*
* @example
* ```ts
* import pl from "@jackfiszr/pl2xl";
*
* // Create a DataFrame
* const df = pl.DataFrame({ a: [1, 2, 3] });
*
* // Write to Excel
* await df.writeExcel("file.xlsx");
*
* // Read from Excel
* const df2 = await pl.readExcel("file.xlsx");
* ```
*/
export default extendedPl as ExtendedPolars;
export { type ExtendedDataFrame, readExcel, writeExcel };