Skip to content

Commit 4fe162a

Browse files
committed
feat(migrate): replace lodash.isnil
1 parent d37f51f commit 4fe162a

File tree

9 files changed

+28
-30
lines changed

9 files changed

+28
-30
lines changed

packages/sync-actions/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"fast-equals": "^2.0.0",
2727
"jsondiffpatch": "^0.4.0",
2828
"lodash.intersection": "^4.4.0",
29-
"lodash.isnil": "^4.0.0",
3029
"lodash.shuffle": "^4.2.0",
3130
"lodash.sortby": "^4.7.0",
3231
"lodash.uniqwith": "^4.5.0",

packages/sync-actions/src/customer-actions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import isNil from 'lodash.isnil'
2-
import clone from './utils/clone'
1+
import clone, { notEmpty } from './utils/clone'
32
import {
43
buildBaseAttributesActions,
54
buildReferenceActions,
@@ -214,5 +213,5 @@ function buildAuthenticationModeActions({ actions, diff, oldObj, newObj }) {
214213
[value]: newObj.password,
215214
}
216215
})
217-
.filter((action) => !isNil(action))
216+
.filter(notEmpty)
218217
}

packages/sync-actions/src/inventories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import type {
44
SyncActionConfig,
55
UpdateAction,
66
} from '@commercetools/sdk-client-v2'
7+
import { actionsMapBase, actionsMapReferences } from './inventory-actions'
78
import { SyncAction } from './types/update-actions'
89
import actionsMapCustom from './utils/action-map-custom'
910
import createBuildActions from './utils/create-build-actions'
1011
import createMapActionGroup from './utils/create-map-action-group'
1112
import { diff } from './utils/diffpatcher'
12-
import { actionsMapBase, actionsMapReferences } from './inventory-actions'
1313

1414
export const actionGroups = ['base', 'references']
1515

packages/sync-actions/src/types-actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { deepEqual } from 'fast-equals'
12
import sortBy from 'lodash.sortby'
23
import { buildBaseAttributesActions } from './utils/common-actions'
34
import createBuildArrayActions, {
@@ -6,7 +7,6 @@ import createBuildArrayActions, {
67
} from './utils/create-build-array-actions'
78
import { getDeltaValue } from './utils/diffpatcher'
89
import extractMatchingPairs from './utils/extract-matching-pairs'
9-
import { deepEqual } from 'fast-equals'
1010

1111
const REGEX_NUMBER = new RegExp(/^\d+$/)
1212
const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export default function clone(obj) {
22
return JSON.parse(JSON.stringify(obj))
33
}
4+
5+
export function notEmpty<TValue>(
6+
value: TValue | null | undefined
7+
): value is TValue {
8+
return value !== null && value !== undefined
9+
}

packages/sync-actions/src/utils/common-actions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import isNil from 'lodash.isnil'
2-
import clone from './clone'
1+
import clone, { notEmpty } from './clone'
32
import { getDeltaValue, patch } from './diffpatcher'
43

54
const normalizeValue = (value) =>
@@ -64,7 +63,7 @@ export function buildBaseAttributesActions({
6463
const patched = patch(clone(before), delta)
6564
return { action: item.action, [actionKey]: patched }
6665
})
67-
.filter((action) => !isNil(action))
66+
.filter(notEmpty)
6867
}
6968

7069
/**

packages/sync-actions/src/utils/copy-empty-array-props.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import isNil from 'lodash.isnil'
2-
31
const CUSTOM = 'custom'
42

53
/**
@@ -11,7 +9,7 @@ const CUSTOM = 'custom'
119
* @returns {Array} Ordered Array [oldObj, newObj]
1210
*/
1311
export default function copyEmptyArrayProps(oldObj = {}, newObj = {}) {
14-
if (!isNil(oldObj) && !isNil(newObj)) {
12+
if (oldObj && newObj) {
1513
const nextObjectWithEmptyArray = Object.entries(oldObj).reduce(
1614
(merged, [key, value]) => {
1715
// Ignore CUSTOM key as this object is dynamic and its up to the user to dynamically change it
@@ -26,13 +24,13 @@ export default function copyEmptyArrayProps(oldObj = {}, newObj = {}) {
2624
}, {})
2725
for (let i = 0; i < newObj[key].length; i++) {
2826
if (
29-
!isNil(newObj[key][i]) &&
27+
newObj[key][i] &&
3028
typeof newObj[key][i] === 'object' &&
31-
!isNil(newObj[key][i].id)
29+
newObj[key][i].id
3230
) {
3331
// Since its unordered array elements then check if the element on `oldObj` exists by id
3432
const foundObject = hashMapValue[newObj[key][i].id]
35-
if (!isNil(foundObject)) {
33+
if (foundObject) {
3634
const [, nestedObject] = copyEmptyArrayProps(
3735
foundObject,
3836
newObj[key][i]
@@ -50,11 +48,11 @@ export default function copyEmptyArrayProps(oldObj = {}, newObj = {}) {
5048
return merged
5149
}
5250
if (Array.isArray(value)) {
53-
merged[key] = isNil(newObj[key]) ? [] : newObj[key]
51+
merged[key] = newObj[key] ? newObj[key] : []
5452
return merged
5553
}
5654
if (
57-
!isNil(newObj[key]) &&
55+
newObj[key] &&
5856
typeof value === 'object' &&
5957
// Ignore Date as this will create invalid object since typeof date === 'object' return true
6058
// ex: {date: new Date()} will result {date: {}}

packages/sync-actions/src/utils/create-build-actions.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isNil from 'lodash.isnil'
21
import { deepEqual } from 'fast-equals'
32

43
function applyOnBeforeDiff(before, now, fn?: (before, now) => Array<any>) {
@@ -35,10 +34,9 @@ function injectMissingPriceIds(nextVariants, previousVariants) {
3534
if (!prices) return restOfVariant
3635
const oldVariant = previousVariants.find(
3736
(previousVariant) =>
38-
(!isNil(previousVariant.id) && previousVariant.id === newVariant.id) ||
39-
(!isNil(previousVariant.key) &&
40-
previousVariant.key === newVariant.key) ||
41-
(!isNil(previousVariant.sku) && previousVariant.sku === newVariant.sku)
37+
(previousVariant.id && previousVariant.id === newVariant.id) ||
38+
(previousVariant.key && previousVariant.key === newVariant.key) ||
39+
(previousVariant.sku && previousVariant.sku === newVariant.sku)
4240
)
4341

4442
return {
@@ -49,13 +47,17 @@ function injectMissingPriceIds(nextVariants, previousVariants) {
4947

5048
if (oldPrice) {
5149
// copy ID if not provided
52-
if (!newPrice.id) newPrice.id = oldPrice.id
50+
if (!newPrice.id) {
51+
newPrice.id = oldPrice.id
52+
}
5353

54-
if (isNil(newPrice.value.type))
54+
if (!newPrice.value.type) {
5555
newPrice.value.type = oldPrice.value.type
56+
}
5657

57-
if (isNil(newPrice.value.fractionDigits))
58+
if (!newPrice.value.fractionDigits) {
5859
newPrice.value.fractionDigits = oldPrice.value.fractionDigits
60+
}
5961
}
6062

6163
return newPrice

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,11 +6886,6 @@ lodash.isfunction@^3.0.9:
68866886
resolved "https://registry.yarnpkg.com/lodash.isfunction/-/lodash.isfunction-3.0.9.tgz#06de25df4db327ac931981d1bdb067e5af68d051"
68876887
integrity sha512-AirXNj15uRIMMPihnkInB4i3NHeb4iBtNg9WRWuK2o31S+ePwwNmDPaTL3o7dTJ+VXNZim7rFs4rxN4YU1oUJw==
68886888

6889-
lodash.isnil@^4.0.0:
6890-
version "4.0.0"
6891-
resolved "https://registry.yarnpkg.com/lodash.isnil/-/lodash.isnil-4.0.0.tgz#49e28cd559013458c814c5479d3c663a21bfaa6c"
6892-
integrity sha512-up2Mzq3545mwVnMhTDMdfoG1OurpA/s5t88JmQX809eH3C8491iu2sfKhTfhQtKY78oPNhiaHJUpT/dUDAAtng==
6893-
68946889
lodash.isplainobject@^4.0.6:
68956890
version "4.0.6"
68966891
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

0 commit comments

Comments
 (0)