-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ability to create a shareable token by providing a key for its …
…symbol
- Loading branch information
Showing
9 changed files
with
171 additions
and
71 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
56 changes: 1 addition & 55 deletions
56
packages/ditox/src/ditox.ts → packages/ditox/src/container.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
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,54 @@ | ||
import {optional, token} from './tokens'; | ||
|
||
describe('token()', () => { | ||
it('should return a token with description it is specified', () => { | ||
expect(token().symbol.description).toBeUndefined(); | ||
expect(token('text1').symbol.description).toBe('text1'); | ||
expect(token({description: 'text2'}).symbol.description).toBe('text2'); | ||
}); | ||
|
||
it('should return independent tokens if key is not specified', () => { | ||
const t1 = token(); | ||
const t2 = token(); | ||
|
||
expect(t1).not.toBe(t2); | ||
expect(t1.symbol).not.toBe(t2.symbol); | ||
expect(t1.isOptional).not.toBeTruthy(); | ||
}); | ||
|
||
it('should return tokens with the same symbol if key is specified', () => { | ||
const source = token({key: 'test-token'}); | ||
const clone = token({key: 'test-token'}); | ||
const something = token({key: 'something-else'}); | ||
|
||
expect(source).not.toBe(clone); | ||
expect(source.symbol).toBe(clone.symbol); | ||
|
||
expect(something).not.toBe(source); | ||
expect(something.symbol).not.toBe(source); | ||
}); | ||
}); | ||
|
||
describe('optional()', () => { | ||
it('should decorate a source token to attach an optional value', () => { | ||
const t1 = token<number>(); | ||
expect(t1.isOptional).not.toBeTruthy(); | ||
|
||
const o1 = optional(t1); | ||
expect(o1.symbol).toBe(t1.symbol); | ||
expect(o1.isOptional).toBe(true); | ||
|
||
const o2 = optional(t1, -1); | ||
expect(o2.symbol).toBe(t1.symbol); | ||
expect(o2.isOptional).toBe(true); | ||
expect(o2.optionalValue).toBe(-1); | ||
}); | ||
|
||
it('should reuse symbol from a source token ifits key is specified', () => { | ||
const source = token({key: 'token-key'}); | ||
const clone = token({key: 'token-key'}); | ||
const optClone = optional(clone); | ||
|
||
expect(optClone.symbol).toBe(source.symbol); | ||
}); | ||
}); |
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,88 @@ | ||
/** | ||
* @ignore | ||
* Binding token for mandatory value | ||
*/ | ||
export type RequiredToken<T> = { | ||
symbol: symbol; | ||
type?: T; // Anchor for Typescript type inference. | ||
isOptional?: false; | ||
}; | ||
|
||
/** | ||
* @ignore | ||
* Binding token for optional value | ||
*/ | ||
export type OptionalToken<T> = { | ||
symbol: symbol; | ||
type?: T; // Anchor for Typescript type inference. | ||
isOptional: true; | ||
optionalValue: T; | ||
}; | ||
|
||
/** | ||
* Binding token | ||
*/ | ||
export type Token<T> = RequiredToken<T> | OptionalToken<T>; | ||
|
||
/** | ||
* Token options | ||
*/ | ||
export type TokenOptions = | ||
| { | ||
/** | ||
* Key for token's symbol. It allows to create shareable tokens. | ||
*/ | ||
key: string; | ||
|
||
/** @ignore */ | ||
description?: undefined; | ||
} | ||
| { | ||
/** Description for better error messages */ | ||
description?: string; | ||
|
||
/** @ignore */ | ||
key?: undefined; | ||
}; | ||
|
||
/** | ||
* Creates a new binding token. | ||
* @param description - Token description for better error messages. | ||
*/ | ||
export function token<T>(description?: string): Token<T>; | ||
/** | ||
* Creates a new binding token. | ||
* @param options - Token description for better error messages. | ||
*/ export function token<T>(options?: TokenOptions): Token<T>; | ||
export function token<T>(options?: TokenOptions | string): Token<T> { | ||
const normalized: TokenOptions | undefined = | ||
typeof options === 'string' ? {description: options} : options; | ||
|
||
const symbol: symbol = normalized?.key | ||
? Symbol.for(normalized.key) | ||
: Symbol(normalized?.description); | ||
|
||
return {symbol}; | ||
} | ||
|
||
/** | ||
* Decorate a token with an optional value. | ||
* This value is be used as default value in case a container does not have registered token. | ||
* @param token - Existed token. | ||
* @param optionalValue - Default value for the resolver. | ||
*/ | ||
export function optional<T>( | ||
token: Token<T>, | ||
optionalValue: T, | ||
): OptionalToken<T>; | ||
export function optional<T>(token: Token<T>): OptionalToken<T | undefined>; | ||
export function optional<T>( | ||
token: Token<T>, | ||
optionalValue?: T, | ||
): OptionalToken<T | undefined> { | ||
return { | ||
symbol: token.symbol, | ||
isOptional: true, | ||
optionalValue, | ||
}; | ||
} |
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