|
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 |
|
5 "use strict"; |
|
6 |
|
7 module.metadata = { |
|
8 "stability": "unstable" |
|
9 }; |
|
10 |
|
11 const { Cc, Ci } = require('chrome'); |
|
12 const { EventEmitter } = require('../deprecated/events'); |
|
13 const { isValidURI, URL } = require('../url'); |
|
14 const { contract } = require('../util/contract'); |
|
15 const { extend } = require('../util/object'); |
|
16 |
|
17 // map of property validations |
|
18 const validItem = { |
|
19 id: { |
|
20 is: ['number', 'undefined', 'null'], |
|
21 }, |
|
22 group: { |
|
23 is: ['object', 'number', 'undefined', 'null'], |
|
24 ok: function (value) { |
|
25 return value && |
|
26 (value.toString && value.toString() === '[object Group]') || |
|
27 typeof value === 'number' || |
|
28 value.type === 'group'; |
|
29 }, |
|
30 msg: 'The `group` property must be a valid Group object' |
|
31 }, |
|
32 index: { |
|
33 is: ['undefined', 'null', 'number'], |
|
34 map: function (value) value == null ? -1 : value, |
|
35 msg: 'The `index` property must be a number.' |
|
36 }, |
|
37 updated: { |
|
38 is: ['number', 'undefined'] |
|
39 } |
|
40 }; |
|
41 |
|
42 const validTitle = { |
|
43 title: { |
|
44 is: ['string'], |
|
45 msg: 'The `title` property must be defined.' |
|
46 } |
|
47 }; |
|
48 |
|
49 const validURL = { |
|
50 url: { |
|
51 is: ['string'], |
|
52 ok: isValidURI, |
|
53 msg: 'The `url` property must be a valid URL.' |
|
54 } |
|
55 }; |
|
56 |
|
57 const validTags = { |
|
58 tags: { |
|
59 is: ['object'], |
|
60 ok: function (tags) tags instanceof Set, |
|
61 map: function (tags) { |
|
62 if (Array.isArray(tags)) |
|
63 return new Set(tags); |
|
64 if (tags == null) |
|
65 return new Set(); |
|
66 return tags; |
|
67 }, |
|
68 msg: 'The `tags` property must be a Set, or an array' |
|
69 } |
|
70 }; |
|
71 |
|
72 exports.bookmarkContract = contract( |
|
73 extend(validItem, validTitle, validURL, validTags)); |
|
74 exports.separatorContract = contract(validItem); |
|
75 exports.groupContract = contract(extend(validItem, validTitle)); |