Skip to content

Commit d6011f4

Browse files
committed
Merge remote-tracking branch 'remotes/origin/MAGETWO-71425' into team3-delivery
2 parents 8238fdd + ca2982e commit d6011f4

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'underscore'
9+
], function ($, _) {
10+
'use strict';
11+
12+
/**
13+
* @constructor
14+
*/
15+
var ConditionsDataNormalizer = function () {
16+
this.patterns = {
17+
validate: /^[a-z0-9_-][a-z0-9_-]*(?:\[(?:\d*|[a-z0-9_-]+)\])*$/i,
18+
key: /[a-z0-9_-]+|(?=\[\])/gi,
19+
push: /^$/,
20+
fixed: /^\d+$/,
21+
named: /^[a-z0-9_-]+$/i
22+
};
23+
};
24+
25+
ConditionsDataNormalizer.prototype = {
26+
/**
27+
* Will convert an object:
28+
* {
29+
* "foo[bar][1][baz]": 123,
30+
* "foo[bar][1][blah]": 321
31+
* "foo[bar][1--1][ah]": 456
32+
* }
33+
*
34+
* to
35+
* {
36+
* "foo": {
37+
* "bar": {
38+
* "1": {
39+
* "baz": 123,
40+
* "blah": 321
41+
* },
42+
* "1--1": {
43+
* "ah": 456
44+
* }
45+
* }
46+
* }
47+
* }
48+
*/
49+
normalize: function normalize(value) {
50+
var el, _this = this;
51+
52+
this.pushes = {};
53+
this.data = {};
54+
55+
_.each(value, function (e, i) {
56+
el = {};
57+
el[i] = e;
58+
59+
_this._addPair({
60+
name: i,
61+
value: e
62+
});
63+
});
64+
65+
return this.data;
66+
},
67+
68+
/**
69+
* @param {Object} base
70+
* @param {String} key
71+
* @param {String} value
72+
* @return {Object}
73+
* @private
74+
*/
75+
_build: function build(base, key, value) {
76+
base[key] = value;
77+
78+
return base;
79+
},
80+
81+
/**
82+
* @param {Object} root
83+
* @param {String} value
84+
* @return {*}
85+
* @private
86+
*/
87+
_makeObject: function makeObject(root, value) {
88+
var keys = root.match(this.patterns.key),
89+
k, idx; // nest, nest, ..., nest
90+
91+
while ((k = keys.pop()) !== undefined) {
92+
// foo[]
93+
if (this.patterns.push.test(k)) {
94+
idx = this._incrementPush(root.replace(/\[\]$/, ''));
95+
value = this._build([], idx, value);
96+
} // foo[n]
97+
else if (this.patterns.fixed.test(k)) {
98+
value = this._build({}, k, value);
99+
} // foo; foo[bar]
100+
else if (this.patterns.named.test(k)) {
101+
value = this._build({}, k, value);
102+
}
103+
}
104+
105+
return value;
106+
},
107+
108+
/**
109+
* @param {String} key
110+
* @return {Number}
111+
* @private
112+
*/
113+
_incrementPush: function incrementPush(key) {
114+
if (this.pushes[key] === undefined) {
115+
this.pushes[key] = 0;
116+
}
117+
118+
return this.pushes[key]++;
119+
},
120+
121+
/**
122+
* @param {Object} pair
123+
* @return {Object}
124+
* @private
125+
*/
126+
_addPair: function addPair(pair) {
127+
var obj = this._makeObject(pair.name, pair.value);
128+
129+
if (!this.patterns.validate.test(pair.name)) {
130+
return this;
131+
}
132+
133+
this.data = $.extend(true, this.data, obj);
134+
135+
return this;
136+
}
137+
};
138+
139+
return ConditionsDataNormalizer;
140+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'Magento_Rule/conditions-data-normalizer'
9+
], function ($, Normalizer) {
10+
'use strict';
11+
12+
describe('Magento_Rule/conditions-data-normalizer', function () {
13+
var normalizer;
14+
15+
beforeEach(function () {
16+
normalizer = new Normalizer();
17+
});
18+
19+
it('Check for empty object when input is a falsey value', function () {
20+
expect(normalizer.normalize('')).toEqual({});
21+
expect(normalizer.normalize()).toEqual({});
22+
expect(normalizer.normalize(false)).toEqual({});
23+
expect(normalizer.normalize(null)).toEqual({});
24+
expect(normalizer.normalize(0)).toEqual({});
25+
});
26+
27+
it('Check single level normalization.', function () {
28+
var normal = normalizer.normalize({
29+
foo: 'bar',
30+
bar: 123
31+
});
32+
33+
expect(normal.foo).toEqual('bar');
34+
expect(normal.bar).toEqual(123);
35+
});
36+
37+
it('Check one sub-level of normalization.', function () {
38+
var normal = normalizer.normalize({
39+
'foo[value]': 'bar',
40+
'foo[name]': 123
41+
});
42+
43+
expect(normal.foo.value).toEqual('bar');
44+
expect(normal.foo.name).toEqual(123);
45+
});
46+
47+
it('Check two sub-levels of normalization.', function () {
48+
var normal = normalizer.normalize({
49+
'foo[prefix][value]': 'bar',
50+
'foo[prefix][name]': 123
51+
});
52+
53+
expect(normal.foo.prefix.value).toEqual('bar');
54+
expect(normal.foo.prefix.name).toEqual(123);
55+
});
56+
57+
it('Check that numeric types don\'t get converted to array form.', function () {
58+
var normal = normalizer.normalize({
59+
'foo[1][name]': 'bar',
60+
'foo[1][value]': 123,
61+
'foo[1--1]': 321
62+
});
63+
64+
expect(normal.foo['1'].name).toEqual('bar');
65+
expect(normal.foo['1'].value).toEqual(123);
66+
expect(normal.foo['1--1']).toEqual(321);
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)