inferred-types 0.41.2 → 0.41.3
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 +72 -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 +7 -2
package/README.md
CHANGED
|
@@ -1,53 +1,85 @@
|
|
|
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
|
+
## Examples
|
|
21
|
+
|
|
22
|
+
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
23
|
|
|
11
24
|
### String Literals
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
#### Casing Conventions
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
- use `PascalCase<T>`, `CamelCase<T>`, `SnakeCase<T>`, and `KebabCase<T>` to convert a string to one of several familiar naming conventions
|
|
29
|
+
- use `AllCaps<T>` to make all alphanumeric characters into their uppercase variant
|
|
17
30
|
|
|
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
|
-
```
|
|
31
|
+
#### Pluralization
|
|
25
32
|
|
|
26
|
-
|
|
27
|
-
- use `StripLeading`/`StripTrailing` to perform the opposite functionality
|
|
33
|
+
Convert the _type_ and _value_ of a variable from it's singular form to it's plural form:
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
```ts
|
|
36
|
+
import { pluralize } from "inferred-types";
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
```
|
|
38
|
+
// "people"
|
|
39
|
+
const people = pluralize("person");
|
|
40
|
+
```
|
|
35
41
|
|
|
36
|
-
|
|
42
|
+
> **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
43
|
|
|
38
|
-
|
|
39
|
-
// "Hello world"
|
|
40
|
-
type C = Concat<["Hello ", "world"]>;
|
|
41
|
-
// "one, two, three"
|
|
42
|
-
type J = Join<["one", "two", "three"], ", ">;
|
|
43
|
-
```
|
|
44
|
+
#### Consistency with Ensure/Strip utilities
|
|
44
45
|
|
|
45
|
-
|
|
46
|
+
- it's often desireable to _ensure_ that a string _starts with_ or _ends with_ a given string
|
|
47
|
+
- the inverse can also be useful (aka, to _ensure_ a string _does not_ start or end with a given string literal)
|
|
48
|
+
- this library provides `EnsureLeading`, `EnsureTrailing`, `StripLeading`, and `StripTrailing` utilities to meet these type needs
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
```ts
|
|
51
|
+
import type { EnsureLeading } from "inferred-types";
|
|
52
|
+
// "start-${string}"
|
|
53
|
+
type T1 = EnsureLeading<string, "start-">;
|
|
54
|
+
// "start-with"
|
|
55
|
+
type T2 = EnsureLeading<"start-with", "start-">;
|
|
56
|
+
// "start-with"
|
|
57
|
+
type T3 = EnsureLeading<"with", "start-">;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### Character Sets
|
|
61
|
+
|
|
62
|
+
- 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
|
|
63
|
+
|
|
64
|
+
#### Data Patterns
|
|
65
|
+
|
|
66
|
+
- 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
|
|
67
|
+
|
|
68
|
+
### Numeric Literals
|
|
69
|
+
|
|
70
|
+
- use the `Add<A,B>`, `Subtract<A,B>`, `Increment<T>`, `Decrement<T>` utilities to work with numeric types (or even _numeric_ string literals)
|
|
71
|
+
- get the length of a string or tuple with the `Length<T>` utility
|
|
72
|
+
- ensure an _integer_ value with `Integer<T>`; ensure a _float_ value with `Float<T>`
|
|
73
|
+
- compare numeric literals with `LessThan<A,B>`, `LessThanOrEqual<A,B>`, and `GreaterThan<A,B>`
|
|
74
|
+
- ensure a _positive_ number with `Abs<T>`
|
|
75
|
+
|
|
76
|
+
### Lists
|
|
77
|
+
|
|
78
|
+
- Use `AfterFirst`, `First`, `Last` to index into a tuple / list value
|
|
79
|
+
- Use `AsArray<T>` to ensure an array value for T
|
|
80
|
+
- Use `Flatten<T>` to _flatten_ an array
|
|
81
|
+
- Use `Slice<T,S,E>` to take out a subset of elements in a list
|
|
82
|
+
- Use `Reverse` to reverse a list
|
|
51
83
|
|
|
52
84
|
### Object / Dictionaries
|
|
53
85
|
|
|
@@ -88,44 +120,14 @@ Below you'll find a cursory set of examples of both _type_ and _runtime_ utiliti
|
|
|
88
120
|
type N = WithoutValue<Obj, string>;
|
|
89
121
|
```
|
|
90
122
|
|
|
91
|
-
##
|
|
92
|
-
|
|
93
|
-
### String Literals
|
|
94
|
-
|
|
95
|
-
Many runtime utilities matchup well to their _type utility_ siblings. A good case in point is in converting a variable to a standardized naming convention. Whereas the type utility only changes the type, the runtime utility will ensure both runtime value and type are changed:
|
|
96
|
-
|
|
97
|
-
```ts
|
|
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
|
-
```
|
|
105
|
-
|
|
106
|
-
You can also leverage the `pluralize()` function to change a variable to the plural version of the word (type and runtime value):
|
|
123
|
+
## Contributing
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
// "people"
|
|
110
|
-
const people = pluralize("person");
|
|
111
|
-
```
|
|
125
|
+
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.
|
|
112
126
|
|
|
113
|
-
|
|
127
|
+
See the plentiful examples that exist under the `tests/` folder for inspiration.
|
|
114
128
|
|
|
115
|
-
|
|
116
|
-
// ["one","two","three"]
|
|
117
|
-
const parts = split("one, two, three", ", ");
|
|
118
|
-
// "one, two, three"
|
|
119
|
-
const joined = join(parts, ", ");
|
|
120
|
-
```
|
|
129
|
+
**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.
|
|
121
130
|
|
|
122
|
-
|
|
131
|
+
## Licensing
|
|
123
132
|
|
|
124
|
-
|
|
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
|
-
```
|
|
133
|
+
This repo is offered under the highly permissive MIT license.
|