contract.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Cc, Ci } = require('chrome');
  9. const { EventEmitter } = require('../deprecated/events');
  10. const { isValidURI, URL } = require('../url');
  11. const { contract } = require('../util/contract');
  12. const { extend } = require('../util/object');
  13. // map of property validations
  14. const validItem = {
  15. id: {
  16. is: ['number', 'undefined', 'null'],
  17. },
  18. group: {
  19. is: ['object', 'number', 'undefined', 'null'],
  20. ok: function (value) {
  21. return value &&
  22. (value.toString && value.toString() === '[object Group]') ||
  23. typeof value === 'number' ||
  24. value.type === 'group';
  25. },
  26. msg: 'The `group` property must be a valid Group object'
  27. },
  28. index: {
  29. is: ['undefined', 'null', 'number'],
  30. map: function (value) value == null ? -1 : value,
  31. msg: 'The `index` property must be a number.'
  32. },
  33. updated: {
  34. is: ['number', 'undefined']
  35. }
  36. };
  37. const validTitle = {
  38. title: {
  39. is: ['string'],
  40. msg: 'The `title` property must be defined.'
  41. }
  42. };
  43. const validURL = {
  44. url: {
  45. is: ['string'],
  46. ok: isValidURI,
  47. msg: 'The `url` property must be a valid URL.'
  48. }
  49. };
  50. const validTags = {
  51. tags: {
  52. is: ['object'],
  53. ok: function (tags) tags instanceof Set,
  54. map: function (tags) {
  55. if (Array.isArray(tags))
  56. return new Set(tags);
  57. if (tags == null)
  58. return new Set();
  59. return tags;
  60. },
  61. msg: 'The `tags` property must be a Set, or an array'
  62. }
  63. };
  64. exports.bookmarkContract = contract(
  65. extend(validItem, validTitle, validURL, validTags));
  66. exports.separatorContract = contract(validItem);
  67. exports.groupContract = contract(extend(validItem, validTitle));