utils.js 645 B

1234567891011121314151617181920212223242526272829
  1. module.exports.isUuid = function (uuid) {
  2. if (!uuid) return false;
  3. uuid = uuid.toString().toLowerCase();
  4. return /[0-9a-f]{8}\-?[0-9a-f]{4}\-?4[0-9a-f]{3}\-?[89ab][0-9a-f]{3}\-?[0-9a-f]{12}/.test(uuid)
  5. }
  6. module.exports.isCookieCid = function (cid) {
  7. return /^[0-9]+\.[0-9]+$/.test(cid)
  8. }
  9. module.exports.ensureValidCid = function (uuid) {
  10. if (!this.isUuid(uuid)) {
  11. if (!this.isCookieCid(uuid)) {
  12. return false;
  13. }
  14. return uuid;
  15. }
  16. uuid = uuid.replace(/\-/g, "");
  17. return "" +
  18. uuid.substring(0, 8) + "-" +
  19. uuid.substring(8, 12) + "-" +
  20. uuid.substring(12, 16) + "-" +
  21. uuid.substring(16, 20) + "-" +
  22. uuid.substring(20);
  23. }