scats 1.0.28 → 1.0.33

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
@@ -67,41 +67,48 @@ Represents the result of error prone block.
67
67
  ```typescript
68
68
  import {Try} from 'scats';
69
69
 
70
- const a = Try(() => 1); // a = success(1);
71
- const b = Try(() => { throw new Error(2) }); // b = failure(new Error(2));
70
+ const okResult = Try(() => 1); // a = success(1);
71
+ const failedResult = Try(() => { throw new Error(2) }); // b = failure(new Error(2));
72
72
 
73
- a.toOption; // some(1)
74
- b.toOption; // none
73
+ okResult.toOption; // some(1)
74
+ failedResult.toOption; // none
75
75
 
76
- a.toEither; // right(1)
77
- b.toEither; // left(new Error(2))
76
+ okResult.toEither; // right(1)
77
+ failedResult.toEither; // left(new Error(2))
78
+ okResult.toEitherMapLeft(error => 3); // right(1)
79
+ failedResult.toEitherMapLeft(error => 3); // left(3)
78
80
 
79
- a.map(x => x + 1); // success(2);
80
- b.map(x => x + 1); // failure(new Error(2));
81
+ okResult.map(value => value + 1); // success(2);
82
+ failedResult.map(value => value + 1); // failure(new Error(2));
81
83
 
82
- a.isSuccess; // true
83
- a.isFailure; // false
84
+ okResult.isSuccess; // true
85
+ okResult.isFailure; // false
86
+ failedResult.isSuccess; // false
87
+ failedResult.isFailure; // true
84
88
 
85
- a.match({
86
- success: () => 100,
87
- failure: () => 200
89
+ okResult.match({
90
+ success: value => 100,
91
+ failure: error => 200
88
92
  }); // 100
89
- b.match({
93
+ failedResult.match({
90
94
  success: value => 100,
91
95
  failure: error => 200
92
96
  }); // 200
93
97
 
94
98
 
95
- a.foreach(x => console.log(x)); // prints 1
96
- b.foreach(x => console.log(x)); // prints nothing
99
+ okResult.foreach(value => console.log(value)); // prints 1
100
+ failedResult.foreach(value => console.log(value)); // prints nothing
101
+
102
+ okResult.tapFailure(error => console.log(error.message)); // prints nothing
103
+ failedResult.tapFailure(error => console.log(error.message)); // prints 2
97
104
 
98
- a.recover(e => 2); // success(1)
99
- b.recover(e => 2); // success(2)
100
- b.recover(e => { throw new Error('fallback'); }); // failure(new Error('fallback'));
105
+ okResult.recover(error => 2); // success(1)
106
+ failedResult.recover(error => 2); // success(2)
107
+ failedResult.recover(error => { throw new Error('fallback'); }); // failure(new Error('fallback'));
101
108
 
102
- a.recoverWith(e => success(2)); // success(1)
103
- b.recoverWith(e => success(2)); // success(2)
104
- b.recoverWith(e => failure(new Error('fallback'))); // failure(new Error('fallback'));
109
+ okResult.recoverWith(error => success(2)); // success(1)
110
+ failedResult.recoverWith(error => success(2)); // success(2)
111
+ failedResult.recoverWith(error => failure(new Error('fallback'))); // failure(new Error('fallback'));
105
112
  ```
106
113
 
107
114
  Also works with promises:
package/dist/hashmap.d.ts CHANGED
@@ -2,11 +2,12 @@ import { Option } from './option';
2
2
  import { Collection } from './collection';
3
3
  import { HashSet } from './hashset';
4
4
  import { ArrayIterable } from './array-iterable';
5
- export declare class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>> {
5
+ export declare type Tuple2<K, V> = [K, V];
6
+ export declare class HashMap<K, V> extends ArrayIterable<Tuple2<K, V>, HashMap<K, V>> {
6
7
  private readonly map;
7
- protected fromArray(array: [K, V][]): HashMap<K, V>;
8
+ protected fromArray(array: Tuple2<K, V>[]): HashMap<K, V>;
8
9
  constructor(map: Map<K, V>);
9
- static of<K, V>(...values: [k: K, v: V][]): HashMap<K, V>;
10
+ static of<K, V>(...values: Tuple2<K, V>[]): HashMap<K, V>;
10
11
  static empty: HashMap<any, any>;
11
12
  get size(): number;
12
13
  get isEmpty(): boolean;
@@ -18,15 +19,15 @@ export declare class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>>
18
19
  get keys(): Collection<K>;
19
20
  get values(): Collection<V>;
20
21
  get valueIterator(): IterableIterator<V>;
21
- get entries(): Collection<[K, V]>;
22
- get entriesIterator(): IterableIterator<[K, V]>;
22
+ get entries(): Collection<Tuple2<K, V>>;
23
+ get entriesIterator(): IterableIterator<Tuple2<K, V>>;
23
24
  appendedAll(map: HashMap<K, V>): HashMap<K, V>;
24
25
  concat(map: HashMap<K, V>): HashMap<K, V>;
25
26
  set(key: K, value: V): HashMap<K, V>;
26
27
  remove(key: K): HashMap<K, V>;
27
28
  containsKey(key: K): boolean;
28
29
  updated(key: K, value: V): HashMap<K, V>;
29
- toCollection(): Collection<[K, V]>;
30
+ get toCollection(): Collection<Tuple2<K, V>>;
30
31
  get toMap(): Map<K, V>;
31
- get toArray(): Array<[K, V]>;
32
+ get toArray(): Array<Tuple2<K, V>>;
32
33
  }
package/dist/hashmap.js CHANGED
@@ -78,7 +78,7 @@ class HashMap extends array_iterable_1.ArrayIterable {
78
78
  updated(key, value) {
79
79
  return this.set(key, value);
80
80
  }
81
- toCollection() {
81
+ get toCollection() {
82
82
  return new collection_1.Collection(Array.from(this.map.entries()));
83
83
  }
84
84
  get toMap() {
package/dist/hashset.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare class HashSet<T> extends ArrayIterable<T, HashSet<T>> {
9
9
  static of<T>(...items: T[]): HashSet<T>;
10
10
  get toCollection(): Collection<T>;
11
11
  get toSet(): Set<T>;
12
- contains(item: T): any;
12
+ contains(item: T): boolean;
13
13
  map<B>(f: (item: T) => B): HashSet<B>;
14
14
  flatMap<B>(f: (item: T) => HashSet<B>): HashSet<B>;
15
15
  get toArray(): Array<T>;
package/dist/try.d.ts CHANGED
@@ -26,6 +26,8 @@ export declare abstract class TryLike<T> implements Mappable<T> {
26
26
  mapPromise<B>(f: (v: T) => Promise<B>): Promise<TryLike<B>>;
27
27
  flatMapPromise<B>(f: (item: T) => Promise<TryLike<B>>): Promise<TryLike<B>>;
28
28
  foreach<U>(f: (value: T) => U): void;
29
+ tapFailure(f: (e: Error) => void): TryLike<T>;
30
+ toEitherWithLeft<L>(f: (e: Error) => L): Either<L, T>;
29
31
  }
30
32
  export declare class Success<T> extends TryLike<T> {
31
33
  private readonly result;
package/dist/try.js CHANGED
@@ -25,6 +25,26 @@ class TryLike {
25
25
  f(this.get);
26
26
  }
27
27
  }
28
+ tapFailure(f) {
29
+ return this.match({
30
+ success: () => this,
31
+ failure: e => {
32
+ try {
33
+ f(e);
34
+ return this;
35
+ }
36
+ catch (ex) {
37
+ return failure(ex);
38
+ }
39
+ }
40
+ });
41
+ }
42
+ toEitherWithLeft(f) {
43
+ return this.match({
44
+ success: r => either_1.right(r),
45
+ failure: e => either_1.left(f(e))
46
+ });
47
+ }
28
48
  }
29
49
  exports.TryLike = TryLike;
30
50
  class Success extends TryLike {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scats",
3
- "version": "1.0.28",
3
+ "version": "1.0.33",
4
4
  "description": "Useful scala classes in typescript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/collection.ts CHANGED
@@ -48,9 +48,9 @@ export class Collection<T> extends ArrayIterable<T, Collection<T>>
48
48
  }
49
49
 
50
50
  flatMap<B>(f: (item: T) => Collection<B>): Collection<B> {
51
- let res: B[] = [];
51
+ const res: B[] = [];
52
52
  this.items.forEach(i => {
53
- res = res.concat(f(i).items);
53
+ res.push(...f(i).items);
54
54
  });
55
55
  return new Collection<B>(res);
56
56
  }
@@ -106,10 +106,10 @@ export class Collection<T> extends ArrayIterable<T, Collection<T>>
106
106
  * @param f
107
107
  */
108
108
  async flatMapPromise<B>(f: (item: T) => Promise<Collection<B>>): Promise<Collection<B>> {
109
- let res: B[] = [];
109
+ const res: B[] = [];
110
110
  for (let i = 0; i < this.items.length; i++) {
111
111
  const item = this.items[i];
112
- res = res.concat((await f(item)).items);
112
+ res.push(...(await f(item)).items);
113
113
  }
114
114
  return new Collection<B>(res);
115
115
  }
package/src/hashmap.ts CHANGED
@@ -3,9 +3,11 @@ import {Collection} from './collection';
3
3
  import {HashSet} from './hashset';
4
4
  import {ArrayIterable} from './array-iterable';
5
5
 
6
- export class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>> {
6
+ export type Tuple2<K, V> = [K, V];
7
7
 
8
- protected fromArray(array: [K, V][]): HashMap<K, V> {
8
+ export class HashMap<K, V> extends ArrayIterable<Tuple2<K, V>, HashMap<K, V>> {
9
+
10
+ protected fromArray(array: Tuple2<K, V>[]): HashMap<K, V> {
9
11
  return HashMap.of(...array);
10
12
  }
11
13
 
@@ -13,7 +15,7 @@ export class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>> {
13
15
  super();
14
16
  }
15
17
 
16
- static of<K, V>(...values: [k: K, v: V][]): HashMap<K, V> {
18
+ static of<K, V>(...values: Tuple2<K, V>[]): HashMap<K, V> {
17
19
  return new HashMap<K, V>(new Map(values));
18
20
  }
19
21
 
@@ -59,11 +61,11 @@ export class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>> {
59
61
  return this.map.values();
60
62
  }
61
63
 
62
- get entries(): Collection<[K, V]> {
64
+ get entries(): Collection<Tuple2<K, V>> {
63
65
  return new Collection(Array.from(this.map.entries()));
64
66
  }
65
67
 
66
- get entriesIterator(): IterableIterator<[K, V]> {
68
+ get entriesIterator(): IterableIterator<Tuple2<K, V>> {
67
69
  return this.map.entries();
68
70
  }
69
71
 
@@ -99,15 +101,15 @@ export class HashMap<K, V> extends ArrayIterable<[K, V], HashMap<K, V>> {
99
101
  return this.set(key, value);
100
102
  }
101
103
 
102
- toCollection(): Collection<[K, V]> {
103
- return new Collection<[K, V]>(Array.from(this.map.entries()));
104
+ get toCollection(): Collection<Tuple2<K, V>> {
105
+ return new Collection<Tuple2<K, V>>(Array.from(this.map.entries()));
104
106
  }
105
107
 
106
108
  get toMap(): Map<K, V> {
107
109
  return this.map;
108
110
  }
109
111
 
110
- get toArray(): Array<[K, V]> {
112
+ get toArray(): Array<Tuple2<K, V>> {
111
113
  return Array.from(this.map.entries());
112
114
  }
113
115
  }
package/src/hashset.ts CHANGED
@@ -26,7 +26,7 @@ export class HashSet<T> extends ArrayIterable<T, HashSet<T>>{
26
26
  return this.items;
27
27
  }
28
28
 
29
- contains(item: T): any {
29
+ contains(item: T): boolean {
30
30
  return this.items.has(item);
31
31
  }
32
32
 
package/src/try.ts CHANGED
@@ -49,6 +49,28 @@ export abstract class TryLike<T> implements Mappable<T>{
49
49
  }
50
50
  }
51
51
 
52
+ tapFailure(f: (e: Error) => void): TryLike<T> {
53
+ return this.match<TryLike<T>>({
54
+ success: () => this,
55
+ failure: e => {
56
+ try {
57
+ f(e);
58
+ return this;
59
+ } catch (ex) {
60
+ return failure(ex);
61
+ }
62
+ }
63
+ });
64
+ }
65
+
66
+ toEitherWithLeft<L>(f: (e: Error) => L): Either<L, T> {
67
+ return this.match<Either<L, T>>({
68
+ success: r => right(r),
69
+ failure: e => left(f(e))
70
+ });
71
+
72
+ }
73
+
52
74
  }
53
75
 
54
76