Skip to content

Commit 9616ab8

Browse files
Merge pull request #145 from shiftcode/#144-replace-lodash-with-es6
#144 replace lodash with es6
2 parents 8ba6368 + ce3ef43 commit 9616ab8

File tree

10 files changed

+68
-53
lines changed

10 files changed

+68
-53
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"reflect-metadata": "^0.1.12",
6464
"rimraf": "^2.6.3",
6565
"rxjs": "^6.3.3",
66-
"semantic-release": "^16.0.0-beta.9",
66+
"semantic-release": "^16.0.0-beta.16",
6767
"sort-package-json": "^1.16.0",
6868
"ts-jest": "^23.10.4",
6969
"ts-loader": "^5.3.2",
@@ -78,7 +78,7 @@
7878
},
7979
"peerDependencies": {
8080
"aws-sdk": "^2.382.0",
81-
"lodash": "^4.17.10",
81+
"lodash": "^4.17.11",
8282
"reflect-metadata": "^0.1.12",
8383
"rxjs": "^6.3.3",
8484
"uuid": "^3.3.2"

src/dynamo/expression/condition-expression-builder.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { curry, forEach, isPlainObject } from 'lodash'
1+
import { curry, isPlainObject } from 'lodash'
22
import { Metadata } from '../../decorator/metadata/metadata'
33
import { PropertyMetadata } from '../../decorator/metadata/property-metadata.model'
44
import { toDbOne, typeOf } from '../../mapper'
@@ -58,12 +58,15 @@ export function deepFilter(obj: any, filterFn: (value: any) => boolean): any {
5858
} else if (isPlainObject(obj)) {
5959
const returnObj: Record<string, any> = {}
6060

61-
forEach(obj, (value: any, key: string) => {
62-
const item = deepFilter(value, filterFn)
63-
if (item !== null) {
64-
returnObj[key] = item
61+
for (const key in obj) {
62+
if (obj.hasOwnProperty(key)) {
63+
const value = obj[key]
64+
const item = deepFilter(value, filterFn)
65+
if (item !== null) {
66+
returnObj[key] = item
67+
}
6568
}
66-
})
69+
}
6770

6871
return Object.keys(returnObj).length ? returnObj : null
6972
} else {
@@ -331,6 +334,7 @@ function validateArity(operator: ConditionOperator, values?: any[]) {
331334
export const ERR_VALUES_BETWEEN_TYPE =
332335
'both values for operator BETWEEN must have the same type, got ${value1} and ${value2}'
333336
export const ERR_VALUES_IN = 'the provided value for IN operator must be an array'
337+
334338
// tslint:enable:no-invalid-template-strings
335339

336340
/**

src/dynamo/expression/functions/unique-attribute-value-name.function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const BRACED_INDEX_REGEX = /\[(\d+)]/g
55
/**
66
* Creates a unique attribute value placeholder name to use in the expression
77
*
8-
* @returns {string} The unique attribute value placeholder name in respect to the given existing value names (no duplicates)
8+
* @returns {string} The unique attribute value placeholder name in respect to the given existing value names (no duplicates allowed)
99
*/
1010
export function uniqueAttributeValueName(key: string, existingValueNames?: string[]): string {
1111
key = key.replace(/\./g, '__').replace(BRACED_INDEX_REGEX, attributeNameReplacer)

src/dynamo/expression/logical-operator/merge-conditions.function.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { mapKeys } from 'lodash'
21
import { Metadata } from '../../../decorator/metadata/metadata'
2+
import { Attribute } from '../../../mapper'
33
import { uniqueAttributeValueName } from '../functions/unique-attribute-value-name.function'
44
import { ConditionExpressionDefinitionFunction } from '../type/condition-expression-definition-function'
55
import { Expression } from '../type/expression.type'
@@ -19,19 +19,35 @@ export function mergeConditions(
1919
conditionDefinitionFns.forEach(conditionDefinitionFn => {
2020
// we can reuse the same for multiple conditions
2121
const condition = conditionDefinitionFn(expressionAttributeValues, metadata)
22-
Object.assign(mergedCondition.attributeNames, condition.attributeNames)
22+
mergedCondition.attributeNames = {...mergedCondition.attributeNames, ...condition.attributeNames}
2323

24-
// we need to make sure the value variable name is unique
25-
const attributeValues = mapKeys(condition.attributeValues, (value, key) => {
24+
/*
25+
* we need to make sure the value variable name is unique, this wont' work so the second :name must be renamed
26+
* {
27+
* ":name" : { S: "the name" },
28+
* ":name" : { S: "other name" }
29+
* }
30+
* |
31+
* |
32+
* ▽
33+
* {
34+
* ":name" : { S: "the name" },
35+
* ":name_2" : { S: "other name" }
36+
* }
37+
*
38+
*/
39+
const attributeValues: Record<string, Attribute> = {}
40+
Object.keys(condition.attributeValues).forEach(key => {
2641
const unique = uniqueAttributeValueName(key.replace(':', ''), Object.keys(mergedCondition.attributeValues))
2742
if (key !== unique) {
43+
// rename of the attributeName is required in condition
2844
condition.statement = condition.statement.replace(key, unique)
2945
}
3046

31-
return unique
47+
attributeValues[unique] = condition.attributeValues[key]
3248
})
3349

34-
Object.assign(mergedCondition.attributeValues, attributeValues)
50+
mergedCondition.attributeValues = {...mergedCondition.attributeValues, ...attributeValues }
3551
statements.push(condition.statement)
3652
})
3753

src/dynamo/expression/prepare-and-add-update-expressions.function.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { forEach } from 'lodash'
32
import { Metadata } from '../../decorator/metadata/index'
43
import { Attributes } from '../../mapper/index'
5-
import { SortedUpdateExpressions } from '../request/update/update.request'
64
import { addUpdateExpression } from './param-util'
7-
import { Expression, UpdateExpressionDefinitionFunction } from './type/index'
5+
import { Expression, UpdateExpression, UpdateExpressionDefinitionFunction } from './type/index'
6+
import { UpdateActionKeyword } from './type/update-action-keyword.type'
87

98
export function prepareAndAddUpdateExpressions(
109
metadata: Metadata<any>,
1110
params: DynamoDB.UpdateItemInput | DynamoDB.Update,
1211
updateDefFns: UpdateExpressionDefinitionFunction[],
1312
) {
1413
if (updateDefFns && updateDefFns.length) {
15-
const sortedByActionKeyWord: SortedUpdateExpressions = updateDefFns
14+
const sortedByActionKeyWord: Map<UpdateActionKeyword, UpdateExpression[]> = updateDefFns
1615
.map(updateDefFn => {
1716
return updateDefFn(<any>params.ExpressionAttributeNames, metadata)
1817
})
19-
.reduce(
20-
(result, expr) => {
21-
if (!result[expr.type]) {
22-
result[expr.type] = []
18+
.reduce((result, expr) => {
19+
const actionKeyword = expr.type
20+
if (!result.has(actionKeyword)) {
21+
result.set(actionKeyword, [])
2322
}
2423

25-
result[expr.type].push(expr)
24+
result.get(actionKeyword).push(expr)
2625
return result
2726
},
28-
<SortedUpdateExpressions>{},
27+
new Map(),
2928
)
3029

3130
const actionStatements: string[] = []
3231
let attributeValues: Attributes = {}
3332
let attributeNames: Record<string, string> = {}
3433

35-
forEach(sortedByActionKeyWord, (value, key) => {
34+
for (const [actionKeyword, updateExpressions] of sortedByActionKeyWord) {
3635
const statements: string[] = []
37-
if (value && value.length) {
38-
value.forEach(updateExpression => {
36+
if (updateExpressions && updateExpressions.length) {
37+
updateExpressions.forEach(updateExpression => {
3938
statements.push(updateExpression.statement)
4039
attributeValues = { ...attributeValues, ...updateExpression.attributeValues }
4140
attributeNames = { ...attributeNames, ...updateExpression.attributeNames }
4241
})
43-
actionStatements.push(`${key} ${statements.join(', ')}`)
42+
actionStatements.push(`${actionKeyword} ${statements.join(', ')}`)
4443
}
45-
})
44+
}
4645

4746
const expression: Expression = {
4847
statement: actionStatements.join(' '),

src/dynamo/request/get/get.request.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { values as objValues } from 'lodash'
32
import { Observable } from 'rxjs'
43
import { map, tap } from 'rxjs/operators'
54
import { createLogger, Logger } from '../../../logger/logger'
@@ -28,7 +27,7 @@ export class GetRequest<T> extends StandardRequest<T, DynamoDB.GetItemInput, Get
2827
// tslint:disable-next-line:no-unnecessary-callback-wrapper
2928
const resolved = attributesToGet.map(a => resolveAttributeNames(a))
3029
this.params.ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ')
31-
objValues(resolved).forEach(r => {
30+
Object.values(resolved).forEach(r => {
3231
this.params.ExpressionAttributeNames = { ...this.params.ExpressionAttributeNames, ...r.attributeNames }
3332
})
3433
return this

src/dynamo/request/update/update.request.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import { ModelConstructor } from '../../../model'
77
import { DynamoRx } from '../../dynamo-rx'
88
import { prepareAndAddUpdateExpressions } from '../../expression/prepare-and-add-update-expressions.function'
99
import { addUpdate } from '../../expression/request-expression-builder'
10-
import { RequestUpdateFunction, UpdateExpression, UpdateExpressionDefinitionFunction } from '../../expression/type'
11-
import { UpdateActionKeyword } from '../../expression/type/update-action-keyword.type'
10+
import { RequestUpdateFunction, UpdateExpressionDefinitionFunction } from '../../expression/type'
1211
import { WriteRequest } from '../write.request'
1312

14-
export type SortedUpdateExpressions = Record<UpdateActionKeyword, UpdateExpression[]>
15-
1613
export class UpdateRequest<T> extends WriteRequest<T, DynamoDB.UpdateItemInput, UpdateRequest<T>> {
1714
private readonly logger: Logger
1815

src/mapper/for-type/number.mapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isNumber } from 'lodash'
21
import { NumberAttribute } from '../type/attribute.type'
2+
import { isNumber } from 'lodash'
33
import { MapperForType } from './base.mapper'
44

55
function numberFromDb(attributeValue: NumberAttribute): number {

tools/tslint/test/test.ts.lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash'
1+
import * as moment from 'moment'
22
import * as DynamoDB from 'aws-sdk'
33
import { Config } from 'aws-sdk'
44
import { Key } from 'aws-sdk/clients/dynamodb'

0 commit comments

Comments
 (0)