-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added `properSubset` & `properSuperset` functions * fixed linting
- Loading branch information
1 parent
e857ced
commit f984da5
Showing
16 changed files
with
365 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function properSubset<T>(...sets: Set<T>[]): boolean; | ||
export function properSubset<T>(...sets: ReadonlySet<T>[]): boolean; | ||
|
||
/** | ||
* A set is a proper subset of another if all of its elements | ||
* are elements of the other set (and thereafter), and it has a | ||
* greater cardinality than the other set (and thereafter). | ||
* | ||
* Proper subset is notated A ⊂ B, | ||
* and not proper subset is A ⊄ B. | ||
* | ||
* @description A ⊂ B ⇔ (|A| < |B|) ∧ (∀x : (x ∈ A ⇒ x ∈ B)) | ||
*/ | ||
export function properSubset<T, S extends ReadonlySet<T>>(...sets: S[]): boolean { | ||
if (sets.length < 2) { | ||
return true; | ||
} | ||
|
||
const cardinalities = sets.map(set => set.size); | ||
const primaryCardinality = cardinalities.shift()!; | ||
const allSetsHaveGreaterCardinalities = cardinalities | ||
.every(cardinality => cardinality > primaryCardinality); | ||
|
||
if (!allSetsHaveGreaterCardinalities) { | ||
return false; | ||
} | ||
|
||
for (const value of sets[0]!) { | ||
for (let index = 1; index < sets.length; index++) { | ||
if (!sets[index]?.has(value)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function properSuperset<T>(...sets: Set<T>[]): boolean; | ||
export function properSuperset<T>(...sets: ReadonlySet<T>[]): boolean; | ||
|
||
/** | ||
* A set is a proper subset of another if all of its elements | ||
* are elements of the other set (and thereafter), and it has a | ||
* greater cardinality than the other set (and thereafter). | ||
* | ||
* Proper superset is notated A ⊃ B, | ||
* and not proper superset is A ⊅ B. | ||
* | ||
* @description A ⊃ B ⇔ (|A| > |B|) ∧ (∀x : (x ∈ B ⇒ x ∈ A)) | ||
*/ | ||
export function properSuperset<T, S extends ReadonlySet<T>>(...sets: S[]): boolean { | ||
if (sets.length < 2) { | ||
return true; | ||
} | ||
|
||
const cardinalities = sets.map(set => set.size); | ||
const primaryCardinality = cardinalities.shift()!; | ||
const allSetsHaveLesserCardinalities = cardinalities | ||
.every(cardinality => cardinality < primaryCardinality); | ||
|
||
if (!allSetsHaveLesserCardinalities) { | ||
return false; | ||
} | ||
|
||
for (let index = 1; index < sets.length; index++) { | ||
for (const value of sets[index]!) { | ||
if (!sets[0]?.has(value)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { properSubset } from '../../src'; | ||
import {empty, minimal, setA, setB, setC, setD, universal} from '../constants/testing-constants'; | ||
|
||
describe('proper subset', () => { | ||
it('no sets are proper subsets', () => { | ||
expect(properSubset()).toBe(true); | ||
}); | ||
|
||
it('single set is a proper subset', () => { | ||
expect(properSubset(setA)).toBe(true); | ||
}); | ||
|
||
it('same set is not a proper subset', () => { | ||
expect(properSubset(setA, setA)).toBe(false); | ||
}); | ||
|
||
it('many of the same set are not proper subsets', () => { | ||
expect(properSubset(setA, setA, setA)).toBe(false); | ||
}); | ||
|
||
it('two sets with different values are not proper subsets', () => { | ||
expect(properSubset(setA, setB)).toBe(false); | ||
}); | ||
|
||
it('three sets with different values are not proper subsets', () => { | ||
expect(properSubset(setA, setB, setC)).toBe(false); | ||
}); | ||
|
||
it('the empty set is not a proper subset of itself', () => { | ||
expect(properSubset(empty, empty)).toBe(false); | ||
}); | ||
|
||
/* custom proper subset tests */ | ||
|
||
it('following sets with lower cardinalities are not proper subsets', () => { | ||
expect(properSubset(setA, minimal)).toBe(false); | ||
}); | ||
|
||
it('sets without value bijection are not proper subsets', () => { | ||
expect(properSubset(setD, setA)).toBe(false); | ||
}); | ||
|
||
it('any non-universal set is a proper subset of the universal set', () => { | ||
expect(properSubset(setA, universal)).toBe(true); | ||
}); | ||
|
||
it('the empty set is a proper subset of every non-empty set', () => { | ||
expect(properSubset(empty, minimal, setA, setB, setC, universal)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { properSuperset } from '../../src'; | ||
import { empty, minimal, setA, setB, setC, setD, universal } from '../constants/testing-constants'; | ||
|
||
describe('proper superset', () => { | ||
it('no sets are proper supersets', () => { | ||
expect(properSuperset()).toBe(true); | ||
}); | ||
|
||
it('single set is a proper superset', () => { | ||
expect(properSuperset(setA)).toBe(true); | ||
}); | ||
|
||
it('same set is not a proper superset', () => { | ||
expect(properSuperset(setA, setA)).toBe(false); | ||
}); | ||
|
||
it('many of the same set are not proper supersets', () => { | ||
expect(properSuperset(setA, setA, setA)).toBe(false); | ||
}); | ||
|
||
it('two sets with different values are not proper supersets', () => { | ||
expect(properSuperset(setA, setB)).toBe(false); | ||
}); | ||
|
||
it('three sets with different values are not proper supersets', () => { | ||
expect(properSuperset(setA, setB, setC)).toBe(false); | ||
}); | ||
|
||
it('the empty set is not a proper superset of itself', () => { | ||
expect(properSuperset(empty, empty)).toBe(false); | ||
}); | ||
|
||
/* custom proper superset tests */ | ||
|
||
it('following sets with greater cardinalities are not proper supersets', () => { | ||
expect(properSuperset(minimal, setA)).toBe(false); | ||
}); | ||
|
||
it('sets without value bijection are not proper supersets', () => { | ||
expect(properSuperset(setA, setD)).toBe(false); | ||
}); | ||
|
||
it('any non-empty set is a proper superset of the empty set', () => { | ||
expect(properSuperset(setA, empty)).toBe(true); | ||
}); | ||
|
||
it('the universal set is a proper superset of every non-universal set', () => { | ||
expect(properSuperset(universal, setA, setB, setC, minimal, empty)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.