npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@slugbugblue/point

v1.0.0

Published

Simple integer point (x,y) class

Downloads

3

Readme

point.js API documentation

This module provides a class structure to work with x, y locations as a single point.

Since I don't need it to be super fancy, there isn't a lot of extra functionality here.

Example usage

import { Point } from '@slugbugblue/point.js'

const zero = new Point(0, 0)

const up = zero.up
console.log('up:', up.x, up.y) // up: 0 -1

const rt = zero.right
console.log('rt:', rt) // rt: Point(1,0)

const up2 = zero.dir('up')
console.log('up === up2 ?', up.eq(up2)) // up === up2 ? true

// distances
console.log(zero.distX(up), zero.distX(rt)) // 0 1
console.log(up.distX(rt), up.distY(rt)) // 0 0
console.log(zero.distance(up), zero.distance(rt)) // 1 1
console.log(up.distance(rt)) // 1.4142135623730951

API

Point class

A Point instance is a representation of an immutable point in two dimensional space, represented by an x and a y component. Since this implementation was created for game boards, we assume that all points are integers, though that is not a requirement, and the math will work equally well on non-integers.

constructor

new Point(fromPoint)
new Point(x, y)

You can create a point from either a point-like object (ie, any object with both an x and a y key with numeric values), or from individual x and y numbers. Once a point is created, it cannot be changed; however, you can use its functions to return other points relative to it.

properties

x and y immutable properties

You can return the individual x or y property of a point by accessing it directly. Attempts to change these properties will fail.

up, down, left, right calculated properties

Since points are immutable, to move a point, you need to create a new point. These calculated properties provide quick access to cardinal integer neighboring points.

around calculated property

Or, get an array of all four cardinal points around the existing point.

methods

dir(direction)

Pass in a string named direction (ie, one of up, down, left, or right) to get a point one integer away from the existing point in the direction indicated. This does the exact same thing as the calculated properties with these names, but it can be done using a variable instead.

distX(from), distY(from)

Returns the difference between the x or y properties of two points. This will always be represented by zero or a positive number.

distance(from)

Returns the distance between two points. This will never be a negative number.

eq(to)

Returns true if the given point or point-like object is at the same location as the existing point.

in(a, b)

Pass in two points (or point-like objects) to define a rectangular bounding box. Each point represents opposite corners of the box.

Returns true if the calling point is located inside the box or along any of its edges (ie, both bounding points are considered "inside").

Plays well with others

The Point class also includes functionality to provide useful default results when used in other contexts.

toString()

Returns a string representation of the point in the format Point(x,y). This allows simple logging calls with console.log(point).

toJSON()

JSON cannot encode special classes, but this class can be stored as a bare object with x and y keys when converted to JSON. Note, though, that when parsing the resultant JSON string, all Point functionality is not restored by default. For example:

const zero = new Point(0, 0)
const json = JSON.stringify(zero) // '{"x":0,"y":0}'
const jsonzero = JSON.parse(json) // { x: 0, y: 0 }

console.log(zero.eq(jsonzero)) // true
jsonzero.eq(zero) // TypeError: jsonzero.eq is not a function

// However, the object can easily be turned back into a Point:
const reconstituted = new Point(jsonzero)
console.log(reconstituted.eq(zero)) // true
util.inspect.custom()

When running in the node command line interpreter, a point will be pretty-printed using the magic of util.inspect.

License

Copyright 2022-2023 Chad Transtrum

Licensed under the Apache License, Version 2.0 (the "License"); you may not use the files in this project except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.