Skip to content

Commit

Permalink
refactor(list): get rid of as much if statements as possible 🤮
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Corpa committed Jul 8, 2020
1 parent 4d003e7 commit c3de3d5
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,19 @@ class List<T> {
if (index < this.Count() && index >= 0) {
return this._elements[index]
} else {
const MSG =
throw new Error(
'ArgumentOutOfRangeException: index is less than 0 or greater than or equal to the number of elements in source.'
throw new Error(MSG)
)
}
}

/**
* Returns the element at a specified index in a sequence or a default value if the index is out of range.
*/
public ElementAtOrDefault(index: number): T | null {
if (index < this.Count() && index >= 0) {
return this._elements[index]
} else {
return undefined
}
return index < this.Count() && index >= 0
? this._elements[index]
: undefined
}

/**
Expand Down Expand Up @@ -217,21 +215,16 @@ class List<T> {
*/
public GroupBy<TResult = T>(
grouper: (key: T) => string | number,
mapper?: (element: T) => TResult
mapper: (element: T) => TResult = val => (val as unknown) as TResult
): { [key: string]: TResult[] } {
const initialValue: { [key: string]: TResult[] } = {}
if (!mapper) {
mapper = val => (val as unknown) as TResult
}
return this.Aggregate((ac, v) => {
const key = grouper(v)
const existingGroup = ac[key]
const mappedValue = mapper(v)
if (existingGroup) {
existingGroup.push(mappedValue)
} else {
ac[key] = [mappedValue]
}
existingGroup
? existingGroup.push(mappedValue)
: (ac[key] = [mappedValue])
return ac
}, initialValue)
}
Expand Down

0 comments on commit c3de3d5

Please sign in to comment.