1 /* 2 Copyright 2008-2023 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 <https://www.gnu.org/licenses/> 29 and <https://opensource.org/licenses/MIT/>. 30 */ 31 32 /*global JXG: true, define: true, html_sanitize: true*/ 33 /*jslint nomen: true, plusplus: true*/ 34 35 /** 36 * @fileoverview expect.js provides utilities for parameter magic by normalizing multi-type parameters. 37 */ 38 39 import JXG from "../jxg"; 40 import Type from "./type"; 41 import Const from "../base/constants"; 42 import Coords from "../base/coords"; 43 44 var Expect = { 45 /** 46 * Apply an expect method on every element of an array. 47 * 48 * @param {Array} a 49 * @param {function} format 50 * @param {Boolean} [copy=false] 51 * 52 * @returns {Array} 53 */ 54 each: function (a, format, copy) { 55 var i, 56 len, 57 r = []; 58 59 if (Type.exists(a.length)) { 60 len = a.length; 61 for (i = 0; i < len; i++) { 62 r.push(format.call(this, a[i], copy)); 63 } 64 } 65 66 return r; 67 }, 68 69 /** 70 * Normalize points and coord objects into a coord object. 71 * 72 * @param {JXG.Point|JXG.Coords} c 73 * @param {Boolean} [copy=false] Return a copy, not a reference 74 * 75 * @returns {JXG.Coords} 76 */ 77 coords: function (c, copy) { 78 var coord = c; 79 80 if (c && c.elementClass === Const.OBJECT_CLASS_POINT) { 81 coord = c.coords; 82 } else if (c.usrCoords && c.scrCoords && c.usr2screen) { 83 coord = c; 84 } 85 86 if (copy) { 87 coord = new Coords(Const.COORDS_BY_USER, coord.usrCoords, coord.board); 88 } 89 90 return coord; 91 }, 92 93 /** 94 * Normalize points, coordinate arrays and coord objects into a coordinate array. 95 * 96 * @param {JXG.Point|JXG.Coords|Array} c 97 * @param {Boolean} [copy=false] Return a copy, not a reference 98 * 99 * @returns {Array} Homogeneous coordinates 100 */ 101 coordsArray: function (c, copy) { 102 var coord; 103 104 if (!Type.isArray(c)) { 105 coord = this.coords(c).usrCoords; 106 } else { 107 coord = c; 108 } 109 110 if (coord.length < 3) { 111 coord.unshift(1); 112 } 113 114 if (copy) { 115 coord = [coord[0], coord[1], coord[2]]; 116 } 117 118 return coord; 119 } 120 }; 121 122 JXG.Expect = Expect; 123 124 export default Expect; 125