@commercetools/sync-actions 6.1.1 → 7.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/sync-actions.cjs.js +1353 -92
- package/dist/sync-actions.es.js +1292 -18
- package/dist/sync-actions.umd.js +1357 -95
- package/dist/sync-actions.umd.min.js +1 -1
- package/lib/cart-discounts-actions.js +31 -0
- package/lib/cart-discounts.js +70 -0
- package/lib/categories.js +106 -0
- package/lib/category-actions.js +117 -0
- package/lib/channels.js +0 -0
- package/lib/customer-actions.js +125 -0
- package/lib/customer-group-actions.js +23 -0
- package/lib/customer-group.js +70 -0
- package/lib/customers.js +95 -0
- package/lib/discount-codes-actions.js +31 -0
- package/lib/discount-codes.js +80 -0
- package/lib/index.js +90 -0
- package/lib/inventories.js +89 -0
- package/lib/inventory-actions.js +42 -0
- package/lib/order-actions.js +71 -0
- package/lib/orders.js +89 -0
- package/lib/product-actions.js +779 -0
- package/lib/product-discounts-actions.js +27 -0
- package/lib/product-discounts.js +71 -0
- package/lib/products.js +208 -0
- package/lib/utils/clone.js +9 -0
- package/lib/utils/common-actions.js +157 -0
- package/lib/utils/create-build-actions.js +74 -0
- package/lib/utils/create-build-array-actions.js +146 -0
- package/lib/utils/create-map-action-group.js +35 -0
- package/lib/utils/diffpatcher.js +93 -0
- package/lib/utils/find-matching-pairs.js +67 -0
- package/package.json +4 -5
- package/CHANGELOG.md +0 -211
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
exports.baseActionsList = undefined
|
|
7
|
+
exports.actionsMapBase = actionsMapBase
|
|
8
|
+
|
|
9
|
+
var _commonActions = require('./utils/common-actions')
|
|
10
|
+
|
|
11
|
+
var baseActionsList = (exports.baseActionsList = [
|
|
12
|
+
{ action: 'changeIsActive', key: 'isActive' },
|
|
13
|
+
{ action: 'changeName', key: 'name' },
|
|
14
|
+
{ action: 'changePredicate', key: 'predicate' },
|
|
15
|
+
{ action: 'changeSortOrder', key: 'sortOrder' },
|
|
16
|
+
{ action: 'changeValue', key: 'value' },
|
|
17
|
+
{ action: 'setDescription', key: 'description' },
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
function actionsMapBase(diff, oldObj, newObj) {
|
|
21
|
+
return (0, _commonActions.buildBaseAttributesActions)({
|
|
22
|
+
actions: baseActionsList,
|
|
23
|
+
diff: diff,
|
|
24
|
+
oldObj: oldObj,
|
|
25
|
+
newObj: newObj,
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
exports.actionGroups = undefined
|
|
7
|
+
|
|
8
|
+
var _lodash = require('lodash.flatten')
|
|
9
|
+
|
|
10
|
+
var _lodash2 = _interopRequireDefault(_lodash)
|
|
11
|
+
|
|
12
|
+
var _createBuildActions = require('./utils/create-build-actions')
|
|
13
|
+
|
|
14
|
+
var _createBuildActions2 = _interopRequireDefault(_createBuildActions)
|
|
15
|
+
|
|
16
|
+
var _createMapActionGroup = require('./utils/create-map-action-group')
|
|
17
|
+
|
|
18
|
+
var _createMapActionGroup2 = _interopRequireDefault(_createMapActionGroup)
|
|
19
|
+
|
|
20
|
+
var _productDiscountsActions = require('./product-discounts-actions')
|
|
21
|
+
|
|
22
|
+
var _diffpatcher = require('./utils/diffpatcher')
|
|
23
|
+
|
|
24
|
+
var diffpatcher = _interopRequireWildcard(_diffpatcher)
|
|
25
|
+
|
|
26
|
+
function _interopRequireWildcard(obj) {
|
|
27
|
+
if (obj && obj.__esModule) {
|
|
28
|
+
return obj
|
|
29
|
+
} else {
|
|
30
|
+
var newObj = {}
|
|
31
|
+
if (obj != null) {
|
|
32
|
+
for (var key in obj) {
|
|
33
|
+
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
34
|
+
newObj[key] = obj[key]
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
newObj.default = obj
|
|
38
|
+
return newObj
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function _interopRequireDefault(obj) {
|
|
43
|
+
return obj && obj.__esModule ? obj : { default: obj }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
var actionGroups = (exports.actionGroups = ['base'])
|
|
47
|
+
|
|
48
|
+
function createProductDiscountsMapActions(mapActionGroup) {
|
|
49
|
+
return function doMapActions(diff, newObj, oldObj) {
|
|
50
|
+
var allActions = []
|
|
51
|
+
|
|
52
|
+
allActions.push(
|
|
53
|
+
mapActionGroup('base', function() {
|
|
54
|
+
return (0,
|
|
55
|
+
_productDiscountsActions.actionsMapBase)(diff, oldObj, newObj)
|
|
56
|
+
})
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return (0, _lodash2.default)(allActions)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.default = function(config) {
|
|
64
|
+
var mapActionGroup = (0, _createMapActionGroup2.default)(config)
|
|
65
|
+
var doMapActions = createProductDiscountsMapActions(mapActionGroup)
|
|
66
|
+
var buildActions = (0, _createBuildActions2.default)(
|
|
67
|
+
diffpatcher.diff,
|
|
68
|
+
doMapActions
|
|
69
|
+
)
|
|
70
|
+
return { buildActions: buildActions }
|
|
71
|
+
}
|
package/lib/products.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
exports.actionGroups = undefined
|
|
7
|
+
|
|
8
|
+
var _extends =
|
|
9
|
+
Object.assign ||
|
|
10
|
+
function(target) {
|
|
11
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
12
|
+
var source = arguments[i]
|
|
13
|
+
for (var key in source) {
|
|
14
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
15
|
+
target[key] = source[key]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return target
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var _lodash = require('lodash.flatten')
|
|
23
|
+
|
|
24
|
+
var _lodash2 = _interopRequireDefault(_lodash)
|
|
25
|
+
|
|
26
|
+
var _createBuildActions = require('./utils/create-build-actions')
|
|
27
|
+
|
|
28
|
+
var _createBuildActions2 = _interopRequireDefault(_createBuildActions)
|
|
29
|
+
|
|
30
|
+
var _createMapActionGroup = require('./utils/create-map-action-group')
|
|
31
|
+
|
|
32
|
+
var _createMapActionGroup2 = _interopRequireDefault(_createMapActionGroup)
|
|
33
|
+
|
|
34
|
+
var _productActions = require('./product-actions')
|
|
35
|
+
|
|
36
|
+
var productActions = _interopRequireWildcard(_productActions)
|
|
37
|
+
|
|
38
|
+
var _diffpatcher = require('./utils/diffpatcher')
|
|
39
|
+
|
|
40
|
+
var diffpatcher = _interopRequireWildcard(_diffpatcher)
|
|
41
|
+
|
|
42
|
+
var _findMatchingPairs = require('./utils/find-matching-pairs')
|
|
43
|
+
|
|
44
|
+
var _findMatchingPairs2 = _interopRequireDefault(_findMatchingPairs)
|
|
45
|
+
|
|
46
|
+
function _interopRequireWildcard(obj) {
|
|
47
|
+
if (obj && obj.__esModule) {
|
|
48
|
+
return obj
|
|
49
|
+
} else {
|
|
50
|
+
var newObj = {}
|
|
51
|
+
if (obj != null) {
|
|
52
|
+
for (var key in obj) {
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
54
|
+
newObj[key] = obj[key]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
newObj.default = obj
|
|
58
|
+
return newObj
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function _interopRequireDefault(obj) {
|
|
63
|
+
return obj && obj.__esModule ? obj : { default: obj }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function _toConsumableArray(arr) {
|
|
67
|
+
if (Array.isArray(arr)) {
|
|
68
|
+
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
|
|
69
|
+
arr2[i] = arr[i]
|
|
70
|
+
}
|
|
71
|
+
return arr2
|
|
72
|
+
} else {
|
|
73
|
+
return Array.from(arr)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
var actionGroups = [
|
|
78
|
+
'base',
|
|
79
|
+
'meta',
|
|
80
|
+
'references',
|
|
81
|
+
'prices',
|
|
82
|
+
'attributes',
|
|
83
|
+
'images',
|
|
84
|
+
'variants',
|
|
85
|
+
'categories',
|
|
86
|
+
'categoryOrderHints',
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
function createProductMapActions(mapActionGroup) {
|
|
90
|
+
return function doMapActions(diff, newObj, oldObj, options) {
|
|
91
|
+
var allActions = []
|
|
92
|
+
var sameForAllAttributeNames = options.sameForAllAttributeNames
|
|
93
|
+
|
|
94
|
+
var variantHashMap = (0, _findMatchingPairs2.default)(
|
|
95
|
+
diff.variants,
|
|
96
|
+
oldObj.variants,
|
|
97
|
+
newObj.variants
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
allActions.push(
|
|
101
|
+
mapActionGroup('base', function() {
|
|
102
|
+
return productActions.actionsMapBase(diff, oldObj, newObj)
|
|
103
|
+
})
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
allActions.push(
|
|
107
|
+
mapActionGroup('meta', function() {
|
|
108
|
+
return productActions.actionsMapMeta(diff, oldObj, newObj)
|
|
109
|
+
})
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
allActions.push(
|
|
113
|
+
mapActionGroup('references', function() {
|
|
114
|
+
return productActions.actionsMapReferences(diff, oldObj, newObj)
|
|
115
|
+
})
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
allActions.push(
|
|
119
|
+
mapActionGroup('variants', function() {
|
|
120
|
+
return productActions.actionsMapVariants(diff, oldObj, newObj)
|
|
121
|
+
})
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
allActions.push(productActions.actionsMapMasterVariant(oldObj, newObj))
|
|
125
|
+
|
|
126
|
+
allActions.push(
|
|
127
|
+
mapActionGroup('attributes', function() {
|
|
128
|
+
return productActions.actionsMapAttributes(
|
|
129
|
+
diff,
|
|
130
|
+
oldObj,
|
|
131
|
+
newObj,
|
|
132
|
+
sameForAllAttributeNames || [],
|
|
133
|
+
variantHashMap
|
|
134
|
+
)
|
|
135
|
+
})
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
allActions.push(
|
|
139
|
+
mapActionGroup('images', function() {
|
|
140
|
+
return productActions.actionsMapImages(
|
|
141
|
+
diff,
|
|
142
|
+
oldObj,
|
|
143
|
+
newObj,
|
|
144
|
+
variantHashMap
|
|
145
|
+
)
|
|
146
|
+
})
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
allActions.push(
|
|
150
|
+
mapActionGroup('prices', function() {
|
|
151
|
+
return productActions.actionsMapPrices(
|
|
152
|
+
diff,
|
|
153
|
+
oldObj,
|
|
154
|
+
newObj,
|
|
155
|
+
variantHashMap
|
|
156
|
+
)
|
|
157
|
+
})
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
allActions.push(
|
|
161
|
+
mapActionGroup('categories', function() {
|
|
162
|
+
return productActions.actionsMapCategories(diff)
|
|
163
|
+
})
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
allActions.push(
|
|
167
|
+
mapActionGroup('categories', function() {
|
|
168
|
+
return productActions.actionsMapCategoryOrderHints(diff, oldObj)
|
|
169
|
+
})
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
return (0, _lodash2.default)(allActions)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function moveMasterVariantsIntoVariants(before, now) {
|
|
177
|
+
var move = function move(obj) {
|
|
178
|
+
return _extends({}, obj, {
|
|
179
|
+
masterVariant: undefined,
|
|
180
|
+
variants: [obj.masterVariant].concat(
|
|
181
|
+
_toConsumableArray(obj.variants || [])
|
|
182
|
+
),
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
var hasMasterVariant = function hasMasterVariant(obj) {
|
|
186
|
+
return obj && obj.masterVariant
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return [
|
|
190
|
+
hasMasterVariant(before) ? move(before) : before,
|
|
191
|
+
hasMasterVariant(now) ? move(now) : now,
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
exports.default = function(config) {
|
|
196
|
+
var mapActionGroup = (0, _createMapActionGroup2.default)(config)
|
|
197
|
+
var doMapActions = createProductMapActions(mapActionGroup)
|
|
198
|
+
|
|
199
|
+
var buildActions = (0, _createBuildActions2.default)(
|
|
200
|
+
diffpatcher.diff,
|
|
201
|
+
doMapActions,
|
|
202
|
+
moveMasterVariantsIntoVariants
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
return { buildActions: buildActions }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
exports.actionGroups = actionGroups
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
exports.buildBaseAttributesActions = buildBaseAttributesActions
|
|
7
|
+
exports.buildReferenceActions = buildReferenceActions
|
|
8
|
+
|
|
9
|
+
var _lodash = require('lodash.isnil')
|
|
10
|
+
|
|
11
|
+
var _lodash2 = _interopRequireDefault(_lodash)
|
|
12
|
+
|
|
13
|
+
var _clone = require('./clone')
|
|
14
|
+
|
|
15
|
+
var _clone2 = _interopRequireDefault(_clone)
|
|
16
|
+
|
|
17
|
+
var _diffpatcher = require('./diffpatcher')
|
|
18
|
+
|
|
19
|
+
var diffpatcher = _interopRequireWildcard(_diffpatcher)
|
|
20
|
+
|
|
21
|
+
function _interopRequireWildcard(obj) {
|
|
22
|
+
if (obj && obj.__esModule) {
|
|
23
|
+
return obj
|
|
24
|
+
} else {
|
|
25
|
+
var newObj = {}
|
|
26
|
+
if (obj != null) {
|
|
27
|
+
for (var key in obj) {
|
|
28
|
+
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
29
|
+
newObj[key] = obj[key]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
newObj.default = obj
|
|
33
|
+
return newObj
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _interopRequireDefault(obj) {
|
|
38
|
+
return obj && obj.__esModule ? obj : { default: obj }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function _defineProperty(obj, key, value) {
|
|
42
|
+
if (key in obj) {
|
|
43
|
+
Object.defineProperty(obj, key, {
|
|
44
|
+
value: value,
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
})
|
|
49
|
+
} else {
|
|
50
|
+
obj[key] = value
|
|
51
|
+
}
|
|
52
|
+
return obj
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Builds actions for simple object properties, given a list of actions
|
|
57
|
+
* E.g. [{ action: `changeName`, key: 'name' }]
|
|
58
|
+
*
|
|
59
|
+
* @param {Array} options.actions - a list of actions to be built
|
|
60
|
+
* based on the given property
|
|
61
|
+
* @param {Object} options.diff - the diff object
|
|
62
|
+
* @param {Object} options.oldObj - the object that needs to be updated
|
|
63
|
+
* @param {Object} options.newObj - the new representation of the object
|
|
64
|
+
*/
|
|
65
|
+
function buildBaseAttributesActions(_ref) {
|
|
66
|
+
var actions = _ref.actions,
|
|
67
|
+
diff = _ref.diff,
|
|
68
|
+
oldObj = _ref.oldObj,
|
|
69
|
+
newObj = _ref.newObj
|
|
70
|
+
|
|
71
|
+
return actions
|
|
72
|
+
.map(function(item) {
|
|
73
|
+
var key = item.key // e.g.: name, description, ...
|
|
74
|
+
var actionKey = item.actionKey || item.key
|
|
75
|
+
var delta = diff[key]
|
|
76
|
+
var before = oldObj[key]
|
|
77
|
+
var now = newObj[key]
|
|
78
|
+
var isNotDefinedBefore = (0, _lodash2.default)(oldObj[key])
|
|
79
|
+
var isNotDefinedNow = (0, _lodash2.default)(newObj[key])
|
|
80
|
+
if (!delta) return undefined
|
|
81
|
+
|
|
82
|
+
if (isNotDefinedNow && isNotDefinedBefore) return undefined
|
|
83
|
+
|
|
84
|
+
if (!isNotDefinedNow && isNotDefinedBefore)
|
|
85
|
+
// no value previously set
|
|
86
|
+
return _defineProperty({ action: item.action }, actionKey, now)
|
|
87
|
+
|
|
88
|
+
/* no new value */
|
|
89
|
+
if (isNotDefinedNow && !{}.hasOwnProperty.call(newObj, key))
|
|
90
|
+
return undefined
|
|
91
|
+
|
|
92
|
+
if (isNotDefinedNow && {}.hasOwnProperty.call(newObj, key))
|
|
93
|
+
// value unset
|
|
94
|
+
return {
|
|
95
|
+
action: item.action,
|
|
96
|
+
|
|
97
|
+
// We need to clone `before` as `patch` will mutate it
|
|
98
|
+
}
|
|
99
|
+
var patched = diffpatcher.patch((0, _clone2.default)(before), delta)
|
|
100
|
+
return _defineProperty({ action: item.action }, actionKey, patched)
|
|
101
|
+
})
|
|
102
|
+
.filter(function(action) {
|
|
103
|
+
return action
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Builds actions for simple reference objects, given a list of actions
|
|
109
|
+
* E.g. [{ action: `setTaxCategory`, key: 'taxCategory' }]
|
|
110
|
+
*
|
|
111
|
+
* @param {Array} options.actions - a list of actions to be built
|
|
112
|
+
* based on the given property
|
|
113
|
+
* @param {Object} options.diff - the diff object
|
|
114
|
+
* @param {Object} options.oldObj - the object that needs to be updated
|
|
115
|
+
* @param {Object} options.newObj - the new representation of the object
|
|
116
|
+
*/
|
|
117
|
+
function buildReferenceActions(_ref4) {
|
|
118
|
+
var actions = _ref4.actions,
|
|
119
|
+
diff = _ref4.diff,
|
|
120
|
+
newObj = _ref4.newObj
|
|
121
|
+
|
|
122
|
+
return actions
|
|
123
|
+
.map(function(item) {
|
|
124
|
+
var action = item.action
|
|
125
|
+
var key = item.key
|
|
126
|
+
|
|
127
|
+
if (
|
|
128
|
+
diff[key] &&
|
|
129
|
+
// The `key` value was added or removed
|
|
130
|
+
(Array.isArray(diff[key]) ||
|
|
131
|
+
// The `key` value id changed
|
|
132
|
+
diff[key].id)
|
|
133
|
+
) {
|
|
134
|
+
var newValue = Array.isArray(diff[key])
|
|
135
|
+
? diffpatcher.getDeltaValue(diff[key])
|
|
136
|
+
: newObj[key]
|
|
137
|
+
|
|
138
|
+
if (!newValue) return { action: action }
|
|
139
|
+
|
|
140
|
+
return _defineProperty(
|
|
141
|
+
{
|
|
142
|
+
action: action,
|
|
143
|
+
},
|
|
144
|
+
key,
|
|
145
|
+
{
|
|
146
|
+
typeId: newValue.typeId,
|
|
147
|
+
id: newValue.id,
|
|
148
|
+
}
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return undefined
|
|
153
|
+
})
|
|
154
|
+
.filter(function(action) {
|
|
155
|
+
return action
|
|
156
|
+
})
|
|
157
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
|
|
7
|
+
var _slicedToArray = (function() {
|
|
8
|
+
function sliceIterator(arr, i) {
|
|
9
|
+
var _arr = []
|
|
10
|
+
var _n = true
|
|
11
|
+
var _d = false
|
|
12
|
+
var _e = undefined
|
|
13
|
+
try {
|
|
14
|
+
for (
|
|
15
|
+
var _i = arr[Symbol.iterator](), _s;
|
|
16
|
+
!(_n = (_s = _i.next()).done);
|
|
17
|
+
_n = true
|
|
18
|
+
) {
|
|
19
|
+
_arr.push(_s.value)
|
|
20
|
+
if (i && _arr.length === i) break
|
|
21
|
+
}
|
|
22
|
+
} catch (err) {
|
|
23
|
+
_d = true
|
|
24
|
+
_e = err
|
|
25
|
+
} finally {
|
|
26
|
+
try {
|
|
27
|
+
if (!_n && _i['return']) _i['return']()
|
|
28
|
+
} finally {
|
|
29
|
+
if (_d) throw _e
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _arr
|
|
33
|
+
}
|
|
34
|
+
return function(arr, i) {
|
|
35
|
+
if (Array.isArray(arr)) {
|
|
36
|
+
return arr
|
|
37
|
+
} else if (Symbol.iterator in Object(arr)) {
|
|
38
|
+
return sliceIterator(arr, i)
|
|
39
|
+
} else {
|
|
40
|
+
throw new TypeError(
|
|
41
|
+
'Invalid attempt to destructure non-iterable instance'
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
})()
|
|
46
|
+
|
|
47
|
+
exports.default = createBuildActions
|
|
48
|
+
function applyOnBeforeDiff(before, now, fn) {
|
|
49
|
+
return fn && typeof fn === 'function' ? fn(before, now) : [before, now]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createBuildActions(differ, doMapActions, onBeforeDiff) {
|
|
53
|
+
return function buildActions(now, before) {
|
|
54
|
+
var options =
|
|
55
|
+
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}
|
|
56
|
+
|
|
57
|
+
if (!now || !before)
|
|
58
|
+
throw new Error(
|
|
59
|
+
'Missing either `newObj` or `oldObj` ' +
|
|
60
|
+
'in order to build update actions'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
var _applyOnBeforeDiff = applyOnBeforeDiff(before, now, onBeforeDiff),
|
|
64
|
+
_applyOnBeforeDiff2 = _slicedToArray(_applyOnBeforeDiff, 2),
|
|
65
|
+
processedBefore = _applyOnBeforeDiff2[0],
|
|
66
|
+
processedNow = _applyOnBeforeDiff2[1]
|
|
67
|
+
|
|
68
|
+
var diffed = differ(processedBefore, processedNow)
|
|
69
|
+
|
|
70
|
+
if (!diffed) return []
|
|
71
|
+
|
|
72
|
+
return doMapActions(diffed, processedNow, processedBefore, options)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true,
|
|
5
|
+
})
|
|
6
|
+
|
|
7
|
+
var _typeof =
|
|
8
|
+
typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
|
|
9
|
+
? function(obj) {
|
|
10
|
+
return typeof obj
|
|
11
|
+
}
|
|
12
|
+
: function(obj) {
|
|
13
|
+
return obj &&
|
|
14
|
+
typeof Symbol === 'function' &&
|
|
15
|
+
obj.constructor === Symbol &&
|
|
16
|
+
obj !== Symbol.prototype
|
|
17
|
+
? 'symbol'
|
|
18
|
+
: typeof obj
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.default = createBuildArrayActions
|
|
22
|
+
var REGEX_NUMBER = new RegExp(/^\d+$/)
|
|
23
|
+
var REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/)
|
|
24
|
+
|
|
25
|
+
var ADD_ACTIONS = (exports.ADD_ACTIONS = 'create')
|
|
26
|
+
var REMOVE_ACTIONS = (exports.REMOVE_ACTIONS = 'remove')
|
|
27
|
+
var CHANGE_ACTIONS = (exports.CHANGE_ACTIONS = 'change')
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Generate + configure a function to build actions for nested objects
|
|
31
|
+
* @param {string} key key of the attribute containing the array of
|
|
32
|
+
* nested objects
|
|
33
|
+
* @param {object} config configuration object that can contain the keys
|
|
34
|
+
* [ADD_ACTIONS, REMOVE_ACTIONS, CHANGE_ACTIONS], each of
|
|
35
|
+
* which is a function. The function should accept the old + new arrays and
|
|
36
|
+
* return an action object.
|
|
37
|
+
* @return {Array} The generated array of actions
|
|
38
|
+
*/
|
|
39
|
+
function createBuildArrayActions(key, config) {
|
|
40
|
+
return function buildArrayActions(diff, oldObj, newObj) {
|
|
41
|
+
var addActions = []
|
|
42
|
+
var removeActions = []
|
|
43
|
+
var changeActions = []
|
|
44
|
+
|
|
45
|
+
if (diff[key]) {
|
|
46
|
+
var arrayDelta = diff[key]
|
|
47
|
+
|
|
48
|
+
Object.keys(arrayDelta).forEach(function(index) {
|
|
49
|
+
if (config[ADD_ACTIONS] && isCreateAction(arrayDelta, index)) {
|
|
50
|
+
var actionGenerator = config[ADD_ACTIONS]
|
|
51
|
+
// When adding a new element you don't need the oldObj
|
|
52
|
+
var action = actionGenerator(newObj[key][index], parseInt(index, 10))
|
|
53
|
+
|
|
54
|
+
if (action) addActions.push(action)
|
|
55
|
+
} else if (
|
|
56
|
+
config[CHANGE_ACTIONS] &&
|
|
57
|
+
isChangeAction(arrayDelta, index)
|
|
58
|
+
) {
|
|
59
|
+
var _actionGenerator = config[CHANGE_ACTIONS]
|
|
60
|
+
// When changing an existing element you need both old + new
|
|
61
|
+
var _action = _actionGenerator(
|
|
62
|
+
oldObj[key][index],
|
|
63
|
+
newObj[key][index],
|
|
64
|
+
parseInt(index, 10)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if (_action) changeActions.push(_action)
|
|
68
|
+
} else if (
|
|
69
|
+
config[REMOVE_ACTIONS] &&
|
|
70
|
+
isRemoveAction(arrayDelta, index)
|
|
71
|
+
) {
|
|
72
|
+
var realIndex = index.replace('_', '')
|
|
73
|
+
var _actionGenerator2 = config[REMOVE_ACTIONS]
|
|
74
|
+
// When removing an existing element you don't need the newObj
|
|
75
|
+
var _action2 = _actionGenerator2(
|
|
76
|
+
oldObj[key][realIndex],
|
|
77
|
+
parseInt(realIndex, 10)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
if (_action2) removeActions.push(_action2)
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return changeActions.concat(removeActions, addActions)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Tests a delta to see if it represents a create action.
|
|
91
|
+
* eg. delta:
|
|
92
|
+
* {
|
|
93
|
+
* 0: [ { foo: 'bar' } ]
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* @param {object} obj The delta generated by the diffpatcher
|
|
97
|
+
* @param {string} key key of generated delta to examine
|
|
98
|
+
* @return {Boolean} Returns true if delta represents a create action,
|
|
99
|
+
* false otherwise
|
|
100
|
+
*/
|
|
101
|
+
function isCreateAction(obj, key) {
|
|
102
|
+
return (
|
|
103
|
+
REGEX_NUMBER.test(key) && Array.isArray(obj[key]) && obj[key].length === 1
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Tests a delta to see if it represents a change action.
|
|
109
|
+
* eg. delta:
|
|
110
|
+
*
|
|
111
|
+
* {
|
|
112
|
+
* 0: {
|
|
113
|
+
* foo: ['bar', 'baz']
|
|
114
|
+
* }
|
|
115
|
+
* }
|
|
116
|
+
* @param {object} obj The delta generated by the diffpatcher
|
|
117
|
+
* @param {string} key key of generated delta to examine
|
|
118
|
+
* @return {Boolean} Returns true if delta represents a change action,
|
|
119
|
+
* false otherwise
|
|
120
|
+
*/
|
|
121
|
+
function isChangeAction(obj, key) {
|
|
122
|
+
return REGEX_NUMBER.test(key) && _typeof(obj[key]) === 'object'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Tests a delta to see if it represents a remove action.
|
|
127
|
+
* eg. delta:
|
|
128
|
+
*
|
|
129
|
+
* {
|
|
130
|
+
* _0: [ 'foo', 0, 0 ]
|
|
131
|
+
* }
|
|
132
|
+
* @param {object} obj The delta generated by the diffpatcher
|
|
133
|
+
* @param {string} key key of generated delta to examine
|
|
134
|
+
* @return {Boolean} Returns true if delta represents a remove action,
|
|
135
|
+
* false otherwise
|
|
136
|
+
*/
|
|
137
|
+
function isRemoveAction(obj, key) {
|
|
138
|
+
return (
|
|
139
|
+
REGEX_UNDERSCORE_NUMBER.test(key) &&
|
|
140
|
+
Array.isArray(obj[key]) &&
|
|
141
|
+
obj[key].length === 3 &&
|
|
142
|
+
_typeof(obj[key][0]) === 'object' &&
|
|
143
|
+
obj[key][1] === 0 &&
|
|
144
|
+
obj[key][2] === 0
|
|
145
|
+
)
|
|
146
|
+
}
|