Skip to content

Commit dbc501b

Browse files
committed
improvement(list): Increase branch coverage
1 parent ab21580 commit dbc501b

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

__tests__/list.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ interface IPerson {
1313
Age?: number
1414
}
1515

16+
interface IDriver {
17+
Id: number
18+
Car: String
19+
}
20+
1621
interface IPet {
1722
Name: string
1823
Age?: number
@@ -396,6 +401,24 @@ test('GroupBy', t => {
396401
),
397402
result
398403
)
404+
// example taken from https://stackoverflow.com/a/7325306/2834553
405+
const drivers = new List<IDriver>([
406+
{ Id: 1, Car: 'Ferrari' },
407+
{ Id: 1, Car: 'BMW' },
408+
{ Id: 2, Car: 'Audi' }
409+
])
410+
const result2 = {
411+
'1': ['Ferrari', 'BMW'],
412+
'2': ['Audi']
413+
}
414+
t.deepEqual(
415+
drivers.GroupBy(
416+
p => p.Id,
417+
p => p.Car
418+
// (key, g) => ({ [key]: g.ToList() })
419+
),
420+
result2
421+
)
399422
})
400423

401424
test('GroupJoin', t => {
@@ -1052,6 +1075,13 @@ test('ToDictionary', t => {
10521075
dictionary.Select(x => x.Value),
10531076
people
10541077
)
1078+
// example taken from https://stackoverflow.com/a/3611140/2834553
1079+
const dict = people.ToDictionary(
1080+
x => x,
1081+
x => x.Age
1082+
)
1083+
t.is(dict.Select(x => x.Value).Max(), 50)
1084+
t.is(dict.Select(x => x.Value).Min(), 15)
10551085
})
10561086

10571087
test('ToList', t => {

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"types": "dist/src/index.d.ts",
99
"scripts": {
1010
"build": "tsc",
11-
"check-coverage": "nyc check-coverage --statements 100 --branches 95 --functions 98 --lines 100",
11+
"check-coverage": "nyc check-coverage --statements 100 --branches 98 --functions 98 --lines 100",
1212
"commit": "git-cz",
1313
"cover": "nyc --require ts-node/register --reporter=lcov npm t",
1414
"docs": "typedoc --out ../docs/ src/index.ts -m commonjs -t ES6",
@@ -58,7 +58,7 @@
5858
"tslint-config-prettier": "^1.13.0",
5959
"tslint-config-standard": "^7.1.0",
6060
"typedoc": "^0.26.5",
61-
"typescript": "^5.0.4"
61+
"typescript": "^5.5.4"
6262
},
6363
"config": {
6464
"ghooks": {

src/list.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,14 @@ class List<T> {
220220
): { [key: string]: TResult[] } {
221221
const initialValue: { [key: string]: TResult[] } = {}
222222
return this.Aggregate((ac, v) => {
223-
if (v !== undefined) {
224-
const key = grouper(v)
225-
const existingGroup = isObj(ac)
226-
? (ac as { [key: string]: TResult[] })[key]
227-
: undefined
228-
const mappedValue = mapper(v)
229-
if (existingGroup) {
230-
existingGroup.push(mappedValue)
231-
} else {
232-
;(ac as { [key: string]: TResult[] })[key] = [mappedValue]
233-
}
223+
const key = grouper(v)
224+
const existingGroup =
225+
isObj(ac) && (ac as { [key: string]: TResult[] })[key]
226+
const mappedValue = mapper(v)
227+
if (!!existingGroup) {
228+
existingGroup.push(mappedValue)
229+
} else {
230+
;(ac as { [key: string]: TResult[] })[key] = [mappedValue]
234231
}
235232
return ac
236233
}, initialValue)
@@ -552,7 +549,7 @@ class List<T> {
552549
// : v
553550
dicc.Add({
554551
Key: this.Select(key).ElementAt(i),
555-
Value: value ? this.Select(value).ElementAt(i) : v
552+
Value: !!value ? this.Select(value).ElementAt(i) : v
556553
})
557554
return dicc
558555
}, new List<{ Key: TKey; Value: T | TValue }>())

0 commit comments

Comments
 (0)