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

@binaryme/suncalc-callback

v1.2.1

Published

A callback/promise approach to SunCalc

Downloads

5

Readme

A callback/promise approach to SunCalc. SunCalc provides Date values for a specific date and geolocation.

The getTimes function is passed as a parameter where needed to make it possible to isolate different instances of the SunCalc library.

This library assists with a more procedural approaches.

Usage

Setup a callback function to be called as sunlight events occur:

import { getTimes } from 'suncalc';

onTimes(35.2029, -111.66485, getTimes, name => {
  console.log(`It is now ${name}`);
})

There is also a Promise based function.

import { getTimes } from 'suncalc';

while (true) {
  const [name] = await nextTime(35.2029, -111.66485, getTimes);
  console.log(`It is now ${name}`);
}

If you need the callback to be called 5 minutes prior to every sunlight event an optional parameter can be used.

onTimes(lat, lon, getTimes, (name, date) => {
  console.log(`${name} in 5 min. The time will then be ${date}`);
}, 1000 * 60 * 5)

Same for the promise.

while (true) {
  const [name, date] = await nextTime(lat, lon, getTimes, 1000 * 60 * 5);
  console.log(`${name} in 5 min. The time will then be ${date}`);
}

To get the next and previous sunlight events use getPrevNext.

const { prev, next } = getPrevNext(new Date(), lat, lon, getTimes);
const [prevName, prevDate] = prev;
const [nextName, nextDate] = next;
console.log(`${prevName} was at ${prevDate.toTimeString()}`);
console.log(`${nextName} will be at ${nextDate.toTimeString()}`);

To filter the sunlight events of getPrevNext an optional parameter may be used.

const { prev, next } = getPrevNext(new Date(), lat, lon, getTimes, ['solarNoon']);
const [prevName, prevDate] = prev;
const [nextName, nextDate] = next;
console.log(`Pervious solar noon was at ${prevDate.toTimeString()}`);
console.log(`Next solar noon will be at ${nextDate.toTimeString()}`);

If you are a fan of rxjs an Observable can easily be made.

const $ = new Observable<[TimeName, Date]>(subscriber => {
  const cancel = onTimes(lat, lon, getTimes, (name,date) => subscriber.next([name,date]), msInAdvance);
  return () => cancel();
});
$.subscriber(([name,date]) => {
  console.log(`It is now ${name}`);
})