1.0.197 • Published 8 years ago

angular-helpers v1.0.197

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

exports.safeAngularApply = function (scope, callback, args) { if (!scope.$$phase && (scope.$rootScope ? !scope.$rootScope.$$phase : true)) { if (typeof callback !== "function") scope.$apply(); else if (args instanceof Array) callback.apply(scope, Array.prototype.slice.call(args)) else callback(); } else { setTimeout(function () { exports.safeAngularApply(scope, callback, args); }, 250); } }

exports.safeParseJson = function (str) { try { var p = JSON.parse(str); return p; } catch (e) { return str; } }

exports.getElemById = function (collection, by, id) { for (var i in collection) { if (collectioni == id) return collectioni; } return null; }

exports.checkNested = function (obj) { var args = Array.prototype.slice.call(arguments, 1);

for (var i = 0; i < args.length; i++) {
    if (!obj || !obj.hasOwnProperty(args[i])) {
        return false;
    }
    obj = obj[args[i]];
}
return true;

}

exports.detectIE = function () { var ua = window.navigator.userAgent;

var msie = ua.indexOf('MSIE ');
if (msie > 0) {
    // IE 10 or older => return version number
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}

var trident = ua.indexOf('Trident/');
if (trident > 0) {
    // IE 11 => return version number
    var rv = ua.indexOf('rv:');
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}

var edge = ua.indexOf('Edge/');
if (edge > 0) {
    // Edge (IE 12+) => return version number
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}

// other browser
return null;

}

exports.addToQuerystring = function (query, paramName, paramValue) { if (!query) query = ""; var char = (query.indexOf("?") > -1) ? "&" : "?"; return query + char + paramName + "=" + paramValue; }

exports.removeFromQuerystring = function (url, parameter) { //prefer to use l.search if you have a location/link object var urlparts = url.split('?'); if (urlparts.length >= 2) {

    var prefix = encodeURIComponent(parameter) + '=';
    var pars = urlparts[1].split(/[&;]/g);

    //reverse iteration as may be destructive
    for (var i = pars.length; i-- > 0;) {
        //idiom for string.startsWith
        if (pars[i].lastIndexOf(prefix, 0) !== -1) {
            pars.splice(i, 1);
        }
    }

    url = urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : "");
    return url;
} else {
    return url;
}

}

exports.fromObjectToQuerystring = function (obj, prefix) { var str = []; for (var p in obj) { if (obj.hasOwnProperty(p)) { var k = prefix ? prefix + "" + p + "" : p, v = objp; str.push(typeof v == "object" ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v)); } } return str.join("&"); }

exports.extractDomainFromURL = function (url) { var a = document.createElement('a'); a.href = url; return a.hostname; }

exports.replaceAll = function (str, find, replace) { var escapeRegExp = function (str) { return str.replace(/[.*+?^${}()|]\/g, "\$&"); // $& means the whole matched string } return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); }

exports.storage = { get: function (key) { var val = null; try { val = localStorage.getItem(key); return JSON.parse(val); } catch (e) { return val; } }, set: function (key, value) { var final = ''; if (typeof value === 'object') final = JSON.stringify(value); else final = value; try { localStorage.setItem(key, final); } catch (e) { } }, remove: function (key) { try { localStorage.removeItem(key); } catch (e) { } } }

exports.isParamExist = function (field, url) { url = url ? url : window.location.href; if (url.indexOf('?' + field + '=') != -1) return true; else if (url.indexOf('&' + field + '=') != -1) return true; return false };

exports.getReferrerByUrl = function (url) { var protocol = ''; if (url.indexOf('http:') == 0) protocol = 'http:'; else if (url.indexOf('https:') == 0) protocol = 'https:'; else protocol = window.location.protocol; url = url.replace(protocol, '').replace('//', ''); var indexOfSlash = url.indexOf('/'); if (indexOfSlash > -1) url = url.substring(0, indexOfSlash); return protocol + '//' + url; };

// https://davidwalsh.name/nested-objects exports.Objectifier = (function () {

// Utility method to get and set objects that may or may not exist
var objectifier = function (splits, create, context) {
    var result = context || window;
    for (var i = 0, s; result && (s = splits[i]); i++) {
        result = (s in result ? result[s] : (create ? result[s] = {} : undefined));
    }
    return result;
};

return {
    // Creates an object if it doesn't already exist
    set: function (name, value, context) {
        var splits = name.split('.'), s = splits.pop(), result = objectifier(splits, true, context);
        return result && s ? (result[s] = value) : undefined;
    },
    get: function (name, create, context) {
        return objectifier(name.split('.'), create, context);
    },
    exists: function (name, context) {
        return this.get(name, false, context) !== undefined;
    }
};

})();

// https://jsfiddle.net/Guffa/DDn6W/ exports.randomPassword = function (length) { if (!length) length = 6; var chars = "abcdefghijklmnopqrstuvwxyz!@#$%^&()-+<>ABCDEFGHIJKLMNOP1234567890"; var pass = ""; for (var x = 0; x < length; x++) { var i = Math.floor(Math.random() chars.length); pass += chars.charAt(i); } return pass; }

1.0.197

8 years ago

1.0.196

9 years ago

1.0.195

9 years ago

1.0.194

9 years ago

1.0.193

9 years ago

1.0.192

9 years ago

1.0.191

9 years ago

1.0.19

9 years ago

1.0.17

9 years ago

1.0.16

9 years ago

1.0.15

9 years ago

1.0.14

9 years ago

1.0.13

9 years ago

1.0.12

9 years ago

1.0.11

9 years ago

1.0.10

9 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago