Skip to content

Commit f9437a0

Browse files
committed
Add function overloads
1 parent b089c12 commit f9437a0

15 files changed

Lines changed: 412 additions & 157 deletions

File tree

danfojs-browser/lib/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

danfojs-browser/lib/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

danfojs-browser/src/core/frame.ts

Lines changed: 149 additions & 22 deletions
Large diffs are not rendered by default.

danfojs-browser/src/core/series.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,10 @@ export default class Series extends NDframe implements SeriesInterface {
908908
break;
909909
}
910910
}
911-
return new Series(data);
911+
return new Series(data, {
912+
index: this.index,
913+
config: { ...this.config }
914+
});
912915

913916
}
914917

@@ -1245,7 +1248,7 @@ export default class Series extends NDframe implements SeriesInterface {
12451248
return table(tabledata as any);
12461249
}
12471250

1248-
/**
1251+
/**
12491252
* Returns the logical AND between Series and other. Supports element wise operations and broadcasting.
12501253
* @param other Series, Scalar, Array of Scalars
12511254
*/
@@ -1254,6 +1257,7 @@ export default class Series extends NDframe implements SeriesInterface {
12541257
if (other === undefined) {
12551258
throw new Error("Param Error: other cannot be undefined");
12561259
}
1260+
const newValues: ArrayType1D = [];
12571261

12581262
if (other instanceof Series) {
12591263
if (this.dtypes[0] !== other.dtypes[0]) {
@@ -1264,36 +1268,29 @@ export default class Series extends NDframe implements SeriesInterface {
12641268
throw new Error("Param Error: Series must be of same shape");
12651269
}
12661270

1267-
const newValues: ArrayType1D = [];
1268-
12691271
this.values.forEach((val, i) => {
12701272
newValues.push(Boolean(val) && Boolean(other.values[i]));
12711273
});
12721274

1273-
return new Series(newValues, {
1274-
config: { ...this.config }
1275-
})
1275+
} else if (typeof other === "boolean") {
1276+
1277+
this.values.forEach((val) => {
1278+
newValues.push(Boolean(val) && Boolean(other));
1279+
});
1280+
12761281
} else if (Array.isArray(other)) {
1277-
const newValues: ArrayType1D = [];
12781282

12791283
this.values.forEach((val, i) => {
12801284
newValues.push(Boolean(val) && Boolean(other[i]));
12811285
});
12821286

1283-
return new Series(newValues, {
1284-
config: { ...this.config }
1285-
})
12861287
} else {
1287-
const newValues: ArrayType1D = [];
1288-
1289-
this.values.forEach((val) => {
1290-
newValues.push(Boolean(val) && Boolean(other));
1291-
});
1292-
1293-
return new Series(newValues, {
1294-
config: { ...this.config }
1295-
})
1288+
throw new Error("Param Error: other must be a Series, Scalar, or Array of Scalars");
12961289
}
1290+
return new Series(newValues, {
1291+
index: this.index,
1292+
config: { ...this.config }
1293+
});
12971294
}
12981295

12991296
/**
@@ -1305,6 +1302,7 @@ export default class Series extends NDframe implements SeriesInterface {
13051302
if (other === undefined) {
13061303
throw new Error("Param Error: other cannot be undefined");
13071304
}
1305+
const newValues: ArrayType1D = [];
13081306

13091307
if (other instanceof Series) {
13101308
if (this.dtypes[0] !== other.dtypes[0]) {
@@ -1315,40 +1313,33 @@ export default class Series extends NDframe implements SeriesInterface {
13151313
throw new Error("Param Error: Series must be of same shape");
13161314
}
13171315

1318-
const newValues: ArrayType1D = [];
1319-
13201316
this.values.forEach((val, i) => {
13211317
newValues.push(Boolean(val) || Boolean(other.values[i]));
13221318
});
13231319

1324-
return new Series(newValues, {
1325-
config: { ...this.config }
1326-
})
13271320
} else if (typeof other === "boolean") {
1328-
const newValues: ArrayType1D = [];
13291321

13301322
this.values.forEach((val) => {
13311323
newValues.push(Boolean(val) || Boolean(other));
13321324
});
13331325

1334-
return new Series(newValues, {
1335-
config: { ...this.config }
1336-
})
13371326
} else if (Array.isArray(other)) {
1338-
const newValues: ArrayType1D = [];
13391327

13401328
this.values.forEach((val, i) => {
13411329
newValues.push(Boolean(val) || Boolean(other[i]));
13421330
});
13431331

1344-
return new Series(newValues, {
1345-
config: { ...this.config }
1346-
})
13471332
} else {
13481333
throw new Error("Param Error: other must be a Series, Scalar, or Array of Scalars");
13491334
}
1335+
1336+
return new Series(newValues, {
1337+
index: this.index,
1338+
config: { ...this.config }
1339+
});
13501340
}
13511341

1342+
13521343
/**
13531344
* One-hot encode values in the Series.
13541345
* @param options Options for the operation. The following options are available:

danfojs-browser/src/core/strings.ts

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export default class Str {
4040
* // ["good", "bad", "crazy"]
4141
* ```
4242
*/
43-
toLowerCase(options: { inplace: boolean } = { inplace: false }): Series | void {
44-
const { inplace = false } = options;
43+
toLowerCase(options?: { inplace?: boolean }): Series
44+
toLowerCase(options?: { inplace?: boolean }): Series | void {
45+
const { inplace } = { inplace: false, ...options }
4546
const newArr: Array<string | number> = [];
4647
this.values.map((val) => {
4748
if (isNaN(val as number) && typeof val != "string") {
@@ -76,8 +77,9 @@ export default class Str {
7677
* // ["GOOD", "BAD", "CRAZY"]
7778
* ```
7879
*/
79-
toUpperCase(options: { inplace: boolean } = { inplace: false }): Series | void {
80-
const { inplace = false } = options;
80+
toUpperCase(options?: { inplace?: boolean }): Series
81+
toUpperCase(options?: { inplace?: boolean }): Series | void {
82+
const { inplace } = { inplace: false, ...options }
8183
const newArr: Array<string | number> = [];
8284
this.values.map((val) => {
8385
if (isNaN(val as number) && typeof val != "string") {
@@ -111,8 +113,9 @@ export default class Str {
111113
* // ["Good", "Bad", "Crazy"]
112114
* ```
113115
*/
114-
capitalize(options: { inplace: boolean } = { inplace: false }): Series | void {
115-
const { inplace = false } = options;
116+
capitalize(options?: { inplace?: boolean }): Series
117+
capitalize(options?: { inplace?: boolean }): Series | void {
118+
const { inplace } = { inplace: false, ...options }
116119
const newArr: Array<string | number> = [];
117120
this.values.map((val) => {
118121
if (isNaN(val as number) && typeof val != "string") {
@@ -151,8 +154,9 @@ export default class Str {
151154
* // ["o", "a", "r"]
152155
* ```
153156
*/
154-
charAt(index = 0, options: { inplace: boolean } = { inplace: false }): Series | void {
155-
const { inplace = false } = options;
157+
charAt(index: number, options?: { inplace?: boolean }): Series
158+
charAt(index = 0, options?: { inplace?: boolean }): Series | void {
159+
const { inplace } = { inplace: false, ...options }
156160
const newArr: Array<string | number> = [];
157161
this.values.map((val) => {
158162
if (isNaN(val as number) && typeof val != "string") {
@@ -186,8 +190,9 @@ export default class Str {
186190
* // ["Good_new", "bad_new", "crazy_new"
187191
* ```
188192
*/
189-
concat(other: Array<string> | string, position = 1, options: { inplace: boolean } = { inplace: false }): Series | void {
190-
const { inplace = false } = options;
193+
concat(other: Array<string> | string, position: number, options?: { inplace?: boolean }): Series
194+
concat(other: Array<string> | string, position = 1, options?: { inplace?: boolean }): Series | void {
195+
const { inplace } = { inplace: false, ...options }
191196
const newArr: Array<string | number> = [];
192197

193198
if (Array.isArray(other)) {
@@ -246,8 +251,9 @@ export default class Str {
246251
* // [true, false, false]
247252
* ```
248253
*/
249-
startsWith(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
250-
const { inplace = false } = options;
254+
startsWith(str: string, options?: { inplace?: boolean }): Series
255+
startsWith(str = "", options?: { inplace?: boolean }): Series | void {
256+
const { inplace } = { inplace: false, ...options }
251257
const newArr: Array<boolean | number> = [];
252258
this.values.forEach((val) => {
253259
if (isNaN(val as number) && typeof val != "string") {
@@ -280,8 +286,9 @@ export default class Str {
280286
* // [true, true, false]
281287
* ```
282288
*/
283-
endsWith(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
284-
const { inplace = false } = options;
289+
endsWith(str: string, options?: { inplace?: boolean }): Series
290+
endsWith(str = "", options?: { inplace?: boolean }): Series | void {
291+
const { inplace } = { inplace: false, ...options }
285292
const newArr: Array<boolean | number> = [];
286293
this.values.map((val) => {
287294
if (isNaN(val as number) && typeof val != "string") {
@@ -314,8 +321,9 @@ export default class Str {
314321
* // [true, true, false]
315322
* ```
316323
*/
317-
includes(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
318-
const { inplace = false } = options;
324+
includes(str: string, options?: { inplace?: boolean }): Series
325+
includes(str = "", options?: { inplace?: boolean }): Series | void {
326+
const { inplace } = { inplace: false, ...options }
319327
const newArr: Array<boolean | number> = [];
320328
this.values.map((val) => {
321329
if (isNaN(val as number) && typeof val != "string") {
@@ -348,8 +356,9 @@ export default class Str {
348356
* // [3, 2, -1]
349357
* ```
350358
*/
351-
indexOf(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
352-
const { inplace = false } = options;
359+
indexOf(str: string, options?: { inplace?: boolean }): Series
360+
indexOf(str = "", options?: { inplace?: boolean }): Series | void {
361+
const { inplace } = { inplace: false, ...options }
353362
const newArr: Array<number> = [];
354363
this.values.map((val) => {
355364
if (isNaN(val as number) && typeof val != "string") {
@@ -382,8 +391,9 @@ export default class Str {
382391
* // [3, 2, -1]
383392
* ```
384393
*/
385-
lastIndexOf(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
386-
const { inplace = false } = options;
394+
lastIndexOf(str: string, options?: { inplace?: boolean }): Series
395+
lastIndexOf(str = "", options?: { inplace?: boolean }): Series | void {
396+
const { inplace } = { inplace: false, ...options }
387397
const newArr: Array<string | number> = [];
388398
this.values.map((val) => {
389399
if (isNaN(val as number) && typeof val != "string") {
@@ -418,8 +428,9 @@ export default class Str {
418428
* // ["Goo7", "o77", "crazy"]
419429
* ```
420430
*/
421-
replace(searchValue = "", replaceValue = "", options: { inplace: boolean } = { inplace: false }): Series | void {
422-
const { inplace = false } = options;
431+
replace(searchValue: string, replaceValue: string, options?: { inplace?: boolean }): Series
432+
replace(searchValue = "", replaceValue = "", options?: { inplace?: boolean }): Series | void {
433+
const { inplace } = { inplace: false, ...options }
423434
const newArr: Array<string | number> = [];
424435
this.values.map((val) => {
425436
if (isNaN(val as number) && typeof val != "string") {
@@ -452,8 +463,9 @@ export default class Str {
452463
* // ["GoodGood", "oddodd", "crazycrazy"]
453464
* ```
454465
*/
455-
repeat(num = 1, options: { inplace: boolean } = { inplace: false }): Series | void {
456-
const { inplace = false } = options;
466+
repeat(num: number, options?: { inplace?: boolean }): Series
467+
repeat(num = 1, options?: { inplace?: boolean }): Series | void {
468+
const { inplace } = { inplace: false, ...options }
457469
const newArr: Array<string | number> = [];
458470
this.values.map((val) => {
459471
if (isNaN(val as number) && typeof val != "string") {
@@ -486,8 +498,9 @@ export default class Str {
486498
* console.log(newSf.values)
487499
* ```
488500
*/
489-
search(str = "", options: { inplace: boolean } = { inplace: false }): Series | void {
490-
const { inplace = false } = options;
501+
search(str: string, options?: { inplace?: boolean }): Series
502+
search(str = "", options?: { inplace?: boolean }): Series | void {
503+
const { inplace } = { inplace: false, ...options }
491504
const newArr: Array<string | number> = [];
492505
this.values.map((val) => {
493506
if (isNaN(val as number) && typeof val != "string") {
@@ -521,8 +534,9 @@ export default class Str {
521534
* // ["G", "o", "c"]
522535
* ```
523536
*/
524-
slice(startIndex = 0, endIndex = 1, options: { inplace: boolean } = { inplace: false }): Series | void {
525-
const { inplace = false } = options;
537+
slice(startIndex: number, endIndex: number, options?: { inplace?: boolean }): Series
538+
slice(startIndex = 0, endIndex = 1, options?: { inplace?: boolean }): Series | void {
539+
const { inplace } = { inplace: false, ...options }
526540
const newArr: Array<string | number> = [];
527541
this.values.map((val) => {
528542
if (isNaN(val as number) && typeof val != "string") {
@@ -554,8 +568,9 @@ export default class Str {
554568
* console.log(newSf.values)
555569
* ```
556570
*/
557-
split(splitVal = " ", options: { inplace: boolean } = { inplace: false }): Series | void {
558-
const { inplace = false } = options;
571+
split(splitVal: string, options?: { inplace?: boolean }): Series
572+
split(splitVal = " ", options?: { inplace?: boolean }): Series | void {
573+
const { inplace } = { inplace: false, ...options }
559574
const newArr: Array<string | number> = [];
560575
this.values.map((val) => {
561576
if (isNaN(val as number) && typeof val != "string") {
@@ -587,8 +602,9 @@ export default class Str {
587602
* const newSf = sf.str.substr(d)
588603
* ```
589604
*/
590-
substr(startIndex = 0, num = 1, options: { inplace: boolean } = { inplace: false }): Series | void {
591-
const { inplace = false } = options;
605+
substr(startIndex: number, num: number, options?: { inplace?: boolean }): Series
606+
substr(startIndex = 0, num = 1, options?: { inplace?: boolean }): Series | void {
607+
const { inplace } = { inplace: false, ...options }
592608
const newArr: Array<string | number> = [];
593609
this.values.map((val) => {
594610
if (isNaN(val as number) && typeof val != "string") {
@@ -620,8 +636,9 @@ export default class Str {
620636
* const newSf = sf.str.substring(d)
621637
* ```
622638
*/
623-
substring(startIndex = 0, endIndex = 1, options: { inplace: boolean } = { inplace: false }): Series | void {
624-
const { inplace = false } = options;
639+
substring(startIndex: number, endIndex: number, options?: { inplace?: boolean }): Series
640+
substring(startIndex = 0, endIndex = 1, options?: { inplace?: boolean }): Series | void {
641+
const { inplace } = { inplace: false, ...options }
625642
const newArr: Array<string | number> = [];
626643
this.values.map((val) => {
627644
if (isNaN(val as number) && typeof val != "string") {
@@ -652,8 +669,9 @@ export default class Str {
652669
* ["Good", "odd", "grade"]
653670
* ```
654671
*/
655-
trim(options: { inplace: boolean } = { inplace: false }): Series | void {
656-
const { inplace = false } = options;
672+
trim(options?: { inplace?: boolean }): Series
673+
trim(options?: { inplace?: boolean }): Series | void {
674+
const { inplace } = { inplace: false, ...options }
657675
const newArr: Array<string | number> = [];
658676
this.values.map((val) => {
659677
if (isNaN(val as number) && typeof val != "string") {
@@ -686,8 +704,9 @@ export default class Str {
686704
* // ["Good_new", "odd_new", "grade_new"]
687705
* ```
688706
*/
689-
join(valToJoin = "", joinChar = " ", options: { inplace: boolean } = { inplace: false }): Series | void {
690-
const { inplace = false } = options;
707+
join(valToJoin: string, joinChar: string, options?: { inplace?: boolean }): Series
708+
join(valToJoin = "", joinChar = " ", options?: { inplace?: boolean }): Series | void {
709+
const { inplace } = { inplace: false, ...options }
691710
const newArr: Array<string | number> = [];
692711
this.values.map((val) => {
693712
if (isNaN(val as number) && typeof val != "string") {
@@ -721,8 +740,9 @@ export default class Str {
721740
* // [4,3,5]
722741
* ```
723742
*/
724-
len(options: { inplace: boolean } = { inplace: false }): Series | void {
725-
const { inplace = false } = options;
743+
len(options?: { inplace?: boolean }): Series
744+
len(options?: { inplace?: boolean }): Series | void {
745+
const { inplace } = { inplace: false, ...options }
726746
const newArr: Array<string | number> = [];
727747
this.values.map((val) => {
728748
if (isNaN(val as number) && typeof val != "string") {

danfojs-browser/src/shared/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export interface CsvInputOptions extends ParseConfig { }
113113

114114
//Start of Series class types
115115
export interface SeriesInterface extends NDframeInterface {
116-
iloc(rows: Array<string | number>): Series;
116+
iloc(rows: Array<string | number> | boolean[]): Series;
117117
head(rows: number): Series
118118
tail(rows: number): Series
119119
sample(num: number, options?: { seed?: number }): Promise<Series>;
@@ -157,12 +157,12 @@ export interface SeriesInterface extends NDframeInterface {
157157
cumMin(options?: { inplace?: boolean }): Series | void
158158
cumMax(options?: { inplace?: boolean }): Series | void
159159
cumProd(options?: { inplace?: boolean }): Series | void
160-
lt(other: Series | number | Array<number>): Series
161-
gt(other: Series | number | Array<number>): Series
162-
le(other: Series | number | Array<number>): Series
163-
ge(other: Series | number | Array<number>): Series
164-
ne(other: Series | number | Array<number>): Series
165-
eq(other: Series | number | Array<number>): Series
160+
lt(other: Series | number | Array<number> | boolean[]): Series
161+
gt(other: Series | number | Array<number>| boolean[]): Series
162+
le(other: Series | number | Array<number>| boolean[]): Series
163+
ge(other: Series | number | Array<number>| boolean[]): Series
164+
ne(other: Series | number | Array<number>| boolean[]): Series
165+
eq(other: Series | number | Array<number>| boolean[]): Series
166166
replace(oldValue: string | number | boolean, newValue: string | number | boolean, options?: { inplace?: boolean }): Series | void
167167
dropNa(options?: { inplace?: boolean }): Series | void
168168
argSort(): Series

0 commit comments

Comments
 (0)