Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 "use strict";
7 module.metadata = {
8 "stability": "unstable"
9 };
11 const { validateOptions: valid } = require("../deprecated/api-utils");
13 // Function takes property validation rules and returns function that given
14 // an `options` object will return validated / normalized options back. If
15 // option(s) are invalid validator will throw exception described by rules.
16 // Returned will also have contain `rules` property with a given validation
17 // rules and `properties` function that can be used to generate validated
18 // property getter and setters can be mixed into prototype. For more details
19 // see `properties` function below.
20 function contract(rules) {
21 function validator(options) {
22 return valid(options || {}, rules);
23 }
24 validator.rules = rules
25 validator.properties = function(modelFor) {
26 return properties(modelFor, rules);
27 }
28 return validator;
29 }
30 exports.contract = contract
32 // Function takes `modelFor` instance state model accessor functions and
33 // a property validation rules and generates object with getters and setters
34 // that can be mixed into prototype. Property accessors update model for the
35 // given instance. If you wish to react to property updates you can always
36 // override setters to put specific logic.
37 function properties(modelFor, rules) {
38 let descriptor = Object.keys(rules).reduce(function(descriptor, name) {
39 descriptor[name] = {
40 get: function() { return modelFor(this)[name] },
41 set: function(value) {
42 let change = {};
43 change[name] = value;
44 modelFor(this)[name] = valid(change, rules)[name];
45 }
46 }
47 return descriptor
48 }, {});
49 return Object.create(Object.prototype, descriptor);
50 }
51 exports.properties = properties