From dbc501b6a0cb27c28aea4cfefb12e878c5cf8d69 Mon Sep 17 00:00:00 2001 From: Flavio Corpa Date: Wed, 7 Aug 2024 13:27:13 +0200 Subject: [PATCH] improvement(list): Increase branch coverage --- __tests__/list.test.ts | 30 ++++++++++++++++++++++++++++++ package-lock.json | 11 ++++++----- package.json | 4 ++-- src/list.ts | 21 +++++++++------------ 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/__tests__/list.test.ts b/__tests__/list.test.ts index 64a21c6..457a2a9 100644 --- a/__tests__/list.test.ts +++ b/__tests__/list.test.ts @@ -13,6 +13,11 @@ interface IPerson { Age?: number } +interface IDriver { + Id: number + Car: String +} + interface IPet { Name: string Age?: number @@ -396,6 +401,24 @@ test('GroupBy', t => { ), result ) + // example taken from https://stackoverflow.com/a/7325306/2834553 + const drivers = new List([ + { Id: 1, Car: 'Ferrari' }, + { Id: 1, Car: 'BMW' }, + { Id: 2, Car: 'Audi' } + ]) + const result2 = { + '1': ['Ferrari', 'BMW'], + '2': ['Audi'] + } + t.deepEqual( + drivers.GroupBy( + p => p.Id, + p => p.Car + // (key, g) => ({ [key]: g.ToList() }) + ), + result2 + ) }) test('GroupJoin', t => { @@ -1052,6 +1075,13 @@ test('ToDictionary', t => { dictionary.Select(x => x.Value), people ) + // example taken from https://stackoverflow.com/a/3611140/2834553 + const dict = people.ToDictionary( + x => x, + x => x.Age + ) + t.is(dict.Select(x => x.Value).Max(), 50) + t.is(dict.Select(x => x.Value).Min(), 15) }) test('ToList', t => { diff --git a/package-lock.json b/package-lock.json index cc26fee..cb6cf3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,7 @@ "tslint-config-prettier": "^1.13.0", "tslint-config-standard": "^7.1.0", "typedoc": "^0.26.5", - "typescript": "^5.0.4" + "typescript": "^5.5.4" } }, "node_modules/@ampproject/remapping": { @@ -6177,16 +6177,17 @@ } }, "node_modules/typescript": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz", - "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==", + "version": "5.5.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", + "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==", "dev": true, + "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=12.20" + "node": ">=14.17" } }, "node_modules/uc.micro": { diff --git a/package.json b/package.json index d004433..80ce718 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "types": "dist/src/index.d.ts", "scripts": { "build": "tsc", - "check-coverage": "nyc check-coverage --statements 100 --branches 95 --functions 98 --lines 100", + "check-coverage": "nyc check-coverage --statements 100 --branches 98 --functions 98 --lines 100", "commit": "git-cz", "cover": "nyc --require ts-node/register --reporter=lcov npm t", "docs": "typedoc --out ../docs/ src/index.ts -m commonjs -t ES6", @@ -58,7 +58,7 @@ "tslint-config-prettier": "^1.13.0", "tslint-config-standard": "^7.1.0", "typedoc": "^0.26.5", - "typescript": "^5.0.4" + "typescript": "^5.5.4" }, "config": { "ghooks": { diff --git a/src/list.ts b/src/list.ts index 66e1999..c131272 100644 --- a/src/list.ts +++ b/src/list.ts @@ -220,17 +220,14 @@ class List { ): { [key: string]: TResult[] } { const initialValue: { [key: string]: TResult[] } = {} return this.Aggregate((ac, v) => { - if (v !== undefined) { - const key = grouper(v) - const existingGroup = isObj(ac) - ? (ac as { [key: string]: TResult[] })[key] - : undefined - const mappedValue = mapper(v) - if (existingGroup) { - existingGroup.push(mappedValue) - } else { - ;(ac as { [key: string]: TResult[] })[key] = [mappedValue] - } + const key = grouper(v) + const existingGroup = + isObj(ac) && (ac as { [key: string]: TResult[] })[key] + const mappedValue = mapper(v) + if (!!existingGroup) { + existingGroup.push(mappedValue) + } else { + ;(ac as { [key: string]: TResult[] })[key] = [mappedValue] } return ac }, initialValue) @@ -552,7 +549,7 @@ class List { // : v dicc.Add({ Key: this.Select(key).ElementAt(i), - Value: value ? this.Select(value).ElementAt(i) : v + Value: !!value ? this.Select(value).ElementAt(i) : v }) return dicc }, new List<{ Key: TKey; Value: T | TValue }>())