a zip code crypto-currency system good for red ONLY

wrap-for-optimizing.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var Hack = require('./hack');
  2. var Marker = require('../tokenizer/marker');
  3. var Token = require('../tokenizer/token');
  4. var Match = {
  5. ASTERISK: '*',
  6. BACKSLASH: '\\',
  7. BANG: '!',
  8. BANG_SUFFIX_PATTERN: /!\w+$/,
  9. IMPORTANT_TOKEN: '!important',
  10. IMPORTANT_TOKEN_PATTERN: new RegExp('!important$', 'i'),
  11. IMPORTANT_WORD: 'important',
  12. IMPORTANT_WORD_PATTERN: new RegExp('important$', 'i'),
  13. SUFFIX_BANG_PATTERN: /!$/,
  14. UNDERSCORE: '_',
  15. VARIABLE_REFERENCE_PATTERN: /var\(--.+\)$/
  16. };
  17. function wrapAll(properties, includeVariable, skipProperties) {
  18. var wrapped = [];
  19. var single;
  20. var property;
  21. var i;
  22. for (i = properties.length - 1; i >= 0; i--) {
  23. property = properties[i];
  24. if (property[0] != Token.PROPERTY) {
  25. continue;
  26. }
  27. if (!includeVariable && someVariableReferences(property)) {
  28. continue;
  29. }
  30. if (skipProperties && skipProperties.indexOf(property[1][1]) > -1) {
  31. continue;
  32. }
  33. single = wrapSingle(property);
  34. single.all = properties;
  35. single.position = i;
  36. wrapped.unshift(single);
  37. }
  38. return wrapped;
  39. }
  40. function someVariableReferences(property) {
  41. var i, l;
  42. var value;
  43. // skipping `property` and property name tokens
  44. for (i = 2, l = property.length; i < l; i++) {
  45. value = property[i];
  46. if (value[0] != Token.PROPERTY_VALUE) {
  47. continue;
  48. }
  49. if (isVariableReference(value[1])) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. function isVariableReference(value) {
  56. return Match.VARIABLE_REFERENCE_PATTERN.test(value);
  57. }
  58. function isMultiplex(property) {
  59. var value;
  60. var i, l;
  61. for (i = 3, l = property.length; i < l; i++) {
  62. value = property[i];
  63. if (value[0] == Token.PROPERTY_VALUE && (value[1] == Marker.COMMA || value[1] == Marker.FORWARD_SLASH)) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. function hackFrom(property) {
  70. var match = false;
  71. var name = property[1][1];
  72. var lastValue = property[property.length - 1];
  73. if (name[0] == Match.UNDERSCORE) {
  74. match = [Hack.UNDERSCORE];
  75. } else if (name[0] == Match.ASTERISK) {
  76. match = [Hack.ASTERISK];
  77. } else if (lastValue[1][0] == Match.BANG && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN)) {
  78. match = [Hack.BANG];
  79. } else if (lastValue[1].indexOf(Match.BANG) > 0 && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN) && Match.BANG_SUFFIX_PATTERN.test(lastValue[1])) {
  80. match = [Hack.BANG];
  81. } else if (lastValue[1].indexOf(Match.BACKSLASH) > 0 && lastValue[1].indexOf(Match.BACKSLASH) == lastValue[1].length - Match.BACKSLASH.length - 1) {
  82. match = [Hack.BACKSLASH, lastValue[1].substring(lastValue[1].indexOf(Match.BACKSLASH) + 1)];
  83. } else if (lastValue[1].indexOf(Match.BACKSLASH) === 0 && lastValue[1].length == 2) {
  84. match = [Hack.BACKSLASH, lastValue[1].substring(1)];
  85. }
  86. return match;
  87. }
  88. function isImportant(property) {
  89. if (property.length < 3)
  90. return false;
  91. var lastValue = property[property.length - 1];
  92. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  93. return true;
  94. } else if (Match.IMPORTANT_WORD_PATTERN.test(lastValue[1]) && Match.SUFFIX_BANG_PATTERN.test(property[property.length - 2][1])) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. function stripImportant(property) {
  100. var lastValue = property[property.length - 1];
  101. var oneButLastValue = property[property.length - 2];
  102. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  103. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_TOKEN_PATTERN, '');
  104. } else {
  105. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_WORD_PATTERN, '');
  106. oneButLastValue[1] = oneButLastValue[1].replace(Match.SUFFIX_BANG_PATTERN, '');
  107. }
  108. if (lastValue[1].length === 0) {
  109. property.pop();
  110. }
  111. if (oneButLastValue[1].length === 0) {
  112. property.pop();
  113. }
  114. }
  115. function stripPrefixHack(property) {
  116. property[1][1] = property[1][1].substring(1);
  117. }
  118. function stripSuffixHack(property, hackFrom) {
  119. var lastValue = property[property.length - 1];
  120. lastValue[1] = lastValue[1]
  121. .substring(0, lastValue[1].indexOf(hackFrom[0] == Hack.BACKSLASH ? Match.BACKSLASH : Match.BANG))
  122. .trim();
  123. if (lastValue[1].length === 0) {
  124. property.pop();
  125. }
  126. }
  127. function wrapSingle(property) {
  128. var importantProperty = isImportant(property);
  129. if (importantProperty) {
  130. stripImportant(property);
  131. }
  132. var whichHack = hackFrom(property);
  133. if (whichHack[0] == Hack.ASTERISK || whichHack[0] == Hack.UNDERSCORE) {
  134. stripPrefixHack(property);
  135. } else if (whichHack[0] == Hack.BACKSLASH || whichHack[0] == Hack.BANG) {
  136. stripSuffixHack(property, whichHack);
  137. }
  138. return {
  139. block: property[2] && property[2][0] == Token.PROPERTY_BLOCK,
  140. components: [],
  141. dirty: false,
  142. hack: whichHack,
  143. important: importantProperty,
  144. name: property[1][1],
  145. multiplex: property.length > 3 ? isMultiplex(property) : false,
  146. position: 0,
  147. shorthand: false,
  148. unused: false,
  149. value: property.slice(2)
  150. };
  151. }
  152. module.exports = {
  153. all: wrapAll,
  154. single: wrapSingle
  155. };