Skip to content

Commit 41c1d45

Browse files
authored
Fixed SonarQube warnings
1 parent 476d488 commit 41c1d45

File tree

9 files changed

+33
-17
lines changed

9 files changed

+33
-17
lines changed

src/main/kotlin/g2601_2700/s2619_array_prototype_last/solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ declare global {
77
}
88

99
Array.prototype.last = function () { //NOSONAR
10-
return this.length !== 0 ? this[this.length - 1] : -1
10+
return this.length !== 0 ? this[this.length - 1] : -1 //NOSONAR
1111
}
1212

1313
/*
1414
* const arr = [1, 2, 3];
1515
* arr.last(); // 3
1616
*/
1717

18-
export {}
18+
export {} //NOSONAR

src/main/kotlin/g2601_2700/s2624_snail_traversal/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Array.prototype.snail = function (rowsCount: number, colsCount: number): number[
2424
* arr.snail(1,4); // [[1,2,3,4]]
2525
*/
2626

27-
export {}
27+
export {} //NOSONAR

src/main/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type Fn = (accum: number, curr: number) => number
44

55
function reduce(nums: number[], fn: Fn, init: number): number {
66
let accumulator = init
7-
nums.forEach((num) => {
7+
nums.forEach((num) => { //NOSONAR
88
accumulator = fn(accumulator, num)
99
})
1010
return accumulator

src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function memoize(fn: Fn): Fn {
3636

3737
let value = fn(...args)
3838

39-
currentCache.set(args[args.length - 1], value)
39+
currentCache.set(args[args.length - 1], value) //NOSONAR
4040
return value
4141
}
4242
}

src/main/kotlin/g2601_2700/s2631_group_by/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ Array.prototype.groupBy = function <T>(fn: (item: T) => string) { //NOSONAR
2323
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
2424
*/
2525

26-
export {}
26+
export {} //NOSONAR

src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Function.prototype.callPolyfill = function (context, ...args): any { //NOSONAR
1616
* increment.callPolyfill({count: 1}); // 2
1717
*/
1818

19-
export {}
19+
export {} //NOSONAR

src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
// #Medium #2023_07_29_Time_45_ms_(99.58%)_Space_44.6_MB_(72.08%)
1+
// #Medium #2023_09_13_Time_50_ms_(90.72%)_Space_45.2_MB_(5.06%)
22

33
type Callback = (...args: any[]) => any
44
type Subscription = {
55
unsubscribe: () => void
66
}
77

88
class EventEmitter {
9-
subs: Record<string, Callback[]> = {}
9+
eventMap: Map<string, Set<Callback>>
10+
11+
constructor() {
12+
this.eventMap = new Map()
13+
}
1014

1115
subscribe(eventName: string, callback: Callback): Subscription {
12-
if (!this.subs[eventName]) this.subs[eventName] = []
13-
const idx = this.subs[eventName].push(callback) - 1
16+
if (this.eventMap.has(eventName)) {
17+
const set = this.eventMap.get(eventName)!
18+
set.add(callback)
19+
this.eventMap.set(eventName, set)
20+
} else {
21+
const set = new Set<Callback>()
22+
set.add(callback)
23+
this.eventMap.set(eventName, set)
24+
}
25+
1426
return {
15-
unsubscribe: () => this.subs[eventName].splice(idx, 1),
27+
unsubscribe: () => {
28+
this.eventMap.get(eventName).delete(callback)
29+
},
1630
}
1731
}
1832

1933
emit(eventName: string, args: any[] = []): any[] {
20-
return this.subs[eventName]?.map((callback) => callback(...args)) || []
34+
const res = []
35+
this.eventMap.get(eventName)?.forEach((cb) => res.push(cb(...args))) //NOSONAR
36+
return res
2137
}
2238
}
2339

src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// #Medium #2023_07_29_Time_78_ms_(99.38%)_Space_53.4_MB_(71.88%)
1+
// #Medium #2023_09_14_Time_80_ms_(88.30%)_Space_53.2_MB_(70.41%)
22

33
type Obj = Record<any, any>
44

55
function compactObject(obj: Obj): Obj {
66
if (Array.isArray(obj)) {
77
let retArr = []
8-
obj.forEach((e, idx) => {
8+
obj.forEach((e, idx) => { //NOSONAR
99
if (e) {
1010
retArr.push(compactObject(e))
1111
}

src/main/kotlin/g2701_2800/s2726_calculator_with_method_chaining/solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #Easy #2023_08_03_Time_43_ms_(99.15%)_Space_43.4_MB_(9.15%)
1+
// #Easy #2023_09_19_Time_39_ms_(99.67%)_Space_42.1_MB_(95.49%)
22

33
class Calculator {
44
init: number
@@ -23,7 +23,7 @@ class Calculator {
2323
}
2424

2525
divide(value: number): Calculator { //NOSONAR
26-
if (value === 0) throw Error('Division by zero is not allowed')
26+
if (value === 0) throw new Error('Division by zero is not allowed')
2727
this.init /= value
2828
return this
2929
}

0 commit comments

Comments
 (0)