@@ -17,53 +17,35 @@ npm i gift-exchange
1717The library ships CommonJS, ES module, and UMD builds. The UMD build makes the
1818library available with the ` GiftExchange ` name.
1919
20- ` gift-exchange ` exports two functions (` calculateSync ` and ` calculate `
21- (deprecated)) and an Error, ` DerangementError ` .
22-
23- A ` Person ` array is always required. A ` Person ` must have a unique ` name ` and
24- optionally a ` group ` . A ` Person ` cannot be matched with another person in the
25- same ` group ` nor with themselves. A mix of people that both have and do not
26- have a ` group ` is supported. Additional exclusion logic can be configured with
27- [ Exclusions] ( #exclusions ) .
20+ ### ` calculate `
2821
2922``` typescript
30- import { Person } from ' gift-exchange' ;
31-
32- const people: Person [] = [
33- {
34- name: ' Brian' ,
35- group: ' Mitchell'
36- },
37- {
38- name: ' Freja'
39- }
40- ];
41- ```
42-
43- ### ` calculateSync `
44-
45- ``` typescript
46- function calculateSync(
47- people : Person [],
48- exclusions ? : Exclusion []
49- ): Person [];
23+ function calculate(people : Person [], exclusions ? : Exclusion []): Person [];
5024// or
51- function calculateSync (
25+ function calculate (
5226 people : Person [],
53- // timeout in ms
54- options ? : { exclusions? : Exclusion [], timeout? : number }
27+ options ? : {
28+ exclusions? : Exclusion [];
29+ timeout? : number ;
30+ }
5531): Person [];
5632```
5733
58- This returns a new ` Person ` array or throws a ` DerangementError ` if
34+ A ` Person ` array is always required. A ` Person ` must have a unique ` name ` and
35+ optionally a ` group ` . A ` Person ` cannot be matched with another person in the
36+ same ` group ` nor with themselves. A mix of people that both have and do not
37+ have a ` group ` is supported. Additional exclusion logic can be configured with
38+ [ Exclusions] ( #exclusions ) .
39+
40+ ` calculate ` returns a new ` Person ` array or throws an Error if
5941the matching algorithm fails to find a valid match after 1 second (or custom
6042timeout, if provided), indicating that an impossible combination of people and
6143exclusions was provided. If given an impossible configuration or one with few
6244possible matches, and many people, this will block the thread. To avoid this,
6345it is recommended to run the script in a WebWorker.
6446
6547``` typescript
66- import { calculateSync , Person } from ' gift-exchange' ;
48+ import { calculate , Person } from ' gift-exchange' ;
6749
6850const people: Person [] = [
6951 {
@@ -75,7 +57,7 @@ const people: Person[] = [
7557];
7658
7759try {
78- const matches = calculateSync (people );
60+ const matches = calculate (people );
7961 const pairs: { from: string ; to: string }[] = people .map ((person , i ) => ({
8062 from: person .name ,
8163 to: matches [i ].name
@@ -86,46 +68,26 @@ try {
8668}
8769```
8870
89- ### ` calculate ` (deprecated)
90-
91- ** Using a ` Promise ` is straightforward and still thread blocking, so this will
92- be removed in the next major version.**
93-
94- This function takes the same arguments as ` calculateSync ` , but returns a
95- ` Promise ` resolved with the ` Person ` array, or rejected with the
96- ` DerangementError ` .
71+ ### ` validateMatches `
9772
9873``` typescript
99- import { calculate , Person } from ' gift-exchange' ;
100-
101- const people: Person [] = [
102- {
103- name: ' Brian'
104- },
105- {
106- name: ' Freja'
107- }
108- ];
109-
110- calculate (people ).then (matches => {
111- const pairs: { from: string ; to: string }[] = people .map ((person , i ) => ({
112- from: person .name ,
113- to: matches [i ].name
114- }));
115- console .table (pairs );
116- });
74+ validateMatches (a : Person [], b : Person [], exclusions ?: Exclusion []): boolean ;
11775```
11876
77+ This is an internal helper function that validates that two ` Person ` arrays
78+ and an optional ` Exclusion ` array are valid matches where no person is matched
79+ with themselves, in the same group, or violating any exclusions. This could
80+ be helpful if you are creating your own implementation.
81+
11982### Exclusions
12083
121- The ` calculateSync ` and ` calculate ` functions can also be called with a second
122- argument ` exclusions ` . This builds upon the concept that no person can match
123- another in the same group.
84+ Exclusions build beyond the existing concept that no person can match another
85+ in the same group.
12486
12587Exclusions are single directional. Use the ` type ` and ` subject ` properties to
126- select a base Person or group of Persons. Then select an ` excludedType ` and
127- ` excludedSubject ` to select the Person or group of Persons that the previously
128- selected Person/group of Persons cannot be matched with.
88+ select a base Person or group of Persons (base selection) . Then select an
89+ ` excludedType ` and ` excludedSubject ` to select the Person or group of Persons
90+ that the base selection cannot be matched with.
12991
13092The There are two exclusion types, one of type ` name ` and one of type
13193` group ` . The ` type ` refers to a key on the ` Person ` interface. The ` subject ` is
0 commit comments