Advertisement
renix1

Std.js

Nov 29th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var std = {
  2.   foo: {}
  3. };
  4.  
  5. try {
  6.  
  7.   std.foo.nop = function() {};
  8.   std.foo.nul = function() {
  9.     return null;
  10.   };
  11.   std.foo.fal = function() {
  12.     return false;
  13.   };
  14.   std.foo.tru = function() {
  15.     return true;
  16.   };
  17.   std.foo.id = function(o) {
  18.     return o;
  19.   };
  20.   std.if_null = function(o, def) {
  21.     return std.isNull(o) ? def : o;
  22.   };
  23.  
  24.   std.f_null = function(o, fields, def) {
  25.     if (std.isArray(fields) && !std.isNull(o)) {
  26.       var result = o;
  27.       for (var key in fields) {
  28.         var field_nm = fields[key];
  29.         if (std.isNull(result[field_nm])) {
  30.           return def;
  31.         }
  32.         result = result[field_nm];
  33.       }
  34.       return result;
  35.     } else {
  36.       return !std.isNull(o) && !std.isNull(o[fields]) ? o[fields] : def;
  37.     }
  38.   };
  39.  
  40.   std.f_null2 = function(o, fields, def) {
  41.     var result = {
  42.       path: '',
  43.       success: false,
  44.       val: def
  45.     };
  46.     if (std.isArray(fields)) {
  47.       result.val = o;
  48.       for (var key in fields) {
  49.         var field_nm = fields[key];
  50.         result.path += '/' + field_nm;
  51.         if (std.isNull(result.val[field_nm])) {
  52.           return result;
  53.         }
  54.         result.val = result.val[field_nm];
  55.       }
  56.       result.success = true;
  57.       return result;
  58.     } else {
  59.       if (!std.isNull(o) && !std.isNull(o[fields])) {
  60.         result.path = '/' + fields;
  61.         result.success = true;
  62.         result.val = o[fields];
  63.       }
  64.       return result;
  65.     }
  66.   };
  67.  
  68.   std.isNull = function(o) {
  69.     return o === null || o === undefined;
  70.   };
  71.  
  72.   std.if_empty = function(o, def) {
  73.     return std.isEmpty(o) ? def : o;
  74.   };
  75.  
  76.   std.inBetween = function(c, ini, end) {
  77.     return c >= ini && c <= end;
  78.   };
  79.  
  80.   std.isContainer = function(c) {
  81.     return !std.isNull(c) && (std.isArray(c) || std.isObject(c));
  82.   };
  83.  
  84.   std.isDate = function(d) {
  85.     return (d instanceof Date);
  86.   };
  87.  
  88.   std.isObject = function(item) {
  89.     return (typeof item === "object" && !Array.isArray(item) && !std.isNull(item));
  90.   };
  91.  
  92.   std.isArray = function(item) {
  93.     return Array.isArray(item) === true;
  94.   };
  95.  
  96.   std.isQueue = function(q) {
  97.     return !std.isNull(q) && !std.isNull(q.max) && !std.isNull(q.items);
  98.   };
  99.  
  100.   std.isFunction = function(functionToCheck) {
  101.     var getType = {};
  102.     return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  103.   };
  104.  
  105.   std.isString = function(o) {
  106.     return (typeof o === 'string' || o instanceof String);
  107.   };
  108.  
  109.   std.isElement = function(o) {
  110.     return (
  111.       typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
  112.           (o && typeof o === "object" && o !== null
  113.             && o.nodeType === 1 && typeof o.nodeName==="string")
  114.     );
  115.   };
  116.  
  117.   std.isEmpty = function(o) {
  118.     if (std.isNull(o)) {
  119.       return true;
  120.     }
  121.     if (std.isArray(o)) {
  122.       return o.length === 0;
  123.     }
  124.     if (std.isQueue(o)) {
  125.       return o.items.length === 0;
  126.     }
  127.     if (std.isString(o)) {
  128.       return o.length === 0;
  129.     }
  130.     if (std.isObject(o)) {
  131.       return std.sizeOf(o) === 0;
  132.     }
  133.     return o.toString().length === 0;
  134.   };
  135.  
  136.   std.sizeOf = function(obj) {
  137.     var result = 0;
  138.     if (std.isQueue(obj)) {
  139.       result = obj.items.length;
  140.     } else if (std.isObject(obj)) {
  141.       for (var k in obj) {
  142.         if (obj.hasOwnProperty(k)) {
  143.           ++result;
  144.         }
  145.       }
  146.     } else if (std.isArray(obj)) {
  147.       result = obj.length;
  148.     }
  149.     return result;
  150.   };
  151.  
  152.   std.clamp = function(val, min, max) {
  153.     if ( val < min ) {
  154.       val = min;
  155.     }
  156.     if ( val > max ) {
  157.       val = max;      
  158.     }
  159.     return val;
  160.   };
  161.  
  162.   std.wrap = function(val, min, max) {
  163.     var delta = Math.abs(max - min);
  164.     while ( val < min ) {
  165.       val += delta;
  166.     }
  167.     while ( val >= max ) {
  168.       val -= delta;
  169.     }
  170.     return val;
  171.   };
  172.  
  173.   std.min = function(p1, p2, p3, p4, p5, p6, p7, p8, p9) {
  174.     var result = p1;
  175.     std.for_each( arguments, function(e) {
  176.       if ( e < result ) {
  177.         result = e;
  178.       }
  179.     });
  180.     return result;
  181.   };
  182.  
  183.   std.foo.apply = function(foo, p1, p2, p3, p4, p5) {
  184.     if (!std.isNull(foo)) {
  185.       foo(p1, p2, p3, p4, p5);
  186.     }
  187.   };
  188.  
  189.   std.find = function(container, pred) {
  190.     var element;
  191.     if (std.isContainer(container)) {
  192.       if (std.isFunction(pred)) {
  193.         for (var i in container) {
  194.           element = container[i];
  195.           if (pred(element, i)) {
  196.             return {
  197.               e: element,
  198.               i: i
  199.             };
  200.           }
  201.         }
  202.       } else {
  203.         for (var j in container) {
  204.           element = container[j];
  205.           if (element === pred) {
  206.             return {
  207.               e: element,
  208.               i: j
  209.             };
  210.           }
  211.         }
  212.       }
  213.     }
  214.     return null;
  215.   };
  216.  
  217.   std.find_do = function(container, pred, exec) {
  218.     if (std.isContainer(container)) {
  219.       var it = std.find(container, pred);
  220.       if (it !== null) {
  221.         exec(it);
  222.       }
  223.     }
  224.   };
  225.  
  226.   std.for_each = function(container, fun) {
  227.     var element;
  228.     if (std.isObject(container)) {
  229.       for (var i in container) {
  230.         if (container.hasOwnProperty(i)) {
  231.           element = container[i];
  232.           fun(element, i);
  233.         }
  234.       }
  235.     } else if (std.isContainer(container)) {
  236.       for (var i2 in container) {
  237.         element = container[i2];
  238.         fun(element, i2);
  239.       }
  240.     }
  241.   };
  242.  
  243.   std.for_each_if = function(container, pred, fun) {
  244.     if (std.isContainer(container)) {
  245.       for (var i in container) {
  246.         var element = container[i];
  247.         if (pred(element, i)) {
  248.           fun(element, i);
  249.         }
  250.       }
  251.     }
  252.   };
  253.  
  254.   std.count_if = function(container, pred) {
  255.     var result = 0;
  256.     std.for_each_if(container, pred, function( /* e, i */ ) {
  257.       ++result;
  258.     });
  259.     return result;
  260.   };
  261.  
  262.   std.copy_fields = function(obj, fields) {
  263.     var result = {};
  264.     std.for_each(fields, function(e /* , i */ ) {
  265.       result[e] = obj[e];
  266.     });
  267.     return result;
  268.   };
  269.  
  270.   std.copy = function(container, out) {
  271.     if (std.isNull(out)) {
  272.       var result = std.isArray(container) ? [] : {};
  273.       std.for_each(container, function(e, i) {
  274.         if (e instanceof Date) {
  275.           result[i] = e;
  276.         } else if (std.isObject(e) || std.isArray(e)) {
  277.           result[i] = std.copy(e);
  278.         } else {
  279.           result[i] = e;
  280.         }
  281.       });
  282.       return result;
  283.     } else {
  284.       std.for_each(container, function(e, i) {
  285.         out[i] = e;
  286.       });
  287.     }
  288.   };
  289.  
  290.   std.merge_obj_into = function(source, into) {
  291.     if (std.isObject(source) && std.isObject(into)) {
  292.       std.for_each(source, function(e, i) {
  293.         into[i] = e;
  294.       });
  295.     }
  296.   };
  297.  
  298.   std.ensure = function(map, key, newFoo, onInsert, p1, p2, p3, p4, p5, p6, p7) {
  299.     var result = null;
  300.     if (std.isObject(map)) {
  301.       result = map[key];
  302.       if (std.isNull(result)) {
  303.         if (std.isFunction(newFoo)) {
  304.           result = newFoo(key, p1, p2, p3, p4, p5, p6, p7);
  305.           map[key] = result;
  306.         } else {
  307.           result = newFoo;
  308.           map[key] = result;
  309.         }
  310.         if (onInsert) {
  311.           onInsert(key, result);
  312.         }
  313.       }
  314.     }
  315.     return result;
  316.   };
  317.  
  318.   std.join = function(container, foo, comma, left, right) {
  319.     if (std.isNull(left)) {
  320.       left = '[';
  321.     }
  322.  
  323.     if (std.isNull(comma)) {
  324.       comma = ',';
  325.     }
  326.  
  327.     var result = left;
  328.     if (std.isFunction(foo)) {
  329.       std.for_each(container, function(e, i) {
  330.         if (i > 0) {
  331.           result += comma;
  332.         }
  333.         result += foo(e, i);
  334.       });
  335.     } else {
  336.       std.for_each(container, function(e, i) {
  337.         if (i > 0) {
  338.           result += comma;
  339.         }
  340.         result += std.toString(e);
  341.       });
  342.     }
  343.     if (std.isNull(right)) {
  344.       right = ']';
  345.     }
  346.  
  347.     return result + right;
  348.   };
  349.  
  350. } catch (e) {
  351.   console.debug('Error initializing std : ' + e);
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement