Skip to content

Commit 1cb6ef3

Browse files
Pass all the linting (#453)
1 parent 1a4a897 commit 1cb6ef3

File tree

27 files changed

+323
-242
lines changed

27 files changed

+323
-242
lines changed

.eslintrc

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"ecmaFeatures": {
77
"jsx": true
88
},
9-
"ecmaVersion": 2018,
9+
"ecmaVersion": 2020,
1010
"sourceType": "module"
1111
},
1212
"env": {
13-
"browser": true,
13+
"browser": false,
1414
"es6": true,
1515
"jest": true,
16-
"node": true
16+
"node": true,
17+
"shelljs": true
1718
},
1819
"extends": [
1920
"eslint:recommended",
@@ -29,7 +30,7 @@
2930
},
3031
"plugins": ["@typescript-eslint"],
3132
"rules": {
32-
"@typescript-eslint/array-type": ["error", { "default": "array-simple" }],
33+
"@typescript-eslint/array-type": "off",
3334
"@typescript-eslint/explicit-function-return-type": [
3435
"warn",
3536
{

exercises/practice/crypto-square/.meta/proof.ci.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export class Crypto {
22
constructor(private readonly input: string) {}
33

4-
get plaintext() {
4+
private get plaintext(): string {
55
return this.input.toLowerCase().replace(/[^a-zA-Z0-9]/g, '')
66
}
77

8-
get ciphertext() {
8+
public get ciphertext(): string {
99
const chunkSize = this.size
1010
if (chunkSize === 0) {
1111
return ''
@@ -19,18 +19,18 @@ export class Crypto {
1919
.join(' ')
2020
}
2121

22-
get size(): number {
22+
public get size(): number {
2323
const realLength = Math.sqrt(this.plaintext.length)
2424
return Math.ceil(realLength)
2525
}
2626

27-
ciphertextSegments() {
27+
private ciphertextSegments(): string[] {
2828
const textSegments = this.plaintextSegments()
29-
const columns = []
30-
let i
31-
let j
32-
let currentSegment
33-
let currentLetter
29+
const columns: string[][] = []
30+
let i: number
31+
let j: number
32+
let currentSegment: RegExpMatchArray[number]
33+
let currentLetter: RegExpMatchArray[number][number]
3434

3535
for (i = 0; i < this.size; i += 1) {
3636
columns.push([])
@@ -45,14 +45,15 @@ export class Crypto {
4545
}
4646
}
4747

48+
const result: string[] = []
4849
for (i = 0; i < columns.length; i += 1) {
49-
columns[i] = columns[i].join('')
50+
result[i] = columns[i].join('')
5051
}
5152

52-
return columns
53+
return result
5354
}
5455

55-
plaintextSegments() {
56+
private plaintextSegments(): RegExpMatchArray {
5657
const plainText = this.plaintext
5758
const chunkSize = this.size
5859

exercises/practice/diffie-hellman/.meta/proof.ci.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const PRIMES = [
7676
7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699,
7777
7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829,
7878
7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919,
79-
];
79+
]
8080

8181
export class DiffieHellman {
8282
constructor(private readonly p: number, private readonly g: number) {

exercises/practice/grade-school/.meta/proof.ci.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ type Grade = number
33
type StudentRooster = Record<string, Student[]>
44
type StudentGrades = Map<Student, Grade>
55
export class GradeSchool {
6-
students: StudentGrades
6+
private students: StudentGrades
77

88
constructor() {
99
this.students = new Map()
1010
}
1111

12-
add(student: Student, level: Grade) {
12+
public add(student: Student, level: Grade): void {
1313
this.students.set(student, level)
1414
}
1515

16-
grade(level: Grade) {
16+
public grade(level: Grade): Student[] {
1717
return Array.from(this.students.entries())
1818
.filter(([, studentGrade]) => studentGrade === level)
1919
.map(([student]) => student)
2020
.sort()
2121
}
2222

23-
roster(): StudentRooster {
23+
public roster(): StudentRooster {
2424
const result: StudentRooster = {}
2525

2626
Array.from(this.students.entries()).forEach(([, studentGrade]) => {

exercises/practice/largest-series-product/.meta/proof.ci.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const largestProduct = (digits: string, seriesLength: number) => {
1+
export function largestProduct(digits: string, seriesLength: number): number {
22
if (seriesLength === 0) {
33
return 1
44
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Instructions append
22

33
Using core language features to build and deconstruct arrays via destructuring, and using the array literal `[]` are allowed, but no functions from the `Array.prototype` should be used.
4+
5+
In order to be able to test your solution, ensure `forEach` is implemented.
6+
7+
```typescript
8+
const list = List.create(1, 2)
9+
list.forEach((item) => console.log(item))
10+
// =>
11+
// 1
12+
// 2
13+
```

exercises/practice/list-ops/.meta/proof.ci.ts

+74-48
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ const Null: Cons = {
55
get next() {
66
return this
77
},
8-
get values() {
9-
return []
10-
},
118

129
get() {
1310
return this.value
1411
},
1512

1613
push(item): Cons {
14+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
1715
return new Cons(item, this)
1816
},
1917
length() {
@@ -25,16 +23,22 @@ const Null: Cons = {
2523
concat(): Cons {
2624
return this
2725
},
28-
forEach() {
26+
forEach(): void {
2927
/* done */
3028
},
31-
foldl(_, initial): any {
32-
return initial
29+
foldl<TValue = unknown, TReturn = unknown>(
30+
_: (initial: TReturn, value: TValue) => TReturn,
31+
initial?: TReturn
32+
): TReturn {
33+
return initial as TReturn
3334
},
34-
foldr(_, initial) {
35-
return initial
35+
foldr<TValue = unknown, TReturn = unknown>(
36+
_: (initial: TReturn, value: TValue) => TReturn,
37+
initial?: TReturn
38+
): TReturn {
39+
return initial as TReturn
3640
},
37-
filter() {
41+
filter(): Cons {
3842
return Null
3943
},
4044
reverse(): Cons {
@@ -44,79 +48,101 @@ const Null: Cons = {
4448
return this
4549
},
4650
}
47-
4851
class Cons {
49-
static fromArray([head, ...tail]: any[]) {
50-
if (head === undefined) {
51-
return Null
52-
}
53-
54-
return new Cons(head, Cons.fromArray(tail || []))
55-
}
56-
57-
constructor(public readonly value: any, public next: Cons = Null) {}
58-
59-
get values() {
60-
return [this.value, ...this.next.values]
61-
}
52+
constructor(public readonly value: unknown, public next: Cons = Null) {}
6253

63-
get(i: number) {
54+
public get(i: number): unknown {
6455
return i === 0 ? this.value : this.next.get(i - 1)
6556
}
6657

67-
push(item: any): this {
58+
public push(item: unknown): this {
6859
this.next = this.next.push(item)
6960
return this
7061
}
7162

72-
length(): number {
63+
public length(): number {
7364
return 1 + this.next.length()
7465
}
7566

76-
append(other: Cons): Cons {
67+
public append(other: Cons): Cons {
7768
return other.foldl((result, item) => result.push(item), this)
7869
}
7970

80-
concat(others: Cons): Cons {
81-
return others.foldl((result, other) => result.append(other), this)
71+
public concat(others: Cons): Cons {
72+
return others.foldl<Cons, Cons>(
73+
(result, other) => result.append(other),
74+
this
75+
)
8276
}
8377

84-
foldl(
85-
callback: (initial: any, value: any) => any,
86-
initial: any = undefined
87-
): any {
88-
return this.next.foldl(callback, callback(initial, this.value))
78+
public foldl<TValue = unknown>(
79+
callback: (initial: TValue, value: TValue) => TValue
80+
): TValue
81+
public foldl<TValue = unknown, TReturn = unknown>(
82+
callback: (initial: TReturn, value: TValue) => TReturn,
83+
initial: TReturn
84+
): TReturn
85+
86+
public foldl<TValue = unknown, TReturn = unknown>(
87+
callback: (initial: TReturn | undefined, value: TValue) => TReturn,
88+
initial?: TReturn
89+
): TReturn {
90+
return this.next.foldl<TValue, TReturn>(
91+
callback,
92+
callback(initial, this.value as TValue)
93+
)
8994
}
9095

91-
forEach(callback: (value: any) => void): void {
96+
public forEach(callback: (value: unknown) => void): void {
9297
this.foldl((_, item) => callback(item))
9398
}
9499

95-
foldr(
96-
callback: (initial: any, value: any) => any,
97-
initial: any = undefined
98-
): any {
99-
return callback(this.next.foldr(callback, initial), this.value)
100+
public foldr<TValue = unknown>(
101+
callback: (initial: TValue, value: TValue) => TValue
102+
): TValue
103+
public foldr<TValue = unknown, TReturn = unknown>(
104+
callback: (initial: TReturn, value: TValue) => TReturn,
105+
initial: TReturn
106+
): TReturn
107+
108+
public foldr<TValue = unknown, TReturn = unknown>(
109+
callback: (initial: TReturn, value: TValue) => TReturn,
110+
initial?: TReturn
111+
): TReturn {
112+
return callback(
113+
this.next.foldr<TValue, TReturn>(callback, initial as TReturn),
114+
this.value as TValue
115+
)
100116
}
101117

102-
filter(predicate: (value: any) => boolean): Cons {
103-
return this.foldl(
118+
public filter<TValue = unknown>(predicate: (value: TValue) => boolean): Cons {
119+
return this.foldl<TValue, Cons>(
104120
(result, item) => (predicate(item) && result.push(item)) || result,
105121
Null
106122
)
107123
}
108124

109-
map(expression: (value: any) => any): Cons {
110-
return this.foldl((result, item) => result.push(expression(item)), Null)
125+
public map<TValue = unknown, TReturn = unknown>(
126+
expression: (value: TValue) => TReturn
127+
): Cons {
128+
return this.foldl<TValue, Cons>(
129+
(result, item) => result.push(expression(item)),
130+
Null
131+
)
111132
}
112133

113-
reverse(): Cons {
134+
public reverse(): Cons {
114135
return this.next.reverse().push(this.value)
115136
}
116137
}
117-
118138
export class List {
119-
constructor(values = []) {
120-
return Cons.fromArray(values)
139+
public static create(...values: unknown[]): Cons {
140+
const [head, ...tail] = values
141+
142+
if (head === undefined) {
143+
return Null
144+
}
145+
146+
return new Cons(head, List.create(...tail))
121147
}
122148
}

0 commit comments

Comments
 (0)