afformative
Advanced tools
Comparing version 0.6.0-alpha.1 to 0.6.0-alpha.2
@@ -20,2 +20,15 @@ 'use strict'; | ||
// TODO: Update `FormatDefinition` type based on this snippet. Sadly, TypeScript cannot correctly | ||
// verify the return type of the format definition based on the suggestions, printing errors such | ||
// as `Type 'string' is not assignable to type '"primitive" extends TSuggestion ? string : string'` | ||
// which is, of course, nonsense. Same applies to `FormatChainDefinition`. | ||
// | ||
// interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
// <TSuggestion extends Suggestion>( | ||
// value: TInput, | ||
// usageSuggestions: TSuggestion[], | ||
// dataContext: DataContext, | ||
// ): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput | ||
// } | ||
/** | ||
@@ -51,2 +64,14 @@ * Creates a new formatter. | ||
formatter.formatAsPrimitive = function (value, usageSuggestions, dataContext) { | ||
if (usageSuggestions === void 0) { | ||
usageSuggestions = []; | ||
} | ||
if (dataContext === void 0) { | ||
dataContext = {}; | ||
} | ||
return format(value, ["primitive"].concat(usageSuggestions), dataContext); | ||
}; | ||
formatter.wrap = function (nextFormat, nextFormatterOptions) { | ||
@@ -53,0 +78,0 @@ var nextFormatter = makeFormatter(function (value, usageSuggestions, dataContext) { |
@@ -16,2 +16,15 @@ function _objectWithoutPropertiesLoose(source, excluded) { | ||
// TODO: Update `FormatDefinition` type based on this snippet. Sadly, TypeScript cannot correctly | ||
// verify the return type of the format definition based on the suggestions, printing errors such | ||
// as `Type 'string' is not assignable to type '"primitive" extends TSuggestion ? string : string'` | ||
// which is, of course, nonsense. Same applies to `FormatChainDefinition`. | ||
// | ||
// interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
// <TSuggestion extends Suggestion>( | ||
// value: TInput, | ||
// usageSuggestions: TSuggestion[], | ||
// dataContext: DataContext, | ||
// ): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput | ||
// } | ||
/** | ||
@@ -47,2 +60,14 @@ * Creates a new formatter. | ||
formatter.formatAsPrimitive = function (value, usageSuggestions, dataContext) { | ||
if (usageSuggestions === void 0) { | ||
usageSuggestions = []; | ||
} | ||
if (dataContext === void 0) { | ||
dataContext = {}; | ||
} | ||
return format(value, ["primitive"].concat(usageSuggestions), dataContext); | ||
}; | ||
formatter.wrap = function (nextFormat, nextFormatterOptions) { | ||
@@ -49,0 +74,0 @@ var nextFormatter = makeFormatter(function (value, usageSuggestions, dataContext) { |
@@ -22,2 +22,15 @@ (function (global, factory) { | ||
// TODO: Update `FormatDefinition` type based on this snippet. Sadly, TypeScript cannot correctly | ||
// verify the return type of the format definition based on the suggestions, printing errors such | ||
// as `Type 'string' is not assignable to type '"primitive" extends TSuggestion ? string : string'` | ||
// which is, of course, nonsense. Same applies to `FormatChainDefinition`. | ||
// | ||
// interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
// <TSuggestion extends Suggestion>( | ||
// value: TInput, | ||
// usageSuggestions: TSuggestion[], | ||
// dataContext: DataContext, | ||
// ): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput | ||
// } | ||
/** | ||
@@ -53,2 +66,14 @@ * Creates a new formatter. | ||
formatter.formatAsPrimitive = function (value, usageSuggestions, dataContext) { | ||
if (usageSuggestions === void 0) { | ||
usageSuggestions = []; | ||
} | ||
if (dataContext === void 0) { | ||
dataContext = {}; | ||
} | ||
return format(value, ["primitive"].concat(usageSuggestions), dataContext); | ||
}; | ||
formatter.wrap = function (nextFormat, nextFormatterOptions) { | ||
@@ -55,0 +80,0 @@ var nextFormatter = makeFormatter(function (value, usageSuggestions, dataContext) { |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).Afformative={})}(this,(function(e){"use strict";e.makeFormatter=function e(n,t){var r=function(e){var t=e.children,r=e.suggestions,o=void 0===r?[]:r,i=function(e,n){if(null==e)return{};var t,r,o={},i=Object.keys(e);for(r=0;i.length>r;r++)0>n.indexOf(t=i[r])&&(o[t]=e[t]);return o}(e,["children","suggestions"]);return n(t,o,i)};return r.displayName=null==t?void 0:t.displayName,r.format=function(e,t,r){return void 0===t&&(t=[]),void 0===r&&(r={}),n(e,t,r)},r.wrap=function(n,o){var i=e((function(e,t,o){return n((function(e,n,i){return r.format(e,null!=n?n:t,null!=i?i:o)}),e,t,o)}),null!=o?o:t);return i.innerFormatter=r,i},r},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).Afformative={})}(this,(function(e){"use strict";e.makeFormatter=function e(n,t){var i=function(e){var t=e.children,i=e.suggestions,r=void 0===i?[]:i,o=function(e,n){if(null==e)return{};var t,i,r={},o=Object.keys(e);for(i=0;o.length>i;i++)0>n.indexOf(t=o[i])&&(r[t]=e[t]);return r}(e,["children","suggestions"]);return n(t,r,o)};return i.displayName=null==t?void 0:t.displayName,i.format=function(e,t,i){return void 0===t&&(t=[]),void 0===i&&(i={}),n(e,t,i)},i.formatAsPrimitive=function(e,t,i){return void 0===t&&(t=[]),void 0===i&&(i={}),n(e,["primitive"].concat(t),i)},i.wrap=function(n,r){var o=e((function(e,t,r){return n((function(e,n,o){return i.format(e,null!=n?n:t,null!=o?o:r)}),e,t,r)}),null!=r?r:t);return o.innerFormatter=i,o},i},Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -1,2 +0,4 @@ | ||
declare type Suggestion = "abbreviated" | "icon" | "primitive" | "verbose"; | ||
declare type PrimitiveSuggestion = "primitive"; | ||
declare type SemanticSuggestion = "abbreviated" | "icon" | "verbose"; | ||
declare type Suggestion = PrimitiveSuggestion | SemanticSuggestion; | ||
interface FormatterOptions { | ||
@@ -7,3 +9,3 @@ /** Formatter name, useful for debugging or advanced pattern matching. */ | ||
declare type DataContext = Record<string, any>; | ||
interface FormatDefinition<TInput, TOutput> { | ||
interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
( | ||
@@ -15,14 +17,23 @@ /** Value to format. */ | ||
/** Additional data context to be used by the formatter. */ | ||
dataContext: DataContext): TOutput; | ||
dataContext: DataContext): TOutput | TPrimitiveOutput; | ||
} | ||
interface FormatMethod<TInput, TOutput> { | ||
interface FormatMethod<TInput, TOutput, TPrimitiveOutput> { | ||
<TSuggestion extends Suggestion>( | ||
/** Value to format. */ | ||
value: TInput, | ||
/** Suggestions the formatter should take note of. */ | ||
usageSuggestions?: TSuggestion[], | ||
/** Additional data context the formatter might find useful. */ | ||
dataContext?: DataContext): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput; | ||
} | ||
interface FormatAsPrimitiveMethod<TInput, TPrimitiveOutput> { | ||
( | ||
/** Value to format. */ | ||
value: TInput, | ||
/** Suggestions the formatter should take note of. */ | ||
/** Suggestions the formatter should take note of in addition to `primitive`. */ | ||
usageSuggestions?: Suggestion[], | ||
/** Additional data context the formatter might find useful. */ | ||
dataContext?: DataContext): TOutput; | ||
dataContext?: DataContext): TPrimitiveOutput; | ||
} | ||
interface FormatChainDefinition<TInnerInput, TInnerOutput, TOuterInput, TOuterOutput> { | ||
interface FormatChainDefinition<TInnerInput, TInnerOutput, TInnerPrimitiveInput, TOuterInput, TOuterOutput, TOuterPrimitiveOutput> { | ||
( | ||
@@ -34,3 +45,3 @@ /** | ||
*/ | ||
delegate: FormatMethod<TInnerInput, TInnerOutput>, | ||
delegate: FormatMethod<TInnerInput, TInnerOutput, TInnerPrimitiveInput>, | ||
/** Value to format. */ | ||
@@ -41,3 +52,3 @@ value: TOuterInput, | ||
/** Additional data context the formatter might find useful. */ | ||
dataContext: DataContext): TOuterOutput; | ||
dataContext: DataContext): TOuterOutput | TOuterPrimitiveOutput; | ||
} | ||
@@ -52,9 +63,11 @@ interface FormatterProps<TInput> { | ||
} | ||
export interface Formatter<TInput, TOutput> { | ||
export interface Formatter<TInput, TOutput, TPrimitiveOutput = TOutput> { | ||
/** Formatter name, useful for debugging or advanced pattern matching. */ | ||
displayName?: string; | ||
/** Formats a value. */ | ||
format: FormatMethod<TInput, TOutput>; | ||
format: FormatMethod<TInput, TOutput, TPrimitiveOutput>; | ||
/** Formats a value with the `primitive` suggestion. */ | ||
formatAsPrimitive: FormatAsPrimitiveMethod<TInput, TPrimitiveOutput>; | ||
/** The callee of the `.wrap` method used to produce this formatter. */ | ||
innerFormatter?: Formatter<any, any>; | ||
innerFormatter?: Formatter<any, any, any>; | ||
/** | ||
@@ -64,3 +77,3 @@ * Creates a new formatter from an existing one. Allows overriding of formatter behaviour | ||
*/ | ||
wrap: <TNextInput = TInput, TNextOutput = TOutput>( | ||
wrap: <TNextInput = TInput, TNextOutput = TOutput, TNextPrimitiveOutput = TPrimitiveOutput>( | ||
/** | ||
@@ -71,5 +84,5 @@ * Function used to format the value. Has the same signature as the one passed | ||
*/ | ||
nextFormat: FormatChainDefinition<TInput, TOutput, TNextInput, TNextOutput>, | ||
nextFormat: FormatChainDefinition<TInput, TOutput, TPrimitiveOutput, TNextInput, TNextOutput, TNextPrimitiveOutput>, | ||
/** New formatter options, replacing the original ones. */ | ||
nextFormatterOptions?: FormatterOptions) => Formatter<TNextInput, TNextOutput>; | ||
nextFormatterOptions?: FormatterOptions) => Formatter<TNextInput, TNextOutput, TNextPrimitiveOutput>; | ||
/** | ||
@@ -80,3 +93,3 @@ * Backwards-compatible way to use the formatter as a React component. | ||
*/ | ||
(props: FormatterProps<TInput>): TOutput; | ||
(props: FormatterProps<TInput>): TOutput | TPrimitiveOutput; | ||
} | ||
@@ -89,4 +102,4 @@ /** | ||
*/ | ||
export declare const makeFormatter: <TInput, TOutput>(format: FormatDefinition<TInput, TOutput>, formatterOptions?: FormatterOptions | undefined) => Formatter<TInput, TOutput>; | ||
export declare const makeFormatter: <TInput, TOutput, TPrimitiveOutput = TOutput>(format: FormatDefinition<TInput, TOutput, TPrimitiveOutput>, formatterOptions?: FormatterOptions | undefined) => Formatter<TInput, TOutput, TPrimitiveOutput>; | ||
export {}; | ||
//# sourceMappingURL=makeFormatter.d.ts.map |
{ | ||
"name": "afformative", | ||
"version": "0.6.0-alpha.1", | ||
"version": "0.6.0-alpha.2", | ||
"license": "MIT", | ||
@@ -34,3 +34,3 @@ "main": "dist/afformative.cjs.js", | ||
"sideEffects": false, | ||
"gitHead": "187c3168ffaa87b8b5c4070b04a41e61ff818109" | ||
"gitHead": "370a6f424db00d131caa44a0da2d846a61f1700a" | ||
} |
@@ -30,2 +30,18 @@ import { makeFormatter } from "./makeFormatter" | ||
it("handles formatting with the `primitive` suggestion", () => { | ||
type Structure = { value: string } | ||
const formatter = makeFormatter<Structure, Structure, string>((value, suggestions) => { | ||
if (suggestions.includes("primitive")) { | ||
return value.value | ||
} | ||
return value | ||
}) | ||
expect(formatter.format({ value: "foo" }, ["primitive"])).toBe("foo") | ||
expect(formatter.formatAsPrimitive({ value: "foo" })).toBe("foo") | ||
expect(formatter.formatAsPrimitive({ value: "foo" }, ["primitive"])).toBe("foo") | ||
}) | ||
it("supports simple behavior wrapping", () => { | ||
@@ -54,2 +70,16 @@ const formatter = makeFormatter<string, string>(toUpperCase) | ||
it("supports simple behavior wrapping with the `primitive` suggestion", () => { | ||
type Structure = { value: string } | ||
const formatter = makeFormatter<string, string>(toUpperCase) | ||
const wrappedFormatter = formatter.wrap<Structure, Structure, string>( | ||
(delegate, value, suggestions) => (suggestions.includes("primitive") ? value.value : value), | ||
) | ||
expect(wrappedFormatter.format({ value: "foo" }, ["primitive"])).toBe("foo") | ||
expect(wrappedFormatter.formatAsPrimitive({ value: "foo" })).toBe("foo") | ||
expect(wrappedFormatter.formatAsPrimitive({ value: "foo" }, ["primitive"])).toBe("foo") | ||
}) | ||
it("supports simple behavior wrapping with data context", () => { | ||
@@ -56,0 +86,0 @@ const formatter = makeFormatter<string, string>(toUpperCase) |
@@ -1,2 +0,4 @@ | ||
type Suggestion = "abbreviated" | "icon" | "primitive" | "verbose" | ||
type PrimitiveSuggestion = "primitive" | ||
type SemanticSuggestion = "abbreviated" | "icon" | "verbose" | ||
type Suggestion = PrimitiveSuggestion | SemanticSuggestion | ||
@@ -10,3 +12,16 @@ interface FormatterOptions { | ||
interface FormatDefinition<TInput, TOutput> { | ||
// TODO: Update `FormatDefinition` type based on this snippet. Sadly, TypeScript cannot correctly | ||
// verify the return type of the format definition based on the suggestions, printing errors such | ||
// as `Type 'string' is not assignable to type '"primitive" extends TSuggestion ? string : string'` | ||
// which is, of course, nonsense. Same applies to `FormatChainDefinition`. | ||
// | ||
// interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
// <TSuggestion extends Suggestion>( | ||
// value: TInput, | ||
// usageSuggestions: TSuggestion[], | ||
// dataContext: DataContext, | ||
// ): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput | ||
// } | ||
interface FormatDefinition<TInput, TOutput, TPrimitiveOutput> { | ||
( | ||
@@ -19,17 +34,35 @@ /** Value to format. */ | ||
dataContext: DataContext, | ||
): TOutput | ||
): TOutput | TPrimitiveOutput | ||
} | ||
interface FormatMethod<TInput, TOutput> { | ||
interface FormatMethod<TInput, TOutput, TPrimitiveOutput> { | ||
<TSuggestion extends Suggestion>( | ||
/** Value to format. */ | ||
value: TInput, | ||
/** Suggestions the formatter should take note of. */ | ||
usageSuggestions?: TSuggestion[], | ||
/** Additional data context the formatter might find useful. */ | ||
dataContext?: DataContext, | ||
): PrimitiveSuggestion extends TSuggestion ? TPrimitiveOutput : TOutput | ||
} | ||
interface FormatAsPrimitiveMethod<TInput, TPrimitiveOutput> { | ||
( | ||
/** Value to format. */ | ||
value: TInput, | ||
/** Suggestions the formatter should take note of. */ | ||
/** Suggestions the formatter should take note of in addition to `primitive`. */ | ||
usageSuggestions?: Suggestion[], | ||
/** Additional data context the formatter might find useful. */ | ||
dataContext?: DataContext, | ||
): TOutput | ||
): TPrimitiveOutput | ||
} | ||
interface FormatChainDefinition<TInnerInput, TInnerOutput, TOuterInput, TOuterOutput> { | ||
interface FormatChainDefinition< | ||
TInnerInput, | ||
TInnerOutput, | ||
TInnerPrimitiveInput, | ||
TOuterInput, | ||
TOuterOutput, | ||
TOuterPrimitiveOutput | ||
> { | ||
( | ||
@@ -41,3 +74,3 @@ /** | ||
*/ | ||
delegate: FormatMethod<TInnerInput, TInnerOutput>, | ||
delegate: FormatMethod<TInnerInput, TInnerOutput, TInnerPrimitiveInput>, | ||
/** Value to format. */ | ||
@@ -49,3 +82,3 @@ value: TOuterInput, | ||
dataContext: DataContext, | ||
): TOuterOutput | ||
): TOuterOutput | TOuterPrimitiveOutput | ||
} | ||
@@ -62,9 +95,11 @@ | ||
export interface Formatter<TInput, TOutput> { | ||
export interface Formatter<TInput, TOutput, TPrimitiveOutput = TOutput> { | ||
/** Formatter name, useful for debugging or advanced pattern matching. */ | ||
displayName?: string | ||
/** Formats a value. */ | ||
format: FormatMethod<TInput, TOutput> | ||
format: FormatMethod<TInput, TOutput, TPrimitiveOutput> | ||
/** Formats a value with the `primitive` suggestion. */ | ||
formatAsPrimitive: FormatAsPrimitiveMethod<TInput, TPrimitiveOutput> | ||
/** The callee of the `.wrap` method used to produce this formatter. */ | ||
innerFormatter?: Formatter<any, any> | ||
innerFormatter?: Formatter<any, any, any> | ||
/** | ||
@@ -74,3 +109,3 @@ * Creates a new formatter from an existing one. Allows overriding of formatter behaviour | ||
*/ | ||
wrap: <TNextInput = TInput, TNextOutput = TOutput>( | ||
wrap: <TNextInput = TInput, TNextOutput = TOutput, TNextPrimitiveOutput = TPrimitiveOutput>( | ||
/** | ||
@@ -81,6 +116,13 @@ * Function used to format the value. Has the same signature as the one passed | ||
*/ | ||
nextFormat: FormatChainDefinition<TInput, TOutput, TNextInput, TNextOutput>, | ||
nextFormat: FormatChainDefinition< | ||
TInput, | ||
TOutput, | ||
TPrimitiveOutput, | ||
TNextInput, | ||
TNextOutput, | ||
TNextPrimitiveOutput | ||
>, | ||
/** New formatter options, replacing the original ones. */ | ||
nextFormatterOptions?: FormatterOptions, | ||
) => Formatter<TNextInput, TNextOutput> | ||
) => Formatter<TNextInput, TNextOutput, TNextPrimitiveOutput> | ||
/** | ||
@@ -91,3 +133,3 @@ * Backwards-compatible way to use the formatter as a React component. | ||
*/ | ||
(props: FormatterProps<TInput>): TOutput | ||
(props: FormatterProps<TInput>): TOutput | TPrimitiveOutput | ||
} | ||
@@ -101,8 +143,11 @@ | ||
*/ | ||
export const makeFormatter = <TInput, TOutput>( | ||
format: FormatDefinition<TInput, TOutput>, | ||
export const makeFormatter = <TInput, TOutput, TPrimitiveOutput = TOutput>( | ||
format: FormatDefinition<TInput, TOutput, TPrimitiveOutput>, | ||
formatterOptions?: FormatterOptions, | ||
): Formatter<TInput, TOutput> => { | ||
const formatter: Formatter<TInput, TOutput> = ({ children, suggestions = [], ...dataContext }) => | ||
format(children, suggestions, dataContext) | ||
): Formatter<TInput, TOutput, TPrimitiveOutput> => { | ||
const formatter: Formatter<TInput, TOutput, TPrimitiveOutput> = ({ | ||
children, | ||
suggestions = [], | ||
...dataContext | ||
}) => format(children, suggestions, dataContext) | ||
@@ -112,11 +157,21 @@ formatter.displayName = formatterOptions?.displayName | ||
formatter.format = (value, usageSuggestions = [], dataContext = {}) => | ||
format(value, usageSuggestions, dataContext) | ||
format(value, usageSuggestions, dataContext) as any | ||
formatter.wrap = <TNextInput, TNextOutput>( | ||
nextFormat: FormatChainDefinition<TInput, TOutput, TNextInput, TNextOutput>, | ||
formatter.formatAsPrimitive = (value, usageSuggestions = [], dataContext = {}) => | ||
format(value, ["primitive", ...usageSuggestions], dataContext) as any | ||
formatter.wrap = <TNextInput, TNextOutput, TNextPrimitiveOutput>( | ||
nextFormat: FormatChainDefinition< | ||
TInput, | ||
TOutput, | ||
TPrimitiveOutput, | ||
TNextInput, | ||
TNextOutput, | ||
TNextPrimitiveOutput | ||
>, | ||
nextFormatterOptions?: FormatterOptions, | ||
) => { | ||
const nextFormatter: Formatter<TNextInput, TNextOutput> = makeFormatter( | ||
const nextFormatter: Formatter<TNextInput, TNextOutput, TNextPrimitiveOutput> = makeFormatter( | ||
(value, usageSuggestions, dataContext) => { | ||
const delegate: FormatMethod<TInput, TOutput> = ( | ||
const delegate: FormatMethod<TInput, TOutput, TPrimitiveOutput> = ( | ||
delegatedValue, | ||
@@ -130,5 +185,5 @@ delegatedUsageSuggestions, | ||
delegatedDataContext ?? dataContext, | ||
) | ||
) as any | ||
return nextFormat(delegate, value, usageSuggestions, dataContext) | ||
return nextFormat(delegate, value, usageSuggestions, dataContext) as any | ||
}, | ||
@@ -135,0 +190,0 @@ nextFormatterOptions ?? formatterOptions, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36543
581
15