ArgumentNormalizer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (c) Business Objects 2006. All rights reserved. */
  2. /**
  3. * ArgumentNormalizer names and transforms function arguments based upon a
  4. * set of rules provided by the user.
  5. */
  6. if (typeof bobj == 'undefined') {
  7. bobj = {};
  8. }
  9. bobj.ArgumentNormalizer = function() {
  10. this._rules = [];
  11. };
  12. bobj.ArgumentNormalizer.prototype = {
  13. /**
  14. * Add a rule for naming and transforming arguments.
  15. * When arguments are normalized each rule is applied until a match is
  16. * found. A rule is a set of elements of the following form:
  17. *
  18. * {
  19. * test: [function, null],
  20. * name: [string],
  21. * xform: [function - optional]
  22. * }
  23. *
  24. * There should be one element of the above form for each argument expected
  25. * by the rule. A rule is a match if and only if true is returned by every
  26. * element's test function when passed its corresponding argument. A null
  27. * test function is considered to return true. Rules are tested in the order
  28. * they were added until one matches or there are no more to test.
  29. *
  30. * When a rule matches, it applies the optional xform (transform) functions
  31. * to it arguments and saves the results in a return object with properties
  32. * specified by the names in the rule elements.
  33. *
  34. * Example:
  35. *
  36. * n.addRule({test:isString, name:'description', xform:trim},
  37. * {test:isNumber, name:'id'});
  38. * n.addRule({test:isString, name:'description', xform:trim},
  39. * {test:isString, name:'id', xform:parseInt});
  40. *
  41. * n.normalize(" Blue car", 11); // First rule matches
  42. * -> {description: "Blue car", id: 11}
  43. *
  44. * n.normalize("Green car ", "55"); // Second rule matches
  45. * -> {description: "Green car", id: 55}
  46. *
  47. * Rule elements may be passed as arrays for brevity. The first rule
  48. * from the example above would be added as follows:
  49. *
  50. * n.addRule([isString, 'description', trim], [isNumber, 'id']);
  51. *
  52. * When an element simply names an argument (no test or transform is
  53. * desired), it may be specified as a string. For example:
  54. *
  55. * n.addRule([isString, 'description', trim], 'id');
  56. */
  57. addRule: function() {
  58. this._rules.push(arguments);
  59. },
  60. /**
  61. * Normalize the arguments based upon the rules that have been added
  62. *
  63. * @param arguments Arguments to be normalized
  64. *
  65. * @return An object with a property for each transformed argument or null
  66. */
  67. normalize: function() {
  68. for (var rIdx = 0, nRules = this._rules.length; rIdx < nRules; ++rIdx) {
  69. var rule = this._rules[rIdx];
  70. if (rule.length == arguments.length) {
  71. var normalArgs = {};
  72. for (var aIdx = 0, nArgs = rule.length; aIdx < nArgs; ++aIdx) {
  73. var argVal = arguments[aIdx];
  74. var element = rule[aIdx];
  75. if (bobj.isString(element)) { // No test specified, just name the argument
  76. var argTest = null;
  77. var argName = element;
  78. var argXform = null;
  79. }
  80. else if (bobj.isArray(element)) {
  81. var argTest = element[0];
  82. var argName = element[1];
  83. var argXform = element[2];
  84. }
  85. else {
  86. var argTest = element.test;
  87. var argName = element.name;
  88. var argXform = element.xform;
  89. }
  90. if (!argTest || argTest(argVal)) {
  91. normalArgs[argName] = argXform ? argXform(argVal) : argVal;
  92. if (aIdx+1 == nArgs) { // if no more args to check
  93. return normalArgs; // Rule matched, return normalized args
  94. }
  95. }
  96. else {
  97. break; // Rule didn't match, try the next one
  98. }
  99. }
  100. }
  101. }
  102. return null;
  103. },
  104. /**
  105. * Applies an array of arguments to normalize()
  106. *
  107. * @param argsArray [Array] Array of arguments to normalize
  108. *
  109. * @return Normalized arguments or null
  110. */
  111. normalizeArray: function(argsArray) {
  112. return this.normalize.apply(this, argsArray);
  113. }
  114. };