Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ declare global {
}

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

/*
* const arr = [1, 2, 3];
* arr.last(); // 3
*/

export {}
export {} //NOSONAR
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Array.prototype.snail = function (rowsCount: number, colsCount: number): number[
* arr.snail(1,4); // [[1,2,3,4]]
*/

export {}
export {} //NOSONAR
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Fn = (accum: number, curr: number) => number

function reduce(nums: number[], fn: Fn, init: number): number {
let accumulator = init
nums.forEach((num) => {
nums.forEach((num) => { //NOSONAR
accumulator = fn(accumulator, num)
})
return accumulator
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function memoize(fn: Fn): Fn {

let value = fn(...args)

currentCache.set(args[args.length - 1], value)
currentCache.set(args[args.length - 1], value) //NOSONAR
return value
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g2601_2700/s2631_group_by/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Array.prototype.groupBy = function <T>(fn: (item: T) => string) { //NOSONAR
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
*/

export {}
export {} //NOSONAR
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Function.prototype.callPolyfill = function (context, ...args): any { //NOSONAR
* increment.callPolyfill({count: 1}); // 2
*/

export {}
export {} //NOSONAR
28 changes: 22 additions & 6 deletions src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
// #Medium #2023_07_29_Time_45_ms_(99.58%)_Space_44.6_MB_(72.08%)
// #Medium #2023_09_13_Time_50_ms_(90.72%)_Space_45.2_MB_(5.06%)

type Callback = (...args: any[]) => any
type Subscription = {
unsubscribe: () => void
}

class EventEmitter {
subs: Record<string, Callback[]> = {}
eventMap: Map<string, Set<Callback>>

constructor() {
this.eventMap = new Map()
}

subscribe(eventName: string, callback: Callback): Subscription {
if (!this.subs[eventName]) this.subs[eventName] = []
const idx = this.subs[eventName].push(callback) - 1
if (this.eventMap.has(eventName)) {
const set = this.eventMap.get(eventName)!
set.add(callback)
this.eventMap.set(eventName, set)
} else {
const set = new Set<Callback>()
set.add(callback)
this.eventMap.set(eventName, set)
}

return {
unsubscribe: () => this.subs[eventName].splice(idx, 1),
unsubscribe: () => {
this.eventMap.get(eventName).delete(callback)
},
}
}

emit(eventName: string, args: any[] = []): any[] {
return this.subs[eventName]?.map((callback) => callback(...args)) || []
const res = []
this.eventMap.get(eventName)?.forEach((cb) => res.push(cb(...args))) //NOSONAR
return res
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// #Medium #2023_07_29_Time_78_ms_(99.38%)_Space_53.4_MB_(71.88%)
// #Medium #2023_09_14_Time_80_ms_(88.30%)_Space_53.2_MB_(70.41%)

type Obj = Record<any, any>

function compactObject(obj: Obj): Obj {
if (Array.isArray(obj)) {
let retArr = []
obj.forEach((e, idx) => {
obj.forEach((e, idx) => { //NOSONAR
if (e) {
retArr.push(compactObject(e))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #Easy #2023_08_03_Time_43_ms_(99.15%)_Space_43.4_MB_(9.15%)
// #Easy #2023_09_19_Time_39_ms_(99.67%)_Space_42.1_MB_(95.49%)

class Calculator {
init: number
Expand All @@ -23,7 +23,7 @@ class Calculator {
}

divide(value: number): Calculator { //NOSONAR
if (value === 0) throw Error('Division by zero is not allowed')
if (value === 0) throw new Error('Division by zero is not allowed')
this.init /= value
return this
}
Expand Down