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

point.js

v0.0.5

Published

A JavaScript utility for point calculations

Downloads

5

Readme

Point.js

npm size dependency-count

maintained mit

Point.js is a JavaScript library for point calculations. Point.js has a general definition of "point" and most methods takes multiple parameters, allowing for complex, interdependent chaining and transformations.

Install

npm i point.js

Usage

Point.js is exported as a named export, under P. It can be instantiated with or without using the new operator.

import { P } from 'point.js';

const point = P()
// const point = new P()

// Do some math with multiple parameters and different point signatures
point.add([ 20, 33 ], { x: 2, y: 5 }, 5)
// => P { x: 27, y: 43 }

// Can be chained
point.mult(1.5).floor()
// => P { x: 40, y: 64 }

// End with transformnation
point.toArray()
// => [ 40, 64 ]

API

Static methods

P.add(...points: GeneralPoint[]): Point

Adds addends, instantiating P with the sum.

P.sub(...points: GeneralPoint[]): Point

Subtracts subtrahends, instantiating P with the difference.

P.mult(...points: GeneralPoint[]): Point

Multiplies multiplicands, instantiating P with the product.

P.div(...points: GeneralPoint[]): Point

Divides divisors, instantiating P with the product.

P.mod(...points: GeneralPoint[]): Point

Divides divisors, instantiating P with the modulus.

P.pow(...points: GeneralPoint[]): Point

Exponentiates exponents, instantiating P with the product.

P.random(...args: [max: number] | [min: number, max: number]): Point

Generates a random number, instantiating P with the result.

P.min(...points: GeneralPoint[]): Point

Resolves the min x and min y of points, instantiating P with the result.

P.max(...points: GeneralPoint[]): Point

Resolves the max x and max y of points, instantiating P with the result.

Instance methods

p.set(...point: [GeneralPoint] | [x: number, y: number] | undefined[]): this

Sets this to a given point.

p.add(...points: GeneralPoint[]): this

Adds addends to this, mutating this to the sum.

p.sub(...points: GeneralPoint[]): this

Subtracts subtrahends from this, mutating this to the difference.

p.mult(...points: GeneralPoint[]): this

Multiplies this by multiplicands, mutating this to the product.

p.div(...points: GeneralPoint[]): this

Divides this by divisors, mutating this to the quotient.

p.mod(...points: GeneralPoint[]): this

Divides this by divisors, mutating this to the modulus.

p.pow(...points: GeneralPoint[]): this

Exponentiates this by exponents, mutating this to the product.

p.ceil(precision?: number): this

Mutates this by rounding up to precision.

p.floor(precision?: number): this

Mutates this by rounding down to precision.

p.round(precision?: number): this

Mutates this by rounding to precision.

p.trunc(precision?: number): this

Mutates this by rounding towards 0 to precision.

p.sq(): this

Mutates this by its square.

p.sqrt(): this

Mutates this by its square root.

p.cb(): this

Mutates this by its cube.

p.cbrt(): this

Mutates this by its cube root.

p.abs(): this

Mutates this to its absolute.

p.inv(): this

Mutates this to its inverse.

p.clamp([upper: number] | [lower: number, upper: number]): this

Mutates this by clamping it within the inclusive lower and upper bounds.

P(10, -10).clamp(-5, 5)
// => P { x: 5, y: -5 }

p.between(point: GeneralPoint, distance?: number): this

Mutates this to the point between this and point. The fraction of the distance is determined by the multiplicand distance. distance is expected to be a number between 0-1. where 0 becomes the value ofthis and 1 the value of point

P(10, -10).between(-20, 20)
// => P { x: -5, y: 5 }

p.getSum(): number

Gets the sum of this.

p.getDistSq(point: GeneralPoint): number

Get the square distance between this and point.

p.getDist(point: GeneralPoint): number

Get the distance between this and point.

p.clone(): this

Clones this to a new instance of P.

const p = P(10, -10)
const clone = p.clone()

Object.is(p, clone) // => false 
p.is(clone) // => true 

p.copy(point: Point): this

Copies the properties of point to this. Essentially an alias for this.set .

const p = P(10, -10)
const otherP = P(0, 0)

p.copy(otherP)

Object.is(p, clone) // => false 
p.is(clone) // => true 

p.random(...args: [max: number] | [min: number, max: number]): this

Mutates this to a random number.

p.min(...points: GeneralPoint[]): this

Mutates this to the respective min x and min y of this and points.

p.max(...points: GeneralPoint[]): this

Mutates this to the respective max x and max y of this and points.

p.operation(resolver: (n: number) => number): this

Mutates this by executing a method on its properties.

P(NaN, 10).operation(n => Number.isNaN(n) ? 0 : n)
// => P { x: 0, y: 10 }

p.transform<T>(resolver: (p: this) => T): T

Transforms this to the return type of the resolver.

P(20, 10).transform(p => ({ width: p.x, height: p.y }))
// => P { width: 20, height: 10 }

p.check(resolver: (p: this) => boolean): boolean

Executes a test on this.

P(NaN, 10).operation(p => Number.isNaN(p.x) || Number.isNaN(p.y))
// => true

p.is(point: GeneralPoint, threshold?: number): boolean

Compares the properties of this with point within the threshold.

P(10, 10).is([11,11]) // => false

P(10, 10).is([13,7], 5) // => true

p.toObject(): {x: number, y: number}

Transforms this into an object.

p.eject(): {x: number, y: number}

Alias for p.toObject.

p.toArray(): [x: number, y: number]

Transforms this into an array.

p.toString(): `{x: ${number}, y: ${number}}`

Transforms this into a string.

p.clg(): this

Executes console.log on this.

P(20, 10).clg()

/*
___In the console___
P {x: 20, y: 10}
*/