a zip code crypto-currency system good for red ONLY

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var createInnerCallback = require("./createInnerCallback");
  6. function startsWith(string, searchString) {
  7. var stringLength = string.length;
  8. var searchLength = searchString.length;
  9. // early out if the search length is greater than the search string
  10. if(searchLength > stringLength) {
  11. return false;
  12. }
  13. var index = -1;
  14. while(++index < searchLength) {
  15. if(string.charCodeAt(index) !== searchString.charCodeAt(index)) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. function AliasPlugin(source, options, target) {
  22. this.source = source;
  23. this.name = options.name;
  24. this.alias = options.alias;
  25. this.onlyModule = options.onlyModule;
  26. this.target = target;
  27. }
  28. module.exports = AliasPlugin;
  29. AliasPlugin.prototype.apply = function(resolver) {
  30. var target = this.target;
  31. var name = this.name;
  32. var alias = this.alias;
  33. var onlyModule = this.onlyModule;
  34. resolver.plugin(this.source, function(request, callback) {
  35. var innerRequest = request.request;
  36. if(!innerRequest) return callback();
  37. if(innerRequest === name || (!onlyModule && startsWith(innerRequest, name + "/"))) {
  38. if(innerRequest !== alias && !startsWith(innerRequest, alias + "/")) {
  39. var newRequestStr = alias + innerRequest.substr(name.length);
  40. var obj = Object.assign({}, request, {
  41. request: newRequestStr
  42. });
  43. return resolver.doResolve(target, obj, "aliased with mapping '" + name + "': '" + alias + "' to '" + newRequestStr + "'", createInnerCallback(function(err, result) {
  44. if(arguments.length > 0) return callback(err, result);
  45. // don't allow other aliasing or raw request
  46. callback(null, null);
  47. }, callback));
  48. }
  49. }
  50. return callback();
  51. });
  52. };