1 /*
  2     Copyright 2008-2021
  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'
 47 ], function (JXG, Type, Const, Coords) {
 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, len,
 63                 r = [];
 64 
 65             if (Type.exists(a.length)) {
 66                 len = a.length;
 67                 for (i = 0; i < len; i++) {
 68                     r.push(format.call(this, a[i], copy));
 69                 }
 70             }
 71 
 72             return r;
 73         },
 74 
 75         /**
 76          * Normalize points and coord objects into a coord object.
 77          *
 78          * @param {JXG.Point|JXG.Coords} c
 79          * @param {Boolean} [copy=false] Return a copy, not a reference
 80          *
 81          * @returns {JXG.Coords}
 82          */
 83         coords: function (c, copy) {
 84             var coord = c;
 85 
 86             if (c && c.elementClass === Const.OBJECT_CLASS_POINT) {
 87                 coord = c.coords;
 88             } else if (c.usrCoords && c.scrCoords && c.usr2screen) {
 89                 coord = c;
 90             }
 91 
 92             if (copy) {
 93                 coord = new Coords(Const.COORDS_BY_USER, coord.usrCoords, coord.board);
 94             }
 95 
 96             return coord;
 97         },
 98 
 99         /**
100          * Normalize points, coordinate arrays and coord objects into a coordinate array.
101          *
102          * @param {JXG.Point|JXG.Coords|Array} c
103          * @param {Boolean} [copy=false] Return a copy, not a reference
104          *
105          * @returns {Array} Homogeneous coordinates
106          */
107         coordsArray: function (c, copy) {
108             var coord;
109 
110             if (!Type.isArray(c)) {
111                 coord = this.coords(c).usrCoords;
112             } else {
113                 coord = c;
114             }
115 
116             if (coord.length < 3) {
117                 coord.unshift(1);
118             }
119 
120             if (copy) {
121                 coord = [coord[0], coord[1], coord[2]];
122             }
123 
124             return coord;
125         }
126     };
127 
128     JXG.Expect = Expect;
129 
130     return Expect;
131 });
132