@likec4/core 1.8.0 → 1.9.0

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.
Files changed (89) hide show
  1. package/dist/index.cjs +4368 -0
  2. package/dist/index.d.cts +781 -0
  3. package/dist/index.d.mts +781 -0
  4. package/dist/index.mjs +4296 -0
  5. package/dist/shared/core.9l7eW3pn.d.cts +817 -0
  6. package/dist/shared/core.9l7eW3pn.d.mts +817 -0
  7. package/dist/shared/core.DcPLm41i.cjs +304 -0
  8. package/dist/shared/core.DfUTsojZ.mjs +251 -0
  9. package/dist/types/index.cjs +53 -0
  10. package/dist/types/index.d.cts +1 -0
  11. package/dist/types/index.d.mts +1 -0
  12. package/dist/types/index.mjs +1 -0
  13. package/package.json +41 -33
  14. package/src/colors/index.ts +27 -1
  15. package/src/errors/index.ts +3 -95
  16. package/src/index.ts +5 -6
  17. package/src/model/LikeC4Model.ts +480 -0
  18. package/src/model/LikeC4ViewModel.ts +318 -0
  19. package/src/model/__test__/LikeC4Model.spec.ts +79 -0
  20. package/src/model/__test__/fixture.ts +333 -0
  21. package/src/model/index.ts +2 -0
  22. package/src/model/types.ts +10 -0
  23. package/src/types/_common.ts +4 -2
  24. package/src/types/element.ts +8 -8
  25. package/src/types/expression.ts +3 -3
  26. package/src/types/index.ts +0 -1
  27. package/src/types/model.ts +16 -6
  28. package/src/types/relation.ts +15 -5
  29. package/src/types/theme.ts +28 -12
  30. package/src/types/view-notation.ts +3 -4
  31. package/src/types/view.ts +14 -10
  32. package/src/utils/fqn.ts +7 -4
  33. package/src/utils/relations.spec.ts +1 -3
  34. package/src/utils/relations.ts +7 -17
  35. package/dist/colors/element.d.ts +0 -69
  36. package/dist/colors/element.js +0 -75
  37. package/dist/colors/index.d.ts +0 -133
  38. package/dist/colors/index.js +0 -9
  39. package/dist/colors/relationships.d.ts +0 -58
  40. package/dist/colors/relationships.js +0 -49
  41. package/dist/errors/errors.spec.d.ts +0 -2
  42. package/dist/errors/errors.spec.js +0 -69
  43. package/dist/errors/index.d.ts +0 -39
  44. package/dist/errors/index.js +0 -104
  45. package/dist/index.d.ts +0 -7
  46. package/dist/index.js +0 -4
  47. package/dist/types/_common.d.ts +0 -9
  48. package/dist/types/_common.js +0 -1
  49. package/dist/types/element.d.ts +0 -55
  50. package/dist/types/element.js +0 -15
  51. package/dist/types/expression.d.ts +0 -119
  52. package/dist/types/expression.js +0 -67
  53. package/dist/types/index.d.ts +0 -14
  54. package/dist/types/index.js +0 -14
  55. package/dist/types/model.d.ts +0 -14
  56. package/dist/types/model.js +0 -1
  57. package/dist/types/opaque.d.ts +0 -103
  58. package/dist/types/opaque.js +0 -1
  59. package/dist/types/operators.d.ts +0 -44
  60. package/dist/types/operators.js +0 -59
  61. package/dist/types/overview-graph.d.ts +0 -41
  62. package/dist/types/overview-graph.js +0 -1
  63. package/dist/types/relation.d.ts +0 -30
  64. package/dist/types/relation.js +0 -3
  65. package/dist/types/theme.d.ts +0 -27
  66. package/dist/types/theme.js +0 -1
  67. package/dist/types/view-changes.d.ts +0 -26
  68. package/dist/types/view-changes.js +0 -1
  69. package/dist/types/view-notation.d.ts +0 -9
  70. package/dist/types/view-notation.js +0 -1
  71. package/dist/types/view.d.ts +0 -243
  72. package/dist/types/view.js +0 -50
  73. package/dist/utils/fqn.d.ts +0 -39
  74. package/dist/utils/fqn.js +0 -102
  75. package/dist/utils/fqn.spec.d.ts +0 -2
  76. package/dist/utils/fqn.spec.js +0 -82
  77. package/dist/utils/guards.d.ts +0 -5
  78. package/dist/utils/guards.js +0 -7
  79. package/dist/utils/index.d.ts +0 -5
  80. package/dist/utils/index.js +0 -4
  81. package/dist/utils/promises.d.ts +0 -2
  82. package/dist/utils/promises.js +0 -8
  83. package/dist/utils/relations.d.ts +0 -30
  84. package/dist/utils/relations.js +0 -79
  85. package/dist/utils/relations.spec.d.ts +0 -2
  86. package/dist/utils/relations.spec.js +0 -210
  87. package/src/errors/errors.spec.ts +0 -88
  88. package/src/reset.d.ts +0 -2
  89. package/src/types/opaque.ts +0 -108
@@ -1,104 +0,0 @@
1
- import { CustomError } from 'ts-custom-error';
2
- import { isString } from '../utils/guards';
3
- /**
4
- * Base class for all errors in the LikeC4 library.
5
- */
6
- export class BaseError extends CustomError {
7
- constructor(message, options) {
8
- super(message, options);
9
- // Set name explicitly as minification can mangle class names
10
- Object.defineProperty(this, 'name', { value: 'BaseError' });
11
- }
12
- }
13
- export class InvalidArgError extends BaseError {
14
- constructor(message, options) {
15
- super(message, options);
16
- // Set name explicitly as minification can mangle class names
17
- Object.defineProperty(this, 'name', { value: 'InvalidArgError' });
18
- }
19
- }
20
- /**
21
- * Unknown error, mosly probably a bug, unhandled case or coming from a third-party library.
22
- */
23
- export class UnknownError extends BaseError {
24
- constructor(message, options) {
25
- super(message, options);
26
- // Set name explicitly as minification can mangle class names
27
- Object.defineProperty(this, 'name', { value: 'UnknownError' });
28
- }
29
- }
30
- export class NullableError extends BaseError {
31
- constructor(message, options) {
32
- super(message, options);
33
- // Set name explicitly as minification can mangle class names
34
- Object.defineProperty(this, 'name', { value: 'NullableError' });
35
- }
36
- }
37
- export class InvariantError extends BaseError {
38
- constructor(message, options) {
39
- super(message, options);
40
- // Set name explicitly as minification can mangle class names
41
- Object.defineProperty(this, 'name', { value: 'InvariantError' });
42
- }
43
- }
44
- // Ensure that the value is NonNullable
45
- // Mostly as safer `value!`
46
- // eslint-disable-next-line @typescript-eslint/ban-types
47
- export function nonNullable(value, message) {
48
- if (typeof value === 'undefined' || value == null) {
49
- throw new NullableError(message ?? `Expected defined value, but received ${value}`);
50
- }
51
- return value;
52
- }
53
- export class RelationRefError extends BaseError {
54
- constructor(message, options) {
55
- super(message, options);
56
- // Set name explicitly as minification can mangle class names
57
- Object.defineProperty(this, 'name', { value: 'RelationRefError' });
58
- }
59
- }
60
- export function normalizeError(e) {
61
- if (e instanceof BaseError || e instanceof Error) {
62
- return e;
63
- }
64
- const message = isString(e) ? e : String(e);
65
- const error = new UnknownError(message);
66
- try {
67
- // @ts-ignore
68
- Error.captureStackTrace(error, normalizeError);
69
- }
70
- catch {
71
- // Ignore
72
- }
73
- return error;
74
- }
75
- // Throw an error if the condition fails
76
- // > Not providing an inline default argument for message as the result is smaller
77
- export function invariant(
78
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
- condition,
80
- // Can provide a string, or a function that returns a string for cases where
81
- // the message takes a fair amount of effort to compute
82
- message) {
83
- if (condition) {
84
- return;
85
- }
86
- throw new InvariantError(message ?? 'Invariant failed');
87
- }
88
- export class NonExhaustiveError extends BaseError {
89
- constructor(message, options) {
90
- super(message, options);
91
- // Set name explicitly as minification can mangle class names
92
- Object.defineProperty(this, 'name', { value: 'NonExhaustiveError' });
93
- }
94
- }
95
- export function nonexhaustive(value) {
96
- throw new NonExhaustiveError(`NonExhaustive value: ${value}`);
97
- }
98
- export class InvalidModelError extends BaseError {
99
- constructor(message, options) {
100
- super(message, options);
101
- // Set name explicitly as minification can mangle class names
102
- Object.defineProperty(this, 'name', { value: 'InvalidModelError' });
103
- }
104
- }
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export * from './colors';
2
- export * from './errors';
3
- export * from './types';
4
- export * from './utils';
5
- export type * as likec4 from './types';
6
- export type * as c4 from './types';
7
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js DELETED
@@ -1,4 +0,0 @@
1
- export * from './colors';
2
- export * from './errors';
3
- export * from './types';
4
- export * from './utils';
@@ -1,9 +0,0 @@
1
- import type { Opaque } from './opaque';
2
- export type NonEmptyArray<T> = [T, ...T[]];
3
- export type IconUrl = Opaque<string, 'IconUrl'>;
4
- export type Point = readonly [x: number, y: number];
5
- export interface XYPoint {
6
- x: number;
7
- y: number;
8
- }
9
- //# sourceMappingURL=_common.d.ts.map
@@ -1 +0,0 @@
1
- export {};
@@ -1,55 +0,0 @@
1
- import type { IconUrl, NonEmptyArray } from './_common';
2
- import type { Opaque } from './opaque';
3
- import type { ThemeColor } from './theme';
4
- export type Fqn = Opaque<string, 'Fqn'>;
5
- export declare function AsFqn(name: string, parent?: Fqn | null): Fqn;
6
- export declare const BorderStyles: readonly ["solid", "dashed", "dotted", "none"];
7
- export type BorderStyle = typeof BorderStyles[number];
8
- export type ElementKind = Opaque<string, 'ElementKind'>;
9
- export declare const ElementShapes: readonly ["rectangle", "person", "browser", "mobile", "cylinder", "storage", "queue"];
10
- export type ElementShape = typeof ElementShapes[number];
11
- export declare const DefaultThemeColor = "primary";
12
- export declare const DefaultElementShape = "rectangle";
13
- export interface ElementStyle {
14
- border?: BorderStyle;
15
- opacity?: number;
16
- }
17
- export type Tag = Opaque<string, 'Tag'>;
18
- export interface TagSpec {
19
- readonly id: Tag;
20
- readonly style: ElementStyle;
21
- }
22
- export interface Link {
23
- readonly title?: string;
24
- readonly url: string;
25
- }
26
- export interface Element {
27
- readonly id: Fqn;
28
- readonly kind: ElementKind;
29
- readonly title: string;
30
- readonly description: string | null;
31
- readonly technology: string | null;
32
- readonly tags: NonEmptyArray<Tag> | null;
33
- readonly links: NonEmptyArray<Link> | null;
34
- readonly icon?: IconUrl;
35
- readonly shape?: ElementShape;
36
- readonly color?: ThemeColor;
37
- readonly style?: ElementStyle;
38
- readonly notation?: string;
39
- readonly metadata?: {
40
- [key: string]: string;
41
- };
42
- }
43
- export interface ElementKindSpecificationStyle {
44
- shape?: ElementShape;
45
- icon?: IconUrl;
46
- color?: ThemeColor;
47
- border?: BorderStyle;
48
- opacity?: number;
49
- }
50
- export interface ElementKindSpecification {
51
- readonly technology?: string;
52
- readonly notation?: string;
53
- readonly style: ElementKindSpecificationStyle;
54
- }
55
- //# sourceMappingURL=element.d.ts.map
@@ -1,15 +0,0 @@
1
- export function AsFqn(name, parent) {
2
- return (parent ? parent + '.' + name : name);
3
- }
4
- export const BorderStyles = ['solid', 'dashed', 'dotted', 'none'];
5
- export const ElementShapes = [
6
- 'rectangle',
7
- 'person',
8
- 'browser',
9
- 'mobile',
10
- 'cylinder',
11
- 'storage',
12
- 'queue'
13
- ];
14
- export const DefaultThemeColor = 'primary';
15
- export const DefaultElementShape = 'rectangle';
@@ -1,119 +0,0 @@
1
- import type { IconUrl } from './_common';
2
- import type { BorderStyle, ElementKind, ElementShape, Fqn, Tag } from './element';
3
- import type { WhereOperator } from './operators';
4
- import type { RelationshipArrowType, RelationshipLineType } from './relation';
5
- import type { ThemeColor } from './theme';
6
- import type { ViewID } from './view';
7
- interface BaseExpr {
8
- where?: never;
9
- element?: never;
10
- custom?: never;
11
- expanded?: never;
12
- elementKind?: never;
13
- elementTag?: never;
14
- isEqual?: never;
15
- isDescedants?: never;
16
- wildcard?: never;
17
- source?: never;
18
- target?: never;
19
- inout?: never;
20
- incoming?: never;
21
- outgoing?: never;
22
- customRelation?: never;
23
- }
24
- export interface ElementRefExpr extends Omit<BaseExpr, 'element' | 'isDescedants'> {
25
- element: Fqn;
26
- isDescedants?: boolean;
27
- }
28
- export declare function isElementRef(expr: Expression): expr is ElementRefExpr;
29
- export interface ExpandedElementExpr extends Omit<BaseExpr, 'expanded'> {
30
- expanded: Fqn;
31
- }
32
- export declare function isExpandedElementExpr(expr: Expression): expr is ExpandedElementExpr;
33
- export interface CustomElementExpr extends Omit<BaseExpr, 'custom'> {
34
- custom: {
35
- expr: ElementExpression | ElementWhereExpr;
36
- title?: string;
37
- description?: string;
38
- technology?: string;
39
- notation?: string;
40
- shape?: ElementShape;
41
- color?: ThemeColor;
42
- icon?: IconUrl;
43
- border?: BorderStyle;
44
- opacity?: number;
45
- navigateTo?: ViewID;
46
- };
47
- }
48
- export declare function isCustomElement(expr: Expression): expr is CustomElementExpr;
49
- export interface WildcardExpr extends Omit<BaseExpr, 'wildcard'> {
50
- wildcard: true;
51
- }
52
- export declare function isWildcard(expr: Expression): expr is WildcardExpr;
53
- export interface ElementKindExpr extends Omit<BaseExpr, 'elementKind' | 'isEqual'> {
54
- elementKind: ElementKind;
55
- isEqual: boolean;
56
- }
57
- export declare function isElementKindExpr(expr: Expression): expr is ElementKindExpr;
58
- export interface ElementTagExpr extends Omit<BaseExpr, 'elementTag' | 'isEqual'> {
59
- elementTag: Tag;
60
- isEqual: boolean;
61
- }
62
- export declare function isElementTagExpr(expr: Expression): expr is ElementTagExpr;
63
- export type ElementExpression = ElementRefExpr | WildcardExpr | ElementKindExpr | ElementTagExpr | ExpandedElementExpr;
64
- export declare function isElement(expr: Expression): expr is ElementExpression;
65
- export interface ElementWhereExpr extends Omit<BaseExpr, 'where'> {
66
- where: {
67
- expr: ElementExpression;
68
- condition: WhereOperator<string, string>;
69
- };
70
- }
71
- export declare function isElementWhere(expr: Expression): expr is ElementWhereExpr;
72
- export type ElementPredicateExpression = ElementExpression | ElementWhereExpr | CustomElementExpr;
73
- export declare function isElementPredicateExpr(expr: Expression): expr is ElementPredicateExpression;
74
- export interface RelationExpr extends Omit<BaseExpr, 'source' | 'target'> {
75
- source: ElementExpression;
76
- target: ElementExpression;
77
- isBidirectional?: boolean;
78
- }
79
- export declare function isRelation(expr: Expression): expr is RelationExpr;
80
- export interface InOutExpr extends Omit<BaseExpr, 'inout'> {
81
- inout: ElementExpression;
82
- }
83
- export declare function isInOut(expr: Expression): expr is InOutExpr;
84
- export interface IncomingExpr extends Omit<BaseExpr, 'incoming'> {
85
- incoming: ElementExpression;
86
- }
87
- export declare function isIncoming(expr: Expression): expr is IncomingExpr;
88
- export interface OutgoingExpr extends Omit<BaseExpr, 'outgoing'> {
89
- outgoing: ElementExpression;
90
- }
91
- export declare function isOutgoing(expr: Expression): expr is OutgoingExpr;
92
- export type RelationExpression = RelationExpr | InOutExpr | IncomingExpr | OutgoingExpr;
93
- export declare function isRelationExpression(expr: Expression): expr is RelationExpression;
94
- export interface RelationWhereExpr extends Omit<BaseExpr, 'where'> {
95
- where: {
96
- expr: RelationExpression;
97
- condition: WhereOperator<string, string>;
98
- };
99
- }
100
- export declare function isRelationWhere(expr: Expression): expr is RelationWhereExpr;
101
- export interface CustomRelationExpr extends Omit<BaseExpr, 'customRelation'> {
102
- customRelation: {
103
- relation: RelationExpression | RelationWhereExpr;
104
- title?: string;
105
- description?: string;
106
- technology?: string;
107
- notation?: string;
108
- color?: ThemeColor;
109
- line?: RelationshipLineType;
110
- head?: RelationshipArrowType;
111
- tail?: RelationshipArrowType;
112
- };
113
- }
114
- export declare function isCustomRelationExpr(expr: Expression): expr is CustomRelationExpr;
115
- export type RelationPredicateExpression = RelationExpression | RelationWhereExpr | CustomRelationExpr;
116
- export declare function isRelationPredicateExpr(expr: Expression): expr is RelationPredicateExpression;
117
- export type Expression = ElementPredicateExpression | RelationPredicateExpression;
118
- export {};
119
- //# sourceMappingURL=expression.d.ts.map
@@ -1,67 +0,0 @@
1
- export function isElementRef(expr) {
2
- return 'element' in expr;
3
- }
4
- export function isExpandedElementExpr(expr) {
5
- return 'expanded' in expr;
6
- }
7
- export function isCustomElement(expr) {
8
- return 'custom' in expr && (isElement(expr.custom.expr) || isElementWhere(expr.custom.expr));
9
- }
10
- export function isWildcard(expr) {
11
- return 'wildcard' in expr;
12
- }
13
- export function isElementKindExpr(expr) {
14
- return 'elementKind' in expr && 'isEqual' in expr;
15
- }
16
- export function isElementTagExpr(expr) {
17
- return 'elementTag' in expr && 'isEqual' in expr;
18
- }
19
- export function isElement(expr) {
20
- return isElementRef(expr)
21
- || isWildcard(expr)
22
- || isElementKindExpr(expr)
23
- || isElementTagExpr(expr)
24
- || isExpandedElementExpr(expr);
25
- }
26
- export function isElementWhere(expr) {
27
- return 'where' in expr && isElement(expr.where.expr);
28
- }
29
- export function isElementPredicateExpr(expr) {
30
- return isElement(expr) || isElementWhere(expr) || isCustomElement(expr);
31
- }
32
- export function isRelation(expr) {
33
- return 'source' in expr && 'target' in expr;
34
- }
35
- export function isInOut(expr) {
36
- return 'inout' in expr;
37
- }
38
- export function isIncoming(expr) {
39
- return 'incoming' in expr;
40
- }
41
- export function isOutgoing(expr) {
42
- return 'outgoing' in expr;
43
- }
44
- export function isRelationExpression(expr) {
45
- return isRelation(expr) || isInOut(expr) || isIncoming(expr) || isOutgoing(expr);
46
- }
47
- export function isRelationWhere(expr) {
48
- return 'where' in expr && isRelationExpression(expr.where.expr);
49
- }
50
- export function isCustomRelationExpr(expr) {
51
- return 'customRelation' in expr;
52
- }
53
- export function isRelationPredicateExpr(expr) {
54
- return isRelationExpression(expr) || isRelationWhere(expr) || isCustomRelationExpr(expr);
55
- }
56
- // export const Expr = {
57
- // isElementRef,
58
- // isWildcard,
59
- // isElementKindExpr,
60
- // isElementTagExpr,
61
- // isElement,
62
- // isRelation,
63
- // isInOut,
64
- // isIncoming,
65
- // isOutgoing,
66
- // isAnyRelation,
67
- // }
@@ -1,14 +0,0 @@
1
- export * from './_common';
2
- export * from './element';
3
- export * from './expression';
4
- export * from './model';
5
- export * from './opaque';
6
- export * from './operators';
7
- export * from './overview-graph';
8
- export * from './relation';
9
- export * from './theme';
10
- export * from './view';
11
- export * from './view-changes';
12
- export * from './view-notation';
13
- export * as Expr from './expression';
14
- //# sourceMappingURL=index.d.ts.map
@@ -1,14 +0,0 @@
1
- export * from './_common';
2
- export * from './element';
3
- export * from './expression';
4
- export * from './model';
5
- export * from './opaque';
6
- export * from './operators';
7
- export * from './overview-graph';
8
- export * from './relation';
9
- export * from './theme';
10
- export * from './view';
11
- export * from './view-changes';
12
- export * from './view-notation';
13
- import * as Expr_1 from './expression';
14
- export { Expr_1 as Expr };
@@ -1,14 +0,0 @@
1
- import type { Element, Fqn } from './element';
2
- import type { Relation, RelationID } from './relation';
3
- import type { ComputedView, LikeC4View, ViewID } from './view';
4
- export interface LikeC4Model {
5
- elements: Record<Fqn, Element>;
6
- relations: Record<RelationID, Relation>;
7
- views: Record<ViewID, LikeC4View>;
8
- }
9
- export interface LikeC4ComputedModel {
10
- elements: Record<Fqn, Element>;
11
- relations: Record<RelationID, Relation>;
12
- views: Record<ViewID, ComputedView>;
13
- }
14
- //# sourceMappingURL=model.d.ts.map
@@ -1 +0,0 @@
1
- export {};
@@ -1,103 +0,0 @@
1
- declare const tag: unique symbol;
2
- type Tagged<Token> = {
3
- readonly [tag]: Token;
4
- };
5
- /**
6
- Create an opaque type, which hides its internal details from the public, and can only be created by being used explicitly.
7
-
8
- The generic type parameter can be anything. It doesn't have to be an object.
9
-
10
- [Read more about opaque types.](https://codemix.com/opaque-types-in-javascript/)
11
-
12
- There have been several discussions about adding this feature to TypeScript via the `opaque type` operator, similar to how Flow does it. Unfortunately, nothing has (yet) moved forward:
13
- - [Microsoft/TypeScript#202](https://github.com/microsoft/TypeScript/issues/202)
14
- - [Microsoft/TypeScript#15408](https://github.com/Microsoft/TypeScript/issues/15408)
15
- - [Microsoft/TypeScript#15807](https://github.com/Microsoft/TypeScript/issues/15807)
16
-
17
- @example
18
- ```
19
- import type {Opaque} from 'type-fest';
20
-
21
- type AccountNumber = Opaque<number, 'AccountNumber'>;
22
- type AccountBalance = Opaque<number, 'AccountBalance'>;
23
-
24
- // The `Token` parameter allows the compiler to differentiate between types, whereas "unknown" will not. For example, consider the following structures:
25
- type ThingOne = Opaque<string>;
26
- type ThingTwo = Opaque<string>;
27
-
28
- // To the compiler, these types are allowed to be cast to each other as they have the same underlying type. They are both `string & { __opaque__: unknown }`.
29
- // To avoid this behaviour, you would instead pass the "Token" parameter, like so.
30
- type NewThingOne = Opaque<string, 'ThingOne'>;
31
- type NewThingTwo = Opaque<string, 'ThingTwo'>;
32
-
33
- // Now they're completely separate types, so the following will fail to compile.
34
- function createNewThingOne (): NewThingOne {
35
- // As you can see, casting from a string is still allowed. However, you may not cast NewThingOne to NewThingTwo, and vice versa.
36
- return 'new thing one' as NewThingOne;
37
- }
38
-
39
- // This will fail to compile, as they are fundamentally different types.
40
- const thingTwo = createNewThingOne() as NewThingTwo;
41
-
42
- // Here's another example of opaque typing.
43
- function createAccountNumber(): AccountNumber {
44
- return 2 as AccountNumber;
45
- }
46
-
47
- function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {
48
- return 4 as AccountBalance;
49
- }
50
-
51
- // This will compile successfully.
52
- getMoneyForAccount(createAccountNumber());
53
-
54
- // But this won't, because it has to be explicitly passed as an `AccountNumber` type.
55
- getMoneyForAccount(2);
56
-
57
- // You can use opaque values like they aren't opaque too.
58
- const accountNumber = createAccountNumber();
59
-
60
- // This will not compile successfully.
61
- const newAccountNumber = accountNumber + 2;
62
-
63
- // As a side note, you can (and should) use recursive types for your opaque types to make them stronger and hopefully easier to type.
64
- type Person = {
65
- id: Opaque<number, Person>;
66
- name: string;
67
- };
68
- ```
69
-
70
- @category Type
71
- */
72
- export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;
73
- /**
74
- Revert an opaque type back to its original type by removing the readonly `[tag]`.
75
-
76
- Why is this necessary?
77
-
78
- 1. Use an `Opaque` type as object keys
79
- 2. Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"
80
-
81
- @example
82
- ```
83
- import type {Opaque, UnwrapOpaque} from 'type-fest';
84
-
85
- type AccountType = Opaque<'SAVINGS' | 'CHECKING', 'AccountType'>;
86
-
87
- const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
88
- SAVINGS: 99,
89
- CHECKING: 0.1
90
- };
91
-
92
- // Without UnwrapOpaque, the following expression would throw a type error.
93
- const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
94
-
95
- // Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
96
- type WontWork = UnwrapOpaque<string>;
97
- ```
98
-
99
- @category Type
100
- */
101
- export type UnwrapOpaque<OpaqueType extends Tagged<unknown>> = OpaqueType extends Opaque<infer Type, OpaqueType[typeof tag]> ? Type : OpaqueType;
102
- export {};
103
- //# sourceMappingURL=opaque.d.ts.map
@@ -1 +0,0 @@
1
- export {};
@@ -1,44 +0,0 @@
1
- import type { NonEmptyArray } from './_common';
2
- export type EqualOperator<V> = {
3
- eq: V;
4
- neq?: never;
5
- } | {
6
- eq?: never;
7
- neq: V;
8
- };
9
- type AllNever = {
10
- not?: never;
11
- and?: never;
12
- or?: never;
13
- tag?: never;
14
- kind?: never;
15
- };
16
- export type TagEqual<Tag> = Omit<AllNever, 'tag'> & {
17
- tag: EqualOperator<Tag>;
18
- };
19
- export declare const isTagEqual: <Tag>(operator: WhereOperator<Tag, any>) => operator is TagEqual<Tag>;
20
- export type KindEqual<Kind> = Omit<AllNever, 'kind'> & {
21
- kind: EqualOperator<Kind>;
22
- };
23
- export declare const isKindEqual: <Kind>(operator: WhereOperator<any, Kind>) => operator is KindEqual<Kind>;
24
- export type NotOperator<Tag, Kind> = Omit<AllNever, 'not'> & {
25
- not: WhereOperator<Tag, Kind>;
26
- };
27
- export declare const isNotOperator: <Tag, Kind>(operator: WhereOperator<Tag, Kind>) => operator is NotOperator<Tag, Kind>;
28
- export type AndOperator<Tag, Kind> = Omit<AllNever, 'and'> & {
29
- and: NonEmptyArray<WhereOperator<Tag, Kind>>;
30
- };
31
- export declare const isAndOperator: <Tag, Kind>(operator: WhereOperator<Tag, Kind>) => operator is AndOperator<Tag, Kind>;
32
- export type OrOperator<Tag, Kind> = Omit<AllNever, 'or'> & {
33
- or: NonEmptyArray<WhereOperator<Tag, Kind>>;
34
- };
35
- export declare const isOrOperator: <Tag, Kind>(operator: WhereOperator<Tag, Kind>) => operator is OrOperator<Tag, Kind>;
36
- export type WhereOperator<Tag, Kind> = TagEqual<Tag> | KindEqual<Kind> | NotOperator<Tag, Kind> | AndOperator<Tag, Kind> | OrOperator<Tag, Kind>;
37
- type Filterable<FTag extends string = string, FKind extends string = string> = {
38
- tags?: FTag[] | null;
39
- kind?: FKind;
40
- };
41
- type OperatorPredicate<V extends Filterable> = (value: V) => boolean;
42
- export declare function whereOperatorAsPredicate<FTag extends string = string, FKind extends string = string>(operator: WhereOperator<FTag, FKind>): OperatorPredicate<Filterable<FTag, FKind>>;
43
- export {};
44
- //# sourceMappingURL=operators.d.ts.map
@@ -1,59 +0,0 @@
1
- import { allPass, anyPass, isNot, isNullish } from 'remeda';
2
- import { nonexhaustive } from '../errors';
3
- export const isTagEqual = (operator) => {
4
- return 'tag' in operator;
5
- };
6
- export const isKindEqual = (operator) => {
7
- return 'kind' in operator;
8
- };
9
- export const isNotOperator = (operator) => {
10
- return 'not' in operator;
11
- };
12
- export const isAndOperator = (operator) => {
13
- return 'and' in operator;
14
- };
15
- export const isOrOperator = (operator) => {
16
- return 'or' in operator;
17
- };
18
- export function whereOperatorAsPredicate(operator) {
19
- switch (true) {
20
- case isTagEqual(operator): {
21
- if ('eq' in operator.tag) {
22
- const tag = operator.tag.eq;
23
- return (value) => {
24
- return Array.isArray(value.tags) && value.tags.includes(tag);
25
- };
26
- }
27
- const tag = operator.tag.neq;
28
- return (value) => {
29
- return !Array.isArray(value.tags) || !value.tags.includes(tag);
30
- };
31
- }
32
- case isKindEqual(operator): {
33
- if ('eq' in operator.kind) {
34
- const kind = operator.kind.eq;
35
- return (value) => {
36
- return value.kind === kind;
37
- };
38
- }
39
- const kind = operator.kind.neq;
40
- return (value) => {
41
- return isNullish(value.kind) || value.kind !== kind;
42
- };
43
- }
44
- case isNotOperator(operator): {
45
- const predicate = whereOperatorAsPredicate(operator.not);
46
- return isNot(predicate);
47
- }
48
- case isAndOperator(operator): {
49
- const predicates = operator.and.map(whereOperatorAsPredicate);
50
- return allPass(predicates);
51
- }
52
- case isOrOperator(operator): {
53
- const predicates = operator.or.map(whereOperatorAsPredicate);
54
- return anyPass(predicates);
55
- }
56
- default:
57
- nonexhaustive(operator);
58
- }
59
- }