michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Cc, Ci } = require('chrome'); michael@0: const { EventEmitter } = require('../deprecated/events'); michael@0: const { isValidURI, URL } = require('../url'); michael@0: const { contract } = require('../util/contract'); michael@0: const { extend } = require('../util/object'); michael@0: michael@0: // map of property validations michael@0: const validItem = { michael@0: id: { michael@0: is: ['number', 'undefined', 'null'], michael@0: }, michael@0: group: { michael@0: is: ['object', 'number', 'undefined', 'null'], michael@0: ok: function (value) { michael@0: return value && michael@0: (value.toString && value.toString() === '[object Group]') || michael@0: typeof value === 'number' || michael@0: value.type === 'group'; michael@0: }, michael@0: msg: 'The `group` property must be a valid Group object' michael@0: }, michael@0: index: { michael@0: is: ['undefined', 'null', 'number'], michael@0: map: function (value) value == null ? -1 : value, michael@0: msg: 'The `index` property must be a number.' michael@0: }, michael@0: updated: { michael@0: is: ['number', 'undefined'] michael@0: } michael@0: }; michael@0: michael@0: const validTitle = { michael@0: title: { michael@0: is: ['string'], michael@0: msg: 'The `title` property must be defined.' michael@0: } michael@0: }; michael@0: michael@0: const validURL = { michael@0: url: { michael@0: is: ['string'], michael@0: ok: isValidURI, michael@0: msg: 'The `url` property must be a valid URL.' michael@0: } michael@0: }; michael@0: michael@0: const validTags = { michael@0: tags: { michael@0: is: ['object'], michael@0: ok: function (tags) tags instanceof Set, michael@0: map: function (tags) { michael@0: if (Array.isArray(tags)) michael@0: return new Set(tags); michael@0: if (tags == null) michael@0: return new Set(); michael@0: return tags; michael@0: }, michael@0: msg: 'The `tags` property must be a Set, or an array' michael@0: } michael@0: }; michael@0: michael@0: exports.bookmarkContract = contract( michael@0: extend(validItem, validTitle, validURL, validTags)); michael@0: exports.separatorContract = contract(validItem); michael@0: exports.groupContract = contract(extend(validItem, validTitle));