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

dynamapping

v1.6.0

Published

replaces keys dynamically within the values for a raw, flat object

Downloads

31

Readme

A function that replaces keys from dynamically specified objects within the values for a raw object.

dynamapping

codecov

Version Downloads/week License

Geenee Template

:clipboard: Why

You may want to update objects retrieved from a static source (such as a file) using a session with dynamically set values. It's a pain to set up the logic of mapping through session data and calling replace.

:white_check_mark: What

A single function that you can pass in a flat object to and replace strings in the object values with your session values.

Your replacements (the values in your session object) can include a string, a number, a boolean, or a JSON-stringified object. dynomapping will determine the intended type as follows:

  1. A boolean or a string of a boolean will be converted to a boolean unless it is contained in a larger string
  2. Similar treatment is done for valid number strings or JSON stringifications.

:bulb: How

Include the function:

npm i dynamapping

You can insert into the values for keys of a given object a string in the form __session.<key>__. Then, dynamically set the session object to have a value that will be replaced when you call dynamapping. For instance,

const dynamapping = require('dynamapping')
const session = {
		userName: 'Filbert'
	}
let obj = {
		hello: 'hi __session.userName__'
	};
obj = dynamapping( obj, session, {})

// obj will be set to { hello: 'hi Filbert' }

Notes:

  1. object currently needs to be of depth 1 (no recursion is currently implemented.)
  2. You can currently use one of two mappings: session and answer. (The session object is so named to be generic. The answer object can be useful for an interactive session, for instance using inquirer).
  3. if you set a value in session (or answers) to 'true' or 'false' then dynamapping will assume that you intended the boolean value and will return the boolean true or false respectively. For instance:
  const obj = {
		testCase: '__session.isTrue__'
	};
  const sessionObj = {
		userName: 'Filbert',
		isTrue: 'false'
	}
  obj replaceGlobalValuesInObject(obj, sessionObj, ansObj)
  // obj = {testCase: false} rather than {testCase: 'false'}

But if it's embedded within a larger string, it will remain a string e.g. 'it is __session.isTrue__ now! 4.Numbers are treated the same way. If you have a key set to a single number, then it will remain a number.