inferred-types 0.41.2 → 0.41.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 +78 -70
- package/dist/inferred-types/tsconfig.tsbuildinfo +1 -1
- package/dist/runtime/tsconfig.tsbuildinfo +1 -1
- package/dist/types/containers/WithKey.d.ts +9 -5
- package/dist/types/containers/WithKey.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -1,53 +1,91 @@
|
|
|
1
|
-
# Inferred Types
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+

|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
## Overview
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
A collection of Typescript utilities which try to preserve as much strong and narrow typing as is possible. In many cases, these _type utilities_ will be paired with a runtime function which provides a runtime mirror to help keep both design time types and runtime values in sync.
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
All types -- as well as any pairing runtime functions -- are intended to be "self documenting" as much as is possible. This means that they will _at least_ have a description comment which your editor should expose as a popover. In some cases, there are also code examples included in these comments to further flesh out intended use.
|
|
9
|
+
|
|
10
|
+
To keep things DRY, the documentation here will be kept to a minimum. This README is intended more as a high level introduction of scope and structure of this repo than documentation.
|
|
11
|
+
|
|
12
|
+
## Directory Structure
|
|
13
|
+
|
|
14
|
+
Under the `src/` folder you'll find the the following subdirectories:
|
|
15
|
+
|
|
16
|
+
- `constants` - a set of runtime constants that can provide _utility_ to both runtime functions as well as provide foundation for enumerated types.
|
|
17
|
+
- `types` - this folder represents the heart of the repo in the form of _type utilities_ and is further broken down by an attempt at functional classification that hopefully aides somewhat in feature discovery.
|
|
18
|
+
- `runtime` - this is where you'll find runtime functions which mutate state while taking care to provide as much as type information that compliments the runtime environment as is possible.
|
|
19
|
+
|
|
20
|
+
## Runtime synchronization with Types
|
|
21
|
+
|
|
22
|
+
You will find many runtime functions like `ensureleading(str, substring)` which have a similarly named type (in this case `EnsureLeading<TStr, TSubstring>`). This is no accident and at some future point there may even be something approximating 1:1 parity.
|
|
23
|
+
|
|
24
|
+
This connection between the type system and the runtime environment allows both a harmonization of variables across both environments and helps to ensure their consistency but it the runtime environment also often really needs strong type utilities to extract out narrow type definitions in the runtime environment.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
What follows is not meant to be comprehensive set of examples but rather just a sampling that hopefully gives some perspective on the breadth of scope of this library.
|
|
10
29
|
|
|
11
30
|
### String Literals
|
|
12
31
|
|
|
13
|
-
|
|
32
|
+
#### Casing Conventions
|
|
14
33
|
|
|
15
|
-
|
|
16
|
-
|
|
34
|
+
- use `PascalCase<T>`, `CamelCase<T>`, `SnakeCase<T>`, and `KebabCase<T>` to convert a string to one of several familiar naming conventions
|
|
35
|
+
- use `AllCaps<T>` to make all alphanumeric characters into their uppercase variant
|
|
17
36
|
|
|
18
|
-
|
|
19
|
-
import type { EnsureLeading } from "inferred-types";
|
|
20
|
-
// "start-${string}"
|
|
21
|
-
type T1 = EnsureLeading<string, "start-">;
|
|
22
|
-
// "start-with"
|
|
23
|
-
type T2 = EnsureLeading<"start-with", "start-">;
|
|
24
|
-
```
|
|
37
|
+
#### Pluralization
|
|
25
38
|
|
|
26
|
-
|
|
27
|
-
- use `StripLeading`/`StripTrailing` to perform the opposite functionality
|
|
39
|
+
Convert the _type_ and _value_ of a variable from it's singular form to it's plural form:
|
|
28
40
|
|
|
29
|
-
|
|
41
|
+
```ts
|
|
42
|
+
import { pluralize } from "inferred-types";
|
|
30
43
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
```
|
|
44
|
+
// "people"
|
|
45
|
+
const people = pluralize("person");
|
|
46
|
+
```
|
|
35
47
|
|
|
36
|
-
|
|
48
|
+
> **Note:** not only does this utility provide all major grammatical rules used in conversion but has a dictionary of common exceptions it will use too.
|
|
37
49
|
|
|
38
|
-
|
|
39
|
-
// "Hello world"
|
|
40
|
-
type C = Concat<["Hello ", "world"]>;
|
|
41
|
-
// "one, two, three"
|
|
42
|
-
type J = Join<["one", "two", "three"], ", ">;
|
|
43
|
-
```
|
|
50
|
+
#### Consistency with Ensure/Strip utilities
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
- it's often desireable to _ensure_ that a string _starts with_ or _ends with_ a given string
|
|
53
|
+
- the inverse can also be useful (aka, to _ensure_ a string _does not_ start or end with a given string literal)
|
|
54
|
+
- this library provides `EnsureLeading`, `EnsureTrailing`, `StripLeading`, and `StripTrailing` utilities to meet these type needs
|
|
46
55
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
```ts
|
|
57
|
+
import type { EnsureLeading } from "inferred-types";
|
|
58
|
+
// "start-${string}"
|
|
59
|
+
type T1 = EnsureLeading<string, "start-">;
|
|
60
|
+
// "start-with"
|
|
61
|
+
type T2 = EnsureLeading<"start-with", "start-">;
|
|
62
|
+
// "start-with"
|
|
63
|
+
type T3 = EnsureLeading<"with", "start-">;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Character Sets
|
|
67
|
+
|
|
68
|
+
- types like `Alpha`, `NumericChar`, `Whitespace`, `Consonants`, etc. try to represent character set building blocks that we may have to isolate on for our own types definitions
|
|
69
|
+
|
|
70
|
+
#### Data Patterns
|
|
71
|
+
|
|
72
|
+
- types like `Url`, `IpAddress`, `CSV`, `DotPath`, `Hexadecimal`, `ZipCode`, and `DomainName` attempt to provide an out of the box type for common data structure patterns we find in the real world
|
|
73
|
+
|
|
74
|
+
### Numeric Literals
|
|
75
|
+
|
|
76
|
+
- use the `Add<A,B>`, `Subtract<A,B>`, `Increment<T>`, `Decrement<T>` utilities to work with numeric types (or even _numeric_ string literals)
|
|
77
|
+
- get the length of a string or tuple with the `Length<T>` utility
|
|
78
|
+
- ensure an _integer_ value with `Integer<T>`; ensure a _float_ value with `Float<T>`
|
|
79
|
+
- compare numeric literals with `LessThan<A,B>`, `LessThanOrEqual<A,B>`, and `GreaterThan<A,B>`
|
|
80
|
+
- ensure a _positive_ number with `Abs<T>`
|
|
81
|
+
|
|
82
|
+
### Lists
|
|
83
|
+
|
|
84
|
+
- Use `AfterFirst`, `First`, `Last` to index into a tuple / list value
|
|
85
|
+
- Use `AsArray<T>` to ensure an array value for T
|
|
86
|
+
- Use `Flatten<T>` to _flatten_ an array
|
|
87
|
+
- Use `Slice<T,S,E>` to take out a subset of elements in a list
|
|
88
|
+
- Use `Reverse` to reverse a list
|
|
51
89
|
|
|
52
90
|
### Object / Dictionaries
|
|
53
91
|
|
|
@@ -88,44 +126,14 @@ Below you'll find a cursory set of examples of both _type_ and _runtime_ utiliti
|
|
|
88
126
|
type N = WithoutValue<Obj, string>;
|
|
89
127
|
```
|
|
90
128
|
|
|
91
|
-
##
|
|
129
|
+
## Contributing
|
|
92
130
|
|
|
93
|
-
|
|
131
|
+
If you are using this library and would like to take the next step of _contributing_; that effort is welcome but please do make sure to always provide both runtime and type tests for any code changes which you submit as a pull request.
|
|
94
132
|
|
|
95
|
-
|
|
133
|
+
See the plentiful examples that exist under the `tests/` folder for inspiration.
|
|
96
134
|
|
|
97
|
-
|
|
98
|
-
import { toCamelCase, toSnakeCase } from "inferred-types";
|
|
99
|
-
|
|
100
|
-
// "camelCase"
|
|
101
|
-
const camel = toCamelCase("camel-case");
|
|
102
|
-
// "snake_case"
|
|
103
|
-
const snake = toSnakeCase("snake-case");
|
|
104
|
-
```
|
|
135
|
+
**Note:** as of 2024 I'm also starting to add "type performance" tests; not an absolute requirement but in general adding something like you'll find in the `benches/` folder for any new type utility would be appreciated.
|
|
105
136
|
|
|
106
|
-
|
|
137
|
+
## Licensing
|
|
107
138
|
|
|
108
|
-
|
|
109
|
-
// "people"
|
|
110
|
-
const people = pluralize("person");
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
Also the common operations of `split` and `join` are supported as well:
|
|
114
|
-
|
|
115
|
-
```ts
|
|
116
|
-
// ["one","two","three"]
|
|
117
|
-
const parts = split("one, two, three", ", ");
|
|
118
|
-
// "one, two, three"
|
|
119
|
-
const joined = join(parts, ", ");
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
You can also ensure that a given string _is_ or _is not_ included in a string literal:
|
|
123
|
-
|
|
124
|
-
```ts
|
|
125
|
-
// "bar"
|
|
126
|
-
const bar = stripLeading("foobar", "foo");
|
|
127
|
-
const bar2 = stripLeading("bar", "foo");
|
|
128
|
-
// "foobar"
|
|
129
|
-
const foobar = ensureLeading("bar", "foo");
|
|
130
|
-
const foobar2 = ensureLeading("foobar", "foo");
|
|
131
|
-
```
|
|
139
|
+
This repo is offered under the highly permissive MIT license.
|