Skip to content

Instantly share code, notes, and snippets.

@belichuk
Created December 6, 2021 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belichuk/6180e21cb48b595868369bd476581b6b to your computer and use it in GitHub Desktop.
Save belichuk/6180e21cb48b595868369bd476581b6b to your computer and use it in GitHub Desktop.
const shift = (offset, points) => points.map(pt => {
const point = typeof pt == 'object' ? pt : JSON.parse(pt);
return {x: point.x + offset.x, y: point.y + offset.y};
});
const polyline = [
{x: 0, y: 0},
{x: 10, y: 10},
'{"x": 20, "y": 20}',
{x: 30, y: 30},
];
const shifted = shift({x: 10, y: 10}, polyline);
console.log(shifted);
console.log(polyline);
@tshemsedinov
Copy link

  • Don't use ==
  • I'd prefer to avoid ternary here
    • Use collection for branching (strategy pattern)
  • Respect separation of concerns principle
    • Decompose shift to parsing, iteration, and moving functions
    • Use class or closure to hold intermediate state
    • Avoid long expressions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment