Skip to content

Commit

Permalink
feat(list): Support TypeScript Strict mode! (#185)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ToDictionary() and all *OrDefault() functions now behave differently

fix #170
  • Loading branch information
kutyel authored Aug 1, 2024
1 parent efd83df commit ab21580
Show file tree
Hide file tree
Showing 7 changed files with 1,443 additions and 2,157 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/hydrogen
lts/iron
78 changes: 40 additions & 38 deletions __tests__/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ class Person implements IPerson {

constructor(pet: IPet) {
this.Name = pet.Name
this.Age = pet.Age
this.Age = pet.Age ?? 0
}
}

class Pet implements IPet {
public Name: string
public Age: number
public Owner: Person
public Owner?: Person
public Vaccinated: boolean

constructor(pet: IPet) {
this.Name = pet.Name
this.Age = pet.Age
this.Age = pet.Age ?? 0
this.Owner = pet.Owner
this.Vaccinated = pet.Vaccinated
this.Vaccinated = pet.Vaccinated ?? false
}
}

Expand Down Expand Up @@ -187,7 +187,7 @@ test('Average', t => {
])
t.is(grades.Average(), 77.6)
t.is(
people.Average(x => x.Age),
people.Average(x => x?.Age ?? 0),
30
)
})
Expand Down Expand Up @@ -339,7 +339,7 @@ test('ElementAtOrDefault', t => {
const b = new List<number>([2, 1, 0, -1, -2])
t.is(a.ElementAtOrDefault(0), 'hey')
t.is(b.ElementAtOrDefault(2), 0)
t.is(a.ElementAtOrDefault(4), undefined)
t.is(a.ElementAtOrDefault(4), null)
})

test('Except', t => {
Expand All @@ -364,10 +364,10 @@ test('First', t => {

test('FirstOrDefault', t => {
t.is(
new List<string>(['hey', 'hola', 'que', 'tal']).FirstOrDefault(),
new List<string>(['hey', 'hola', 'que', 'tal']).FirstOrDefault('boo'),
'hey'
)
t.is(new List<string>().FirstOrDefault(), undefined)
t.is(new List<string>().FirstOrDefault('default'), 'default')
})

test('ForEach', t => {
Expand Down Expand Up @@ -477,10 +477,7 @@ test('Insert', t => {
test('Intersect', t => {
const id1 = new List<number>([44, 26, 92, 30, 71, 38])
const id2 = new List<number>([39, 59, 83, 47, 26, 4, 30])
t.is(
id1.Intersect(id2).Sum(x => x),
56
)
t.is(id1.Intersect(id2).Sum(), 56)
})

test('Join', t => {
Expand Down Expand Up @@ -533,10 +530,12 @@ test('Last', t => {

test('LastOrDefault', t => {
t.is(
new List<string>(['hey', 'hola', 'que', 'tal']).LastOrDefault(),
new List<string>(['hey', 'hola', 'que', 'tal']).LastOrDefault(
'not happening'
),
'tal'
)
t.is(new List<string>().LastOrDefault(), undefined)
t.is(new List<string>().LastOrDefault('default'), 'default')
})

test('Max', t => {
Expand All @@ -546,7 +545,7 @@ test('Max', t => {
{ Age: 50, Name: 'Bob' }
])
t.is(
people.Max(x => x.Age),
people.Max(x => x.Age ?? 0),
50
)
t.is(
Expand All @@ -562,7 +561,7 @@ test('Min', t => {
{ Age: 50, Name: 'Bob' }
])
t.is(
people.Min(x => x.Age),
people.Min(x => x.Age ?? 0),
15
)
t.is(
Expand Down Expand Up @@ -909,20 +908,20 @@ test('SingleOrDefault', t => {
const fruits2 = new List<string>(['orange'])
const fruits3 = new List<string>(['orange', 'apple'])
const numbers1 = new List([1, 2, 3, 4, 5, 5])
t.is(fruits1.SingleOrDefault(), undefined)
t.is(fruits2.SingleOrDefault(), 'orange')
t.throws(() => fruits3.SingleOrDefault(), {
t.is(fruits1.SingleOrDefault('default'), 'default')
t.is(fruits2.SingleOrDefault('default'), 'orange')
t.throws(() => fruits3.SingleOrDefault('default'), {
message: /The collection does not contain exactly one element./
})
t.is(
numbers1.SingleOrDefault(x => x === 1),
1
)
t.is(
numbers1.SingleOrDefault(x => x > 5),
undefined
)
t.throws(() => numbers1.SingleOrDefault(x => x === 5), {
// t.is(
// numbers1.SingleOrDefault(x => x === 1),
// 1
// )
// t.is(
// numbers1.SingleOrDefault(x => x > 5),
// undefined
// )
t.throws(() => numbers1.SingleOrDefault(1), {
message: /The collection does not contain exactly one element./
})
})
Expand Down Expand Up @@ -971,7 +970,7 @@ test('Sum', t => {
10
)
t.is(
people.Sum(x => x.Age),
people.Sum(x => x?.Age ?? 0),
90
)
})
Expand Down Expand Up @@ -1025,20 +1024,23 @@ test('ToDictionary', t => {
{ Age: 50, Name: 'Bob' }
])
const dictionary = people.ToDictionary<string, IPerson>(x => x.Name)
t.deepEqual(dictionary['Bob'], { Age: 50, Name: 'Bob' })
t.is(dictionary['Bob'].Age, 50)
const dictionary2 = people.ToDictionary(
x => x.Name,
y => y.Age
)
t.is(dictionary2['Alice'], 25)
// t.deepEqual(dictionary['Bob'] as List<{ Key: string; Value: IPerson }>, {
// Age: 50,
// Name: 'Bob'
// })
// t.is(dictionary['Bob'].Age, 50)
// const dictionary2 = people.ToDictionary(
// x => x.Name,
// y => y.Age
// )
// t.is(dictionary2['Alice'], 25)
// Dictionary should behave just like in C#
t.is(
dictionary.Max(x => x.Value.Age),
dictionary.Max(x => x?.Value?.Age ?? 0),
50
)
t.is(
dictionary.Min(x => x.Value.Age),
dictionary.Min(x => x?.Value?.Age ?? 0),
15
)
const expectedKeys = new List(['Cathy', 'Alice', 'Bob'])
Expand Down
Loading

0 comments on commit ab21580

Please sign in to comment.