scats 1.2.0 → 1.3.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
@@ -413,8 +413,8 @@ set.add(1); // true. set = [1]
413
413
  set.add(1); // false. set = [1]
414
414
  set.clear(); // []]
415
415
  set.add(1); // true. set = [1]
416
- set.remove(2); false. set = [1]
417
- set.remove(1); true. set = []
418
- set.addAll([2, 3]); set = [1, 2, 3]
416
+ set.remove(2); // false. set = [1]
417
+ set.remove(1); // true. set = []
418
+ set.addAll([2, 3]); // set = [1, 2, 3]
419
419
  set.filterInPlace(x => x > 2); // [3]
420
420
  ```
@@ -47,11 +47,12 @@ export declare const Nil: Collection<any>;
47
47
  export declare class ArrayBuffer<T> extends ArrayBackedCollection<T, ArrayBuffer<T>> implements Mappable<T>, Filterable<T, ArrayBuffer<T>> {
48
48
  protected readonly items: T[];
49
49
  static get empty(): ArrayBuffer<any>;
50
- constructor(items: T[]);
50
+ constructor(items?: T[]);
51
51
  static of<T>(...elements: T[]): ArrayBuffer<T>;
52
52
  static from<T>(elements: Iterable<T>): ArrayBuffer<T>;
53
53
  static fill<A>(len: number): (elem: (idx: number) => A) => ArrayBuffer<A>;
54
54
  protected fromArray(array: T[]): ArrayBuffer<T>;
55
+ private normalized;
55
56
  update(index: number, element: T): void;
56
57
  set(index: number, element: T): void;
57
58
  clear(): void;
@@ -65,6 +66,12 @@ export declare class ArrayBuffer<T> extends ArrayBackedCollection<T, ArrayBuffer
65
66
  subtractOne(element: T): this;
66
67
  subtractAll(elements: Iterable<T>): this;
67
68
  sort(compareFn?: (a: T, b: T) => number): this;
69
+ filterInPlace(p: (element: T) => boolean): this;
70
+ dropInPlace(n: number): this;
71
+ dropRightInPlace(n: number): this;
72
+ takeInPlace(n: number): this;
73
+ takeRightInPlace(n: number): this;
74
+ sliceInPlace(start: number, end: number): this;
68
75
  get toCollection(): Collection<T>;
69
76
  flatMap<B>(f: (item: T) => ArrayBuffer<B>): ArrayBuffer<B>;
70
77
  flatMapOption<B>(f: (item: T) => Option<B>): ArrayBuffer<B>;
@@ -187,7 +187,7 @@ exports.Collection = Collection;
187
187
  Collection.empty = new Collection([]);
188
188
  exports.Nil = Collection.empty;
189
189
  class ArrayBuffer extends ArrayBackedCollection {
190
- constructor(items) {
190
+ constructor(items = []) {
191
191
  super();
192
192
  this.items = items;
193
193
  }
@@ -217,6 +217,9 @@ class ArrayBuffer extends ArrayBackedCollection {
217
217
  return new ArrayBuffer(array);
218
218
  }
219
219
  }
220
+ normalized(n) {
221
+ return Math.min(Math.max(n, 0), this.length);
222
+ }
220
223
  update(index, element) {
221
224
  this.checkWithinBounds(index, index + 1);
222
225
  this.items[index] = element;
@@ -283,6 +286,45 @@ class ArrayBuffer extends ArrayBackedCollection {
283
286
  this.items.sort(compareFn);
284
287
  return this;
285
288
  }
289
+ filterInPlace(p) {
290
+ let i = 0, j = 0;
291
+ while (i < this.size) {
292
+ if (p(this.items[i])) {
293
+ if (i != j) {
294
+ this.items[j] = this.items[i];
295
+ }
296
+ j += 1;
297
+ }
298
+ i += 1;
299
+ }
300
+ if (i == j) {
301
+ return this;
302
+ }
303
+ else {
304
+ return this.takeInPlace(j);
305
+ }
306
+ }
307
+ dropInPlace(n) {
308
+ this.remove(0, this.normalized(n));
309
+ return this;
310
+ }
311
+ dropRightInPlace(n) {
312
+ const norm = this.normalized(n);
313
+ this.remove(this.length - norm, norm);
314
+ return this;
315
+ }
316
+ takeInPlace(n) {
317
+ const norm = this.normalized(n);
318
+ this.remove(norm, this.length - norm);
319
+ return this;
320
+ }
321
+ takeRightInPlace(n) {
322
+ this.remove(0, this.length - this.normalized(n));
323
+ return this;
324
+ }
325
+ sliceInPlace(start, end) {
326
+ return this.takeInPlace(end).dropInPlace(start);
327
+ }
286
328
  get toCollection() {
287
329
  return new Collection(this.items.slice(0));
288
330
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scats",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
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
@@ -293,7 +293,7 @@ export class ArrayBuffer<T> extends ArrayBackedCollection<T, ArrayBuffer<T>> imp
293
293
  return new ArrayBuffer<any>([]);
294
294
  }
295
295
 
296
- constructor(protected readonly items: T[]) {
296
+ constructor(protected readonly items: T[] = []) {
297
297
  super();
298
298
  }
299
299
 
@@ -323,6 +323,9 @@ export class ArrayBuffer<T> extends ArrayBackedCollection<T, ArrayBuffer<T>> imp
323
323
  }
324
324
  }
325
325
 
326
+ private normalized(n: number): number {
327
+ return Math.min(Math.max(n, 0), this.length);
328
+ }
326
329
 
327
330
 
328
331
  /** Replaces element at given index with a new value.
@@ -477,6 +480,53 @@ export class ArrayBuffer<T> extends ArrayBackedCollection<T, ArrayBuffer<T>> imp
477
480
  return this;
478
481
  }
479
482
 
483
+
484
+ filterInPlace(p: (element: T) => boolean): this {
485
+ let i = 0, j = 0;
486
+ while (i < this.size) {
487
+ if (p(this.items[i])) {
488
+ if (i != j) {
489
+ this.items[j] = this.items[i];
490
+ }
491
+ j += 1;
492
+ }
493
+ i += 1;
494
+ }
495
+
496
+ if (i == j) {
497
+ return this;
498
+ } else {
499
+ return this.takeInPlace(j);
500
+ }
501
+
502
+ }
503
+
504
+ dropInPlace(n: number): this {
505
+ this.remove(0, this.normalized(n));
506
+ return this;
507
+ }
508
+
509
+ dropRightInPlace(n: number): this {
510
+ const norm = this.normalized(n);
511
+ this.remove(this.length - norm, norm);
512
+ return this;
513
+ }
514
+
515
+ takeInPlace(n: number): this {
516
+ const norm = this.normalized(n);
517
+ this.remove(norm, this.length - norm);
518
+ return this;
519
+ }
520
+
521
+ takeRightInPlace(n: number): this {
522
+ this.remove(0, this.length - this.normalized(n));
523
+ return this;
524
+ }
525
+
526
+ sliceInPlace(start: number, end: number): this {
527
+ return this.takeInPlace(end).dropInPlace(start);
528
+ }
529
+
480
530
  get toCollection(): Collection<T> {
481
531
  return new Collection<T>(this.items.slice(0));
482
532
  }