afformative 0.7.0-beta.2 → 0.7.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/index.cjs +7 -5
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +20 -26
- package/dist/index.d.ts +27 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/package.json +4 -4
- package/dist/index.d.mts +0 -33
- package/dist/index.mjs +0 -14
package/README.md
CHANGED
|
@@ -109,22 +109,22 @@ amounts.sort(amountFormatter.compare)
|
|
|
109
109
|
// [{ EUR, 1 }, { EUR, 5 }, { USD, 2 }, { USD, 3 }]
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
-
##
|
|
112
|
+
## Context
|
|
113
113
|
|
|
114
|
-
You can pass
|
|
114
|
+
You can pass context to all formatter methods. Let's use a dummy table component as an example.
|
|
115
115
|
|
|
116
116
|
```tsx
|
|
117
117
|
import { Formatter } from "afformative"
|
|
118
118
|
import { ReactNode } from "react"
|
|
119
119
|
|
|
120
|
-
interface
|
|
120
|
+
interface TableFormatterContext {
|
|
121
121
|
row: number[]
|
|
122
122
|
cellIndex: number
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
interface TableProps {
|
|
126
126
|
rows: number[][]
|
|
127
|
-
formatter: Formatter<number, ReactNode,
|
|
127
|
+
formatter: Formatter<number, ReactNode, TableFormatterContext>
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
const Table = ({ rows, formatter }: TableProps) => (
|
|
@@ -140,17 +140,17 @@ const Table = ({ rows, formatter }: TableProps) => (
|
|
|
140
140
|
)
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
Context allows the users of this table component to write purpose-built formatters, making it possible to take other values in the same row into account. For example, the following formatter would change the color of the cell value based on the previous value in the same row.
|
|
144
144
|
|
|
145
145
|
```tsx
|
|
146
146
|
import { createFormatter } from "afformative"
|
|
147
147
|
import { ReactNode } from "react"
|
|
148
148
|
|
|
149
|
-
import {
|
|
149
|
+
import { TableFormatterContext } from "./Table"
|
|
150
150
|
|
|
151
|
-
const rowTrendFormatter = createFormatter<number, ReactNode,
|
|
152
|
-
format: (value, { row, cellIndex }) => {
|
|
153
|
-
if (cellIndex
|
|
151
|
+
const rowTrendFormatter = createFormatter<number, ReactNode, TableFormatterContext>({
|
|
152
|
+
format: (value, { row, cellIndex } = {}) => {
|
|
153
|
+
if (!cellIndex || !row) {
|
|
154
154
|
return <span>{value}</span>
|
|
155
155
|
}
|
|
156
156
|
|
|
@@ -163,7 +163,7 @@ const rowTrendFormatter = createFormatter<number, ReactNode, TableFormatterUsage
|
|
|
163
163
|
|
|
164
164
|
This formatter only makes sense in the context of our table component.
|
|
165
165
|
|
|
166
|
-
Because `row` and `cellIndex` are passed as
|
|
166
|
+
Because `row` and `cellIndex` are passed as context, the formatter still receives only the cell value as its first parameter. This means generic formatters (e.g. a currency formatter) can be passed to the table component without any changes.
|
|
167
167
|
|
|
168
168
|
## Accessing React Context
|
|
169
169
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/createFormatter.ts
|
|
3
3
|
const createFormatter = (param) => {
|
|
4
|
-
const { format: formatParam, stringify: stringifyParam, compare: compareParam,
|
|
5
|
-
const format = (value,
|
|
6
|
-
const stringify = stringifyParam ? (value,
|
|
4
|
+
const { format: formatParam, stringify: stringifyParam, compare: compareParam, meta } = param;
|
|
5
|
+
const format = (value, ctx) => formatParam(value, ctx);
|
|
6
|
+
const stringify = stringifyParam ? (value, ctx) => stringifyParam(value, ctx) : (value, ctx) => String(format(value, ctx));
|
|
7
7
|
return {
|
|
8
|
-
compare: compareParam ? (a, b,
|
|
8
|
+
compare: compareParam ? (a, b, ctx) => compareParam(a, b, ctx) : (a, b, ctx) => stringify(a, ctx).localeCompare(stringify(b, ctx)),
|
|
9
9
|
format,
|
|
10
10
|
stringify,
|
|
11
|
-
|
|
11
|
+
meta
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
14
|
//#endregion
|
|
15
15
|
exports.createFormatter = createFormatter;
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/createFormatter.ts"],"sourcesContent":["export interface FormatterFormat<TInput, TOutput, TContext = unknown> {\n (value: TInput, ctx?: TContext): TOutput\n}\n\nexport interface FormatterStringify<TInput, TContext = unknown> {\n (value: TInput, ctx?: TContext): string\n}\n\nexport interface FormatterCompare<TInput, TContext = unknown> {\n (a: TInput, b: TInput, ctx?: TContext): number\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unused-vars\nexport interface FormatterMeta<TInput, TOutput, TContext = unknown> {}\n\nexport interface Formatter<TInput, TOutput, TContext = unknown> {\n compare: FormatterCompare<TInput, TContext>\n format: FormatterFormat<TInput, TOutput, TContext>\n stringify: FormatterStringify<TInput, TContext>\n meta?: FormatterMeta<TInput, TOutput, TContext>\n}\n\nexport interface CreateFormatterParam<TInput, TOutput, TContext = unknown> {\n compare?: FormatterCompare<TInput, TContext>\n format: FormatterFormat<TInput, TOutput, TContext>\n stringify?: FormatterStringify<TInput, TContext>\n meta?: FormatterMeta<TInput, TOutput, TContext>\n}\n\nexport const createFormatter = <TInput, TOutput, TContext = unknown>(\n param: CreateFormatterParam<TInput, TOutput, TContext>,\n): Formatter<TInput, TOutput, TContext> => {\n const { format: formatParam, stringify: stringifyParam, compare: compareParam, meta } = param\n\n const format: FormatterFormat<TInput, TOutput, TContext> = (value, ctx) => formatParam(value, ctx)\n\n const stringify: FormatterStringify<TInput, TContext> = stringifyParam\n ? (value, ctx) => stringifyParam(value, ctx)\n : (value, ctx) => String(format(value, ctx))\n\n const compare: FormatterCompare<TInput, TContext> = compareParam\n ? (a, b, ctx) => compareParam(a, b, ctx)\n : (a, b, ctx) => stringify(a, ctx).localeCompare(stringify(b, ctx))\n\n return {\n compare,\n format,\n stringify,\n meta,\n }\n}\n"],"mappings":";;AA6BA,MAAa,mBACX,UACyC;CACzC,MAAM,EAAE,QAAQ,aAAa,WAAW,gBAAgB,SAAS,cAAc,SAAS;CAExF,MAAM,UAAsD,OAAO,QAAQ,YAAY,OAAO,GAAG;CAEjG,MAAM,YAAkD,kBACnD,OAAO,QAAQ,eAAe,OAAO,GAAG,KACxC,OAAO,QAAQ,OAAO,OAAO,OAAO,GAAG,CAAC;CAM7C,OAAO;EACL,SALkD,gBAC/C,GAAG,GAAG,QAAQ,aAAa,GAAG,GAAG,GAAG,KACpC,GAAG,GAAG,QAAQ,UAAU,GAAG,GAAG,EAAE,cAAc,UAAU,GAAG,GAAG,CAAC;EAIlE;EACA;EACA;CACF;AACF"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
//#region src/createFormatter.d.ts
|
|
2
|
-
interface
|
|
3
|
-
(value: TInput,
|
|
2
|
+
interface FormatterFormat<TInput, TOutput, TContext = unknown> {
|
|
3
|
+
(value: TInput, ctx?: TContext): TOutput;
|
|
4
4
|
}
|
|
5
|
-
interface
|
|
6
|
-
(value: TInput,
|
|
5
|
+
interface FormatterStringify<TInput, TContext = unknown> {
|
|
6
|
+
(value: TInput, ctx?: TContext): string;
|
|
7
7
|
}
|
|
8
|
-
interface
|
|
9
|
-
(
|
|
8
|
+
interface FormatterCompare<TInput, TContext = unknown> {
|
|
9
|
+
(a: TInput, b: TInput, ctx?: TContext): number;
|
|
10
10
|
}
|
|
11
|
-
interface
|
|
12
|
-
|
|
11
|
+
interface FormatterMeta<TInput, TOutput, TContext = unknown> {}
|
|
12
|
+
interface Formatter<TInput, TOutput, TContext = unknown> {
|
|
13
|
+
compare: FormatterCompare<TInput, TContext>;
|
|
14
|
+
format: FormatterFormat<TInput, TOutput, TContext>;
|
|
15
|
+
stringify: FormatterStringify<TInput, TContext>;
|
|
16
|
+
meta?: FormatterMeta<TInput, TOutput, TContext>;
|
|
13
17
|
}
|
|
14
|
-
interface
|
|
15
|
-
|
|
18
|
+
interface CreateFormatterParam<TInput, TOutput, TContext = unknown> {
|
|
19
|
+
compare?: FormatterCompare<TInput, TContext>;
|
|
20
|
+
format: FormatterFormat<TInput, TOutput, TContext>;
|
|
21
|
+
stringify?: FormatterStringify<TInput, TContext>;
|
|
22
|
+
meta?: FormatterMeta<TInput, TOutput, TContext>;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
(a: TInput, b: TInput, usageContext?: Partial<TUsageContext>): number;
|
|
19
|
-
}
|
|
20
|
-
interface Formatter<TInput, TOutput, TUsageContext extends object = object> {
|
|
21
|
-
compare: FormatterCompare<TInput, TUsageContext>;
|
|
22
|
-
format: FormatterFormat<TInput, TOutput, TUsageContext>;
|
|
23
|
-
stringify: FormatterStringify<TInput, TUsageContext>;
|
|
24
|
-
name?: string;
|
|
25
|
-
}
|
|
26
|
-
interface CreateFormatterParam<TInput, TOutput, TUsageContext extends object = object> {
|
|
27
|
-
compare?: FormatterCompareDefinition<TInput, TUsageContext>;
|
|
28
|
-
format: FormatterFormatDefinition<TInput, TOutput, TUsageContext>;
|
|
29
|
-
stringify?: FormatterStringifyDefinition<TInput, TUsageContext>;
|
|
30
|
-
}
|
|
31
|
-
declare const createFormatter: <TInput, TOutput, TUsageContext extends object = object>(param: CreateFormatterParam<TInput, TOutput, TUsageContext>) => Formatter<TInput, TOutput, TUsageContext>;
|
|
24
|
+
declare const createFormatter: <TInput, TOutput, TContext = unknown>(param: CreateFormatterParam<TInput, TOutput, TContext>) => Formatter<TInput, TOutput, TContext>;
|
|
32
25
|
//#endregion
|
|
33
|
-
export { CreateFormatterParam, Formatter, FormatterCompare,
|
|
26
|
+
export { CreateFormatterParam, Formatter, FormatterCompare, FormatterFormat, FormatterMeta, FormatterStringify, createFormatter };
|
|
27
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/createFormatter.d.ts
|
|
2
|
+
interface FormatterFormat<TInput, TOutput, TContext = unknown> {
|
|
3
|
+
(value: TInput, ctx?: TContext): TOutput;
|
|
4
|
+
}
|
|
5
|
+
interface FormatterStringify<TInput, TContext = unknown> {
|
|
6
|
+
(value: TInput, ctx?: TContext): string;
|
|
7
|
+
}
|
|
8
|
+
interface FormatterCompare<TInput, TContext = unknown> {
|
|
9
|
+
(a: TInput, b: TInput, ctx?: TContext): number;
|
|
10
|
+
}
|
|
11
|
+
interface FormatterMeta<TInput, TOutput, TContext = unknown> {}
|
|
12
|
+
interface Formatter<TInput, TOutput, TContext = unknown> {
|
|
13
|
+
compare: FormatterCompare<TInput, TContext>;
|
|
14
|
+
format: FormatterFormat<TInput, TOutput, TContext>;
|
|
15
|
+
stringify: FormatterStringify<TInput, TContext>;
|
|
16
|
+
meta?: FormatterMeta<TInput, TOutput, TContext>;
|
|
17
|
+
}
|
|
18
|
+
interface CreateFormatterParam<TInput, TOutput, TContext = unknown> {
|
|
19
|
+
compare?: FormatterCompare<TInput, TContext>;
|
|
20
|
+
format: FormatterFormat<TInput, TOutput, TContext>;
|
|
21
|
+
stringify?: FormatterStringify<TInput, TContext>;
|
|
22
|
+
meta?: FormatterMeta<TInput, TOutput, TContext>;
|
|
23
|
+
}
|
|
24
|
+
declare const createFormatter: <TInput, TOutput, TContext = unknown>(param: CreateFormatterParam<TInput, TOutput, TContext>) => Formatter<TInput, TOutput, TContext>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { CreateFormatterParam, Formatter, FormatterCompare, FormatterFormat, FormatterMeta, FormatterStringify, createFormatter };
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/createFormatter.ts
|
|
2
|
+
const createFormatter = (param) => {
|
|
3
|
+
const { format: formatParam, stringify: stringifyParam, compare: compareParam, meta } = param;
|
|
4
|
+
const format = (value, ctx) => formatParam(value, ctx);
|
|
5
|
+
const stringify = stringifyParam ? (value, ctx) => stringifyParam(value, ctx) : (value, ctx) => String(format(value, ctx));
|
|
6
|
+
return {
|
|
7
|
+
compare: compareParam ? (a, b, ctx) => compareParam(a, b, ctx) : (a, b, ctx) => stringify(a, ctx).localeCompare(stringify(b, ctx)),
|
|
8
|
+
format,
|
|
9
|
+
stringify,
|
|
10
|
+
meta
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { createFormatter };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/createFormatter.ts"],"sourcesContent":["export interface FormatterFormat<TInput, TOutput, TContext = unknown> {\n (value: TInput, ctx?: TContext): TOutput\n}\n\nexport interface FormatterStringify<TInput, TContext = unknown> {\n (value: TInput, ctx?: TContext): string\n}\n\nexport interface FormatterCompare<TInput, TContext = unknown> {\n (a: TInput, b: TInput, ctx?: TContext): number\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unused-vars\nexport interface FormatterMeta<TInput, TOutput, TContext = unknown> {}\n\nexport interface Formatter<TInput, TOutput, TContext = unknown> {\n compare: FormatterCompare<TInput, TContext>\n format: FormatterFormat<TInput, TOutput, TContext>\n stringify: FormatterStringify<TInput, TContext>\n meta?: FormatterMeta<TInput, TOutput, TContext>\n}\n\nexport interface CreateFormatterParam<TInput, TOutput, TContext = unknown> {\n compare?: FormatterCompare<TInput, TContext>\n format: FormatterFormat<TInput, TOutput, TContext>\n stringify?: FormatterStringify<TInput, TContext>\n meta?: FormatterMeta<TInput, TOutput, TContext>\n}\n\nexport const createFormatter = <TInput, TOutput, TContext = unknown>(\n param: CreateFormatterParam<TInput, TOutput, TContext>,\n): Formatter<TInput, TOutput, TContext> => {\n const { format: formatParam, stringify: stringifyParam, compare: compareParam, meta } = param\n\n const format: FormatterFormat<TInput, TOutput, TContext> = (value, ctx) => formatParam(value, ctx)\n\n const stringify: FormatterStringify<TInput, TContext> = stringifyParam\n ? (value, ctx) => stringifyParam(value, ctx)\n : (value, ctx) => String(format(value, ctx))\n\n const compare: FormatterCompare<TInput, TContext> = compareParam\n ? (a, b, ctx) => compareParam(a, b, ctx)\n : (a, b, ctx) => stringify(a, ctx).localeCompare(stringify(b, ctx))\n\n return {\n compare,\n format,\n stringify,\n meta,\n }\n}\n"],"mappings":";AA6BA,MAAa,mBACX,UACyC;CACzC,MAAM,EAAE,QAAQ,aAAa,WAAW,gBAAgB,SAAS,cAAc,SAAS;CAExF,MAAM,UAAsD,OAAO,QAAQ,YAAY,OAAO,GAAG;CAEjG,MAAM,YAAkD,kBACnD,OAAO,QAAQ,eAAe,OAAO,GAAG,KACxC,OAAO,QAAQ,OAAO,OAAO,OAAO,GAAG,CAAC;CAM7C,OAAO;EACL,SALkD,gBAC/C,GAAG,GAAG,QAAQ,aAAa,GAAG,GAAG,GAAG,KACpC,GAAG,GAAG,QAAQ,UAAU,GAAG,GAAG,EAAE,cAAc,UAAU,GAAG,GAAG,CAAC;EAIlE;EACA;EACA;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "afformative",
|
|
3
|
-
"version": "0.7.0-beta.
|
|
3
|
+
"version": "0.7.0-beta.4",
|
|
4
4
|
"description": "A standardized way to format values in your React components.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"type": "module",
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
|
-
"import": "./dist/index.
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
31
|
"require": "./dist/index.cjs"
|
|
32
32
|
},
|
|
33
33
|
"./package.json": "./package.json"
|
|
34
34
|
},
|
|
35
35
|
"main": "./dist/index.cjs",
|
|
36
|
-
"module": "./dist/index.
|
|
36
|
+
"module": "./dist/index.js",
|
|
37
37
|
"types": "./dist/index.d.cts",
|
|
38
38
|
"files": [
|
|
39
39
|
"dist"
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"test:eslint": "eslint src",
|
|
47
47
|
"test:prettier": "prettier --check src",
|
|
48
48
|
"test:typescript": "tsc --noEmit",
|
|
49
|
-
"test:vitest": "vitest run"
|
|
49
|
+
"test:vitest": "vitest run --typecheck"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@eslint/js": "^10.0.1",
|
package/dist/index.d.mts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
//#region src/createFormatter.d.ts
|
|
2
|
-
interface FormatterFormatDefinition<TInput, TOutput, TUsageContext extends object = object> {
|
|
3
|
-
(value: TInput, usageContext: Partial<TUsageContext>): TOutput;
|
|
4
|
-
}
|
|
5
|
-
interface FormatterFormat<TInput, TOutput, TUsageContext extends object = object> {
|
|
6
|
-
(value: TInput, usageContext?: Partial<TUsageContext>): TOutput;
|
|
7
|
-
}
|
|
8
|
-
interface FormatterStringifyDefinition<TInput, TUsageContext extends object = object> {
|
|
9
|
-
(value: TInput, usageContext: Partial<TUsageContext>): string;
|
|
10
|
-
}
|
|
11
|
-
interface FormatterStringify<TInput, TUsageContext extends object = object> {
|
|
12
|
-
(value: TInput, usageContext?: Partial<TUsageContext>): string;
|
|
13
|
-
}
|
|
14
|
-
interface FormatterCompareDefinition<TInput, TUsageContext extends object = object> {
|
|
15
|
-
(a: TInput, b: TInput, usageContext: Partial<TUsageContext>): number;
|
|
16
|
-
}
|
|
17
|
-
interface FormatterCompare<TInput, TUsageContext extends object = object> {
|
|
18
|
-
(a: TInput, b: TInput, usageContext?: Partial<TUsageContext>): number;
|
|
19
|
-
}
|
|
20
|
-
interface Formatter<TInput, TOutput, TUsageContext extends object = object> {
|
|
21
|
-
compare: FormatterCompare<TInput, TUsageContext>;
|
|
22
|
-
format: FormatterFormat<TInput, TOutput, TUsageContext>;
|
|
23
|
-
stringify: FormatterStringify<TInput, TUsageContext>;
|
|
24
|
-
name?: string;
|
|
25
|
-
}
|
|
26
|
-
interface CreateFormatterParam<TInput, TOutput, TUsageContext extends object = object> {
|
|
27
|
-
compare?: FormatterCompareDefinition<TInput, TUsageContext>;
|
|
28
|
-
format: FormatterFormatDefinition<TInput, TOutput, TUsageContext>;
|
|
29
|
-
stringify?: FormatterStringifyDefinition<TInput, TUsageContext>;
|
|
30
|
-
}
|
|
31
|
-
declare const createFormatter: <TInput, TOutput, TUsageContext extends object = object>(param: CreateFormatterParam<TInput, TOutput, TUsageContext>) => Formatter<TInput, TOutput, TUsageContext>;
|
|
32
|
-
//#endregion
|
|
33
|
-
export { CreateFormatterParam, Formatter, FormatterCompare, FormatterCompareDefinition, FormatterFormat, FormatterFormatDefinition, FormatterStringify, FormatterStringifyDefinition, createFormatter };
|
package/dist/index.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
//#region src/createFormatter.ts
|
|
2
|
-
const createFormatter = (param) => {
|
|
3
|
-
const { format: formatParam, stringify: stringifyParam, compare: compareParam, ...rest } = param;
|
|
4
|
-
const format = (value, usageContext) => formatParam(value, usageContext ?? {});
|
|
5
|
-
const stringify = stringifyParam ? (value, usageContext) => stringifyParam(value, usageContext ?? {}) : (value, usageContext) => String(format(value, usageContext ?? {}));
|
|
6
|
-
return {
|
|
7
|
-
compare: compareParam ? (a, b, usageContext) => compareParam(a, b, usageContext ?? {}) : (a, b, usageContext) => stringify(a, usageContext ?? {}).localeCompare(stringify(b, usageContext ?? {})),
|
|
8
|
-
format,
|
|
9
|
-
stringify,
|
|
10
|
-
...rest
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
//#endregion
|
|
14
|
-
export { createFormatter };
|