all files / montage/core/converter/ converter.js

90.91% Statements 10/11
100% Branches 2/2
50% Functions 1/2
90.91% Lines 10/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                                                                17×                                                                                                                                                
/**
 * Provides common conversion, validation, and formatting functions for
 * different types of values.
 *
 * @module montage/core/converter/converter
 * @requires montage/core/core
 */
var Montage = require("../core").Montage;
 
/**
 * @const {string}
 */
var FUNCTION_CLASS = '[object Function]',
    BOOLEAN_CLASS = '[object Boolean]',
    NUMBER_CLASS = '[object Number]',
    STRING_CLASS = '[object String]',
    ARRAY_CLASS = '[object Array]',
    DATE_CLASS = '[object Date]';
 
var _toString = Object.prototype.toString;
 
/**
 * @exports module:montage/core/converter#isNumber
 * @function
 * @private
 */
var isNumber = function (object) {
    return _toString.call(object) === NUMBER_CLASS;
};
exports.isNumber = isNumber;
 
 
/**
 * @exports module:montage/core/converter#isDef
 * @function
 * @private
 */
var isDef = function (obj) {
    return (obj && typeof obj !== 'undefined');
};
exports.isDef = isDef;
 
// Validators
/**
 * Base validator object.
 * @class Validator
 * @extends Montage
 */
var Validator = exports.Validator = Montage.specialize( /** @lends Validator# */{
    /**
     * @type {Object}
     * @default null
     */
    validate: {
        value: null
    }
});
 
 
// Converters
 
/**
 * Converts and optionally reverts values between two domains. The converter
 * interface consists of two methods:
 *
 * -   `convert(input)`: convert input to output
 * -   `revert(output)`: optional, converts output back to input
 *
 * @class Converter
 * @classdesc An abstract type for converters, objects that can convert and
 * optionally revert values between domains, like strings and numbers.
 */
var Converter = exports.Converter = Montage.specialize( /** @lends Converter# */ {
 
    /**
     * Specifies whether the converter allows partial conversion.
     * @type {boolean}
     * @default true
     */
    allowPartialConversion: {
        value: true
    },
 
    /**
     * Converts values from the input domain into the output range.
     * @function
     * @default null
     */
    convert: {
        enumerable: false,
        value: null
    },
 
    /**
     * Optionally, reverts values from the output range, back into the input
     * range. This may not be possible with high fidelity depending on the
     * relationship between these domains.
     * @function
     * @default null
     */
    revert: {
        enumerable: false,
        value: null
    }
 
}, {
 
    blueprintModuleId: require("../core")._blueprintModuleIdDescriptor,
 
    blueprint: require("../core")._blueprintDescriptor
 
});