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

filtra

v1.0.11

Published

validate and filter incoming data

Downloads

15

Readme

Filtra

Validate and filter incoming data

Installation

npm i --save filtra

Example usage

const Filtra = require('filtra')
const moment = require('moment')

const request = {
  ownerid: 1,
  name: '  Gizmo ',
  weight: '.72',
  commentary: '  Don’t get him wet, \n\r  keep him out of bright light,   and\t never feed him after midnight   ',
  registered: '3/1/18',
  externalid: '416ac246-e7ac-49ff-93b4-f7e94d997e6b',
  price: '9.99',
  priceunit: 'usd',
  garbageProperty1: 'dq2dq2da2dre2e',
  garbageProperty2: {
    a: 'dasd'
  }
}

try {
  const data =
  Filtra(request)
  .int('ownerid').min(1).rename('owner_id')
  .str('name').trim().range(3, 100).toLowerCase().match(/^[a-z]+$/).rename('pet_name')
  .num('weight').min(0)
  .str('weightunit', 'kg').toLowerCase().any(['kg', 'g', 'oz', 'lb', 'cwt']).rename('weight_unit')
  .strOpt('commentary').trimDeep()
  .date('registered', '1/1/18').format('M/D/YY').range('1/1/18', moment()).toString('YYYY-MM-DD')
  .uuidV4('externalid').rename('external_id')
  .str('priceunit').toUpperCase().any(['USD', 'CENT']).rename('price_unit')
  .num('price').min(0).apply((price, props) => {
    if (props.price_unit === 'USD') {
      props.price_unit = 'CENT'
      return price * 100
    } else return price
  }).integer().rename('price_cents')
  .delete('price_unit')
  .flag('iscat', false).rename('is_cat')
  .filter()

  console.log(data)
  /* prints
  { owner_id: 1,
    pet_name: 'gizmo',
    weight: 0.72,
    weight_unit: 'kg',
    commentary: 'Don’t get him wet, keep him out of bright light, and never feed him after midnight',
    registered: '2018-03-01',
    external_id: '416ac246-e7ac-49ff-93b4-f7e94d997e6b',
    price_cents: 999,
    is_cat: false }
  */

} catch (error) {
  console.log('what\'s wrong:', error.message)
  throw error
}

try {
  Filtra(request)
  .int('ownerid').min(100)
} catch (error) {
  console.log('what\'s wrong:', error.message)
  console.log('param:', error.param)
  /* prints
    what's wrong: ownerid can't be less than 100
    param: ownerid
  */
  throw error
}

Usage

Filtra(sourceObject)

returns new FiltraChainLink, throws if !sourceObject

Chain methods

.filter()

retuns filtered object

.require(key)

thows if key doesn't exists in sourceObject

.delete(key)

delete already added property

Common property methods

.apply(handler)

executes handler(property, props) with current property value and already filtered properties. handler should return new value of property

.any(array) OR .in(array)

checks what property is in array

.check(handler, optionalMessage)

executes handler(property, [message]) with current property value. handler should return true or false. throws error if false

.rename(newKey)

rename property

Property types

.prop(key, default)

reads property by key, replaces undefined -> null

For next someOpt() null values are allowable:

.flag(key, default) .flagOpt(key, default)

reads boolean property: true, false, 'true', 'false', 't', 'f', 'yes', 'no'

.str(key, default) .strOpt(key, default)

reads string methods: min(minLenght), max(maxLenght), range(min, max), match(regExp), notEmpty(), toUpperCase(), toLowerCase(), trim(), trimDeep()

.num(key, default) .numOpt(key, default)

reads number methods: min(min), max(max), range(min, max), integer()

.int(key, default) .intOpt(key, default)

reads int methods: min(min), max(max), range(min, max)

.date(key, default).format(FORMAT) .dateOpt(key, default).format(FORMAT)

reads date as string methods: min(min), max(max), range(min, max), toString(format) (min & max are Moment instances or strings formatted with FORMAT)

.uuidV4(key, default) .uuidV4Opt(key, default)

reads UUIDv4 as string