Skip to content

Commit

Permalink
fixed flat typings, added example
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-surinin committed Jan 2, 2019
1 parent 8e1c372 commit 9afcb7e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
51 changes: 51 additions & 0 deletions __tests__/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { extend } from '../src/Symbol'
import { ObjectEntry, Indexed } from '../src/types'

describe('General', () => {
it('should find dependencies with different versions', () => {

const p1 = {
'name': 'package1',
'version': '1.0.0',
'devDependencies': {
'jest': '23.6.0',
'prettier': '1.15.3'
}
}

const p2 = {
'name': 'package2',
'version': '1.0.0',
'devDependencies': {
'jest': '22.0.0',
'prettier': '1.15.3'
}
}

function toHaveMoreVersions(entry: ObjectEntry<ObjectEntry<string>[]>): boolean {
return entry.value[extend]()
.unique()
.length > 1
}

function toInvalidDependency(entry: ObjectEntry<ObjectEntry<string>[]>) {
return {
name: entry.key,
versions: entry.value.map(e => e.value)
}
}

const invalidDeps = [p1, p2]
.map(pck => pck.devDependencies as Indexed<string>) // object[]
.map(pck => pck[extend]().entries())[extend]() // {key, value}[][]
.flat()[extend]() // {key, value}[]
.groupBy('key')[extend]() // {dependencyName: {name: version}[]}
.entries() // {key, value}[]
.filter(toHaveMoreVersions)
.map(toInvalidDependency)
expect(invalidDeps).toHaveLength(1)
expect(invalidDeps[0].name).toBe('jest')
expect(invalidDeps[0].versions).toMatchObject(['23.6.0', '22.0.0'])

})
})
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
{
"name": "declarative-js-symbols",
"version": "1.0.0",
"author": "Pavel Surinin <pavel.surinin@gmail.com>",
"description": "Symbols to extend JavaScript Array and Object prototypes with declarative-js functionality.",
"main": "index.js",
"types": "index.d.ts",
"homepage": "https://pavel-surinin.github.io/declarativejssymbols/#/",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/pavel-surinin/declarativejssymbols"
},
"badges": [
{
"description": "Build Status",
"href": "https://travis-ci.org/pavel-surinin/declarative-js-symbols",
"url": "https://travis-ci.org/pavel-surinin/declarativejssymbols.svg?branch=master"
},
{
"description": "Coverage Status",
"href": "https://coveralls.io/github/pavel-surinin/declarativejssymbols?branch=master",
"url": "https://coveralls.io/repos/github/pavel-surinin/declarativejssymbols/badge.svg?branch=master"
}
],
"bugs": {
"url": "https://github.com/pavel-surinin/declarativejssymbols/issues",
"email": "pavel.surinin@gmail.com"
},
"scripts": {
"test": "jest",
"lint": "tslint --project ./tsconfig.json",
Expand All @@ -21,8 +43,6 @@
"docs:commit": "npm run docsify && git add . && git commit -m \"Updated generated documentation\" && git push",
"docs:start": "docsify serve ./docs"
},
"author": "Pavel Surinin <pavel.surinin@gmail.com>",
"license": "ISC",
"dependencies": {
"declarative-js": "3.2.5"
},
Expand All @@ -36,4 +56,4 @@
"tslint": "5.10.0",
"typescript": "3.2.2"
}
}
}
53 changes: 52 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

Symbol that extends `array` and `object` with additional functions from declarative-js package.

## Install
```
npm i declarative-js-symbols --save
```

## Usage
```javascript
import { extends } from 'declarative-js-symbols'
Expand All @@ -21,6 +26,52 @@ const transformedData =
.groupBy('taskName')
```

Imagine, that we maintain two npm packages and want to ensure that all dependencies have same versions.
```javascript
import { extends } from 'declarative-js-symbols'

const p1 = {
"name": "package1",
"version": "1.0.0",
"devDependencies": {
"jest": "23.6.0", // <==
"prettier": "1.15.3"
}
}
const p2 = {
"name": "package2",
"version": "1.0.0",
"devDependencies": {
"jest": "22.0.0", // <==
"prettier": "1.15.3"
}
}

function toHaveMoreVersions(entry) {
return entry.value[extends]()
.unique()
.length > 1
}

function toInvalidDependency(entry) {
return {
name: entry.key,
versions: entry.value.map(e => e.value)
}
}

const invalidDeps = [p1, p2]
.map(pck => pck.devDependencies) // object[]
.map(pck => pck[extend]().entries())[extend]() // {key, value}[][]
.flat()[extend]() // {key, value}[]
.groupBy('key')[extend]() // {dependencyName: {name: version}[]}
.entries() // {key, value}[]
.filter(toHaveMoreVersions)
.map(toInvalidDependency)
// [{name: 'Jest', versions: ['23.6.0', '22.0.0']}]

```

## Array

### equal
Expand Down Expand Up @@ -146,7 +197,7 @@ interface ArrayExtension<T> {
Function to make from 2d array simple array
```typescript
interface ArrayExtension<T> {
flat(): Array<T>
flat(): T
}
```
```javascript
Expand Down
2 changes: 1 addition & 1 deletion src/types/ArrayTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export type ArrayExtension<T> = {
* @example
* [[1,2],[3,4]][extend]().flat() // [1,2,3,4]
*/
flat(): Array<T>
flat(): T
/**
* Collects items to object by key from callback. If function resolves key,
* that already exists it will throw an Error. Second callback is value mapper.
Expand Down
2 changes: 1 addition & 1 deletion src/types/ObjectTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extend } from '../Symbol'
declare global {
interface Object {
[extend](): ObjectExtension<any>
[extend]<T>(): ObjectExtension<T>
}
}
export type ObjectExtension<T> = {
Expand Down

0 comments on commit 9afcb7e

Please sign in to comment.