@ekz/option 1.2.6 → 2.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Simply get rid of all nulls in your code.
4
4
 
5
- This is a clone of Scala's Option class to be used with flow.js.
5
+ This is a TypeScript port of Scala's `Option` type.
6
6
 
7
7
  # Examples
8
8
 
package/package.json CHANGED
@@ -1,47 +1,44 @@
1
1
  {
2
2
  "name": "@ekz/option",
3
- "version": "1.2.6",
4
- "packageManager": "yarn@4.12.0",
5
- "description": "Option/Maybe type to be used with flow.js",
6
- "types": "index.d.ts",
7
- "keywords": [
8
- "option",
9
- "maybe",
10
- "flow",
11
- "scala",
12
- "type-safe"
13
- ],
14
- "author": {
15
- "name": "Alan Heitkotter",
16
- "email": "heitkotter@gmail.com"
17
- },
3
+ "version": "2.0.0",
4
+ "license": "MIT",
5
+ "description": "Option/Maybe type for TypeScript",
18
6
  "repository": {
19
7
  "type": "git",
20
- "url": "git+https://github.com/erkez/flow-option.git"
8
+ "url": "https://github.com/erkez/ekz.git",
9
+ "directory": "option"
21
10
  },
22
- "bugs": {
23
- "url": "https://github.com/erkez/flow-option/issues"
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "registry": "https://registry.npmjs.org"
24
14
  },
25
- "homepage": "https://github.com/erkez/flow-option#readme",
26
- "main": "lib/index.js",
27
- "license": "MIT",
15
+ "main": "lib/cjs/index.js",
16
+ "module": "lib/esm/index.js",
17
+ "esnext": "lib/esnext/index.js",
18
+ "typings": "lib/esm/index.d.ts",
28
19
  "files": [
29
- "index.d.ts",
30
20
  "lib",
31
21
  "README.md",
32
22
  "LICENSE"
33
23
  ],
24
+ "keywords": [
25
+ "option",
26
+ "maybe",
27
+ "scala",
28
+ "type-safe"
29
+ ],
34
30
  "devDependencies": {
35
- "babel-cli": "^6.26.0",
36
- "babel-preset-env": "^1.7.0",
37
- "babel-preset-flow": "^6.23.0",
38
- "documentation": "^14.0.3",
39
- "flow-bin": "^0.294.0",
40
- "flow-copy-source": "^2.0.9"
31
+ "npm-run-all": "^4.1.5",
32
+ "typescript": "^5.9.3"
41
33
  },
42
34
  "scripts": {
43
- "build": "babel src/ -d lib/ && flow-copy-source -v src lib",
44
- "docs": "documentation readme src/index.js --section=API",
45
- "prepublish": "yarn run docs && yarn run build"
35
+ "clean": "rm -rf lib/* || true",
36
+ "lint": "npx eslint --ext .ts ./src/main/web",
37
+ "lint:fix": "yarn lint --fix",
38
+ "compile": "run-p \"compile:*\"",
39
+ "compile:esm": "tsc -p ./src/main/web --outDir ./lib/esm",
40
+ "compile:cjs": "tsc -p ./src/main/web -m commonjs --verbatimModuleSyntax false --outDir ./lib/cjs",
41
+ "compile:esnext": "tsc -p ./src/main/web -t esnext --outDir ./lib/esnext",
42
+ "prepublish": "yarn clean && yarn lint && yarn compile"
46
43
  }
47
44
  }
package/index.d.ts DELETED
@@ -1,29 +0,0 @@
1
- declare module '@ekz/option' {
2
-
3
- export class Option<A> {
4
- private constructor();
5
- get(): A;
6
- readonly isEmpty: boolean;
7
- readonly isDefined: boolean;
8
- map<B>(f: (value: A) => B): Option<B>;
9
- mapNullable<B>(f: (value: A) => B | null | void | undefined): Option<B>;
10
- flatMap<B>(f: (value: A) => Option<B>): Option<B>;
11
- forEach(f: (value: A) => any): void;
12
- filter(predicate: (value: A) => boolean): Option<A>;
13
- getOrElse<B>(other: () => B): A | B;
14
- getOrReturn<B>(other: B): A | B;
15
- getOrUndefined(): A | undefined;
16
- equals<B>(other: Option<B>): boolean;
17
- toJSON(): unknown;
18
-
19
- static of<V>(value?: V | null | void | undefined): Option<V>;
20
-
21
- static None: Option<never>;
22
- static Some<A>(value: A): Option<A>;
23
- }
24
-
25
- export const None: Option<never>;
26
-
27
- export function Some<A>(value: A): Option<A>;
28
-
29
- }
package/lib/index.js DELETED
@@ -1,254 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
-
9
- exports.Some = Some;
10
-
11
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
12
-
13
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
14
-
15
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
-
17
- var PrivateToken = Symbol('Option');
18
-
19
- /**
20
- * Represents optional values. Instances of Option are either an instance of Some or the object None.
21
- */
22
-
23
- var Option = exports.Option = function () {
24
- /**
25
- * Returns the option's value.
26
- */
27
- function Option($privateToken) {
28
- _classCallCheck(this, Option);
29
-
30
- if ($privateToken !== PrivateToken) {
31
- throw new Error('Option cannot be manually instantiated. Use Option.of, Some or None.');
32
- }
33
- }
34
-
35
- /**
36
- * Returns true if the option is an instance of Some, false otherwise.
37
- */
38
-
39
-
40
- /**
41
- * Returns true if the option is None, false otherwise.
42
- */
43
-
44
-
45
- _createClass(Option, [{
46
- key: 'map',
47
-
48
-
49
- /**
50
- * Returns a Some containing the result of applying f to this Option's value if this Option is nonempty.
51
- */
52
- value: function map(f) {
53
- return this.isEmpty ? None : Some(f(this.get()));
54
- }
55
-
56
- /**
57
- * Like map, but if resulting value is null, then returns None.
58
- */
59
-
60
- }, {
61
- key: 'mapNullable',
62
- value: function mapNullable(f) {
63
- return this.isEmpty ? None : Option.of(f(this.get()));
64
- }
65
-
66
- /**
67
- * Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).
68
- */
69
-
70
- }, {
71
- key: 'flatMap',
72
- value: function flatMap(f) {
73
- return this.isEmpty ? None : f(this.get());
74
- }
75
-
76
- /**
77
- * Apply the given procedure f to the option's value, if it is nonempty.
78
- */
79
-
80
- }, {
81
- key: 'forEach',
82
- value: function forEach(f) {
83
- if (!this.isEmpty) {
84
- f(this.get());
85
- }
86
- }
87
-
88
- /**
89
- * Returns this Option if it is nonempty and applying the predicate to this Option's value returns true.
90
- */
91
-
92
- }, {
93
- key: 'filter',
94
- value: function filter(predicate) {
95
- return this.isEmpty || predicate(this.get()) ? this : None;
96
- }
97
-
98
- /**
99
- * Returns the option's value if the option is nonempty, otherwise return the result of evaluating other.
100
- */
101
-
102
- }, {
103
- key: 'getOrElse',
104
- value: function getOrElse(other) {
105
- return this.isEmpty ? other() : this.get();
106
- }
107
-
108
- /**
109
- * Returns the option's value if the option is nonempty, otherwise return other.
110
- */
111
-
112
- }, {
113
- key: 'getOrReturn',
114
- value: function getOrReturn(other) {
115
- return this.isEmpty ? other : this.get();
116
- }
117
-
118
- /**
119
- * Returns the option's value if the option is nonempty, otherwise returns undefined.
120
- */
121
-
122
- }, {
123
- key: 'getOrUndefined',
124
- value: function getOrUndefined() {
125
- return this.isEmpty ? undefined : this.get();
126
- }
127
-
128
- /**
129
- * Compares the option's value with other option's value and returns true when they match. None always matches other None.
130
- */
131
-
132
- }, {
133
- key: 'equals',
134
- value: function equals(other) {
135
- return this.isDefined && other.isDefined ? this.get() === other.get() : this === other;
136
- }
137
-
138
- /**
139
- * Returns value if present, null otherwise. If value contains a method `toJSON`,
140
- * returns the result of method call.
141
- */
142
-
143
- }, {
144
- key: 'toJSON',
145
- value: function toJSON() {
146
- var value = this.getOrReturn(null);
147
-
148
- if (value != null && typeof value.toJSON === 'function') {
149
- // $FlowFixMe
150
- return value.toJSON();
151
- } else {
152
- return value;
153
- }
154
- }
155
-
156
- /**
157
- * An Option factory which creates Some(x) if the argument is not null, and None if it is null.
158
- */
159
-
160
- }, {
161
- key: 'isDefined',
162
- get: function get() {
163
- return !this.isEmpty;
164
- }
165
- }], [{
166
- key: 'of',
167
- value: function of(value) {
168
- return value == null ? None : Some(value);
169
- }
170
-
171
- /**
172
- * The empty None object
173
- */
174
-
175
-
176
- /**
177
- * Creates Some(x). Note that Some(null) is valid.
178
- */
179
-
180
- }]);
181
-
182
- return Option;
183
- }();
184
-
185
- var $None = function (_Option) {
186
- _inherits($None, _Option);
187
-
188
- function $None() {
189
- _classCallCheck(this, $None);
190
-
191
- return _possibleConstructorReturn(this, ($None.__proto__ || Object.getPrototypeOf($None)).apply(this, arguments));
192
- }
193
-
194
- _createClass($None, [{
195
- key: 'get',
196
- value: function get() {
197
- throw new Error('No such element');
198
- }
199
- }, {
200
- key: 'isEmpty',
201
- get: function get() {
202
- return true;
203
- }
204
- }]);
205
-
206
- return $None;
207
- }(Option);
208
-
209
- /**
210
- * The empty None object.
211
- */
212
-
213
-
214
- var None = exports.None = new $None(PrivateToken);
215
-
216
- Option.None = None;
217
-
218
- var $Some = function (_Option2) {
219
- _inherits($Some, _Option2);
220
-
221
- function $Some(value) {
222
- _classCallCheck(this, $Some);
223
-
224
- var _this2 = _possibleConstructorReturn(this, ($Some.__proto__ || Object.getPrototypeOf($Some)).call(this, PrivateToken));
225
-
226
- _this2._value = value;
227
- return _this2;
228
- }
229
-
230
- _createClass($Some, [{
231
- key: 'get',
232
- value: function get() {
233
- return this._value;
234
- }
235
- }, {
236
- key: 'isEmpty',
237
- get: function get() {
238
- return false;
239
- }
240
- }]);
241
-
242
- return $Some;
243
- }(Option);
244
-
245
- /**
246
- * Creates Some(x). Note that Some(null) is valid.
247
- */
248
-
249
-
250
- function Some(value) {
251
- return new $Some(value);
252
- }
253
-
254
- Option.Some = Some;
package/lib/index.js.flow DELETED
@@ -1,172 +0,0 @@
1
- // @flow
2
- 'use strict';
3
-
4
- const PrivateToken = Symbol('Option');
5
-
6
- /**
7
- * Represents optional values. Instances of Option are either an instance of Some or the object None.
8
- */
9
- export class Option<+A> {
10
- /**
11
- * Returns the option's value.
12
- */
13
- +get: () => A;
14
-
15
- /**
16
- * Returns true if the option is None, false otherwise.
17
- */
18
- +isEmpty: boolean;
19
-
20
- constructor($privateToken: typeof PrivateToken) {
21
- if ($privateToken !== PrivateToken) {
22
- throw new Error('Option cannot be manually instantiated. Use Option.of, Some or None.');
23
- }
24
- }
25
-
26
- /**
27
- * Returns true if the option is an instance of Some, false otherwise.
28
- */
29
- get isDefined(): boolean {
30
- return !this.isEmpty;
31
- }
32
-
33
- /**
34
- * Returns a Some containing the result of applying f to this Option's value if this Option is nonempty.
35
- */
36
- map<B>(f: A => B): Option<B> {
37
- return this.isEmpty ? None : Some(f(this.get()));
38
- }
39
-
40
- /**
41
- * Like map, but if resulting value is null, then returns None.
42
- */
43
- mapNullable<B>(f: A => ?B): Option<B> {
44
- return this.isEmpty ? None : Option.of(f(this.get()));
45
- }
46
-
47
- /**
48
- * Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).
49
- */
50
- flatMap<B>(f: A => Option<B>): Option<B> {
51
- return this.isEmpty ? None : f(this.get());
52
- }
53
-
54
- /**
55
- * Apply the given procedure f to the option's value, if it is nonempty.
56
- */
57
- forEach(f: A => any): void {
58
- if (!this.isEmpty) {
59
- f(this.get());
60
- }
61
- }
62
-
63
- /**
64
- * Returns this Option if it is nonempty and applying the predicate to this Option's value returns true.
65
- */
66
- filter(predicate: A => boolean): Option<A> {
67
- return this.isEmpty || predicate(this.get()) ? this : None;
68
- }
69
-
70
- /**
71
- * Returns the option's value if the option is nonempty, otherwise return the result of evaluating other.
72
- */
73
- getOrElse<B>(other: () => B): A | B {
74
- return this.isEmpty ? other() : this.get();
75
- }
76
-
77
- /**
78
- * Returns the option's value if the option is nonempty, otherwise return other.
79
- */
80
- getOrReturn<B>(other: B): A | B {
81
- return this.isEmpty ? other : this.get();
82
- }
83
-
84
- /**
85
- * Returns the option's value if the option is nonempty, otherwise returns undefined.
86
- */
87
- getOrUndefined(): A | void {
88
- return this.isEmpty ? undefined : this.get();
89
- }
90
-
91
- /**
92
- * Compares the option's value with other option's value and returns true when they match. None always matches other None.
93
- */
94
- equals<B>(other: Option<B>): boolean {
95
- return this.isDefined && other.isDefined ? this.get() === other.get() : this === other;
96
- }
97
-
98
- /**
99
- * Returns value if present, null otherwise. If value contains a method `toJSON`,
100
- * returns the result of method call.
101
- */
102
- toJSON(): mixed {
103
- let value = this.getOrReturn(null);
104
-
105
- if (value != null && typeof value.toJSON === 'function') {
106
- // $FlowFixMe
107
- return value.toJSON();
108
- } else {
109
- return value;
110
- }
111
- }
112
-
113
- /**
114
- * An Option factory which creates Some(x) if the argument is not null, and None if it is null.
115
- */
116
- static of<V>(value?: ?V): Option<V> {
117
- return value == null ? None : Some(value);
118
- }
119
-
120
- /**
121
- * The empty None object
122
- */
123
- static None: Option<empty>;
124
-
125
- /**
126
- * Creates Some(x). Note that Some(null) is valid.
127
- */
128
- static Some: <A>(value: A) => Option<A>;
129
- }
130
-
131
- class $None extends Option<empty> {
132
- get() {
133
- throw new Error('No such element');
134
- }
135
-
136
- get isEmpty(): boolean {
137
- return true;
138
- }
139
- }
140
-
141
- /**
142
- * The empty None object.
143
- */
144
- export const None: Option<empty> = new $None(PrivateToken);
145
-
146
- Option.None = None;
147
-
148
- class $Some<A> extends Option<A> {
149
- _value: A;
150
-
151
- constructor(value: A) {
152
- super(PrivateToken);
153
- this._value = value;
154
- }
155
-
156
- get(): A {
157
- return this._value;
158
- }
159
-
160
- get isEmpty(): boolean {
161
- return false;
162
- }
163
- }
164
-
165
- /**
166
- * Creates Some(x). Note that Some(null) is valid.
167
- */
168
- export function Some<+A>(value: A): Option<A> {
169
- return new $Some(value);
170
- }
171
-
172
- Option.Some = Some;