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

expand-obj

v2.0.2

Published

📦 NODE.JS - Create a multi-key object with the same/multiple values by passing just a string or array.

Downloads

3

Readme

expand-obj

📦 NODE.JS - Create a multi-key object with the same/multiple values by passing just a string or array.

npm version npm downloads Build Status Hackage-Deps Visitors

Installation

npm install expand-obj --save

or

yarn add expand-obj

Importation

commonjs

const expand = require('expand-obj');

or ES6 (export default)

import expand from 'expand-obj';

or ES6 (named export)

import { expand } from 'expand-obj';

How to use

Basically you just need to enter an object that contains one or more keys separated by some character or an array as a key.

const expand = require('expand-obj');

const foo = expand({
  ['a, b, c']: 123,
});

console.log(foo); // result: { a: 123, b: 123, c: 123 }

With options

const foo = expand(
  {
    ['a, b, c']: [1, 2, 3],
  },
  { splitValues: true }
);

console.log(foo); // result: { a: 1, b: 2, c: 3 }

Resolve functions

const foo = await expand({
  ['a, b, c']: async (val: string) => `test=${val}`,
});

console.log(foo); // result: { a: `test=a`, b: `test=b`, c: `test=c` }

React example

const expand = require('expand-obj');

const styled = await expand({
    'h1, h2, p, span': { fontSize: '2rem', fontWeight: 'bold' },
    'roundedBorder, cardBorder, buttonBorder': { borderRadius: '7px' },
    'span': { fontStyle: 'italic' },
});

<span style={styled.span}>Font Size 2rem and Italic</span>
<SomeReactComponent style={{...styled.h1, ...styled.roundedBorder}} />

Options

By default these are the configuration options

export type ExpandOptions = {
  separator?: string, // default: ','
  splitValues?: boolean, // default: false
  deleteRawKey?: boolean, // default: true
  trimSpaces?: boolean, // default: true
  tryJoinRepeatedKeys?: boolean, // default: true
  resolveFuncs?: boolean, // default: true
  useSubkeyAsParams?: boolean, // default: true
};

separator (default = ",")

define the subkey separator

const options = {
  separator: '|',
};

const obj = await expand(
  {
    'a, b, c': 123,
  },
  options
);

console.log(obj); // result: { a: 123, b: 123, c: 123 }

splitValues (default = false)

when true, spreads the values if the property value is of type array

const options = {
  splitValues: true,
};

const obj = await expand(
  {
    'a, b, c': 123,
    'h, i, j': [4, 5, 6],
    'x, y, z': [7, 8],
  },
  options
);

console.log(obj); // result: { a: 123, b: 123, c: 123, h: 4, i: 5, j: 6, x: 7, y: 8, z: 8 }
// obs: note that the spread made uses the index of the
// current subkey in the property's key list
// and "z" repeats the last value in the array of values

deleteRawKey (default = true)

when false, prevents the raw key from being removed from object entries

const options = {
  deleteRawKey: false,
};

const obj = await expand(
  {
    'a, b, c': 123,
  },
  options
);

console.log(obj); // result: { 'a, b, c': 123, a: 123, b: 123, c: 123 }

trimSpaces (default = true)

when false, keep leading and trailing spaces

const options = {
  trimSpaces: false,
};

const obj = await expand(
  {
    'a, b, c': 123,
  },
  options
);

console.log(obj); // result: { a: 123, ' b': 123, ' c': 123 }

tryJoinRepeatedKeys (default = true)

when true, if the input object has a key that is equal to a subkey and both values are an array or an object the two values will be merged

const options = {
  tryJoinRepeatedKeys: false,
};

const obj = await expand(
  {
    'a, b, c': 123,
    'foo, bar': [4, 5, 6],
    foo: [789],
  },
  options
);

console.log(obj); // result: { a: 123, b: 123, c: 123, foo: [4, 5, 6, 789], bar: [4, 5, 6] }