addon-sdk/source/lib/sdk/places/contract.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/places/contract.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "unstable"
    1.12 +};
    1.13 +
    1.14 +const { Cc, Ci } = require('chrome');
    1.15 +const { EventEmitter } = require('../deprecated/events');
    1.16 +const { isValidURI, URL } = require('../url');
    1.17 +const { contract } = require('../util/contract');
    1.18 +const { extend } = require('../util/object');
    1.19 +
    1.20 +// map of property validations
    1.21 +const validItem = {
    1.22 +  id: {
    1.23 +    is: ['number', 'undefined', 'null'],
    1.24 +  },
    1.25 +  group: {
    1.26 +    is: ['object', 'number', 'undefined', 'null'],
    1.27 +    ok: function (value) {
    1.28 +      return value && 
    1.29 +        (value.toString && value.toString() === '[object Group]') ||
    1.30 +        typeof value === 'number' ||
    1.31 +        value.type === 'group';
    1.32 +    },
    1.33 +    msg: 'The `group` property must be a valid Group object'
    1.34 +  },
    1.35 +  index: {
    1.36 +    is: ['undefined', 'null', 'number'],
    1.37 +    map: function (value) value == null ? -1 : value,
    1.38 +    msg: 'The `index` property must be a number.'
    1.39 +  },
    1.40 +  updated: {
    1.41 +    is: ['number', 'undefined']
    1.42 +  }
    1.43 +};
    1.44 +
    1.45 +const validTitle = {
    1.46 +  title: {
    1.47 +    is: ['string'],
    1.48 +    msg: 'The `title` property must be defined.'
    1.49 +  }
    1.50 +};
    1.51 +
    1.52 +const validURL = {
    1.53 +  url: {
    1.54 +    is: ['string'], 
    1.55 +    ok: isValidURI,
    1.56 +    msg: 'The `url` property must be a valid URL.'
    1.57 +  }
    1.58 +};
    1.59 +
    1.60 +const validTags = {
    1.61 +  tags: {
    1.62 +    is: ['object'],
    1.63 +    ok: function (tags) tags instanceof Set,
    1.64 +    map: function (tags) {
    1.65 +      if (Array.isArray(tags))
    1.66 +        return new Set(tags);
    1.67 +      if (tags == null)
    1.68 +        return new Set();
    1.69 +      return tags;
    1.70 +    },
    1.71 +    msg: 'The `tags` property must be a Set, or an array'
    1.72 +  }
    1.73 +};
    1.74 +
    1.75 +exports.bookmarkContract = contract(
    1.76 +  extend(validItem, validTitle, validURL, validTags));
    1.77 +exports.separatorContract = contract(validItem);
    1.78 +exports.groupContract = contract(extend(validItem, validTitle));

mercurial