a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. /*
  6. <rules>: <rule>
  7. <rules>: [<rule>]
  8. <rule>: {
  9. resource: {
  10. test: <condition>,
  11. include: <condition>,
  12. exclude: <condition>,
  13. },
  14. resource: <condition>, -> resource.test
  15. test: <condition>, -> resource.test
  16. include: <condition>, -> resource.include
  17. exclude: <condition>, -> resource.exclude
  18. resourceQuery: <condition>,
  19. compiler: <condition>,
  20. issuer: <condition>,
  21. use: "loader", -> use[0].loader
  22. loader: <>, -> use[0].loader
  23. loaders: <>, -> use
  24. options: {}, -> use[0].options,
  25. query: {}, -> options
  26. parser: {},
  27. use: [
  28. "loader" -> use[x].loader
  29. ],
  30. use: [
  31. {
  32. loader: "loader",
  33. options: {}
  34. }
  35. ],
  36. rules: [
  37. <rule>
  38. ],
  39. oneOf: [
  40. <rule>
  41. ]
  42. }
  43. <condition>: /regExp/
  44. <condition>: function(arg) {}
  45. <condition>: "starting"
  46. <condition>: [<condition>] // or
  47. <condition>: { and: [<condition>] }
  48. <condition>: { or: [<condition>] }
  49. <condition>: { not: [<condition>] }
  50. <condition>: { test: <condition>, include: <condition>, exclude: <condition> }
  51. normalized:
  52. {
  53. resource: function(),
  54. resourceQuery: function(),
  55. compiler: function(),
  56. issuer: function(),
  57. use: [
  58. {
  59. loader: string,
  60. options: string,
  61. <any>: <any>
  62. }
  63. ],
  64. rules: [<rule>],
  65. oneOf: [<rule>],
  66. <any>: <any>,
  67. }
  68. */
  69. "use strict";
  70. module.exports = class RuleSet {
  71. constructor(rules) {
  72. this.references = Object.create(null);
  73. this.rules = RuleSet.normalizeRules(rules, this.references, "ref-");
  74. }
  75. static normalizeRules(rules, refs, ident) {
  76. if(Array.isArray(rules)) {
  77. return rules.map((rule, idx) => {
  78. return RuleSet.normalizeRule(rule, refs, `${ident}-${idx}`);
  79. });
  80. } else if(rules) {
  81. return [RuleSet.normalizeRule(rules, refs, ident)];
  82. } else {
  83. return [];
  84. }
  85. }
  86. static normalizeRule(rule, refs, ident) {
  87. if(typeof rule === "string")
  88. return {
  89. use: [{
  90. loader: rule
  91. }]
  92. };
  93. if(!rule)
  94. throw new Error("Unexcepted null when object was expected as rule");
  95. if(typeof rule !== "object")
  96. throw new Error("Unexcepted " + typeof rule + " when object was expected as rule (" + rule + ")");
  97. const newRule = {};
  98. let useSource;
  99. let resourceSource;
  100. let condition;
  101. if(rule.test || rule.include || rule.exclude) {
  102. checkResourceSource("test + include + exclude");
  103. condition = {
  104. test: rule.test,
  105. include: rule.include,
  106. exclude: rule.exclude
  107. };
  108. try {
  109. newRule.resource = RuleSet.normalizeCondition(condition);
  110. } catch(error) {
  111. throw new Error(RuleSet.buildErrorMessage(condition, error));
  112. }
  113. }
  114. if(rule.resource) {
  115. checkResourceSource("resource");
  116. try {
  117. newRule.resource = RuleSet.normalizeCondition(rule.resource);
  118. } catch(error) {
  119. throw new Error(RuleSet.buildErrorMessage(rule.resource, error));
  120. }
  121. }
  122. if(rule.resourceQuery) {
  123. try {
  124. newRule.resourceQuery = RuleSet.normalizeCondition(rule.resourceQuery);
  125. } catch(error) {
  126. throw new Error(RuleSet.buildErrorMessage(rule.resourceQuery, error));
  127. }
  128. }
  129. if(rule.compiler) {
  130. try {
  131. newRule.compiler = RuleSet.normalizeCondition(rule.compiler);
  132. } catch(error) {
  133. throw new Error(RuleSet.buildErrorMessage(rule.compiler, error));
  134. }
  135. }
  136. if(rule.issuer) {
  137. try {
  138. newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
  139. } catch(error) {
  140. throw new Error(RuleSet.buildErrorMessage(rule.issuer, error));
  141. }
  142. }
  143. if(rule.loader && rule.loaders)
  144. throw new Error(RuleSet.buildErrorMessage(rule, new Error("Provided loader and loaders for rule (use only one of them)")));
  145. const loader = rule.loaders || rule.loader;
  146. if(typeof loader === "string" && !rule.options && !rule.query) {
  147. checkUseSource("loader");
  148. newRule.use = RuleSet.normalizeUse(loader.split("!"), ident);
  149. } else if(typeof loader === "string" && (rule.options || rule.query)) {
  150. checkUseSource("loader + options/query");
  151. newRule.use = RuleSet.normalizeUse({
  152. loader: loader,
  153. options: rule.options,
  154. query: rule.query
  155. }, ident);
  156. } else if(loader && (rule.options || rule.query)) {
  157. throw new Error(RuleSet.buildErrorMessage(rule, new Error("options/query cannot be used with loaders (use options for each array item)")));
  158. } else if(loader) {
  159. checkUseSource("loaders");
  160. newRule.use = RuleSet.normalizeUse(loader, ident);
  161. } else if(rule.options || rule.query) {
  162. throw new Error(RuleSet.buildErrorMessage(rule, new Error("options/query provided without loader (use loader + options)")));
  163. }
  164. if(rule.use) {
  165. checkUseSource("use");
  166. newRule.use = RuleSet.normalizeUse(rule.use, ident);
  167. }
  168. if(rule.rules)
  169. newRule.rules = RuleSet.normalizeRules(rule.rules, refs, `${ident}-rules`);
  170. if(rule.oneOf)
  171. newRule.oneOf = RuleSet.normalizeRules(rule.oneOf, refs, `${ident}-oneOf`);
  172. const keys = Object.keys(rule).filter((key) => {
  173. return ["resource", "resourceQuery", "compiler", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
  174. });
  175. keys.forEach((key) => {
  176. newRule[key] = rule[key];
  177. });
  178. function checkUseSource(newSource) {
  179. if(useSource && useSource !== newSource)
  180. throw new Error(RuleSet.buildErrorMessage(rule, new Error("Rule can only have one result source (provided " + newSource + " and " + useSource + ")")));
  181. useSource = newSource;
  182. }
  183. function checkResourceSource(newSource) {
  184. if(resourceSource && resourceSource !== newSource)
  185. throw new Error(RuleSet.buildErrorMessage(rule, new Error("Rule can only have one resource source (provided " + newSource + " and " + resourceSource + ")")));
  186. resourceSource = newSource;
  187. }
  188. if(Array.isArray(newRule.use)) {
  189. newRule.use.forEach((item) => {
  190. if(item.ident) {
  191. refs[item.ident] = item.options;
  192. }
  193. });
  194. }
  195. return newRule;
  196. }
  197. static buildErrorMessage(condition, error) {
  198. const conditionAsText = JSON.stringify(condition, (key, value) => {
  199. return value === undefined ? "undefined" : value;
  200. }, 2);
  201. return error.message + " in " + conditionAsText;
  202. }
  203. static normalizeUse(use, ident) {
  204. if(Array.isArray(use)) {
  205. return use
  206. .map((item, idx) => RuleSet.normalizeUse(item, `${ident}-${idx}`))
  207. .reduce((arr, items) => arr.concat(items), []);
  208. }
  209. return [RuleSet.normalizeUseItem(use, ident)];
  210. }
  211. static normalizeUseItemFunction(use, data) {
  212. const result = use(data);
  213. if(typeof result === "string") {
  214. return RuleSet.normalizeUseItem(result);
  215. }
  216. return result;
  217. }
  218. static normalizeUseItemString(useItemString) {
  219. const idx = useItemString.indexOf("?");
  220. if(idx >= 0) {
  221. return {
  222. loader: useItemString.substr(0, idx),
  223. options: useItemString.substr(idx + 1)
  224. };
  225. }
  226. return {
  227. loader: useItemString
  228. };
  229. }
  230. static normalizeUseItem(item, ident) {
  231. if(typeof item === "function")
  232. return item;
  233. if(typeof item === "string") {
  234. return RuleSet.normalizeUseItemString(item);
  235. }
  236. const newItem = {};
  237. if(item.options && item.query)
  238. throw new Error("Provided options and query in use");
  239. if(!item.loader)
  240. throw new Error("No loader specified");
  241. newItem.options = item.options || item.query;
  242. if(typeof newItem.options === "object" && newItem.options) {
  243. if(newItem.options.ident)
  244. newItem.ident = newItem.options.ident;
  245. else
  246. newItem.ident = ident;
  247. }
  248. const keys = Object.keys(item).filter(function(key) {
  249. return ["options", "query"].indexOf(key) < 0;
  250. });
  251. keys.forEach(function(key) {
  252. newItem[key] = item[key];
  253. });
  254. return newItem;
  255. }
  256. static normalizeCondition(condition) {
  257. if(!condition)
  258. throw new Error("Expected condition but got falsy value");
  259. if(typeof condition === "string") {
  260. return str => str.indexOf(condition) === 0;
  261. }
  262. if(typeof condition === "function") {
  263. return condition;
  264. }
  265. if(condition instanceof RegExp) {
  266. return condition.test.bind(condition);
  267. }
  268. if(Array.isArray(condition)) {
  269. const items = condition.map(c => RuleSet.normalizeCondition(c));
  270. return orMatcher(items);
  271. }
  272. if(typeof condition !== "object")
  273. throw Error("Unexcepted " + typeof condition + " when condition was expected (" + condition + ")");
  274. const matchers = [];
  275. Object.keys(condition).forEach(key => {
  276. const value = condition[key];
  277. switch(key) {
  278. case "or":
  279. case "include":
  280. case "test":
  281. if(value)
  282. matchers.push(RuleSet.normalizeCondition(value));
  283. break;
  284. case "and":
  285. if(value) {
  286. const items = value.map(c => RuleSet.normalizeCondition(c));
  287. matchers.push(andMatcher(items));
  288. }
  289. break;
  290. case "not":
  291. case "exclude":
  292. if(value) {
  293. const matcher = RuleSet.normalizeCondition(value);
  294. matchers.push(notMatcher(matcher));
  295. }
  296. break;
  297. default:
  298. throw new Error("Unexcepted property " + key + " in condition");
  299. }
  300. });
  301. if(matchers.length === 0)
  302. throw new Error("Excepted condition but got " + condition);
  303. if(matchers.length === 1)
  304. return matchers[0];
  305. return andMatcher(matchers);
  306. }
  307. exec(data) {
  308. const result = [];
  309. this._run(data, {
  310. rules: this.rules
  311. }, result);
  312. return result;
  313. }
  314. _run(data, rule, result) {
  315. // test conditions
  316. if(rule.resource && !data.resource)
  317. return false;
  318. if(rule.resourceQuery && !data.resourceQuery)
  319. return false;
  320. if(rule.compiler && !data.compiler)
  321. return false;
  322. if(rule.issuer && !data.issuer)
  323. return false;
  324. if(rule.resource && !rule.resource(data.resource))
  325. return false;
  326. if(data.issuer && rule.issuer && !rule.issuer(data.issuer))
  327. return false;
  328. if(data.resourceQuery && rule.resourceQuery && !rule.resourceQuery(data.resourceQuery))
  329. return false;
  330. if(data.compiler && rule.compiler && !rule.compiler(data.compiler))
  331. return false;
  332. // apply
  333. const keys = Object.keys(rule).filter((key) => {
  334. return ["resource", "resourceQuery", "compiler", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
  335. });
  336. keys.forEach((key) => {
  337. result.push({
  338. type: key,
  339. value: rule[key]
  340. });
  341. });
  342. if(rule.use) {
  343. rule.use.forEach((use) => {
  344. result.push({
  345. type: "use",
  346. value: typeof use === "function" ? RuleSet.normalizeUseItemFunction(use, data) : use,
  347. enforce: rule.enforce
  348. });
  349. });
  350. }
  351. if(rule.rules) {
  352. for(let i = 0; i < rule.rules.length; i++) {
  353. this._run(data, rule.rules[i], result);
  354. }
  355. }
  356. if(rule.oneOf) {
  357. for(let i = 0; i < rule.oneOf.length; i++) {
  358. if(this._run(data, rule.oneOf[i], result))
  359. break;
  360. }
  361. }
  362. return true;
  363. }
  364. findOptionsByIdent(ident) {
  365. const options = this.references[ident];
  366. if(!options) throw new Error("Can't find options with ident '" + ident + "'");
  367. return options;
  368. }
  369. };
  370. function notMatcher(matcher) {
  371. return function(str) {
  372. return !matcher(str);
  373. };
  374. }
  375. function orMatcher(items) {
  376. return function(str) {
  377. for(let i = 0; i < items.length; i++) {
  378. if(items[i](str))
  379. return true;
  380. }
  381. return false;
  382. };
  383. }
  384. function andMatcher(items) {
  385. return function(str) {
  386. for(let i = 0; i < items.length; i++) {
  387. if(!items[i](str))
  388. return false;
  389. }
  390. return true;
  391. };
  392. }