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

@tanzanite/wolfram-alpha

v1.1.0

Published

Wolfram|Alpha API Libraries

Downloads

25

Readme

fork of wolfram-alpha-api

Copyright (c) 2018, Wolfram Alpha LLC. Licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Wolfram|Alpha APIs NPM Library

This is a class that allows Node.js applications/backends to simply call (some of) the Wolfram|Alpha APIs.

Before you can use any of the APIs described below, you will need an 'AppID' from the Wolfram|Alpha Developer Portal. (Note, because AppIDs may not be distributed, this library is not intended for use in the frontend.)

First, import the class, and instantiate it with your 'AppID':

import WolframAlphaAPI from "@tanzanite/wolfram-alpha";
const waApi = WolframAlphaAPI("DEMO-APPID");

Then, call one of the following 'get' methods with either a string of a query or an object of parameters, and it will return a Promise that will resolve the result or reject an Error.

getFull(input)

If 'input' is a string, it will call the Wolfram|Alpha Full Results API, and will return a Promise. This Promise will either resolve with an Object of results, or will reject with an Error.

waApi.getFull("sin x").then(console.log).catch(console.error);
// { success: true, error: false, numpods: 13, datatypes: '', ...

waApi.getFull("F9TVlu5AmVzL").then(console.log).catch(console.error);
// { success: false, error: false, numpods: 0, datatypes: '', ...

waApi
  .getFull("sin(x)")
  .then(queryresult => {
    const pods = queryresult.pods;
    const output = pods
      .map(pod => {
        const subpodContent = pod.subpods
          .map(subpod => `  <img src="${subpod.img.src}" alt="${subpod.img.alt}">`)
          .join("\n");
        return `<h2>${pod.title}</h2>\n${subpodContent}`;
      })
      .join("\n");
    console.log(output);
  })
  .catch(console.error);
// <h2>Input</h2>
//   <img src="http://www1.wolframalpha.com/Calculate/MSP/MSP2831bhah3hgdb1d0a6h0000270i2f8f935cf432?MSPStoreType=image/gif&s=14" alt="sin(x)">
// <h2>Plots</h2>
//   <img src="http://www1.wolframalpha.com/Calculate/MSP/MSP2841bhah3hgdb1d0a6h000018c0hieb8d6cc753?MSPStoreType=image/gif&s=14" alt="">
//   <img src="http://www1.wolframalpha.com/Calculate/MSP/MSP2851bhah3hgdb1d0a6h00005ge64a5i3g8g8i6f?MSPStoreType=image/gif&s=14" alt="">
// ...

'input' may also be an Object of parameters. (For more information, see the Wolfram|Alpha Full Results API Documentation - Parameter Reference)

waApi
  .getFull({
    input: "pikachu",
    includepodid: "Statistics:PokemonData",
    format: "plaintext"
  })
  .then(queryresult => {
    console.log(queryresult.pods[0].subpods[0].plaintext);
  })
  .catch(console.error);
// hit points | 35
// attack | 55
// defense | 40
// ...

Note: We are defaulting the 'output' parameter to 'json'. If you set output to 'xml', we will resolve a string of XML.

waApi
  .getFull({
    input: "weather in miami",
    output: "xml"
  })
  .then(console.log)
  .catch(console.error);
// "<?xml version='1.0' encoding='UTF-8'?>
// <queryresult success='true'
//     error='false'
//     numpods='0'
// ..."

getSimple(input)

If 'input' is a string, it will call the Wolfram|Alpha Simple API, and will return a Promise. This Promise will either resolve with a Data URI, or will reject with an Error.

waApi.get("where is the ISS?").then(console.log).catch(console.error);
// "data:image/gif;base64,R0lGODlhHAJlA/cAAAAAAAAEAAgICAsNCxwdHBAUECkqKTk8O..."

waApi.getSimple("F9TVlu5AmVzL").then(console.log).catch(console.error);
// Error: Wolfram|Alpha did not understand your input

waApi
  .getSimple("time until christmas")
  .then(url => {
    console.log(`<img src="${url}">`);
  })
  .catch(console.error);
// "<img src="data:image/gif;base64,R0lGODlhHAKLAvcAAAAAAAAEAAgICAgMCBAQEBA..."

'input' may also be an Object of parameters. (For more information, see the Wolfram|Alpha Simple API Documentation)

waApi
  .getSimple({
    i: "What planes are flying overhead?",
    width: 320,
    background: "224466",
    foreground: "white"
  })
  .then(console.log)
  .catch(console.error);
// "data:image/gif;base64,R0lGODlhQAFvBPcAADI+WSZAYloyRyBEWiBIWhpGWRhIS..."

getShort(input)

If 'input' is a string, it will call the Wolfram|Alpha Short Answers API, and will return a Promise. This Promise will either resolve with a string, or will reject with an Error.

waApi.getShort("16th president of us").then(console.log).catch(console.error);
// Abraham Lincoln (from March 4, 1861 to April 15, 1865)

aApi.getShort("F9TVlu5AmVzL").then(console.log).catch(console.error);
// Error: Wolfram|Alpha did not understand your input

const formatAnswer = answer => `<strong class="answer">${answer}</strong>`;
waApi
  .getShort("20! seconds in years")
  .then(data => {
    console.log(formatAnswer(data));
  })
  .catch(console.error);
// <strong class="answer">77.1 billion years</strong>

'input' may also be an Object of parameters. (For more information, see the Wolfram|Alpha Short Answers API Documentation)

waApi
  .getShort({
    i: "distance between new york and london",
    units: "metric"
  })
  .then(console.log)
  .catch(console.error);
// "5585 kilometers"

getSpoken(input)

If 'input' is a string, it will call the Wolfram|Alpha Spoken Results API, and will return a Promise. This Promise will either resolve with a string, or will reject with an Error.

waApi.getSpoken("when is labor day in 2020").then(console.log).catch(console.error);
// "The answer is Monday, September 7, 2020"

waApi.getSpoken("F9TVlu5AmVzL").then(console.log).catch(console.error);
// Error: Wolfram Alpha did not understand your input

const formatAnswer = answer => `<strong class="answer">${answer}</strong>`;
waApi
  .getSpoken("how far away is mars?")
  .then(data => {
    console.log(formatAnswer(data));
  })
  .catch(console.error);
// <strong class="answer">The answer is about 2.16 astronomical units</strong>

'input' may also be an Object of parameters. (For more information, see the Wolfram|Alpha Spoken Results API Documentation)

waApi
  .getSpoken({
    i: "how tall is the tallest building in chicago",
    units: "metric"
  })
  .then(console.log)
  .catch(console.error);
// "The answer is about 442 meters"