Skip to content

Commit e06de12

Browse files
committed
feat: setup eslint
1 parent ec33f88 commit e06de12

File tree

14 files changed

+2256
-395
lines changed

14 files changed

+2256
-395
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@trantlabs/eslint-config-typescript"]
3+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
},
3939
"homepage": "https://flam3rboy.github.io/missing-native-js-functions/",
4040
"devDependencies": {
41+
"@trantlabs/eslint-config-typescript": "^0.0.0",
4142
"@types/node": "^14.14.14",
43+
"eslint": "^8.30.0",
4244
"rollup": "^2.36.1",
4345
"rollup-plugin-babel-minify": "^10.0.0",
4446
"rollup-plugin-commonjs": "^10.1.0",

src/Array.ts

Lines changed: 138 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,148 @@
1-
import { define } from "./Util";
1+
import { define } from './Util'
22

33
define(Array.prototype, {
4-
remove: function <T>(this: T[], elem: T): T[] {
5-
// do not use filter to modify current array
6-
const index = this.indexOf(elem);
7-
if (index === -1) return this;
4+
remove: function <T> (this: T[], elem: T): T[] {
5+
// do not use filter to modify current array
6+
const index = this.indexOf(elem)
7+
if (index === -1) return this
88

9-
this.splice(index, 1);
10-
return this; //.filter((e) => e !== elem);
11-
},
12-
insert: function <T>(elem: T, index: number) {
13-
if (!index) index = this.length;
14-
this.splice(index, 0, elem);
15-
return this;
16-
},
17-
flat: function (depth: number = 1) {
18-
return this.reduce(
19-
(acc: any, val: any) => ((Array.isArray(val) && depth >= 1) || depth === -1 ? acc.concat(val.flat(depth--)) : acc.concat(val)),
20-
[]
21-
);
22-
},
23-
last: function () {
24-
return this[this.length - 1];
25-
},
26-
first: function () {
27-
return this[0];
28-
},
29-
unique: function <T>(predicate?: (value: T, index: number, obj: T[]) => any) {
30-
if (predicate) {
31-
return [...new Map(this.map((item: T, index: number, obj: T[]) => [predicate(item, index, obj), item])).values()];
32-
}
33-
return [...new Set(this)];
34-
},
35-
random: function () {
36-
return this[Math.floor(Math.random() * this.length)];
37-
},
38-
shuffle: function () {
39-
for (let i = this.length - 1; i > 0; i--) {
40-
const j = Math.floor(Math.random() * (i + 1));
41-
[this[i], this[j]] = [this[j], this[i]];
42-
}
43-
return this;
44-
},
45-
findMap: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): number {
46-
for (let i = 0; i < this.length; i++) {
47-
var result = predicate(this[i], i, this);
48-
if (result) {
49-
return result;
50-
}
51-
}
52-
},
53-
findLast: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): T | undefined {
54-
for (let i = this.length; i >= 0; i--) {
55-
if (predicate(this[i], i, this)) return this[i];
56-
}
57-
return null;
58-
},
59-
findLastIndex: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): number {
60-
for (let i = this.length - 1; i >= 0; i--) {
61-
if (predicate(this[i], i, this)) return i;
62-
}
63-
return -1;
64-
},
65-
count: function (search: RegExp | any) {
66-
const nativeTypes = ["string", "number", "object", "array"];
67-
let count: number = 0;
9+
this.splice(index, 1)
10+
return this // .filter((e) => e !== elem);
11+
},
12+
insert: function <T> (elem: T, index: number) {
13+
if (!index) index = this.length
14+
this.splice(index, 0, elem)
15+
return this
16+
},
17+
flat: function (depth: number = 1) {
18+
return this.reduce(
19+
(acc: any, val: any) =>
20+
(Array.isArray(val) && depth >= 1) || depth === -1
21+
? acc.concat(val.flat(depth--))
22+
: acc.concat(val),
23+
[]
24+
)
25+
},
26+
last: function () {
27+
return this[this.length - 1]
28+
},
29+
first: function () {
30+
return this[0]
31+
},
32+
unique: function <T> (predicate?: (value: T, index: number, obj: T[]) => any) {
33+
if (predicate) {
34+
return [
35+
...new Map(
36+
this.map((item: T, index: number, obj: T[]) => [
37+
predicate(item, index, obj),
38+
item
39+
])
40+
).values()
41+
]
42+
}
43+
return [...new Set(this)]
44+
},
45+
random: function () {
46+
return this[Math.floor(Math.random() * this.length)]
47+
},
48+
shuffle: function () {
49+
for (let i = this.length - 1; i > 0; i--) {
50+
const j = Math.floor(Math.random() * (i + 1));
51+
[this[i], this[j]] = [this[j], this[i]]
52+
}
53+
return this
54+
},
55+
findMap: function <T> (
56+
predicate: (value: T, index: number, obj: T[]) => any | undefined
57+
): number {
58+
for (let i = 0; i < this.length; i++) {
59+
const result = predicate(this[i], i, this)
60+
if (result) {
61+
return result
62+
}
63+
}
64+
},
65+
findLast: function <T> (
66+
predicate: (value: T, index: number, obj: T[]) => any | undefined
67+
): T | undefined {
68+
for (let i = this.length; i >= 0; i--) {
69+
if (predicate(this[i], i, this)) return this[i]
70+
}
71+
return null
72+
},
73+
findLastIndex: function <T> (
74+
predicate: (value: T, index: number, obj: T[]) => any | undefined
75+
): number {
76+
for (let i = this.length - 1; i >= 0; i--) {
77+
if (predicate(this[i], i, this)) return i
78+
}
79+
return -1
80+
},
81+
count: function (search: RegExp | any) {
82+
const nativeTypes = ['string', 'number', 'object', 'array']
83+
let count: number = 0
6884

69-
this.forEach((element: any) => {
70-
if (element === search) {
71-
count++;
72-
return;
73-
}
85+
this.forEach((element: any) => {
86+
if (element === search) {
87+
count++
88+
return
89+
}
7490

75-
// check if searchparam is a native class (l: 99)
76-
if (typeof search == "function") {
77-
const className = search.name.toLowerCase();
78-
if (nativeTypes.includes(className) && typeof element === className) {
79-
count++;
80-
return;
81-
} else if (element instanceof search) {
82-
count++;
83-
return;
84-
}
85-
}
91+
// check if searchparam is a native class (l: 99)
92+
if (typeof search === 'function') {
93+
const className = search.name.toLowerCase()
94+
// eslint-disable-next-line valid-typeof
95+
if (nativeTypes.includes(className) && typeof element === className) {
96+
count++
97+
return
98+
} else if (element instanceof search) {
99+
count++
100+
return
101+
}
102+
}
86103

87-
// if element of array is a string: allow regex patterns
88-
if (typeof element === "string") {
89-
if (element.match(search)) {
90-
count++;
91-
return;
92-
}
93-
}
94-
});
95-
return count;
96-
},
97-
missing: function <T>(arr: T[]): T[] {
98-
return this.filter((x: any) => !arr.includes(x));
99-
},
100-
similarities: function <T>(arr: T[]): T[] {
101-
return this.filter((x: any) => arr.includes(x));
102-
},
103-
});
104+
// if element of array is a string: allow regex patterns
105+
if (typeof element === 'string') {
106+
if (element.match(search)) {
107+
count++
108+
}
109+
}
110+
})
111+
return count
112+
},
113+
missing: function <T> (arr: T[]): T[] {
114+
return this.filter((x: any) => !arr.includes(x))
115+
},
116+
similarities: function <T> (arr: T[]): T[] {
117+
return this.filter((x: any) => arr.includes(x))
118+
}
119+
})
104120

105121
declare global {
106-
interface Array<T> {
107-
remove(o: T): this;
108-
flat(depth?: number): T;
109-
first(): T | undefined;
110-
last(): T | undefined;
111-
findLastIndex(predicate: (value: T, index: number, obj: T[]) => any | undefined): number;
112-
findLast(predicate: (value: T, index: number, obj: T[]) => any | undefined): T | undefined;
113-
findMap(predicate: (value: T, index: number, obj: T[]) => any | undefined): any | undefined;
114-
random(): T | undefined;
115-
unique(predicate?: (value: T, index: number, obj: T[]) => any | undefined): T[];
116-
shuffle(): T[];
117-
insert(elem: T, index: number): this;
118-
count(search: RegExp | any): number;
119-
similarities(arr: T[]): T[];
120-
missing(arr: T[]): T[];
121-
}
122+
interface Array<T> {
123+
remove(o: T): this;
124+
flat(depth?: number): T;
125+
first(): T | undefined;
126+
last(): T | undefined;
127+
findLastIndex(
128+
predicate: (value: T, index: number, obj: T[]) => any | undefined
129+
): number;
130+
findLast(
131+
predicate: (value: T, index: number, obj: T[]) => any | undefined
132+
): T | undefined;
133+
findMap(
134+
predicate: (value: T, index: number, obj: T[]) => any | undefined
135+
): any | undefined;
136+
random(): T | undefined;
137+
unique(
138+
predicate?: (value: T, index: number, obj: T[]) => any | undefined
139+
): T[];
140+
shuffle(): T[];
141+
insert(elem: T, index: number): this;
142+
count(search: RegExp | any): number;
143+
similarities(arr: T[]): T[];
144+
missing(arr: T[]): T[];
145+
}
122146
}
123147

124-
export {};
148+
export {}

src/Date.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import { define } from "./Util";
1+
import { define } from './Util'
22
define(Date.prototype, {
3-
nowSeconds: function () {
4-
return Math.floor(Date.now() / 1000);
5-
},
6-
setTimezone: function (timezone: string) {
7-
return new Date(
8-
this.toLocaleString("en-US", {
9-
timezone,
10-
})
11-
);
12-
},
13-
isInPast: function () {
14-
const today = new Date();
15-
today.setHours(0, 0, 0, 0);
16-
return this < today;
17-
},
18-
});
3+
nowSeconds: function () {
4+
return Math.floor(Date.now() / 1000)
5+
},
6+
setTimezone: function (timezone: string) {
7+
return new Date(
8+
this.toLocaleString('en-US', {
9+
timezone
10+
})
11+
)
12+
},
13+
isInPast: function () {
14+
const today = new Date()
15+
today.setHours(0, 0, 0, 0)
16+
return this < today
17+
}
18+
})
1919

2020
declare global {
21-
//@ts-ignore
22-
interface DateConstructor {
23-
nowSeconds(): number;
24-
setTimezone(): Date;
25-
isInPast(): boolean;
26-
}
21+
interface DateConstructor {
22+
nowSeconds(): number;
23+
setTimezone(): Date;
24+
isInPast(): boolean;
25+
}
2726
}
2827

29-
export {};
28+
export {}

0 commit comments

Comments
 (0)