así que estoy luchando por entender este código. ¿Puede alguien explicar cómo funciona el movimiento de funciones? Como vi una conferencia sobre eso, pero todavía no lo entiendo.
const parse = (point) => { return typeof point === "object" ? point : JSON.parse(point); }; const move = (offset) => (point) =>{ point.x += offset.x; point.y += offset.y; return point }; const polyline = [ { x: 0, y: 0 }, { x: 10, y: 10 }, '{ "x": 20, "y": 20 }', { x: 30, y: 30 }, ]; const offset = move({ x: 10, y: -5 }); const path = polyline.map(parse).map(offset); console.log({ path });Así es como se hizo anteriormente, y este código es una versión optimizada de ese código:
const shift = (offset, points) => { let modifiedPoints = []; points.forEach((point) => { point = parse(point); point.x += offset.x; point.y += offset.y; modifiedPoints.push(point); }); return modifiedPoints; };se llama curry
La función creada pasada en su caso agregará 10 a x y restará 5 de y de cada una de las polilíneas en la matriz
Si lo llamas con
move({ x: 5, y: -15 }); compensará cada línea por 5,-15
const parse = (point) => { return typeof point === "object" ? point : JSON.parse(point); // did we get an object or a string? if the latter parse it }; // move takes an offset and returns a function that uses that offset (closure) const move = (offset) => (point) =>{ point.x += offset.x; point.y += offset.y; return point }; // an array of objects or valid JSON strings const polyline = [ { x: 0, y: 0 }, { x: 10, y: 10 }, '{ "x": 20, "y": 20 }', // this will be parsed { x: 30, y: 30 }, ]; const offset = move({ x: 10, y: -5 }); // offset is returning a function to be used in the map const path = polyline.map(parse).map(offset); // call the function for each parsed entry in the polyline console.log({ path });