Skip to content

Commit

Permalink
improvement(list): Increase branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kutyel committed Aug 7, 2024
1 parent ab21580 commit dbc501b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
30 changes: 30 additions & 0 deletions __tests__/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface IPerson {
Age?: number
}

interface IDriver {
Id: number
Car: String
}

interface IPet {
Name: string
Age?: number
Expand Down Expand Up @@ -396,6 +401,24 @@ test('GroupBy', t => {
),
result
)
// example taken from https://stackoverflow.com/a/7325306/2834553
const drivers = new List<IDriver>([
{ 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 => {
Expand Down Expand Up @@ -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 => {
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
21 changes: 9 additions & 12 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,14 @@ class List<T> {
): { [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)
Expand Down Expand Up @@ -552,7 +549,7 @@ class List<T> {
// : 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 }>())
Expand Down

0 comments on commit dbc501b

Please sign in to comment.