-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from jfrazx/feature/setSupport
feat(set): handle sets
- Loading branch information
Showing
22 changed files
with
168 additions
and
34 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './value'; | ||
export * from './wrap'; |
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
11 changes: 10 additions & 1 deletion
11
src/defaults/defaultsMap.ts → src/handlers/wrap/defaultsMap.ts
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 @@ | ||
import type { TargetReceiver } from './interfaces'; | ||
import type { Property } from '../../interfaces'; | ||
import { DefaultsArray } from './defaultsArray'; | ||
import { isUndefined } from '../../helpers'; | ||
|
||
/** | ||
* @note Sets are not really value retrieving objects. Support is enabled to ensure they function normally if received by defaults | ||
* | ||
* @export | ||
* @class DefaultsSet | ||
* @extends {DefaultsArray<Set<TValue>, TValue>} | ||
* @template TValue | ||
*/ | ||
export class DefaultsSet<TValue = any> extends DefaultsArray<Set<TValue>, TValue> { | ||
get(target: Set<TValue>, event: Property, receiver?: Set<TValue>): TValue { | ||
if (event === 'size') { | ||
return Reflect.get(receiver!, event, target) as TValue; | ||
} | ||
|
||
if (event === 'add') { | ||
return this.handleAdd.bind(this, target, event) as TValue; | ||
} | ||
|
||
return super.get(target, event, receiver!); | ||
} | ||
|
||
private handleAdd(target: Set<TValue>, event: Property, value: TValue): TValue { | ||
const shouldUseDefault = | ||
isUndefined(value) || this.options.setCriteria.call(target, value, event, target); | ||
const useValue = shouldUseDefault ? this.value.supplyDefault(event) : value; | ||
return target.add(useValue) as TValue; | ||
} | ||
|
||
protected getTarget({ target }: TargetReceiver<Set<TValue>>): Set<TValue> { | ||
return target; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './defaults'; | ||
export * from './defaultsArray'; | ||
export * from './defaultsMap'; | ||
export * from './defaultsSet'; |
File renamed without changes.
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,13 @@ | ||
import type { IDefaults } from '../../../interfaces'; | ||
import { DefaultsSet } from '../../../handlers'; | ||
import { DefaultRule } from '../base'; | ||
|
||
export class SetRule<TValue> extends DefaultRule<Set<TValue>, TValue> { | ||
shouldHandle(): boolean { | ||
return this.wrap instanceof Set; | ||
} | ||
|
||
handle(): IDefaults<Set<TValue>, TValue> { | ||
return new DefaultsSet<TValue>(this.options, this.valueHandler); | ||
} | ||
} |
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,55 @@ | ||
import { wrapDefaults } from '../src/'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Set', () => { | ||
it('should use a set as a wrapped value', () => { | ||
const set = new Set<number>(); | ||
|
||
const wrapped = wrapDefaults({ | ||
wrap: set, | ||
defaultValue: 7, | ||
}); | ||
|
||
expect(wrapped).to.be.instanceOf(Set); | ||
|
||
expect(wrapped.has(9)).to.be.false; | ||
expect(wrapped.size).to.equal(0); | ||
}); | ||
|
||
it('should not set primitive values when undefined', () => { | ||
const set = new Set<number>(); | ||
|
||
const wrapped = wrapDefaults({ | ||
wrap: set, | ||
defaultValue: 7, | ||
setUndefined: true, | ||
}); | ||
|
||
expect(wrapped.has(7)).to.be.false; | ||
expect(wrapped.size).to.equal(0); | ||
|
||
wrapped.add(<any>undefined); | ||
|
||
expect(wrapped.has(7)).to.be.true; | ||
expect(wrapped.size).to.equal(1); | ||
expect(wrapped.has(<any>undefined)).to.be.false; | ||
}); | ||
|
||
it('should use setCriteria', () => { | ||
const set = new Set<number>(); | ||
|
||
const wrapped = wrapDefaults({ | ||
wrap: set, | ||
defaultValue: 4, | ||
setCriteria: (v) => v > 10, | ||
}); | ||
|
||
wrapped.add(11); | ||
wrapped.add(-9); | ||
|
||
expect(wrapped.has(4)).to.be.true; | ||
expect(wrapped.has(11)).to.be.false; | ||
expect(wrapped.has(-9)).to.be.true; | ||
expect(wrapped.size).to.equal(2); | ||
}); | ||
}); |