pipeable-js (pjs)

repository·master·Indexed 19 days ago

https://github.com/danielstjules/pjs

A command-line utility inspired by sed, awk, and wc that allows developers to filter, map, and reduce streaming input from stdin or files using JavaScript expressions. Version 0.10.0 provides built-in support for Lodash (_), Ramda (R), JSON output, and expression chaining via $$. It processes data in a specific pipeline order: filter, then map, then reduce.

Tokens
1.5K
Snippets
10
Records
10
Agent score
15%

What's inside pipeable-js

  1. How pjs works: Expressions and Variables

    master

    pjs is a CLI tool that processes input from stdin or files using a streaming API. It applies operations in a specific order: filter, then map, then reduce.

    To make one-liners easier, pjs automatically binds identifiers from String.prototype to the current line. You can also use specific variables to access data:

    • $ : The current line (value).
    • i : The current index.
    • prev : The accumulated value (used in custom reduce expressions).
    • curr : The current value (used in custom reduce expressions).
    • array : The full array (used in custom reduce expressions).

    Additionally, pjs provides access to:

    • _ : Lodash functions.
    • R : Ramda functions.
    • $$ : Used for chaining expressions.
    # Example of using $ and String.prototype methods
    echo 'foo' | pjs -m 'toUpperCase()'
  2. Use the pjs CLI to filter, map, and reduce data

    master

    The pjs CLI tool allows you to process text data from stdin or files through a pipeline of transformations. Functions and expressions are invoked in the following order:

    1. filter
    2. map
    3. reduce

    All functions are passed the current line ($) and the index (i).

    Input Sources

    • Files: Provide file paths as arguments: pjs [options] [files ...]
    • Stdin: If no files are provided, the tool reads from standard input.

    Transformation Logic

    • Filter: Uses a boolean expression to decide which lines to keep.
    • Map: Transforms each line using an expression.
    • Reduce: Aggregates lines into a single value.
      • Built-in reduce functions: length, min, max, sum, avg, concat.
      • Custom reduce expressions accept: prev, curr, i, array.

    Advanced Features

    • Chaining: Use $$ to chain operations.
    • Libraries: Includes lodash (accessible via _) and supports Ramda (accessible via R) in point-free style.
    • Point-free style: If a script starts with R. and does not end with a closing parenthesis and semicolon, pjs automatically appends ($) to invoke it with the current line.
    pjs -f "$.length > 5" -m "$.toUpperCase()" input.txt
  3. Map values with -m

    master

    Use the -m (or --map) flag to transform each line using a JavaScript expression. You can use $ to refer to the line or access String.prototype methods directly.

    # Remove all digits
    pjs -m "replace(/\d/g, '')" file
    
    # Get second item of each line in a CSV
    pjs -m 'split(",")[1]' file
    pjs -m "replace(/\d/g, '')" file
  4. Use Lodash, Ramda, and Chaining in pjs

    master

    pjs supports advanced functional programming patterns:

    • Lodash: Access via the _ object.
    • Ramda: Access via the R object.
    • Chaining: Use $$ to chain expressions.
    # Use Lodash to uppercase the first letter
    echo 'hello' | pjs -m '_.upperFirst($)'
    
    # Chain expressions using $$ and Lodash
    echo 'please-titleize-this-sentence' | \
    pjs -m '$$.lowerCase().split(" ").map(_.upperFirst).join(" ")'
    
    # Use Ramda for point-free style
    echo 'please-titleize-this-sentence' | \
    pjs -m "R.compose(R.replace(/(^|\s)\w/g, R.toUpper), R.replace(/-/g, ' '))"
    echo 'hello' | pjs -m '_.upperFirst($)'
  5. Output as JSON with -j

    master

    Use the -j (or --json) flag to output results as a JSON array. This is useful when combining the current line ($) with other properties in a map operation.

    (echo 'foo' && echo 'foobar') | pjs -jm '{name: $, length: length}'
    # Output:
    # [
    #   {"name":"foo","length":3},
    #   {"name":"foobar","length":6}
    # ]
    (echo 'foo' && echo 'foobar') | pjs -jm '{name: $, length: length}'
  6. Filter lines with -f

    master

    Use the -f (or --filter) flag to keep only lines that satisfy a boolean expression. Expressions can use length (from String.prototype) or i (the index).

    # Print all odd lines (using index i)
    pjs -f 'i % 2 == 0' file
    
    # Print all lines greater than 80 chars in length
    pjs -f 'length > 80' file
    pjs -f 'i % 2 == 0' file
  7. Reduce values with -r

    master

    Use the -r (or --reduce) flag to aggregate lines into a single value. You can use built-in function names or custom expressions using prev and curr.

    Built-in functions: length, min, max, sum, avg, concat.

    # Count lines in a file using built-in length
    pjs -r length file
    
    # Sum all decimal numbers in a file using a custom expression
    pjs -r 'Number(prev) + Number(curr)' file
    
    # Concatenate all lines in multiple files
    pjs -r concat file1 file2
    pjs -r length file
  8. Use the pjs CLI

    master

    The pjs command accepts options and optional file arguments. Operations are executed in the order: filter $\rightarrow$ map $\rightarrow$ reduce.

    Options:

    • -h, --help: Output usage information.
    • -V, --version: Output the version number.
    • -i, --ignore: Ignore empty lines.
    • -j, --json: Output results as JSON.
    • -f, --filter <exp>: Filter lines by a boolean expression.
    • -m, --map <exp>: Map values using the expression.
    • -r, --reduce <func|exp>: Reduce using a built-in function or a custom expression.

    Built-in reduce functions: length, min, max, sum, avg, concat.

    Usage: pjs [options] [files ...]
  9. Reference the pjs CLI options

    master

    The following flags are available for the pjs command line interface:

    FlagLong FlagDescription
    -i--ignoreIgnore empty lines
    -j--jsonOutput the result as JSON
    -f--filter <exp>Filter lines by a boolean expression
    -m--map <exp>Map values using the provided expression
    -r--reduce <func|exp>Reduce using a built-in function or a custom expression

    Exit Codes

    • 3: Invalid expression syntax (SyntaxError or ReferenceError).
    • 1: Other uncaught exceptions.
    pjs --help