@g2crowd/extract-options

1.3.1 • Public • Published

extract-options

This package is responsible for extracting a subset of a hash, based on a namespaced key.

It is useful for reading options out of DOM data attributes, where there may be multiple keys with different concerns.

Installation

npm install @g2crowd/extract-options --save

Usage

This will create a new hash, seperating out keys based on a common prefix:

import { extractOptions } from '@g2crowd/extract-options';
const startingHash = { prefixOne: 1, prefixTwo: 2, otherKey: 3 };

extractOptions(startingHash, 'prefix'); //=> { one: 1, two: 2 }

It will treat keys with an Options suffix as special. It will expect a hash of raw options to follow:

const startingHash = { prefixOne: 1, prefixTwo: 2, otherKey: 3, prefixOptions: { three: 3 } };

extractOptions(startingHash, 'prefix'); //=> { one: 1, two: 2, three: 3 }

Pairs nicely with jQuery.data:

<div id="element" data-prefix-one="1" data-prefix-two="2" data-other-key="3"></div>
let data = $('#element').data();
const options = extractOptions(data, 'prefix'); //=> { one: 1, two: 2 }

Dependents (1)

Package Sidebar

Install

npm i @g2crowd/extract-options

Weekly Downloads

332

Version

1.3.1

License

MIT

Unpacked Size

3.66 kB

Total Files

7

Last publish

Collaborators

  • g2-vincecarollo
  • vgoyette
  • ekarpov-g2
  • ndibek
  • wheeyls
  • hasghari