simple-optional 1.0.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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/optional.d.ts +16 -0
- package/dist/optional.js +66 -0
- package/package.json +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Optional } from './optional';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class Optional<T> {
|
|
2
|
+
private value;
|
|
3
|
+
protected constructor(value: T | null);
|
|
4
|
+
static of<T>(value: T): Optional<T>;
|
|
5
|
+
static ofNullable<T>(value: T | null): Optional<T>;
|
|
6
|
+
static empty<T>(): Optional<T>;
|
|
7
|
+
isEmpty(): boolean;
|
|
8
|
+
isPresent(): boolean;
|
|
9
|
+
ifPresent(consumer: (value: T) => void): void;
|
|
10
|
+
map<Y>(converter: (value: T) => Y): Optional<Y>;
|
|
11
|
+
get(): T;
|
|
12
|
+
set(value: T | null): void;
|
|
13
|
+
orElse(defaultValue: T): T;
|
|
14
|
+
orElseThrow(supplier: () => Error): T;
|
|
15
|
+
private static reportError;
|
|
16
|
+
}
|
package/dist/optional.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Optional = void 0;
|
|
4
|
+
class Optional {
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
8
|
+
static of(value) {
|
|
9
|
+
if (value === null || value === undefined) {
|
|
10
|
+
throw this.reportError('Value cannot be null or undefined');
|
|
11
|
+
}
|
|
12
|
+
return new Optional(value);
|
|
13
|
+
}
|
|
14
|
+
static ofNullable(value) {
|
|
15
|
+
return new Optional(value);
|
|
16
|
+
}
|
|
17
|
+
static empty() {
|
|
18
|
+
return new Optional(null);
|
|
19
|
+
}
|
|
20
|
+
isEmpty() {
|
|
21
|
+
return !this.isPresent();
|
|
22
|
+
}
|
|
23
|
+
isPresent() {
|
|
24
|
+
return this.value !== null;
|
|
25
|
+
}
|
|
26
|
+
ifPresent(consumer) {
|
|
27
|
+
if (this.isPresent()) {
|
|
28
|
+
consumer(this.get());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
map(converter) {
|
|
32
|
+
if (this.isEmpty()) {
|
|
33
|
+
return Optional.empty();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return Optional.of(converter(this.get()));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
get() {
|
|
40
|
+
if (this.value === null) {
|
|
41
|
+
throw Optional.reportError('No value present');
|
|
42
|
+
}
|
|
43
|
+
return this.value;
|
|
44
|
+
}
|
|
45
|
+
set(value) {
|
|
46
|
+
if (value === null || value === undefined) {
|
|
47
|
+
throw Optional.reportError('Value cannot be null or undefined');
|
|
48
|
+
}
|
|
49
|
+
this.value = value;
|
|
50
|
+
}
|
|
51
|
+
orElse(defaultValue) {
|
|
52
|
+
return this.isPresent() ? this.get() : defaultValue;
|
|
53
|
+
}
|
|
54
|
+
orElseThrow(supplier) {
|
|
55
|
+
if (this.isPresent()) {
|
|
56
|
+
return this.get();
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
throw supplier();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
static reportError(message) {
|
|
63
|
+
return new Error(`[${Optional.name}] ${message}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.Optional = Optional;
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-optional",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simplified Optional container inspired by Java 8 implementation.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "npx tsc",
|
|
9
|
+
"test": "npx jest --coverage",
|
|
10
|
+
"test-watch": "npx jest --coverage --watch"
|
|
11
|
+
},
|
|
12
|
+
"author": "victorbarlac",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jest": "^30.0.0",
|
|
17
|
+
"@types/node": "^22.13.10",
|
|
18
|
+
"jest": "^30.2.0",
|
|
19
|
+
"nodemon": "^3.1.9",
|
|
20
|
+
"ts-jest": "^29.4.6",
|
|
21
|
+
"ts-node": "^10.9.2",
|
|
22
|
+
"typescript": "^5.9.3"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"package.json",
|
|
26
|
+
"README.md",
|
|
27
|
+
"dist/**/*"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://gitlab.com/projetos-publicos1/rest-api-express-server.git"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"rest",
|
|
35
|
+
"api",
|
|
36
|
+
"express",
|
|
37
|
+
"http server"
|
|
38
|
+
]
|
|
39
|
+
}
|