Advertisement
Guest User

Untitled

a guest
May 5th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /*global _ */
  2.  
  3. /*jslint
  4. browser : true, continue : true,
  5. devel : true, indent : 2, maxerr : 50,
  6. newcap : true, nomen : true, plusplus : true,
  7. regexp : true, sloppy : true, vars :false,
  8. white : true
  9. */
  10.  
  11. var _ = require('underscore');
  12. var my_functions = {
  13. fail: function(thing) { throw new Error(thing); },
  14. existy: function(x) { return x != null; },
  15. truthy: function(x) { return (x !== false) && my_functions.existy(x); },
  16.  
  17. doWhen: function(cond, fun) {
  18. if(my_functions.truthy(cond)) { return fun(); }
  19. return undefined;
  20. },
  21.  
  22. invoker: function(name, method) {
  23. return function(target /* */) {
  24. if(!my_functions.existy(target)) { my_functions.fail('Must provide a target'); }
  25.  
  26. var
  27. args = _.rest(arguments),
  28. targetMethod = target[name];
  29.  
  30. return my_functions.doWhen((my_functions.existy(targetMethod) && method === targetMethod), function() {
  31. return targetMethod.apply(target, args);
  32. });
  33. };
  34. },
  35.  
  36. makeUniqueStringFunction: function(start) {
  37. var counter = start;
  38. return function(prefix) {
  39. return [prefix, counter++].join('');
  40. };
  41. },
  42.  
  43. checker: function(/* */) {
  44. var validators = _.toArray(arguments);
  45.  
  46. return function(obj) {
  47. return _.reduce(validators, function(errs, check) {
  48. if(check(obj)) { return errs; }
  49. return _.chain(errs).push(check.message).value();
  50. }, []);
  51. };
  52. },
  53.  
  54. validator: function(message, fun) {
  55. var f = function(/* */) {
  56. return fun.apply(fun, arguments);
  57. };
  58.  
  59. f.message = message;
  60. return f;
  61. }
  62. };
  63.  
  64. module.exports = my_functions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement