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

@rsol/pipe

v1.0.0

Published

Pipe: Angular-like string pipes

Downloads

74

Readme

Pipe: Angular-like string pipes

Angular-like string pipes for js/ts

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save @rsol/pipe

Usage

import Pipe from '@rsol/pipe';
// or
const Pipe = require('@rsol/pipe').default;
// or
const { default: Pipe } = require('@rsol/pipe');

There are a list of possible handlers:

const handlers = {
  required: () => (str: string, initialValueString?: string) => {
    if (!str) {
      throw new PipeError(`Required value is empty: ${initialValueString}`);
    }
    return str;
  },
  toNull: () => (str: string) => str || null,
  toInt: () => (str: string) => (str ? +str : 0),
  toBool: () => (str: string) =>
    str === 'true' ? true : str === 'false' ? false : !!str,
  emptyStringToNull: () => (str: string) => (str === '' ? null : str),
  upper: () => (str: string) => str.toUpperCase(),
  lower: () => (str: string) => str.toLowerCase(),
  trim: () => (str: string) => str.trim(),
  trimEnd: () => (str: string) => str.trimEnd(),
  trimStart: () => (str: string) => str.trimStart(),
  at:
    (index = 0) =>
    (str: string) =>
      str.at(index),
  startsWith:
    (searchString: string = ' ', position?: number) =>
    (str: string) =>
      str.startsWith(searchString, position),
  endsWith:
    (searchString: string = '!', length?: number) =>
    (str: string) =>
      str.endsWith(searchString, length),
  includes:
    (searchString: string = ' ', position?: number) =>
    (str: string) =>
      str.includes(searchString, position),
  indexOf:
    (searchValue: string = ' ', fromIndex?: number) =>
    (str: string) =>
      str.indexOf(searchValue, fromIndex),
  lastIndexOf:
    (searchValue: string = ' ', fromIndex?: number) =>
    (str: string) =>
      str.lastIndexOf(searchValue, fromIndex),
  repeat:
    (count = 2) =>
    (str: string) =>
      str.repeat(count),
  replace:
    (searchValue: string = '  ', replaceValue: string = ' ') =>
    (str: string) =>
      str.replace(searchValue, replaceValue),
  padStart:
    (length = 10, fillString = '.') =>
    (str: string) =>
      str.padStart(length, fillString),
  padEnd:
    (length = 10, fillString = '.') =>
    (str: string) =>
      str.padEnd(length, fillString)
};

You can easily add your own handler:

new Pipe().addHandler('equalOne', () => () => 1);
new Pipe().addHandler('addOne', () => (str) => +str + 1);

You can change the last handler, arguments delimiter, functions delimiter:

new Pipe()
  .setLastHandler('addOne')
  .setArgDelimiter('|')
  .setFunctionDelimiter(':');

You can use it like this:

const pipe = new Pipe();

pipe.pipeString('A|padEnd:2');
// 'A.'

const source = new Map([
  ['a', 'a|upper'],
  ['b', 'B|lower']
]);
const result = pipe.pipeMap(source);
// Map(2) { 'a' => 'A', 'b' => 'b' }

const source = new Set(['a|upper', 'b|lower']);
const result = pipe.pipeSet(source);
// Set(2) { 'A', 'b' }

const source = ['a|upper', 'b|lower'];
const result = pipe.pipeArray(source);
// [ 'A', 'b' ]

const source = { a: 'a|upper', b: 'b|lower' };
const result = pipe.pipeObject(source);
// { a: 'A', b: 'b' }

You can call only one method for all supported types:

const pipe = new Pipe();

pipe.pipe('A|padEnd:2');
// 'A.'

const source = new Map([
  ['a', 'a|upper'],
  ['b', 'B|lower']
]);
const result = pipe.pipe(source);
// Map(2) { 'a' => 'A', 'b' => 'b' }

const source = new Set(['a|upper', 'b|lower']);
const result = pipe.pipe(source);
// Set(2) { 'A', 'b' }

const source = ['a|upper', 'b|lower'];
const result = pipe.pipe(source);
// [ 'A', 'b' ]

const source = { a: 'a|upper', b: 'b|lower' };
const result = pipe.pipe(source);
// { a: 'A', b: 'b' }

If you use wrong pipe string, you will get just a value:

new Pipe().pipeString('1|wrongPipe');
// '1'

You can use chain of pipes:

new Pipe().pipe('1|toInt|toBool');
// true

Usefully examples:

const pipe = new Pipe();

// Get environment variable
pipe.addHandler('env', () => (str) => process.env[str]);
pipe.pipe('DB_PORT|env');

// get express request query parameter (`req` is a global variable)
pipe.addHandler('fromQuery', () => (name) => req.query[name]);
const { search, page, limit } = pipe.pipeObject({
  search: 'q|fromQuery|required',
  page: 'page|fromQuery|toInt',
  limit: 'limit|fromQuery|toInt'
});

License

MIT License © Slava Rudnev