a zip code crypto-currency system good for red ONLY

format.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var override = require('../utils/override');
  2. var Breaks = {
  3. AfterAtRule: 'afterAtRule',
  4. AfterBlockBegins: 'afterBlockBegins',
  5. AfterBlockEnds: 'afterBlockEnds',
  6. AfterComment: 'afterComment',
  7. AfterProperty: 'afterProperty',
  8. AfterRuleBegins: 'afterRuleBegins',
  9. AfterRuleEnds: 'afterRuleEnds',
  10. BeforeBlockEnds: 'beforeBlockEnds',
  11. BetweenSelectors: 'betweenSelectors'
  12. };
  13. var IndentWith = {
  14. Space: ' ',
  15. Tab: '\t'
  16. };
  17. var Spaces = {
  18. AroundSelectorRelation: 'aroundSelectorRelation',
  19. BeforeBlockBegins: 'beforeBlockBegins',
  20. BeforeValue: 'beforeValue'
  21. };
  22. var DEFAULTS = {
  23. breaks: breaks(false),
  24. indentBy: 0,
  25. indentWith: IndentWith.Space,
  26. spaces: spaces(false),
  27. wrapAt: false
  28. };
  29. var BEAUTIFY_ALIAS = 'beautify';
  30. var KEEP_BREAKS_ALIAS = 'keep-breaks';
  31. var OPTION_SEPARATOR = ';';
  32. var OPTION_NAME_VALUE_SEPARATOR = ':';
  33. var HASH_VALUES_OPTION_SEPARATOR = ',';
  34. var HASH_VALUES_NAME_VALUE_SEPARATOR = '=';
  35. var FALSE_KEYWORD_1 = 'false';
  36. var FALSE_KEYWORD_2 = 'off';
  37. var TRUE_KEYWORD_1 = 'true';
  38. var TRUE_KEYWORD_2 = 'on';
  39. function breaks(value) {
  40. var breakOptions = {};
  41. breakOptions[Breaks.AfterAtRule] = value;
  42. breakOptions[Breaks.AfterBlockBegins] = value;
  43. breakOptions[Breaks.AfterBlockEnds] = value;
  44. breakOptions[Breaks.AfterComment] = value;
  45. breakOptions[Breaks.AfterProperty] = value;
  46. breakOptions[Breaks.AfterRuleBegins] = value;
  47. breakOptions[Breaks.AfterRuleEnds] = value;
  48. breakOptions[Breaks.BeforeBlockEnds] = value;
  49. breakOptions[Breaks.BetweenSelectors] = value;
  50. return breakOptions;
  51. }
  52. function spaces(value) {
  53. var spaceOptions = {};
  54. spaceOptions[Spaces.AroundSelectorRelation] = value;
  55. spaceOptions[Spaces.BeforeBlockBegins] = value;
  56. spaceOptions[Spaces.BeforeValue] = value;
  57. return spaceOptions;
  58. }
  59. function formatFrom(source) {
  60. if (source === undefined || source === false) {
  61. return false;
  62. }
  63. if (typeof source == 'object' && 'indentBy' in source) {
  64. source = override(source, { indentBy: parseInt(source.indentBy) });
  65. }
  66. if (typeof source == 'object' && 'indentWith' in source) {
  67. source = override(source, { indentWith: mapIndentWith(source.indentWith) });
  68. }
  69. if (typeof source == 'object') {
  70. return override(DEFAULTS, source);
  71. }
  72. if (typeof source == 'object') {
  73. return override(DEFAULTS, source);
  74. }
  75. if (typeof source == 'string' && source == BEAUTIFY_ALIAS) {
  76. return override(DEFAULTS, {
  77. breaks: breaks(true),
  78. indentBy: 2,
  79. spaces: spaces(true)
  80. });
  81. }
  82. if (typeof source == 'string' && source == KEEP_BREAKS_ALIAS) {
  83. return override(DEFAULTS, {
  84. breaks: {
  85. afterAtRule: true,
  86. afterBlockBegins: true,
  87. afterBlockEnds: true,
  88. afterComment: true,
  89. afterRuleEnds: true,
  90. beforeBlockEnds: true
  91. }
  92. });
  93. }
  94. if (typeof source == 'string') {
  95. return override(DEFAULTS, toHash(source));
  96. }
  97. return DEFAULTS;
  98. }
  99. function toHash(string) {
  100. return string
  101. .split(OPTION_SEPARATOR)
  102. .reduce(function (accumulator, directive) {
  103. var parts = directive.split(OPTION_NAME_VALUE_SEPARATOR);
  104. var name = parts[0];
  105. var value = parts[1];
  106. if (name == 'breaks' || name == 'spaces') {
  107. accumulator[name] = hashValuesToHash(value);
  108. } else if (name == 'indentBy' || name == 'wrapAt') {
  109. accumulator[name] = parseInt(value);
  110. } else if (name == 'indentWith') {
  111. accumulator[name] = mapIndentWith(value);
  112. }
  113. return accumulator;
  114. }, {});
  115. }
  116. function hashValuesToHash(string) {
  117. return string
  118. .split(HASH_VALUES_OPTION_SEPARATOR)
  119. .reduce(function (accumulator, directive) {
  120. var parts = directive.split(HASH_VALUES_NAME_VALUE_SEPARATOR);
  121. var name = parts[0];
  122. var value = parts[1];
  123. accumulator[name] = normalizeValue(value);
  124. return accumulator;
  125. }, {});
  126. }
  127. function normalizeValue(value) {
  128. switch (value) {
  129. case FALSE_KEYWORD_1:
  130. case FALSE_KEYWORD_2:
  131. return false;
  132. case TRUE_KEYWORD_1:
  133. case TRUE_KEYWORD_2:
  134. return true;
  135. default:
  136. return value;
  137. }
  138. }
  139. function mapIndentWith(value) {
  140. switch (value) {
  141. case 'space':
  142. return IndentWith.Space;
  143. case 'tab':
  144. return IndentWith.Tab;
  145. default:
  146. return value;
  147. }
  148. }
  149. module.exports = {
  150. Breaks: Breaks,
  151. Spaces: Spaces,
  152. formatFrom: formatFrom
  153. };