Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@
<img alt="codeclimate" src="https://api.codeclimate.com/v1/badges/f40c9fff2927e49c3ea2/maintainability">
</a>
</p>
<p align="center">
<a href="https://greenkeeper.io">
<img alt="greenkeeper" src="https://badges.greenkeeper.io/semantic-release/semantic-release.svg">
</a>
<a href="https://david-dm.org/patrickmichalina/typescript-monads">
<img alt="greenkeeper" src="https://david-dm.org/patrickmichalina/typescript-monads/status.svg">
</a>
<a href="https://david-dm.org/patrickmichalina/typescript-monads?type=dev">
<img alt="greenkeeper" src="https://david-dm.org/patrickmichalina/typescript-monads/dev-status.svg">
</a>
</p>
<p align="center">
<a href="https://github.com/semantic-release/semantic-release">
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
"maybe",
"result",
"either",
"functional"
"list",
"functional",
"list-monad",
"maybe-monad",
"either-monad",
"result-monad"
],
"scripts": {
"test": "jest",
Expand Down
21 changes: 20 additions & 1 deletion src/list/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { List } from './list'
import { listFrom } from './list.factory'

class Animal {
constructor(public name: string) { }
constructor(public name: string, public nickname?: string) { }
}

class Dog extends Animal {
dogtag!: string
dogyear!: number
Expand Down Expand Up @@ -228,6 +229,24 @@ describe(List.name, () => {
})
})

describe('ToDictionary', () => {
const Rex = new Dog('Rex', 'Rdawg')
const Meow = new Cat('Meow')
const sut = List.of<Animal>(Rex, Meow)

it('should handle nominal keyed case', () => {
expect(sut.toDictionary('name')).toEqual({ Rex, Meow })
})

it('should handle unkeyed', () => {
expect(sut.toDictionary()).toEqual({ 0: Rex, 1: Meow })
})

it('should handle missing keys', () => {
expect(sut.toDictionary('nickname')).toEqual({ Rdawg: Rex })
})
})

// describe('OrderBy', () => {
// it('should order by object', () => {
// const dog1 = new Dog('Atlas')
Expand Down
20 changes: 20 additions & 0 deletions src/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ export class List<T> {
return this.filter(a => a instanceof type)
}


/**
* Converts the list into an object with numbered indices mathing the array position of the item.
*/
public toDictionary(): { [key: number]: T }

/**
* Converts the list into an object deriving key from the specified property.
*/
public toDictionary(key: keyof T): { [key: string]: T }
public toDictionary(key?: keyof T): { [key: number]: T } | { [key: string]: T } {
return this.reduce((acc, curr, idx) => {
return key
? curr[key]
? { ...acc, [curr[key] as any]: curr }
: acc
: { ...acc, [idx]: curr }
}, {})
}

// /**
// * Sorts the elements of a sequence in ascending order.
// */
Expand Down