@m00nbyte/data-extractor
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

data-extractor

npm version npm downloads


Description

Effortlessly extract multiple keys from deeply nested structures within an object, resulting in a reduced, flattened output.

Installation

npm install -D @m00nbyte/data-extractor
yarn add -D @m00nbyte/data-extractor

Usage

import extractData from '@m00nbyte/data-extractor';

const sourceObj = {
    level1: {
        '- quoted key -': 'yes',
        level2: {
            id: 'bc04be50',
            level3: {
                test: 'something',
                level4: {
                    arr: ['hello', [123, { test: true }]]
                }
            }
        }
    }
};

const pathMap = {
    quoted: 'level1.- quoted key -',
    id: 'level1.level2.id',
    test: 'level1.level2.level3.test',
    arr_0: 'level1.level2.level3.level4.arr[0]',
    arr_1_0: 'level1.level2.level3.level4.arr[1][0]',
    test2: 'level1.level2.level3.level4.arr[1][1].test',
    almeria: 'level1.level2.level3.level4.level5.almeria'
};

// optional
const defaultValue = 'not-found';

const extractedKeys = extractData(sourceObj, pathMap, defaultValue);

if (extractedKeys.id && extractedKeys.test) {
    // do something
}

Parameters

sourceObject

Type: Object

The source object to extract data from.

pathMap

Type: Object

An object mapping property names to their paths in the source object.

defaultValue

Type: String|undefined

Default: undefined

An optional default value to return if a key does not exist in the source object.

Contribution

Feel free to submit issues or pull requests.

Like my work?

This project needs a ⭐ from you. Don't forget to leave a star.

Buy Me A Coffee

Package Sidebar

Install

npm i @m00nbyte/data-extractor

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

8.47 kB

Total Files

12

Last publish

Collaborators

  • m00nbyte