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

array-expression

v0.0.2

Published

Formatter declaration that can be transported through network and executed securely.

Downloads

3

Readme

Array Expression

Introduction

This library is created to provide formatter declaration that can be transported through network and executed securely. The formatter is written in JSON and it's highly configurable. This formatter declaration is called "array expression".

Syntax

Expression array syntax is specified below:

[expression_name, arg0, arg1, ...]

Expression is categorized into 4 different types:


Type expressions

boolean

Converts value into boolean.

['boolean', value]: boolean

string

Converts value into string.

['string', value]: string

Math expressions

+

Performs addition and return the resulting number.

['+', number1, number2]: number

-

Performs subtraction and return the resulting number.

['-', number1, number2]: number

*

Performs multiplication and return the resulting number.

['*', number1, number2]: number

/

Performs division and return the resulting number.

['/', number1, number2]: number

%

Performs remainder operation and return the resulting number.

['%', number1, number2]: number

^

Returns number1 to the power of number2.

['^', number1, number2]: number

sqrt

Returns the square root of a number.

['sqrt', number]: number

abs

Returns the absolute value of a number.

['abs', number]: number

sin

Returns the sine of a number.

['sin', number]: number

cos

Returns the cosine of a number.

['cos', number]: number

tan

Returns the tangent of a number.

['tan', number]: number

log

Returns natural logarithm (base e) of a number.

['log', number]: number

floor

Returns the greatest integer less than or equal to a value.

['floor', number]: number

ceil

Returns the smallest integer greater than or equal to a value.

['ceil', number]: number

round

Rounds a number to the nearest integer and return the resulting number.

['round', number]: number

min

Returns the lowest number in the arguments.

['min', number1, number2, ...]: number

max

Returns the highest number in the arguments.

['max', number1, number2, ...]: number

String expressions

concat

Concatenate the arguments into one single string.

['concat', value1, value2, ...]: string

downcase

Converts all the alphabetic characters in a string to lowercase and return the resulting string.

['downcase', string]: string

upcase

Converts all the alphabetic characters in a string to uppercase and return the resulting string.

['upcase', string]: string

number-format

Format a number by specifying the minimum and maximum fraction digits.

['number-format', number, minimum_fraction_digits, [maximum_fraction_digits]]: string

Decision expressions

!

Negates a value and return the resulting boolean.

['!', value]: boolean

==

Performs strict equality operation and return the resulting boolean.

['==', value]: boolean

!=

Performs strict inequality operation and return the resulting boolean.

['!=', value]: boolean

<

Returns true if number1 or string1 is less than number2 or string2, otherwise return false.

['<', number1 | string1, number2 | string2]: boolean

<=

Returns true if number1 or string1 is less than or equal to number2 or string2, otherwise return false.

['<=', number1 | string1, number2 | string2]: boolean

>

Returns true if number1 or string1 is greater than number2 or string2, otherwise return false.

['>', number1 | string1, number2 | string2]: boolean

>=

Returns true if number1 or string1 is greater than or equal to number2 or string2, otherwise return false.

['>=', number1 | string1, number2 | string2]: boolean

all

Returns true if all the values evaluate to true.

['all', value1, value2, ...]: boolean

any

Returns true if any of the values evaluate to true.

['any', value1, value2, ...]: boolean

if

Perform if else operation.

['if', condition1, output1, condition2, output2, ..., fallback_output]: boolean

Expression can be nested

Please see the example below:

// Returns 'equal to two'
exp(['if', ['==', 5, 1], 'equal to one', ['==', 2, 2], 'equal to two', 'failure']);

Expression can be injected with data

By specifying a variable using wildcard, we can inject data into the expression.

Please see the example below:

const expression: Expression = [
  'if',
  ['==', '$animal', 'cat'],
  'meow',
  ['==', '$animal', 'dog'],
  'woof',
  'Wa-pa-pa-pa-pa-pa-pow!',
];

// Returns 'meow'
exp(expression, { animal: 'cat' });

// Returns 'woof'
exp(expression, { animal: 'dog' });

// Returns 'Wa-pa-pa-pa-pa-pa-pow!'
exp(expression, { animal: 'fox' });

Realworld example

Below, we will convert unit from liter per minute into US gallon per hour.

const data: Data = {
  // value contains liter per minute
  value: 5,
};

// Returns '79.3 gallon/hour'
exp(
  ['concat', ['number-format', ['*', ['*', '$value', 0.2641722], 60], 0, 1], ' ', 'gallon/hour'],
  data
);

License

array-expression is MIT licensed.