conditions-data-normalizer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'underscore'
  8. ], function ($, _) {
  9. 'use strict';
  10. /**
  11. * @constructor
  12. */
  13. var ConditionsDataNormalizer = function () {
  14. this.patterns = {
  15. validate: /^[a-z0-9_.-][a-z0-9_.-]*(?:\[(?:\d*|[a-z0-9_.-]+)\])*$/i,
  16. key: /[a-z0-9_.-]+|(?=\[\])/gi,
  17. push: /^$/,
  18. fixed: /^\d+$/,
  19. named: /^[a-z0-9_.-]+$/i
  20. };
  21. };
  22. ConditionsDataNormalizer.prototype = {
  23. /**
  24. * Will convert an object:
  25. * {
  26. * "foo[bar][1][baz]": 123,
  27. * "foo[bar][1][blah]": 321
  28. * "foo[bar][1--1][ah]": 456
  29. * }
  30. *
  31. * to
  32. * {
  33. * "foo": {
  34. * "bar": {
  35. * "1": {
  36. * "baz": 123,
  37. * "blah": 321
  38. * },
  39. * "1--1": {
  40. * "ah": 456
  41. * }
  42. * }
  43. * }
  44. * }
  45. */
  46. normalize: function normalize(value) {
  47. var el, _this = this;
  48. this.pushes = {};
  49. this.data = {};
  50. _.each(value, function (e, i) {
  51. el = {};
  52. el[i] = e;
  53. _this._addPair({
  54. name: i,
  55. value: e
  56. });
  57. });
  58. return this.data;
  59. },
  60. /**
  61. * @param {Object} base
  62. * @param {String} key
  63. * @param {String} value
  64. * @return {Object}
  65. * @private
  66. */
  67. _build: function build(base, key, value) {
  68. base[key] = value;
  69. return base;
  70. },
  71. /**
  72. * @param {Object} root
  73. * @param {String} value
  74. * @return {*}
  75. * @private
  76. */
  77. _makeObject: function makeObject(root, value) {
  78. var keys = root.match(this.patterns.key),
  79. k, idx; // nest, nest, ..., nest
  80. while ((k = keys.pop()) !== undefined) {
  81. // foo[]
  82. if (this.patterns.push.test(k)) {
  83. idx = this._incrementPush(root.replace(/\[\]$/, ''));
  84. value = this._build([], idx, value);
  85. } // foo[n]
  86. else if (this.patterns.fixed.test(k)) {
  87. value = this._build({}, k, value);
  88. } // foo; foo[bar]
  89. else if (this.patterns.named.test(k)) {
  90. value = this._build({}, k, value);
  91. }
  92. }
  93. return value;
  94. },
  95. /**
  96. * @param {String} key
  97. * @return {Number}
  98. * @private
  99. */
  100. _incrementPush: function incrementPush(key) {
  101. if (this.pushes[key] === undefined) {
  102. this.pushes[key] = 0;
  103. }
  104. return this.pushes[key]++;
  105. },
  106. /**
  107. * @param {Object} pair
  108. * @return {Object}
  109. * @private
  110. */
  111. _addPair: function addPair(pair) {
  112. var obj = this._makeObject(pair.name, pair.value);
  113. if (!this.patterns.validate.test(pair.name)) {
  114. return this;
  115. }
  116. this.data = $.extend(true, this.data, obj);
  117. return this;
  118. }
  119. };
  120. return ConditionsDataNormalizer;
  121. });