UNPKG

built.io-browserify

Version:

SDK for Built.io Backend which is compatible with Browserify

270 lines (237 loc) 7.31 kB
var R = require('ramda'); var qs = require("qs") /* This proxies a method call on a instance. param | instance0 | Instance on which the method is declared */ module.exports.appProxyHelper = function(methodName){ return function(obj){ return function(){ return obj[methodName].apply(obj, arguments); } } } /* This method iteratively copies properties/methods from one object to other param | copyTo | Target object param | copyFrom | Source object */ module.exports.copyProperties = function(copyTo, copyFrom) { for (method in copyFrom) { copyTo[method] = copyFrom[method] }; } /* This method construct a object as per REST framework param | method | Http method (PUT,POST,DELETE,GET) param | path | URL param | header | Http headers param | entity | Payload (Request body) param | params | URL params */ module.exports.getAdaptorObj = function(method, path, headers, entity, params) { if (!method) { throw new Error('Http method is not specified'); } if (!path) { throw new Error('Path for http request not specified'); } if (!headers) { throw new Error('Headers for http request not specified'); } var adapterObj = {}; adapterObj.path = path; adapterObj.headers = R.mixin({}, headers); adapterObj.method = method; if (entity) { adapterObj.entity = entity } if (params) { adapterObj.path += module.exports.attachQueryString(params) } return adapterObj; } /* This method takes a function and if it is passed arguments it is called with those arguments and if not,it calls the function with default arguments. param | fn | The function to be wrapped param | defaultValues | The default values with which it should be called with */ module.exports.wrapper = function(fn,defaultValues){ // This function is already called by us so the closured function has access to this parameters return function(){ // The function is returned for execution var args = Array.prototype.slice.call(arguments, 0); args = args.concat(defaultValues.slice(-(fn.length-arguments.length))); return fn.apply(this,args); } } /* This method is used to check if a variable is array param | array | The variable to be checked */ module.exports.isArray = function(obj){ return Object.prototype.toString.call(obj) === '[object Array]' } module.exports.isNumber = function(variable){ return typeof variable === "number"; } module.exports.isBoolean = function(variable){ return typeof variable === "boolean"; } module.exports.isString = function(variable){ return typeof variable === "string"; } module.exports.isQueryInstance = function(query){ return !! query.where; } module.exports.isFunction = function(functionToCheck) { var obj = {}; return functionToCheck && obj.toString.call(functionToCheck) === '[object Function]'; } /* This method is used to check if a variable is a JavaScript Object param | object | The variable to be checked */ module.exports.isObject = function(variable){ return typeof variable === "object"; } /** * This method is used to check if a variable is a pure JavaScript Object and not array */ module.exports.isPlainObject = function(variable) { return module.exports.isObject(variable) && !module.exports.isArray(variable) } /* This method is used to check if code is running browser */ module.exports.isBrowser = function(){ //temp implementation return !! (typeof window != 'undefined' && window.document); } module.exports.isLocalStroageAvaliable = function(){ //temp implementation return !! (typeof localStorage != 'undefined'); } /* This method is used to check if the obj passed in a empty object ({}) */ module.exports.isEmpty = function(obj){ if(obj) return Object.keys(obj).length === 0; } /* This method can be used to make object hierarchy dyanmically @example var obj = keyValue("person",keyValue('age',10)); { person:{ age:10 } } */ module.exports.keyValue = function() { var lastKey = null var result = {} for (var i = 0; i < arguments.length; i++) { var elem = arguments[i] if ((i % 2) === 0) { lastKey = elem } else { result[lastKey] = elem } } return result } module.exports.backboneExist = function(){ return !!(typeof Backbone !== "undefined" && Backbone.Model && Backbone.Collection) } module.exports.supportsFormData = function(){ return !! (typeof window === "object" && typeof window.FormData !=="undefined"); } var encodeParam = module.exports.encodeParam = function(property, object) { if (module.exports.isObject(object[property])) { return property+"="+encodeURIComponent(JSON.stringify(object[property])); }else{ return property+"="+object[property]; } } module.exports.addParam = function(path, property, object) { if (path.indexOf("?") >= 0) { path += "&" + encodeParam(property, object); } else { path += "?" + encodeParam(property, object); } return path; } module.exports.findAndRemove = function(item, list){ var index = list.indexOf(item) if(index >= -1){ return list.splice(index, 1) } return list; } /* * Determine the mobile operating system. * This function either returns 'iOS', 'android' or 'other' * * @returns {String} */ module.exports.getDeviceTypeFromUA = function(){ if(module.exports.isBrowser()){ var userAgent = navigator.userAgent || navigator.vendor || window.opera; if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) { return 'ios'; } else if (userAgent.match(/Android/i)) { return 'android'; } else { return 'other'; } } return 'other' } /* Converts structure A to Structure B ========= A ============ "bar.foo.goo" 20 ========== B =========== { bar:{ foo:{ goo: 20 } } } */ // Function: createNestedObject( base, names[, value] ) // base: the object on which to create the hierarchy // names: an array of strings contaning the names of the objects // value (optional): if given, will be the last object in the hierarchy module.exports.createNestedObject = function(base, names, value) { // If a value is given, remove the last name and keep it for later: var lastName = arguments.length === 3 ? names.pop() : false; // Walk the hierarchy, creating new objects where needed. // If the lastName was removed, then the last object is not set yet: for (var i = 0; i < names.length; i++) { base = base[names[i]] = base[names[i]] || {}; } // If a value was given, set it to the last name: if (lastName) base = base[lastName] = value; // Return the last object in the hierarchy: return base; }; module.exports.attachQueryString = function(params) { var queryString = "" if(params.query) { var queryClone = R.mixin({}, params.query) delete params.query queryString = "?query=" + encodeURI(JSON.stringify(queryClone)) if(!module.exports.isEmpty(params)) { queryString += "&" + getQueryString(params) } } else { if(!module.exports.isEmpty(params)) { queryString = "?" + getQueryString(params) } } return queryString } function getQueryString(object) { return qs.stringify(object) }