GithubHelp home page GithubHelp logo

brunos3d / expand-obj Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 59 KB

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

Home Page: https://npmjs.com/package/expand-obj

License: MIT License

JavaScript 20.17% TypeScript 79.83%
javascript object expand multi-keys package

expand-obj's Introduction

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] }

expand-obj's People

Contributors

brunos3d avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.