utils.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const constants = require('../config/constants');
  3. let isObject = function (value) {
  4. return value !== null && typeof value === 'object';
  5. }
  6. let isArray = function (value) {
  7. return Array.isArray(value);
  8. }
  9. let copyObjectTo = function (from, to) {
  10. if (!to) {
  11. return from;
  12. }
  13. for (const name in from) {
  14. if (!from.hasOwnProperty(name)) {
  15. continue;
  16. }
  17. const fromValue = from[name];
  18. const toValue = to[name];
  19. if (isObject(fromValue) || isArray(fromValue)) {
  20. to[name] = copyObjectTo(from[name], to[name]);
  21. } else {
  22. if (fromValue !== toValue) {
  23. to[name] = fromValue;
  24. }
  25. }
  26. }
  27. return to;
  28. }
  29. let base64Encode = function (str) {
  30. return new Buffer(str).toString('base64');
  31. };
  32. let generateUniqueId = function () {
  33. const sourceId = constants.ariaNgNativeConstants.appPrefix + '_' + Math.round(new Date().getTime() / 1000) + '_' + Math.random();
  34. const hashedId = base64Encode(sourceId);
  35. return hashedId;
  36. };
  37. module.exports = {
  38. isObject: isObject,
  39. isArray: isArray,
  40. copyObjectTo: copyObjectTo,
  41. generateUniqueId: generateUniqueId
  42. };