      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 /**
     45  * The JXG.Expect namespace provides method to normalize access on coordinates,
     46  * i.e. provides utilities for parameter magic by normalizing multi-type parameters.
     47  * @namespace
     48  */
     49 JXG.Expect = {
     50     /**
     51      * Apply an expect method on every element of an array.
     52      *
     53      * @param {Array} a
     54      * @param {function} format
     55      * @param {Boolean} [copy=false]
     56      *
     57      * @returns {Array}
     58      */
     59     each: function (a, format, copy) {
     60         var i,
     61             len,
     62             r = [];
     63
     64         if (Type.exists(a.length)) {
     65             len = a.length;
     66             for (i = 0; i < len; i++) {
     67                 r.push(format.call(this, a[i], copy));
     68             }
     69         }
     70
     71         return r;
     72     },
     73
     74     /**
     75      * Normalize points and coord objects into a coord object.
     76      *
     77      * @param {JXG.Point|JXG.Coords} c
     78      * @param {Boolean} [copy=false] Return a copy, not a reference
     79      *
     80      * @returns {JXG.Coords}
     81      */
     82     coords: function (c, copy) {
     83         var coord = c;
     84
     85         if (c && c.elementClass === Const.OBJECT_CLASS_POINT) {
     86             coord = c.coords;
     87         } else if (c.usrCoords && c.scrCoords && c.usr2screen) {
     88             coord = c;
     89         }
     90
     91         if (copy) {
     92             coord = new Coords(Const.COORDS_BY_USER, coord.usrCoords, coord.board);
     93         }
     94
     95         return coord;
     96     },
     97
     98     /**
     99      * Normalize points, coordinate arrays and coord objects into a coordinate array.
    100      *
    101      * @param {JXG.Point|JXG.Coords|Array} c
    102      * @param {Boolean} [copy=false] Return a copy, not a reference
    103      *
    104      * @returns {Array} Homogeneous coordinates
    105      */
    106     coordsArray: function (c, copy) {
    107         var coord;
    108
    109         if (!Type.isArray(c)) {
    110             coord = this.coords(c).usrCoords;
    111         } else {
    112             coord = c;
    113         }
    114
    115         if (coord.length < 3) {
    116             coord.unshift(1);
    117         }
    118
    119         if (copy) {
    120             coord = [coord[0], coord[1], coord[2]];
    121         }
    122
    123         return coord;
    124     }
    125 };
    126
    127 export default JXG.Expect;
    128