-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
188 lines (177 loc) · 4.07 KB
/
mod.ts
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
import originalPl from "polars";
import { readExcel } from "./read_excel.ts";
import { writeExcel } from "./write_excel.ts";
import type {
ExtendedDataFrame,
ReadExcelOptions,
WriteExcelOptions,
} from "./types.d.ts";
export type * from "polars";
/**
* 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 (
...args: Parameters<typeof originalPl.DataFrame>
): ExtendedDataFrame<any> {
const instance = originalPl.DataFrame(...args) as ExtendedDataFrame<any>;
// 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> {
// Call the original method
const newDf = originalMethod(...args);
// Wrap the returned DataFrame to add the writeExcel method
return WrappedDataFrame(newDf);
},
writable: true,
configurable: true,
});
},
);
return instance;
};
// Replace the DataFrame factory with the wrapped one
const extendedPl = {
...originalPl,
DataFrame: WrappedDataFrame,
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);
},
writeExcel,
ExtendedDataFrame: null as unknown as ExtendedDataFrame<any>,
};
export { type ExtendedDataFrame, readExcel, writeExcel };
// Export the extended Polars object
export default extendedPl;