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

nemathode

v1.0.5

Published

Math libs manager with single syntax (nemathode)

Downloads

18

Readme

Quick links


Nemathode is array based mathematical expression notation (further in the text: nematode/s, expression/s).


[0, '+', 1] === (0 + 1)

[0, '+', 1, '*', [2, '/', 3, '-', 4]] === (0 + 1б * (2 / 3 - 4))

Nemathode.js is a JavaScript library for nemathodes evaluation. It lets you to combine any number of libraries while leaving the format of the expressions unchanged (aggregator of solutions under one format)

const res1 = nemathode.evaluate([0, '+', 1]); // 1
const res2 = nemathode.evaluate([0, '+', 1, '*', [2, '/', 1, '-', 3]]); // -1

dependencies bundle size npm npm


Installation

$ npm install --save nemathode

or

$ yarn add nemathode

Fast introduction

Read article here

Examples

  • binary operators
const sumOperator = nemathode.evaluate([1, '+', 1]); // 2
const mulOperator = nemathode.evaluate([1, '*', 1]); // 1
  • functions
const customFunction = nemathode.evaluate([1, '+', ['abs', 1, 2, 3]]); //a rguments are not evaluated (future)
const singleFunction = nemathode.evaluate([['abs', 1, 2, 3]]; // single function syntax (it's not convinient, but for now it's only way)
const boolResult = nemathode.evaluate([['areEqual', 1, 2, 3]]); // returns boolean
// etc
  • math constants
const piConst = nemathode.mathConstants.PI;
const piConstInUse = nemathode.evaluate([1, '+', 'PI']);
  • nested expressions
const resOfNestedExp = nemathode.evaluate([1, '+', 'PI', '*', ['abs', 1, 2, 3], '-', [1, '+', [1, '/', 25]]]);

Configuration

mathConstants

Literally, mathematical constants

  • full example

functions

Set of records with implementations

  • full example

toInputType

It seems excessive but not everyone using pure js functionality. There are set of libraries where intermediate type is not a number, for example: bignumber.js, decimal.js etc (we have on our website set of configs for most popular libs). So you can configure entry handler for them like

const toInputType = (val: unknown): Decimal | unknown => {
	if (typeof val === 'number') {
		const input = new Decimal(val);

		return input;
	}

	return val;
};

toOutputType

Opposite of toInputType. See at an example

const toOutputValue = (val: unknown): number | boolean => {
	if (val instanceof Decimal) {
		return val.toNumber();
	}

	return val;
};

binaryOperators

Let's look at one of examples

...
    '+': {
        precedence: 1,
        implementation: (l: number, r: number): number => {
            return l + r;
        },
    }
...

It's a configuration for operator that consists from two (by now) fields: precedence, implementation (the same as in functions).

example (pure js)

const config = {
		mathConstants: {
			'E': Math.E,
			'LN2': Math.LN2,
			'LN10': Math.LN10,
			'LOG10E': Math.LOG10E,
			'LOG2E': Math.LOG2E,
			'PI': Math.PI,
			'SQRT1_2': Math.SQRT1_2,
			'SQRT2': Math.SQRT2,
		},
		functions: {
			'abs': {
				implementation: Math.abs,
			},
			'min': {
				implementation: Math.min,
			},
			'areEqual': {
				implementation: (...args: number[]): boolean => {
					return args.every(v => v === args[0]);
				},
			},
		},
		toInputType: (val: unknown): unknown => val,
		toOutputValue: (val: unknown): number | boolean => val as number | boolean,
		binaryOperators: {
			'+': {
				precedence: 1,
				implementation: (l: number, r: number): number => {
					return l + r;
				},
			},
			'-': {
				precedence: 1,
				implementation: (l: number, r: number): number => {
					return l - r;
				},
			},
			'*': {
				precedence: 2,
				implementation: (l: number, r: number): number => {
					return l * r;
				},
			},
			'/': {
				precedence: 2,
				implementation: (l: number, r: number): number => {
					return l / r;
				},
			},
			'%': {
				precedence: 2,
				implementation: (l: number, r: number): number => {
					return l % r;
				},
			},
		},
	};

API

import Nemathode from 'nemathode';
import { config } from './config';

const nemathode = Nemathode({
    ...config,
});

// method for expression evaluation
const res = nemathode.evaluate([...]);

// example of getting math constant
nemathode.mathConstants.SOME_CONST;

License

MIT © Kas Elvirov