Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var std = {
- foo: {}
- };
- try {
- std.foo.nop = function() {};
- std.foo.nul = function() {
- return null;
- };
- std.foo.fal = function() {
- return false;
- };
- std.foo.tru = function() {
- return true;
- };
- std.foo.id = function(o) {
- return o;
- };
- std.if_null = function(o, def) {
- return std.isNull(o) ? def : o;
- };
- std.f_null = function(o, fields, def) {
- if (std.isArray(fields) && !std.isNull(o)) {
- var result = o;
- for (var key in fields) {
- var field_nm = fields[key];
- if (std.isNull(result[field_nm])) {
- return def;
- }
- result = result[field_nm];
- }
- return result;
- } else {
- return !std.isNull(o) && !std.isNull(o[fields]) ? o[fields] : def;
- }
- };
- std.f_null2 = function(o, fields, def) {
- var result = {
- path: '',
- success: false,
- val: def
- };
- if (std.isArray(fields)) {
- result.val = o;
- for (var key in fields) {
- var field_nm = fields[key];
- result.path += '/' + field_nm;
- if (std.isNull(result.val[field_nm])) {
- return result;
- }
- result.val = result.val[field_nm];
- }
- result.success = true;
- return result;
- } else {
- if (!std.isNull(o) && !std.isNull(o[fields])) {
- result.path = '/' + fields;
- result.success = true;
- result.val = o[fields];
- }
- return result;
- }
- };
- std.isNull = function(o) {
- return o === null || o === undefined;
- };
- std.if_empty = function(o, def) {
- return std.isEmpty(o) ? def : o;
- };
- std.inBetween = function(c, ini, end) {
- return c >= ini && c <= end;
- };
- std.isContainer = function(c) {
- return !std.isNull(c) && (std.isArray(c) || std.isObject(c));
- };
- std.isDate = function(d) {
- return (d instanceof Date);
- };
- std.isObject = function(item) {
- return (typeof item === "object" && !Array.isArray(item) && !std.isNull(item));
- };
- std.isArray = function(item) {
- return Array.isArray(item) === true;
- };
- std.isQueue = function(q) {
- return !std.isNull(q) && !std.isNull(q.max) && !std.isNull(q.items);
- };
- std.isFunction = function(functionToCheck) {
- var getType = {};
- return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
- };
- std.isString = function(o) {
- return (typeof o === 'string' || o instanceof String);
- };
- std.isElement = function(o) {
- return (
- typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
- (o && typeof o === "object" && o !== null
- && o.nodeType === 1 && typeof o.nodeName==="string")
- );
- };
- std.isEmpty = function(o) {
- if (std.isNull(o)) {
- return true;
- }
- if (std.isArray(o)) {
- return o.length === 0;
- }
- if (std.isQueue(o)) {
- return o.items.length === 0;
- }
- if (std.isString(o)) {
- return o.length === 0;
- }
- if (std.isObject(o)) {
- return std.sizeOf(o) === 0;
- }
- return o.toString().length === 0;
- };
- std.sizeOf = function(obj) {
- var result = 0;
- if (std.isQueue(obj)) {
- result = obj.items.length;
- } else if (std.isObject(obj)) {
- for (var k in obj) {
- if (obj.hasOwnProperty(k)) {
- ++result;
- }
- }
- } else if (std.isArray(obj)) {
- result = obj.length;
- }
- return result;
- };
- std.clamp = function(val, min, max) {
- if ( val < min ) {
- val = min;
- }
- if ( val > max ) {
- val = max;
- }
- return val;
- };
- std.wrap = function(val, min, max) {
- var delta = Math.abs(max - min);
- while ( val < min ) {
- val += delta;
- }
- while ( val >= max ) {
- val -= delta;
- }
- return val;
- };
- std.min = function(p1, p2, p3, p4, p5, p6, p7, p8, p9) {
- var result = p1;
- std.for_each( arguments, function(e) {
- if ( e < result ) {
- result = e;
- }
- });
- return result;
- };
- std.foo.apply = function(foo, p1, p2, p3, p4, p5) {
- if (!std.isNull(foo)) {
- foo(p1, p2, p3, p4, p5);
- }
- };
- std.find = function(container, pred) {
- var element;
- if (std.isContainer(container)) {
- if (std.isFunction(pred)) {
- for (var i in container) {
- element = container[i];
- if (pred(element, i)) {
- return {
- e: element,
- i: i
- };
- }
- }
- } else {
- for (var j in container) {
- element = container[j];
- if (element === pred) {
- return {
- e: element,
- i: j
- };
- }
- }
- }
- }
- return null;
- };
- std.find_do = function(container, pred, exec) {
- if (std.isContainer(container)) {
- var it = std.find(container, pred);
- if (it !== null) {
- exec(it);
- }
- }
- };
- std.for_each = function(container, fun) {
- var element;
- if (std.isObject(container)) {
- for (var i in container) {
- if (container.hasOwnProperty(i)) {
- element = container[i];
- fun(element, i);
- }
- }
- } else if (std.isContainer(container)) {
- for (var i2 in container) {
- element = container[i2];
- fun(element, i2);
- }
- }
- };
- std.for_each_if = function(container, pred, fun) {
- if (std.isContainer(container)) {
- for (var i in container) {
- var element = container[i];
- if (pred(element, i)) {
- fun(element, i);
- }
- }
- }
- };
- std.count_if = function(container, pred) {
- var result = 0;
- std.for_each_if(container, pred, function( /* e, i */ ) {
- ++result;
- });
- return result;
- };
- std.copy_fields = function(obj, fields) {
- var result = {};
- std.for_each(fields, function(e /* , i */ ) {
- result[e] = obj[e];
- });
- return result;
- };
- std.copy = function(container, out) {
- if (std.isNull(out)) {
- var result = std.isArray(container) ? [] : {};
- std.for_each(container, function(e, i) {
- if (e instanceof Date) {
- result[i] = e;
- } else if (std.isObject(e) || std.isArray(e)) {
- result[i] = std.copy(e);
- } else {
- result[i] = e;
- }
- });
- return result;
- } else {
- std.for_each(container, function(e, i) {
- out[i] = e;
- });
- }
- };
- std.merge_obj_into = function(source, into) {
- if (std.isObject(source) && std.isObject(into)) {
- std.for_each(source, function(e, i) {
- into[i] = e;
- });
- }
- };
- std.ensure = function(map, key, newFoo, onInsert, p1, p2, p3, p4, p5, p6, p7) {
- var result = null;
- if (std.isObject(map)) {
- result = map[key];
- if (std.isNull(result)) {
- if (std.isFunction(newFoo)) {
- result = newFoo(key, p1, p2, p3, p4, p5, p6, p7);
- map[key] = result;
- } else {
- result = newFoo;
- map[key] = result;
- }
- if (onInsert) {
- onInsert(key, result);
- }
- }
- }
- return result;
- };
- std.join = function(container, foo, comma, left, right) {
- if (std.isNull(left)) {
- left = '[';
- }
- if (std.isNull(comma)) {
- comma = ',';
- }
- var result = left;
- if (std.isFunction(foo)) {
- std.for_each(container, function(e, i) {
- if (i > 0) {
- result += comma;
- }
- result += foo(e, i);
- });
- } else {
- std.for_each(container, function(e, i) {
- if (i > 0) {
- result += comma;
- }
- result += std.toString(e);
- });
- }
- if (std.isNull(right)) {
- right = ']';
- }
- return result + right;
- };
- } catch (e) {
- console.debug('Error initializing std : ' + e);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement