a zip code crypto-currency system good for red ONLY

concord.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var globToRegExp = require("./globToRegExp").globToRegExp;
  6. function parseType(type) {
  7. var items = type.split("+");
  8. var t = items.shift();
  9. return {
  10. type: t === "*" ? null : t,
  11. features: items
  12. };
  13. }
  14. function isTypeMatched(baseType, testedType) {
  15. if(typeof baseType === "string") baseType = parseType(baseType);
  16. if(typeof testedType === "string") testedType = parseType(testedType);
  17. if(testedType.type && testedType.type !== baseType.type) return false;
  18. return testedType.features.every(function(requiredFeature) {
  19. return baseType.features.indexOf(requiredFeature) >= 0;
  20. });
  21. }
  22. function isResourceTypeMatched(baseType, testedType) {
  23. baseType = baseType.split("/");
  24. testedType = testedType.split("/");
  25. if(baseType.length !== testedType.length) return false;
  26. for(var i = 0; i < baseType.length; i++) {
  27. if(!isTypeMatched(baseType[i], testedType[i]))
  28. return false;
  29. }
  30. return true;
  31. }
  32. function isResourceTypeSupported(context, type) {
  33. return context.supportedResourceTypes && context.supportedResourceTypes.some(function(supportedType) {
  34. return isResourceTypeMatched(supportedType, type);
  35. });
  36. }
  37. function isEnvironment(context, env) {
  38. return context.environments && context.environments.every(function(environment) {
  39. return isTypeMatched(environment, env);
  40. });
  41. }
  42. var globCache = {};
  43. function getGlobRegExp(glob) {
  44. var regExp = globCache[glob] || (globCache[glob] = globToRegExp(glob));
  45. return regExp;
  46. }
  47. function matchGlob(glob, relativePath) {
  48. var regExp = getGlobRegExp(glob);
  49. return regExp.exec(relativePath);
  50. }
  51. function isGlobMatched(glob, relativePath) {
  52. return !!matchGlob(glob, relativePath);
  53. }
  54. function isConditionMatched(context, condition) {
  55. var items = condition.split("|");
  56. return items.some(function testFn(item) {
  57. item = item.trim();
  58. var inverted = /^!/.test(item);
  59. if(inverted) return !testFn(item.substr(1));
  60. if(/^[a-z]+:/.test(item)) {
  61. // match named condition
  62. var match = /^([a-z]+):\s*/.exec(item);
  63. var value = item.substr(match[0].length);
  64. var name = match[1];
  65. switch(name) {
  66. case "referrer":
  67. return isGlobMatched(value, context.referrer);
  68. default:
  69. return false;
  70. }
  71. } else if(item.indexOf("/") >= 0) {
  72. // match supported type
  73. return isResourceTypeSupported(context, item);
  74. } else {
  75. // match environment
  76. return isEnvironment(context, item);
  77. }
  78. });
  79. }
  80. function isKeyMatched(context, key) {
  81. while(true) { //eslint-disable-line
  82. var match = /^\[([^\]]+)\]\s*/.exec(key);
  83. if(!match) return key;
  84. key = key.substr(match[0].length);
  85. var condition = match[1];
  86. if(!isConditionMatched(context, condition)) {
  87. return false;
  88. }
  89. }
  90. }
  91. function getField(context, configuration, field) {
  92. var value;
  93. Object.keys(configuration).forEach(function(key) {
  94. var pureKey = isKeyMatched(context, key);
  95. if(pureKey === field) {
  96. value = configuration[key];
  97. }
  98. });
  99. return value;
  100. }
  101. function getMain(context, configuration) {
  102. return getField(context, configuration, "main");
  103. }
  104. function getExtensions(context, configuration) {
  105. return getField(context, configuration, "extensions");
  106. }
  107. function matchModule(context, configuration, request) {
  108. var modulesField = getField(context, configuration, "modules");
  109. if(!modulesField) return request;
  110. var newRequest = request;
  111. var keys = Object.keys(modulesField);
  112. var iteration = 0;
  113. for(var i = 0; i < keys.length; i++) {
  114. var key = keys[i];
  115. var pureKey = isKeyMatched(context, key);
  116. var match = matchGlob(pureKey, newRequest);
  117. if(match) {
  118. var value = modulesField[key];
  119. if(typeof value !== "string") {
  120. return value;
  121. } else if(/^\(.+\)$/.test(pureKey)) {
  122. newRequest = newRequest.replace(getGlobRegExp(pureKey), value);
  123. } else {
  124. var index = 1;
  125. newRequest = value.replace(/(\/?\*)?\*/g, replaceMatcher);
  126. }
  127. i = -1;
  128. if(iteration++ > keys.length) {
  129. throw new Error("Request '" + request + "' matches recursively");
  130. }
  131. }
  132. }
  133. return newRequest;
  134. function replaceMatcher(find) {
  135. switch(find) {
  136. case "/**":
  137. var m = match[index++];
  138. return m ? "/" + m : "";
  139. case "**":
  140. case "*":
  141. return match[index++];
  142. }
  143. }
  144. }
  145. function matchType(context, configuration, relativePath) {
  146. var typesField = getField(context, configuration, "types");
  147. if(!typesField) return undefined;
  148. var type;
  149. Object.keys(typesField).forEach(function(key) {
  150. var pureKey = isKeyMatched(context, key);
  151. if(isGlobMatched(pureKey, relativePath)) {
  152. var value = typesField[key];
  153. if(!type && /\/\*$/.test(value))
  154. throw new Error("value ('" + value + "') of key '" + key + "' contains '*', but there is no previous value defined");
  155. type = value.replace(/\/\*$/, "/" + type);
  156. }
  157. });
  158. return type;
  159. }
  160. exports.parseType = parseType;
  161. exports.isTypeMatched = isTypeMatched;
  162. exports.isResourceTypeSupported = isResourceTypeSupported;
  163. exports.isEnvironment = isEnvironment;
  164. exports.isGlobMatched = isGlobMatched;
  165. exports.isConditionMatched = isConditionMatched;
  166. exports.isKeyMatched = isKeyMatched;
  167. exports.getField = getField;
  168. exports.getMain = getMain;
  169. exports.getExtensions = getExtensions;
  170. exports.matchModule = matchModule;
  171. exports.matchType = matchType;