No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.mirrorFieldTo = exports.authEmulatorUrl = exports.logError = exports.toUnixTimestamp = exports.randomDigits = exports.randomBase64UrlStr = exports.randomId = exports.parseAbsoluteUri = exports.canonicalizeEmailAddress = exports.isValidPhoneNumber = exports.isValidEmailAddress = void 0;
  4. const url_1 = require("url");
  5. const registry_1 = require("../registry");
  6. const types_1 = require("../types");
  7. const emulatorLogger_1 = require("../emulatorLogger");
  8. function isValidEmailAddress(email) {
  9. return /^[^@]+@[^@]+$/.test(email);
  10. }
  11. exports.isValidEmailAddress = isValidEmailAddress;
  12. function isValidPhoneNumber(phoneNumber) {
  13. return /^\+/.test(phoneNumber);
  14. }
  15. exports.isValidPhoneNumber = isValidPhoneNumber;
  16. function canonicalizeEmailAddress(email) {
  17. return email.toLowerCase();
  18. }
  19. exports.canonicalizeEmailAddress = canonicalizeEmailAddress;
  20. function parseAbsoluteUri(uri) {
  21. try {
  22. return new url_1.URL(uri);
  23. }
  24. catch (_a) {
  25. return undefined;
  26. }
  27. }
  28. exports.parseAbsoluteUri = parseAbsoluteUri;
  29. function randomId(len) {
  30. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  31. let autoId = "";
  32. for (let i = 0; i < len; i++) {
  33. autoId += chars.charAt(Math.floor(Math.random() * chars.length));
  34. }
  35. return autoId;
  36. }
  37. exports.randomId = randomId;
  38. function randomBase64UrlStr(len) {
  39. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
  40. let autoId = "";
  41. for (let i = 0; i < len; i++) {
  42. autoId += chars.charAt(Math.floor(Math.random() * chars.length));
  43. }
  44. return autoId;
  45. }
  46. exports.randomBase64UrlStr = randomBase64UrlStr;
  47. function randomDigits(len) {
  48. let digits = "";
  49. for (let i = 0; i < len; i++) {
  50. digits += Math.floor(Math.random() * 10);
  51. }
  52. return digits;
  53. }
  54. exports.randomDigits = randomDigits;
  55. function toUnixTimestamp(date) {
  56. return Math.floor(date.getTime() / 1000);
  57. }
  58. exports.toUnixTimestamp = toUnixTimestamp;
  59. function logError(err) {
  60. if (!registry_1.EmulatorRegistry.isRunning(types_1.Emulators.AUTH)) {
  61. console.error(err);
  62. }
  63. emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.AUTH).log("WARN", err.stack || err.message || err.constructor.name);
  64. }
  65. exports.logError = logError;
  66. function authEmulatorUrl(req) {
  67. if (registry_1.EmulatorRegistry.isRunning(types_1.Emulators.AUTH)) {
  68. return registry_1.EmulatorRegistry.url(types_1.Emulators.AUTH);
  69. }
  70. else {
  71. return registry_1.EmulatorRegistry.url(types_1.Emulators.AUTH, req);
  72. }
  73. }
  74. exports.authEmulatorUrl = authEmulatorUrl;
  75. function mirrorFieldTo(dest, field, source) {
  76. const value = source[field];
  77. if (value === undefined) {
  78. delete dest[field];
  79. }
  80. else {
  81. dest[field] = value;
  82. }
  83. }
  84. exports.mirrorFieldTo = mirrorFieldTo;