Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielstjules/pjs
Pipeable javascript. Quickly filter, map, and reduce from the terminal
https://github.com/danielstjules/pjs
Last synced: 19 days ago
JSON representation
Pipeable javascript. Quickly filter, map, and reduce from the terminal
- Host: GitHub
- URL: https://github.com/danielstjules/pjs
- Owner: danielstjules
- License: mit
- Created: 2014-08-09T08:14:16.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-02-09T08:55:17.000Z (almost 2 years ago)
- Last Synced: 2024-11-13T00:34:11.282Z (29 days ago)
- Language: JavaScript
- Homepage:
- Size: 57.6 KB
- Stars: 419
- Watchers: 10
- Forks: 19
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs-cn - pjs - 用 JavaScript 实现的快速过滤、映射和累加器的管道命令 (包 / 命令行程序)
- awesome-nodejs-cn - pjs - **star:419** Pipeable JavaScript从终端快速筛选、映射和缩减 (包 / 命令行程序)
- awesome-nodejs - pjs - Pipeable JavaScript. Quickly filter, map, and reduce from the terminal. (Packages / Command-line apps)
- awesome-nodejs - pjs - Pipeable javascript. Quickly filter, map, and reduce from the terminal - ★ 345 (Command-line apps)
- awesome-node - pjs - Pipeable JavaScript. Quickly filter, map, and reduce from the terminal. (Packages / Command-line apps)
- awesome-nodejs-cn - pjs - 可移植的JavaScript。 从终端快速过滤,映射和缩小. (目录 / 命令行应用)
README
![pjs](http://danielstjules.com/github/pjs-logo.png)
Pipeable JavaScript - another utility like sed/awk/wc... but with JS! Quickly
filter, map and reduce from the command line. Features a streaming API.
Inspired by pipeable ruby.[![Build Status](https://api.travis-ci.org/danielstjules/pjs.svg?branch=master)](https://travis-ci.org/danielstjules/pjs)
## Overview
pjs is a cli tool that can accept input on stdin, or read from a list of files.
Its filter, map and reduce options take expressions to be run, in that order,
and applies them to the supplied input. The expressions themselves can contain
identifiers used by keys in String.prototype, which will automatically be bound
to the given line. This lets you save a bit of typing with your one-liners,
while still giving you access to all your JS string functions! Check out some
of the examples below to see how they translate.``` bash
# Return all lines longer than 5 chars
# => lines.filter(function(line) { return line.length > 5; });
ls -1 | pjs -f 'length > 5'# Count characters in each line
# => lines.map(function(line) { return line.length; });
ls -1 | pjs -m 'length'# Uppercase and pad each line
# => lines.map(function(line) { return ' ' + line.toUpperCase()"; });
ls -1 | pjs -m '" " + toUpperCase()'# Return lines longer than 5 chars, and remove any digits
# => lines
# .filter(function(line) { return line.length > 5; })
# .map(function(line) { return line.replace(/\d/g, ''); });
ls -1 | pjs -f 'length > 5' -m 'replace(/\d/g, "")'
```The current line and value can also be accessed via the `$` variable, and the
tool supports json output.``` bash
(echo 'foo' && echo 'foobar') | pjs -jm '{name: $, length: length}'
[
{"name":"foo","length":3},
{"name":"foobar","length":6}
]
```pjs also includes lodash functions, which can be accessed via the `_` object,
and chained using $$``` bash
echo 'hello' | pjs -m '_.upperFirst($)'
# Helloecho 'please-titleize-this-sentence' | \
pjs -m '$$.lowerCase().split(" ").map(_.upperFirst).join(" ")'
# Please Titleize This Sentence
```as well as Ramda and point-free style
``` bash
echo 'please-titleize-this-sentence' | \
pjs -m "R.compose(R.replace(/(^|\s)\w/g, R.toUpper), R.replace(/-/g, ' '))"
# Please Titleize This Sentence
```## Installation
It can be installed via `npm` using:
```
npm install -g pipeable-js
```## Usage
```
Usage: pjs [options] [files ...]Functions and expressions are invoked in the following order:
filter, map, reduceAll functions are passed the line ($) and index (i)
Built-in reduce functions: length, min, max, sum, avg, concat
Custom reduce expressions accept: prev, curr, i, array
Includes lodash (_), and can be chained using $$
Supports Ramda (R) and point-free styleOptions:
-h, --help output usage information
-V, --version output the version number
-i, --ignore ignore empty lines
-j, --json output as json
-f, --filter filter by a boolean expression
-m, --map map values using the expression
-r, --reduce reduce using a function or expression
```## Examples
### filter
``` bash
# Print all odd lines
# awk 'NR % 2 == 1' file
pjs -f 'i % 2 == 0' file# Print all lines greater than 80 chars in length
# awk 'length($0) > 80' file
pjs -f 'length > 80' file
```### map
``` bash
# Remove all digits
# tr -d 0-9 < file
pjs -m "replace(/\d/g, '')" file# Get second item of each line in csv
# awk -F "," '{print $2}' file
pjs -m 'split(",")[1]' file
```### reduce
``` bash
# Count lines in file
# wc -l file
# awk 'END { print NR }' file
pjs -r length file# Sum all decimal numbers in a file
# awk '{ sum += $1 } END { print sum }' file
# perl -nle '$sum += $_ } END { print $sum' file
pjs -r 'Number(prev) + Number(curr)' file
pjs -r '(+prev) + (+curr)' file
pjs -r sum file# Concatenate all lines in multiple files
# awk '{printf $0;}' file1 file2
# cat file1 file2 | tr -d '\n'
pjs -r concat file1 file2
```### mixed
``` bash
# Print the length of the longest line
# awk '{ if (length($0) > max) max = length($0) } END { print max }' file
pjs -m 'length' -r max file
```## Comparison
| Features | pjs | pythonpy | pru |
|-----------------------|------------|------------------------|------------|
| Language | JavaScript | Python | Ruby |
| Streaming | Yes | Limited [1] | Yes |
| Implementation | Streams | Iterables | Generators |
| Easy JSON output | Yes | No | No |
| WebscaleTM | YES | No | No |[1] Can't perform "tail -f logfile | py -x x"