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

nolu

v0.0.3

Published

Node.js one-liner utility. Provides Ruby compatible options such as -n, -p, -a and more to extend Node.js's simple one-liner option -e

Downloads

9

Readme

nolu

Build Status

nolu is a small command-line tool to write one-liners in JavaScript (Node.js).

$ cat test.txt
Lorem 100
Ipsum 42
DolorSitAmet 1
$ cat test.txt | nolu -an --BEGIN "total = 0" -e "total += Number($F[1])" --END "console.log(total)"
143

The Node.js binary node provides nothing for one-liners except -e <code> option which simply executes eval(code). Nolu extends this -e with Ruby compatible options such as -n (process each line), -a (auto-split mode) and more.

Install

You need to have Node.js. Install nolu globally with:

npm i -g nolu

Leave off -g if you don't want to install globally.

Usage

$ nolu [options]

The following options are available:

|Option|Description| |:----:|:------------| |-a|Turns on auto-split mode when used with -n or -p. In auto-split mode, $F = $_.split(/\s/) at beginning of each loop.| |-B <code>, --BEGIN <code>|Execute code before any -es.| |-e <code>|Execute code. Multiple -e's are executed in given order.| |-E <code>, --END <code>|Execute code after all -e's.| |-f|Assume your code for -e is a function and call it with $_ and $F. When using with -p, print the return value instead of $_.| |-F <pattern>|Use pattern (regexp) for split() in auto-split mode (-a).| |-h, --help|Output the help.| |-j|Read process.stdin as JSON and set it to $_ before execute -e.| |-J|Apply JSON.stringify() for printing (-p).| |-l|Enables automatic line-ending processing, which means to chops every "\n" at the end of line. Works with -n or -p.| |-n|Read process.stdin and store it $_ and execute -e for each line.| |-p|Same with -n but print $_ (or the returned value when -f) after each loop.| |-t|Read process.stdin and set it to $_ before execute -e (after --BEGIN).| |-v, --version|Output the version.|

Examples

A Classical Text Operation: Calculate The Average

$ cat test.txt
Lorem 100
Ipsum 42
DolorSitAmet 1
$ cat test.txt | nolu -an --BEGIN 'total = 0, lines = 0' -e 'lines++; total += Number($F[1])' --END 'console.log(total/lines)'
47.666666666666664

Use --BEGIN and --END options (-B and -E for short, respectively) instead of BEGIN{...} and END{...} block in Ruby. Any variables are shared between all codes.

NOTE that you may have to use single quote (') to write codes to avoid expanding $_ as an environment variable by your shell.

Handling JSON

For simple JSON transformation, jq may be a good option. nolu provides another, more plain-JavaScript friendly way to write complex filter.

A simple example:

$ cat test.json
{
  "value": 42,
  "v2": 100,
  "foo": 20
}
$ cat test.json | nolu -jJpe '$_ = Object.keys($_).map(k => $_[k])'
[
  42,
  100,
  20
]

When -j option is specified, nolu reads stdin as JSON (and store it to $_). -J modifies -p to print JSON.stringify($_, null, 2).

More functional way

Instead of refering/assigning $_, you can use a function by -f options.

$ cat test.txt
Lorem
Ipsum
DolorSitAmet
$ cat test.txt | nolu -pfe 'line => line.toUpperCase()'
LOREM
IPSU
DOLORSITAMET

The return value of the funciton is used to print (-p).

Simple pipe

The -t option simply read the whole content of process.stdin and give it to $_.

$ cat test.txt
Lorem
Ipsum
DolorSitAmet
$ cat test.txt | nolu -te 'fs.writeFileSync("out.txt", $_.toLowerCase())'
$ cat out.txt
lorem
ipsum
dolorsitamet

Note that fs is the fs core module of Node.js. No fs = require("fs") is needed. Any code evaluated in nolu are able to refer the core modules with their names.

License

MIT. See LICENSE.