1 /* 2 Copyright 2008-2013 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <http://www.gnu.org/licenses/> 29 and <http://opensource.org/licenses/MIT/>. 30 */ 31 32 33 /*global JXG: true, define: true, html_sanitize: true*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 base/constants 39 */ 40 41 /** 42 * @fileoverview expect.js provides utilities for parameter magic by normalizing multi-type parameters. 43 */ 44 45 define([ 46 'jxg', 'utils/type', 'base/constants', 'base/coords', 'base/point' 47 ], function (JXG, Type, Const, Coords, Point) { 48 49 "use strict"; 50 51 var Expect = { 52 /** 53 * Apply an expect method on every element of an array. 54 * 55 * @param {Array} a 56 * @param {function} format 57 * @param {Boolean} [copy=false] 58 * 59 * @returns {Array} 60 */ 61 each: function (a, format, copy) { 62 var i, 63 r = []; 64 65 for (i = 0; i < a.length; i++) { 66 r.push(format.call(this, a[i], copy)); 67 } 68 69 return r; 70 }, 71 72 /** 73 * Normalize points and coord objects into a coord object. 74 * 75 * @param {JXG.Point|JXG.Coords} c 76 * @param {Boolean} [copy=false] Return a copy, not a reference 77 * 78 * @returns {JXG.Coords} 79 */ 80 coords: function (c, copy) { 81 var coord = c; 82 83 if (c && c.elementClass === Const.OBJECT_CLASS_POINT) { 84 coord = c.coords; 85 } else if (c.usrCoords && c.scrCoords && c.usr2screen) { 86 coord = c; 87 } 88 89 if (copy) { 90 coord = new Coords(Const.COORDS_BY_USER, coord.usrCoords, coord.board); 91 } 92 93 return coord; 94 }, 95 96 /** 97 * Normalize points, coordinate arrays and coord objects into a coordinate array. 98 * 99 * @param {JXG.Point|JXG.Coords|Array} c 100 * @param {Boolean} [copy=false] Return a copy, not a reference 101 * 102 * @returns {Array} Homogeneous coordinates 103 */ 104 coordsArray: function (c, copy) { 105 var coord; 106 107 if (!Type.isArray(c)) { 108 coord = this.coords(c).usrCoords; 109 } else { 110 coord = c; 111 } 112 113 if (coord.length < 3) { 114 coord.unshift(1); 115 } 116 117 if (copy) { 118 coord = [coord[0], coord[1], coord[2]]; 119 } 120 121 return coord; 122 } 123 }; 124 125 JXG.Expect = Expect; 126 127 return Expect; 128 });