UNPKG

@irysius/grid-math

Version:

Tools to assist with grid math and algorithms

50 lines (49 loc) 2 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Direction; (function (Direction) { Direction[Direction["None"] = 0] = "None"; Direction[Direction["North"] = 1] = "North"; Direction[Direction["South"] = 2] = "South"; Direction[Direction["East"] = 4] = "East"; Direction[Direction["West"] = 8] = "West"; Direction[Direction["NorthEast"] = 5] = "NorthEast"; Direction[Direction["NorthWest"] = 9] = "NorthWest"; Direction[Direction["SouthEast"] = 6] = "SouthEast"; Direction[Direction["SouthWest"] = 10] = "SouthWest"; })(Direction = exports.Direction || (exports.Direction = {})); function hasDirection(value, targetDirection) { if (targetDirection === Direction.None) { return value === Direction.None; } else { return (value & targetDirection) === targetDirection; } } exports.hasDirection = hasDirection; function directionToString(value) { let strings = [ hasDirection(value, Direction.North) ? 'North' : null, hasDirection(value, Direction.South) ? 'South' : null, hasDirection(value, Direction.East) ? 'East' : null, hasDirection(value, Direction.West) ? 'West' : null ].filter(x => x); if (strings.length === 0) { return 'Direction.None'; } else { return `Direction.${strings.join('')}`; } } exports.directionToString = directionToString; exports.default = Direction; });