objra
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

objra - Object.replaceAll()

Declarative replaceAll function for Objects

Installation

npm i objra

Usage

import objectReplaceAll from 'objra'

objectReplaceAll(regexp: string | RegExp, replacement: string, sourceObject: object)

Note: The replacement will be available for string[] in the near future.

Real World Usage

Convert nullish values in object to given string.

import objectReplaceAll from 'objra'

const response = fetch(/* ... */)
const data = response.json()

// Replace all null values with empty strings

const dataWithoutNulls = objectReplaceAll(null, '', data)

Replace wildcard values in object to given string.

import objectReplaceAll from 'objra'

const config = {
  './*': {
    types: './dist/*.d.ts',
    import: './dist/*.js',
  },
}

// This will be simplified in the near future.
const filenames = ['button', 'input', 'checkbox']
const resolvedArray = filenames.map((filename) =>
  objectReplaceAll('*', filename, config)
)

const result = Object.assign({}, ...resolvedArray)

// Output:
// {
//   './button': {
//     types: './dist/button.d.ts',
//     import: './dist/button.js',
//   },
//   './input': {
//     types: './dist/input.d.ts',
//     import: './dist/input.js',
//   },
//   './checkbox': {
//     types: './dist/checkbox.d.ts',
//     import: './dist/checkbox.js',
//   },
// }

Package Sidebar

Install

npm i objra

Weekly Downloads

3

Version

0.0.1

License

MIT

Unpacked Size

5.15 kB

Total Files

7

Last publish

Collaborators

  • devjiwonchoi