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

recurpolate

v1.2.1

Published

Recursive, self-referential interpolation of objects.

Downloads

13

Readme

recurpolate

Recursive interpolation of objects.

npm Build Status Code Coverage JavaScript Style Guide

A portmanteau of "recurse" and "interpolate".

Useful e.g. to DRY up configurations, à la Spring.

Supports self-reference. Supports array references (e.g. a.b[3].c). Should detect circular references and throw error.

Install

npm i -S recurpolate (copy) yarn add recurpolate (copy)

Use

import recurpolate from 'recurpolate'

const obj = {
  api: {
    base: 'https://api.example.com',
    v0: '${api.base}/v0',
    v1: '${api.base}/v1',
    last: '${api.v1}'
  },
  services: {
    profile: '${api.last}/profile',
    fullProfile: '${services.profile}?view=full',
    login: '${api.last}/login'
  }
}

const resolved = recurpolate(obj)

console.log(resolved)

yields:

{
  base: {
    api: {
      base: 'https://api.example.com',
      v0: 'https://api.example.com/v0',
      v1: 'https://api.example.com/v1',
      last: 'https://api.example.com/v1'
    },
  },
  services: {
    profile: 'https://api.example.com/v1/profile',
    fullProfile: 'https://api.example.com/v1/profile?view=full',
    login: 'https://api.example.com/v1/login'
  }
}

Using the context option (or something like lodash.merge), you can easily created environment-specific configurations that all inherit from a default, e.g.:

// default.json
{
  "api": {
    "products": "${api.base}/products/v1",
    "coupons": "${api.base}/coupons/v2",
  }
}
// development.json
{
  "api": {
    "base": "https://dev.api.example.com"
  }
}
// production.json
{
  "api": {
    "base": "https://api.example.com"
  }
}
import recurpolate from 'recurpolate'
import defaultConfig from './default.json'
import developmentContext from './development.json'
import productionContext from './production.json'

const isProduction = process.env.NODE_ENV === 'production'
const context = isProduction ? productionContext : developmentContext

const config = recurpolate(defaultConfig, {
  context
})

results in config being this in production:

{
  "api": {
    "products": "https://api.example.com/products/v1",
    "coupons": "https://api.example.com/coupons/v2",
  }
}

but this in development:

{
  "api": {
    "products": "https://dev.api.example.com/products/v1",
    "coupons": "https://dev.api.example.com/coupons/v2",
  }
}

Options

const options = { /* ... */ }
recurpolate(obj, options)

context

{Object} [=null]

If provided, serves as a fallback source for references when unresolved in object itself. Allows interpolating to values that aren't actually in the source/target object.

maxDepth

{Number} [=null]

Used to prevent long processing times or infinite loops due to circular references missed by the library.

reportUnresolved

{String} [='warn']

Reporting behavior when encountering a reference that resolves to an undefined value.

  • 'warn', 'error', etc. - any method on console
  • 'throw' - throws an error when
  • 'quiet' - no warnings

replaceUnresolved

{String} [='empty']

Replacement behavior when encountering a reference that resolves to an undefined value.

  • 'empty' - removes the references from the strings, replacing with empty string
  • 'keep' - keeps the references in the strings, e.g. ${some.undefined.value}