route4me-node
Version:
Access Route4Me's logistics-as-a-service API using our Node.js SDK
266 lines (214 loc) • 5.72 kB
JavaScript
;
/*
* @module route4me-node/utils
* @protected
*/
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var debug = require("debug")("route4me:utils");
var errors = require("./errors");
/**
* ILogger interface
*
* @interface ILogger
* @category Route4Me
* @public
*/
var ILogger = function () {
function ILogger() {
_classCallCheck(this, ILogger);
}
_createClass(ILogger, [{
key: "debug",
/**
* @typedef ILogger~LoggerParams
* @property {string} [msg] Message to log
* @property {Error} [err] Error object, if error occured
*/
/**
* Debug
* @param {ILogger~LoggerParams|Error|string} [arg] Something to log
*/
// eslint-disable-next-line class-methods-use-this, no-unused-vars
value: function debug(arg) {}
/**
* Info
* @param {ILogger~LoggerParams|Error|string} [arg] Something to log
*/
}, {
key: "info",
value: function info(arg) {} // eslint-disable-line class-methods-use-this, no-unused-vars
/**
* Warning
* @param {ILogger~LoggerParams|Error|string} [arg] Something to log
*/
}, {
key: "warn",
value: function warn(arg) {} // eslint-disable-line class-methods-use-this, no-unused-vars
/**
* Error
* @param {ILogger~LoggerParams|Error|string} [arg] Something to log
*/
}, {
key: "error",
value: function error(arg) {} // eslint-disable-line class-methods-use-this, no-unused-vars
}]);
return ILogger;
}();
var CustomInternalPostProcessing = function () {
function CustomInternalPostProcessing() {
_classCallCheck(this, CustomInternalPostProcessing);
}
_createClass(CustomInternalPostProcessing, null, [{
key: "fromJsonWithStatus",
/**
* status
*
* @private
*
* @example
* Sample = {
* "status": true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {boolean} - Success
*/
value: function fromJsonWithStatus(data, ctx, res) {
// HACK: currently, API returns 'text/plain', so
// the response in not parsed automatically
if ("{\"status\":true}" === res.text) {
debug("fromJsonWithStatus: HACK for wrong content-type");
return true;
}
if (!data || "boolean" !== typeof data["status"]) {
return new errors.Route4MeValidationError("Invalid response", data);
}
if (true === data["status"]) {
return true;
}
// TODO: parse real error
return new errors.Route4MeApiError("Failed", res);
}
}]);
return CustomInternalPostProcessing;
}();
/*
=============================
TYPECONV
=============================
*/
function isObject(obj) {
return null !== obj && "object" === typeof obj;
}
function get(obj, prop, def) {
if (undefined === obj || null === obj) {
return def;
}
var val = obj[prop];
if (undefined === val) {
return def;
}
return val;
}
function mapObject(obj, map) {
if (null === obj || undefined === obj) {
return obj;
}
var res = {};
Object.keys(map).forEach(function (tgt) {
var src = map[tgt];
res[src] = obj[tgt];
});
return res;
}
/**
* Deep clone an object
*
* @protected
*
* @param {any} obj - Original object
* @return {any} - The deep copy of an object
*/
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function uniq(arr) {
var uq = {};
var res = [];
arr.forEach(function (i) {
if (!(i in uq)) {
uq[i] = true;
res.push(i);
}
});
return res;
}
function toStringArray(arg, trim) {
var a = arg;
if ("number" === typeof a) {
return [`${a}`];
}
if ("string" === typeof a) {
if (false !== trim) {
a = a.trim().split(/[,\s]+/);
} else {
a = a.split(/,+/);
}
}
if (Array.isArray(a)) {
a = a.map(function (x) {
return `${x}`;
});
return a;
}
throw new errors.Route4MeError("Argument is not a number OR CSV-string OR string OR array");
}
function toIntArray(arg) {
var a = arg;
if ("number" === typeof a) {
return [a];
}
if ("string" === typeof a) {
a = a.split(/[,\s]+/);
}
if (Array.isArray(a)) {
a = a.map(function (x) {
return Number(x);
}).filter(function (x) {
return "number" === typeof x;
});
return a;
}
throw new errors.Route4MeError("Argument is not a number OR CSV-string OR string OR array");
}
function toOptimizationStatesSafe(states) {
function _isInStateRange(state) {
if (1 > state) return false;
if (6 < state) return false;
return true;
}
var arr = void 0;
try {
arr = toIntArray(states);
} catch (err) {
return err;
}
arr = uniq(arr.filter(_isInStateRange));
return arr.join(",");
}
function toIsoDateString(d) {
return d.toISOString().substring(0, 10);
}
exports.ILogger = ILogger;
exports.CustomInternalPostProcessing = CustomInternalPostProcessing;
exports.isObject = isObject;
exports.get = get;
exports.clone = clone;
exports.mapObject = mapObject;
exports.toStringArray = toStringArray;
exports.toIntArray = toIntArray;
exports.toOptimizationStatesSafe = toOptimizationStatesSafe;
exports.toIsoDateString = toIsoDateString;