66## Example
77
88``` ts
9- import { Key , Stack } from " @dldc/stack" ;
9+ import { createKey , Stack } from " @dldc/stack" ;
1010
1111// 1. Create a key with a name and a type
12- const NumKey = Key . create <number >(" Num" );
12+ const NumKey = createKey <number >(" Num" );
1313
1414// 2. Create a stack
1515const stack = new Stack ();
@@ -24,9 +24,7 @@ expect(stack2.get(NumKey.Consumer)).toBe(42);
2424## Installation
2525
2626``` bash
27- npm install @dldc/stack
28- # or
29- yarn add @dldc/stack
27+ deno add jsr:@dldc/stack
3028```
3129
3230## Usage
@@ -36,24 +34,28 @@ yarn add @dldc/stack
3634To write a value to a stack, you need to create a ` Key ` with a name and a type.
3735
3836``` ts
39- const NumKey = Key . create <number >(" Num" );
37+ const NumKey = createKey <number >(" Num" );
4038```
4139
42- A key is a object with two properties:
40+ A key is a object with three properties:
4341
4442- ` Provider ` : a function used to write a value to a stack
4543- ` Consumer ` : an object used to read a value from a stack
44+ - ` Reset ` : an object used to reset a value from a stack
4645
4746You can also create an empty key that will not have a value:
4847
4948``` ts
50- const EmptyKey = Key . createEmpty (" Empty" );
49+ const EmptyKey = createEmptyKey (" Empty" );
5150```
5251
52+ Note: ` get() ` will always return ` undefined ` for an empty key, use ` has() ` to
53+ check if the key is defined.
54+
5355Finally you ca define a default value for a key:
5456
5557``` ts
56- const NumKey = Key . createWithDefault <number >(" Num" , 42 );
58+ const NumKey = createKeyWithDefault <number >(" Num" , 42 );
5759```
5860
5961This key will always return a value if you try to get it from a stack (either
@@ -67,7 +69,8 @@ To create a new empty stack you can instantiate a new `Stack`:
6769const stack = new Stack ();
6870```
6971
70- _ Note_ : Do not pass any argument to the constructor !
72+ _ Note_ : The Stack constructor accept one argument, but you should NEVER use it
73+ as it is an internal implementation detail !
7174
7275### Writing to a ` Stack `
7376
@@ -81,9 +84,6 @@ const stack2 = stack.with(NumKey.Provider(42));
8184The ` with() ` method will return a new instance of ` Stack ` with the new value.
8285The original ` stack ` is not modified.
8386
84- _ Note_ : You cannot remove a value from a stack, you can only override it with a
85- new value.
86-
8787You can pass multiple providers to the ` with() ` method:
8888
8989``` ts
@@ -100,16 +100,16 @@ const value = stack.get(NumKey.Consumer);
100100```
101101
102102This will return the value that was set using the ` Provider ` of the key or
103- ` undefined ` if no value was setand the key does not have a default value.
103+ ` undefined ` if no value was set and the key does not have a default value.
104104
105105If you expect the Key to be defined you can use the ` getOrFail() ` method:
106106
107107``` ts
108108const value = stack .getOrFail (NumKey .Consumer );
109109```
110110
111- If the key does not have a default value and no value was set, this will throw a
112- ` MissingContextError ` error.
111+ If the key does not have a default value and no value was set, this will throw
112+ an error.
113113
114114If you only want to know if a value was set, you can use the ` has() ` method:
115115
@@ -119,6 +119,24 @@ const hasValue = stack.has(NumKey.Consumer);
119119
120120This will return ` true ` if a value was set using the ` Provider ` .
121121
122+ ### Resetting a ` Stack `
123+
124+ To reset a value from a stack, you need to use the ` Reset ` of a key and pass it
125+ to the ` with() ` method of your ` Stack ` instance.
126+
127+ ``` ts
128+ const stack2 = stack .with (NumKey .Reset );
129+ ```
130+
131+ This will return a new instance of ` Stack ` with the value removed. The original
132+ ` stack ` is not modified.
133+
134+ Calling ` has() ` on a key that was reset will return ` false ` .
135+
136+ _ Note_ : The stack will still hold a reference to the previous values, so don't
137+ rely on ` Reset ` for garbage collection. If you really need to, you can call
138+ ` .dedupe() ` on the stack to remove all the references to the previous values.
139+
122140### Debugging a ` Stack `
123141
124142To inspect the content of a stack, you can use the ` inspect() ` method. It will
@@ -130,18 +148,22 @@ want to inspect the content of a stack, you need to use the `inspect()` method.
130148#### Customizing the ` inspect() ` method
131149
132150By default the ` inspect() ` method will print a serialized version of the values
133- contained in the stack. You can customize this behavior by provising a
134- ` serailize ` function as the last argument of ` Key.create ()` , ` Key.createEmpty() `
135- or ` Key.createWithDefault ()` .
151+ contained in the stack. You can customize this behavior by providing a
152+ ` serialize ` function as the last argument of ` createKey ()` or
153+ ` createKeyWithDefault ()` .
136154
137155``` ts
138- const NumKey = Key . create <number >(" Num" , (value ) => value .toFixed (2 ));
156+ const NumKey = createKey <number >(" Num" , (value ) => value .toFixed (2 ));
139157
140158const stack = new Stack ().with (NumKey .Provider (42 ));
141159console .log (stack .inspect ());
142160// Stack { Num: 42.00 }
143161```
144162
163+ ### Handling errors
164+
165+ This library uses [ @dldc/erreur ] ( https://jsr.io/@dldc/erreur ) to handle errors.
166+
145167## Extending ` Stack ` (advanced)
146168
147169You can create your own ` Stack ` , this is useful to define custom properties and
0 commit comments