simple-optional 1.0.0 → 1.0.1
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 +59 -0
- package/package.json +7 -6
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Simple Optional
|
|
2
|
+
|
|
3
|
+
A lightweight TypeScript implementation of an Optional container (inspired by Java's Optional). Encapsulates a value that may be present or absent, and provides helpers for safe access and transformations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
- Create present, nullable, or empty optionals
|
|
7
|
+
- Check presence with `isPresent()` / `isEmpty()`
|
|
8
|
+
- Safe retrieval with `get()`, `orElse()`, and `orElseThrow()`
|
|
9
|
+
- Transform values with `map()`
|
|
10
|
+
- Conditional execution with `ifPresent()`
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Import or include the class in your project, then:
|
|
15
|
+
|
|
16
|
+
- Create an Optional with a non-null value:
|
|
17
|
+
```typescript
|
|
18
|
+
const o = Optional.of(42);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- Create an Optional that may be null:
|
|
22
|
+
```typescript
|
|
23
|
+
const maybe = Optional.ofNullable<number>(null);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- Create an empty Optional:
|
|
27
|
+
```typescript
|
|
28
|
+
const empty = Optional.empty<number>();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- Check presence:
|
|
32
|
+
```typescript
|
|
33
|
+
if (o.isPresent()) { /* ... */ }
|
|
34
|
+
if (empty.isEmpty()) { /* ... */ }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- Execute when present:
|
|
38
|
+
```typescript
|
|
39
|
+
o.ifPresent(v => console.log(v));
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- Transform a value:
|
|
43
|
+
```typescript
|
|
44
|
+
const str = Optional.of(5).map(n => 'Number ' + n); // Optional<string>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- Retrieve value with default or throw:
|
|
48
|
+
```typescript
|
|
49
|
+
const x = maybe.orElse(10);
|
|
50
|
+
const y = maybe.orElseThrow(() => new Error('Missing value'));
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Error handling
|
|
54
|
+
Errors are thrown as `Error` instances with class-prefixed messages, e.g. `[Optional] No value present`.
|
|
55
|
+
|
|
56
|
+
## Notes & assumptions
|
|
57
|
+
- `of()` and `set()` treat both `null` and `undefined` as invalid.
|
|
58
|
+
- `ofNullable()` intentionally allows `null` to represent emptiness.
|
|
59
|
+
- The class uses a private constructor—use static factories to create instances.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-optional",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Simplified Optional container inspired by Java 8 implementation.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,12 +28,13 @@
|
|
|
28
28
|
],
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
31
|
-
"url": "https://gitlab.com/projetos-publicos1/
|
|
31
|
+
"url": "https://gitlab.com/projetos-publicos1/simple-optional.git"
|
|
32
32
|
},
|
|
33
33
|
"keywords": [
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
34
|
+
"optional",
|
|
35
|
+
"null",
|
|
36
|
+
"undefined",
|
|
37
|
+
"utility",
|
|
38
|
+
"java.util"
|
|
38
39
|
]
|
|
39
40
|
}
|