Skip to content

Commit 7d16866

Browse files
author
Shogun
committed
feat: Drop lodash dependencies.
1 parent ea575ea commit 7d16866

File tree

10 files changed

+344
-257
lines changed

10 files changed

+344
-257
lines changed

lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const ajv_1 = __importDefault(require("ajv"));
1414
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
1515
const http_errors_1 = __importStar(require("http-errors"));
1616
const http_status_codes_1 = require("http-status-codes");
17-
const lodash_upperfirst_1 = __importDefault(require("lodash.upperfirst"));
1817
const statuses_1 = __importDefault(require("statuses"));
1918
const properties_1 = require("./properties");
19+
const utils_1 = require("./utils");
2020
const validation_1 = require("./validation");
2121
var properties_2 = require("./properties");
2222
exports.addAdditionalProperties = properties_2.addAdditionalProperties;
@@ -65,13 +65,13 @@ function handleErrors(error, request, reply) {
6565
}
6666
}
6767
else if (code === 'INVALID_CONTENT_TYPE' || code === 'FST_ERR_CTP_INVALID_MEDIA_TYPE') {
68-
error = http_errors_1.default(http_status_codes_1.UNSUPPORTED_MEDIA_TYPE, lodash_upperfirst_1.default(validation_1.validationMessagesFormatters.contentType()));
68+
error = http_errors_1.default(http_status_codes_1.UNSUPPORTED_MEDIA_TYPE, utils_1.upperFirst(validation_1.validationMessagesFormatters.contentType()));
6969
}
7070
else if (code === 'FST_ERR_CTP_EMPTY_JSON_BODY') {
71-
error = http_errors_1.default(http_status_codes_1.BAD_REQUEST, lodash_upperfirst_1.default(validation_1.validationMessagesFormatters.jsonEmpty()));
71+
error = http_errors_1.default(http_status_codes_1.BAD_REQUEST, utils_1.upperFirst(validation_1.validationMessagesFormatters.jsonEmpty()));
7272
}
7373
else if (code === 'MALFORMED_JSON' || error.message === 'Invalid JSON' || error.stack.includes('at JSON.parse')) {
74-
error = http_errors_1.default(http_status_codes_1.BAD_REQUEST, lodash_upperfirst_1.default(validation_1.validationMessagesFormatters.json()));
74+
error = http_errors_1.default(http_status_codes_1.BAD_REQUEST, utils_1.upperFirst(validation_1.validationMessagesFormatters.json()));
7575
}
7676
// Get the status code
7777
let { statusCode, headers } = error;

lib/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function upperFirst(source) {
4+
// tslint:disable-next-line strict-type-predicates
5+
if (typeof source !== 'string' || !source.length) {
6+
return source;
7+
}
8+
return source[0].toUpperCase() + source.substring(1);
9+
}
10+
exports.upperFirst = upperFirst;
11+
function get(target, path) {
12+
const tokens = path.split('.').map((t) => t.trim());
13+
for (const token of tokens) {
14+
if (typeof target === 'undefined' || target === null) {
15+
// We're supposed to be still iterating, but the chain is over - Return undefined
16+
target = undefined;
17+
break;
18+
}
19+
const index = token.match(/^(\d+)|(?:\[(\d+)\])$/);
20+
if (index) {
21+
target = target[parseInt(index[1] || index[2], 10)];
22+
}
23+
else {
24+
target = target[token];
25+
}
26+
}
27+
return target;
28+
}
29+
exports.get = get;

lib/validation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const http_errors_1 = __importDefault(require("http-errors"));
77
const http_status_codes_1 = require("http-status-codes");
8-
const get = require("lodash.get");
8+
const utils_1 = require("./utils");
99
function niceJoin(array, lastSeparator = ' and ', separator = ', ') {
1010
switch (array.length) {
1111
case 0:
@@ -122,7 +122,7 @@ function convertValidationErrors(section, data, validationErrors) {
122122
break;
123123
case 'pattern':
124124
const pattern = e.params.pattern;
125-
const value = get(data, key);
125+
const value = utils_1.get(data, key);
126126
if (pattern === '.+' && !value) {
127127
message = exports.validationMessagesFormatters.presentString();
128128
}

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"types": "types/index.d.ts",
2727
"scripts": {
2828
"lint": "tslint --project tsconfig.test.json -t stylish src/*.ts test/*.ts",
29-
"test": "jest test --coverage --coverageReporters=html --coverageReporters=text",
29+
"test": "jest test --verbose --coverage --coverageReporters=html --coverageReporters=text",
3030
"ci": "yarn lint && jest --coverage --coverageThreshold='{\"global\":{\"branches\":90,\"functions\":90,\"lines\":90,\"statements\":90}}' --ci --coverageReporters=json",
3131
"prebuild": "rm -rf lib/* types/* && yarn lint",
3232
"build": "tsc -p .",
@@ -38,16 +38,12 @@
3838
"fastify-plugin": "^1.6.0",
3939
"http-errors": "^1.7.3",
4040
"http-status-codes": "^1.4.0",
41-
"lodash.get": "^4.4.2",
42-
"lodash.upperfirst": "^4.3.1",
4341
"statuses": "^1.5.0"
4442
},
4543
"devDependencies": {
4644
"@cowtech/tslint-config": "^5.13.0",
4745
"@types/http-errors": "^1.6.2",
4846
"@types/jest": "^24.0.23",
49-
"@types/lodash.get": "^4.4.6",
50-
"@types/lodash.upperfirst": "^4.3.6",
5147
"@types/node": "^12.12.8",
5248
"@types/statuses": "^1.5.0",
5349
"fastify": "^2.10.0",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import fastifyPlugin from 'fastify-plugin'
44
import { IncomingMessage, Server, ServerResponse } from 'http'
55
import createError, { HttpError, InternalServerError, NotFound } from 'http-errors'
66
import { BAD_REQUEST, INTERNAL_SERVER_ERROR, UNSUPPORTED_MEDIA_TYPE } from 'http-status-codes'
7-
import upperFirst from 'lodash.upperfirst'
87
import statuses from 'statuses'
98
import { FastifyDecoratedRequest, GenericObject, NodeError, RequestSection } from './interfaces'
109
import { addAdditionalProperties, serializeError } from './properties'
10+
import { upperFirst } from './utils'
1111
import { addResponseValidation, convertValidationErrors, validationMessagesFormatters } from './validation'
1212

1313
export * from './interfaces'

src/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export function upperFirst(source: any): string {
2+
// tslint:disable-next-line strict-type-predicates
3+
if (typeof source !== 'string' || !source.length) {
4+
return source
5+
}
6+
7+
return source[0].toUpperCase() + source.substring(1)
8+
}
9+
10+
export function get<T>(target: any, path: string): T {
11+
const tokens = path.split('.').map((t: string) => t.trim())
12+
13+
for (const token of tokens) {
14+
if (typeof target === 'undefined' || target === null) {
15+
// We're supposed to be still iterating, but the chain is over - Return undefined
16+
target = undefined
17+
break
18+
}
19+
20+
const index = token.match(/^(\d+)|(?:\[(\d+)\])$/)
21+
if (index) {
22+
target = target[parseInt(index[1] || index[2], 10)]
23+
} else {
24+
target = target[token]
25+
}
26+
}
27+
28+
return target
29+
}

src/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ValidationFormatter,
1010
Validations
1111
} from './interfaces'
12-
import get = require('lodash.get')
12+
import { get } from './utils'
1313

1414
export function niceJoin(array: Array<string>, lastSeparator: string = ' and ', separator: string = ', '): string {
1515
switch (array.length) {
@@ -145,7 +145,7 @@ export function convertValidationErrors(
145145
break
146146
case 'pattern':
147147
const pattern = e.params.pattern
148-
const value = get(data, key) as string
148+
const value = get<string>(data, key)
149149

150150
if (pattern === '.+' && !value) {
151151
message = validationMessagesFormatters.presentString()

test/utils.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { get, upperFirst } from '../src/utils'
2+
3+
describe('Utils', function(): void {
4+
describe('.upperFirst', function(): void {
5+
it('should correctly convert strings', function(): void {
6+
expect(upperFirst('abc')).toEqual('Abc')
7+
expect(upperFirst('aBC')).toEqual('ABC')
8+
expect(upperFirst('')).toEqual('')
9+
expect(upperFirst(3)).toEqual(3)
10+
})
11+
})
12+
13+
describe('.get', function(): void {
14+
it('should correctly return paths', function(): void {
15+
const target = {
16+
a: [{ b: { c: 1 } }],
17+
b: null
18+
}
19+
20+
expect(get(target, 'a')).toEqual([{ b: { c: 1 } }])
21+
expect(get(target, 'b.c')).toBeUndefined()
22+
expect(get(target, 'a.0.b')).toEqual({ c: 1 })
23+
expect(get(target, 'a.0.b.c')).toEqual(1)
24+
expect(get(target, 'a.[0].b')).toEqual({ c: 1 })
25+
expect(get(target, 'a.[2].b')).toBeUndefined()
26+
expect(get(target, 'a.[2]a.b')).toBeUndefined()
27+
expect(get(target, 'a.0.c.e.f')).toBeUndefined()
28+
})
29+
})
30+
})

types/utils.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare function upperFirst(source: any): string;
2+
export declare function get<T>(target: any, path: string): T;

0 commit comments

Comments
 (0)