Skip to content

Commit a17904c

Browse files
authored
Issue 65: replace lodash with native/sublibs (#73)
* use alternatives to lodash * prettier * remove lodash * use reduce * slice and pop * lint, reduce and format
1 parent 7d22a0e commit a17904c

File tree

10 files changed

+132
-56
lines changed

10 files changed

+132
-56
lines changed

__tests__/decorators.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as _ from 'lodash'
21
import {
32
ContentType,
43
Controller,
@@ -19,8 +18,12 @@ import {
1918
} from '../src'
2019
import { ModelDto } from './fixtures/models'
2120

21+
type IndexedRoutes = {
22+
[method: string]: IRoute
23+
}
24+
2225
describe('decorators', () => {
23-
let routes: { [method: string]: IRoute }
26+
let routes: IndexedRoutes
2427

2528
beforeEach(() => {
2629
getMetadataArgsStorage().reset()
@@ -219,7 +222,10 @@ describe('decorators', () => {
219222
}
220223
}
221224

222-
routes = _.keyBy(parseRoutes(getMetadataArgsStorage()), 'action.method')
225+
routes = parseRoutes(getMetadataArgsStorage()).reduce((acc, route) => {
226+
acc[route.action.method] = route
227+
return acc
228+
}, {} as IndexedRoutes)
223229
})
224230

225231
it('merges keywords defined in @OpenAPI decorator into operation', () => {
@@ -591,7 +597,7 @@ describe('decorators', () => {
591597
})
592598

593599
describe('@OpenAPI-decorated class', () => {
594-
let routes: { [method: string]: IRoute }
600+
let routes: IndexedRoutes
595601

596602
beforeEach(() => {
597603
getMetadataArgsStorage().reset()
@@ -622,7 +628,10 @@ describe('@OpenAPI-decorated class', () => {
622628
}
623629
}
624630

625-
routes = _.keyBy(parseRoutes(getMetadataArgsStorage()), 'action.method')
631+
routes = parseRoutes(getMetadataArgsStorage()).reduce((acc, route) => {
632+
acc[route.action.method] = route
633+
return acc
634+
}, {} as IndexedRoutes)
626635
})
627636

628637
it('applies controller OpenAPI props to each method with method-specific props taking precedence', () => {

__tests__/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// tslint:disable:no-implicit-dependencies no-submodule-imports
22
const { defaultMetadataStorage } = require('class-transformer/cjs/storage')
33
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'
4-
import * as _ from 'lodash'
4+
import _merge from 'lodash.merge'
55
import { getMetadataArgsStorage } from 'routing-controllers'
66

77
import {
@@ -144,7 +144,7 @@ describe('index', () => {
144144
})
145145

146146
it('gets full OpenAPI-formatted paths', () => {
147-
const route = _.cloneDeep(routes[0])
147+
const route = _merge({}, routes[0])
148148
expect(getFullPath(route)).toEqual('/api/users/')
149149

150150
route.options.routePrefix = undefined
@@ -178,7 +178,7 @@ describe('index', () => {
178178
})
179179

180180
it('gets OpenAPI Operation IDs', () => {
181-
const route = _.cloneDeep(routes[0])
181+
const route = _merge({}, routes[0])
182182
expect(getOperationId(route)).toEqual('UsersController.listUsers')
183183

184184
route.action.target = class AnotherController {}

__tests__/parameters.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as _ from 'lodash'
21
import {
32
Get,
43
getMetadataArgsStorage,
@@ -150,7 +149,7 @@ describe('parameters', () => {
150149
})
151150

152151
it('ignores @Param if corresponding name is not found in path string', () => {
153-
expect(_.filter(getPathParams(route), { name: 'invalid' })).toEqual([])
152+
expect(getPathParams(route).filter((r) => r.name === 'invalid')).toEqual([])
154153
})
155154

156155
it('parses query param from @QueryParam decorator', () => {

package-lock.json

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"singleQuote": true
3030
},
3131
"dependencies": {
32-
"lodash": "^4.17.15",
32+
"lodash.merge": "^4.6.2",
33+
"lodash.capitalize": "^4.2.1",
34+
"lodash.startcase": "^4.4.0",
3335
"openapi3-ts": "^2.0.1",
3436
"path-to-regexp": "^2.2.1",
3537
"reflect-metadata": "^0.1.13",
@@ -38,7 +40,9 @@
3840
"devDependencies": {
3941
"@team-griffin/install-self-peers": "^1.1.1",
4042
"@types/jest": "^26.0.20",
41-
"@types/lodash": "^4.14.168",
43+
"@types/lodash.merge": "^4.6.6",
44+
"@types/lodash.capitalize": "^4.2.6",
45+
"@types/lodash.startcase": "^4.4.6",
4246
"@types/node": "^13.13.46",
4347
"@types/prettier": "^2.2.2",
4448
"@types/reflect-metadata": "^0.1.0",

src/decorators.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash'
1+
import _merge from 'lodash.merge'
22
import {
33
OperationObject,
44
ReferenceObject,
@@ -53,9 +53,9 @@ export function applyOpenAPIDecorator(
5353
]
5454

5555
return openAPIParams.reduce((acc: OperationObject, oaParam: OpenAPIParam) => {
56-
return _.isFunction(oaParam)
56+
return typeof oaParam === 'function'
5757
? oaParam(acc, route)
58-
: _.merge({}, acc, oaParam)
58+
: _merge({}, acc, oaParam)
5959
}, originalOperation) as OperationObject
6060
}
6161

@@ -134,7 +134,7 @@ export function ResponseSchema(
134134

135135
if (oldSchema?.$ref || oldSchema?.items || oldSchema?.oneOf) {
136136
// case where we're adding multiple schemas under single statuscode/contentType
137-
const newStatusCodeResponse = _.merge(
137+
const newStatusCodeResponse = _merge(
138138
{},
139139
source.responses[statusCode],
140140
responses[statusCode]
@@ -150,7 +150,7 @@ export function ResponseSchema(
150150
return source
151151
}
152152

153-
return _.merge({}, source, { responses })
153+
return _merge({}, source, { responses })
154154
}
155155

156156
return source

0 commit comments

Comments
 (0)