Skip to content

Commit

Permalink
0.1.8 - added LinkedCollection and more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmed committed Oct 20, 2022
1 parent a5ffdd1 commit 83cd828
Show file tree
Hide file tree
Showing 26 changed files with 2,824 additions and 617 deletions.
96 changes: 86 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ I considered adding parallel factory patterns instead of instance-based structur
- [SizedLinkedList](#user-content-sizedlinkedlist-docs-src)
- [LinkedSet](#user-content-linkedset-docs-src)
- [SortedSet](#user-content-sortedset-docs-src)
- [LinkedCollection](#user-content-linkedcollection-docs-src)
- [Queue](#user-content-queue-docs-src)
- [UniQueue](#user-content-uniqueue-docs-src)
- [PriorityQueue](#user-content-priorityqueue-docs-src)
Expand All @@ -70,12 +71,16 @@ I considered adding parallel factory patterns instead of instance-based structur
┃ ╋━ UniQueue
┃ ┃
┃ ╋━ SortedSet
╹ ╹ ┻━ PriorityQueue
┃ ╹ ┻━ PriorityQueue
┣━ LinkedCollection
┃ ┃
╹ ┻━ SortedCollection [Coming soon]
ObjectPool < LinkedList
```

### LinkedList ([docs](docs/classes/LinkedList.LinkedList-1.md)) ([src](src/LinkedList.ts))
### LinkedList ([docs](docs/classes/LinkedList.md)) ([src](src/LinkedList.ts))

For all your linked-list needs!

Expand All @@ -98,7 +103,7 @@ for (const s of bar) {
}
```

### SizedLinkedList ([docs](docs/classes/SizedLinkedList.SizedLinkedList-1.md)) ([src](src/SizedLinkedList.ts))
### SizedLinkedList ([docs](docs/classes/SizedLinkedList.md)) ([src](src/SizedLinkedList.ts))

##### (extends [LinkedList](#user-content-linkedlist-docs-src))

Expand All @@ -110,7 +115,7 @@ import { SizedLinkedList as LinkedList } from '@zimmed/prefab';
const baz = LinkedList.from('green eggs and spam'.split(' ')).size; // -> 4
```

### LinkedSet ([docs](docs/classes/LinkedSet.LinkedSet-1.md)) ([src](src/LinkedSet.ts))
### LinkedSet ([docs](docs/classes/LinkedSet.md)) ([src](src/LinkedSet.ts))

##### (extends [LinkedList](#user-content-linkedlist-docs-src))

Expand All @@ -120,7 +125,7 @@ while supporting additional functionality that the native Set does not support,
as insert, pop, shift, and more.

```typescript
import { SortedSet } from '@zimmed/prefab';
import { LinkedSet } from '@zimmed/prefab';

const set = new LinkedSet(['one', 'two', 'three', 'two', 'four', 'one']); // -> LinkedSet { 'one' 'two' 'three' 'four' }

Expand All @@ -144,7 +149,7 @@ const five = set.pop(); // -> 'five'
set.size; // -> 4
```

### SortedSet ([docs](docs/classes/SortedSet.SortedSet-1.md)) ([src](src/SortedSet.ts))
### SortedSet ([docs](docs/classes/SortedSet.md)) ([src](src/SortedSet.ts))

##### (extends [LinkedSet](#user-content-linkedset-docs-src))

Expand All @@ -166,7 +171,7 @@ collection
.delete(collection.find((x) => x.name === 'bar')); // -> SortedSet { 'baz' 'foo' }
```

### Queue ([docs](docs/classes/Queue.Queue-1.md)) ([src](src/Queue.ts))
### Queue ([docs](docs/classes/Queue.md)) ([src](src/Queue.ts))

##### (extends [SizedLinkedList](#user-content-sizedlinkedlist-docs-src))

Expand All @@ -190,7 +195,7 @@ stack.pop(); // -> 5
// lifo -> Queue { 1 2 3 }
```

### UniQueue ([docs](docs/classes/UniQueue.UniQueue-1.md)) ([src](src/UniQueue.ts))
### UniQueue ([docs](docs/classes/UniQueue.md)) ([src](src/UniQueue.ts))

##### (extends [LinkedSet](#user-content-linkedset-docs-src))

Expand All @@ -206,7 +211,7 @@ const q = Queue.from([1, 2, 3, 4, 4, 3]); // -> UniQueue { 1, 2, 3, 4 }
q.has(3); // -> Hashmap lookup time
```

### PriorityQueue ([docs](docs/classes/PriorityQueue.PriorityQueue-1.md)) ([src](src/PriorityQueue.ts))
### PriorityQueue ([docs](docs/classes/PriorityQueue.md)) ([src](src/PriorityQueue.ts))

##### (extends [SortedSet](#user-content-sortedset-docs-src))

Expand All @@ -227,7 +232,78 @@ q.enqueue(5, 1)
.dequeue(); // -> 200
```
### ObjectPool ([docs](docs/classes/ObjectPool.ObjectPool-1.md)) ([src](src/ObjectPool.ts))
### LinkedCollection ([docs](docs/classes/LinkedCollection.md)) ([src](src/LinkedCollection.ts))
##### (extends [LinkedList](#user-content-linkedlist-docs-src))
Similar to the LinkedSet, but uses primary key lookups and identifiers, and requires items to be objects. Useful for keeping key -> value records with constant lookups, as well as linked list iterations.
```typescript
import { LinkedCollection } from '@zimmed/prefab';

class Item {
constructor(
public readonly id: string,
public data?: any,
public readonly cat: string = 'default'
) {}
}

const collection = new LinkedCollection('id', [
new Item('one'),
new Item('two'),
new Item('three'),
new Item('two'),
]); // -> LinkedCollection { Item('one'), Item('two'), Item('three') }

collection.add(new Item('four'));
collection.size; // -> 4

for (const item of collection) {
// same as collection.values()
// -> Item('one'), Item('two'), Item('three'), ...
}

for (const key of collection.keys()) {
// -> 'one', 'two', 'three', ...
}

for (const [key, item] of collection.entries()) {
// -> ['one', Item('one')], ...
}

// Respects same Set methods
const four = collection.pop(); // -> Item('four')
collection.size; // -> 4

// Introduces expected Map method and changes signatures to be key lookups instead of item lookups
collection.get('two'); // -> Item('two')
collection.has('one'); // -> true
collection.delete('three'); // -> true

// Upserting / Uppending
// Safely update existing item or add/insert new one
collection.uppend(new Item('seven')); // -> same as collection.add or collection.append
collection.uppend(new Item('seven', { foo: 'bar' })); // -> replaces existing Item('seven') with new Item('seven', { foo: 'bar' }) maintaining link position
collection.upsert(new Item('zero')); // -> same as collection.insert or collection.unshift
collection.upsert(new Item('zero', 14)); // -> updates existing Item('zero') with new Item('zero', 14) maintaining link position

// Additional groupBy Helper
// Groups items into arrays based on provided key
new LinkedCollection('id', [
new Item('one'),
new Item('two'),
new Item('three', undefined, 'special'),
]).groupBy('cat'); // -> { default: [Item('one'), Item('two')], special: [Item('three')] }
// if key is the collection identifier key, returns a key->value object
new LinkedCollection('id', [new Item('one'), new Item('two')]).groupBy('id'); // -> { one: Item('one'), two: Item('two') }
```
### SortedCollection (Coming Soon...)
##### (extends [LinkedCollection](#user-content-linkedcollection-docs-src))
### ObjectPool ([docs](docs/classes/ObjectPool.md)) ([src](src/ObjectPool.ts))
##### (uses [LinkedList](#user-content-linkedlist-docs-src))
Expand Down
2 changes: 1 addition & 1 deletion coverage/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 83cd828

Please sign in to comment.