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

filtrex-interpolated

v2.1.0

Published

Interpolate a string with filtrex expressions

Downloads

2

Readme

Filtrex-interpolated

Interpolate a string with filtrex expressions

Supports custom functions, custom getProperty function, custom tag functions and validation function

Examples

String Interpolation

import Interpolated from 'filtrex-interpolated'

const interpolated = new Interpolated('I am {age}')({age: 52})
expect(interpolated).toBe('I am 52')

String expression

import Interpolated from 'filtrex-interpolated'

const interpolated = new Interpolated('{age}')({age: 52})
expect(interpolated).toBe(52) // NOTE: type is Number and not String

Custom function (second argument to filtrex)

import Interpolated from 'filtrex-interpolated'
import {get} from 'lodash/object'

const scope = {
  users: [
    {name: 'U0'},
    {name: 'U1'},
    {name: 'U2'}
  ],
  userIndex: 1
}
const compiled = new Interpolated('Welcome {get(users, userIndex, "name")}', {
  custom: {
    get: (src, ...path) => get(src, path)
  }
})
expect(compiled(scope)).toBe('Welcome U1') 

Using custom getProperty function (3rd argument to filtrex)

class Name {
  constructor(first, last) {
    this.first = first
    this.last = last
  }

  get full() {
    return this.first + ' ' + this.last
  }
}

function getProperty(propertyName, get, object) {
  return (object instanceof Name) && propertyName === 'full' ? object[propertyName] : get(propertyName)
}
const interpolated4 = new Interpolated('{full of name}', { getProperty })({ name: new Name('FIRST', 'LAST') })
expect(interpolated4).toBe('FIRST LAST')

Using custom tag function

// import tag -> the default tag function
import Interpolated, { tag } from 'filtrex-interpolated' 

const interpolated1 = new Interpolated(
  'collection/{id of doc}',
  {
    tag: (strings, ...values) => tag(strings, ...values)
  }
)({})
expect(interpolated1).toBe('collection/')

Using validation function

// import validate -> a tag factory that takes a custom validation function as argument
// The custom validation function is called for every expression value and must return an array with 2 elements
//  [
//    boolean: true when validation for value was successful, 
//    any: interpolation value to return when validation failed
//  ]
// The validation function receives the following arguments:
//  validate(value, index, values, strings)
//    - value: value of the current expression
//    - index: index of the current expression in values array
//    - values: all expression values
//    - strings: all strings
import Interpolated, { tag, validate } from 'filtrex-interpolated'

const interpolated1 = new Interpolated(
  'collection/{id}',
  {
    tag: validate(value => [value !== undefined, undefined])
  }
)({})
// validation failed -> interpolated value = second element in array returned from validate function
expect(interpolated1).toBe(undefined)  

const interpolated2 = new Interpolated(
  'collection/{id}',
  {
    tag: validate(value => [value !== undefined, undefined]) 
  }
)({
  id: 'my-id'
})
// validation success -> second element in array returned from validate function is ignored
expect(interpolated2).toBe('collection/my-id') 

Changing interpolation regexp

import Interpolated from 'filtrex-interpolated'
Interpolated.INTERPOLATE_REGEXP = /\$\{([^\}]+)\}/g
const interpolated = new Interpolated('${age}')({age: 52})
expect(interpolated).toBe(52)