Skip to content

Getter for values from various sources, with JSON-friendly config.

License

Notifications You must be signed in to change notification settings

InlineManual/value-source

Repository files navigation

Value Source

Getter for values from various sources, with JSON-friendly config.

npm npm David Travis

How to use

Install the library via NPM:

npm install @fczbkk/value-source --save

Then use in your project like this:

import {constructSourceGetter} from '@fczbkk/value-source';

// create getter with default sources 
const my_sources = constructSourceGetter();

// get simple value
my_sources.getValue({source: 'direct', parameters: ['aaa']}); // "aaa"

// get value of global property
window.aaa = {bbb: 'ccc'};
my_sources.getValue({source: 'global', parameters: ['aaa.bbb']}); // "ccc"

// get element
my_sources.getValue({source: 'element', parameters: ['body']}); // document.body

You can set your own sources:

my_sources.addSources({
  square: function (input) {return input * input;}
});

my_sources.getValue({source: 'square', parameters: [2]}); // 4

You can remove sources:

my_sources.removeSources(['square']);

Documentation

Table of Contents

SourcePath

Dot separated path to a property inside SourcesList.

Type: string

getValueFromSource

Returns value from selected source(s).

Parameters

  • sources SourcesList (optional, default {})
  • config GetValueConfig (optional, default {})
    • config.source (optional, default [])
    • config.parameters (optional, default [])
    • config.default_value (optional, default null)

Returns any

SourcesList

Object containing getter functions. The keys are names, the values are either functions or other SourcesList objects.

Type: Object

GetValueConfig

Type: Object

Properties

  • source SourcePathList First non-null value returned by getters will be returned.
  • parameters (any | Array) List of parameters to be used when calling source getters. Single item of any other type than Array will be converted to Array.
  • default_value any? Value to be returned when sources do not produce any non-null value.

SourcePathList

Single path or list of paths to sources properties.

Type: (SourcePath | Array<SourcePath>)

constructSourceGetter

Constructs object with methods for working with sources and getValue method to execute getValueFromSource on them.

Parameters

  • sources SourcesList (optional, default default_sources)

Returns {getSources: getSources, setSources: setSources, addSources: addSources, removeSources: removeSources, getValue: getValue}

getSources

Returns currently set sources list.

Returns SourcesList

setSources

Replaces existing sources with new ones.

Parameters

Returns SourcesList

addSources

Adds sources to existing ones.

Parameters

Returns SourcesList

removeSources

Removes specific sources by paths.

Parameters

Returns SourcesList

getValue

Applies getValueFromSource on currently set sources.

Parameters

Returns any

default_sources

Source of predefined source getters.

direct

Directly returns unchanged value.

Parameters

  • value any

Returns any

global

Finds value in global namespace.

Parameters

  • path string Dot separated path to value in global namespace. (optional, default '')
  • parameters Array (optional, default [])

Returns any

current_url

Returns full URL of current document.

Returns string

frame_name

Returns frame name.

Returns string

frame_id

Returns ID of parent frame element, or null if ID is not set or there's no parent frame.

Returns (null | string)

frame_depth

Returns number of parent frames above current document.

Returns number

frame_element

Returns reference to a FRAME or IFRAME element.

Returns Object

frame_attribute

Returns value of frame element's attribute. Returns null if it is not possible.

Parameters

Returns (string | null)

window_focus

Returns true if current window (or any of its frames) currently has focus.

Returns boolean

element

Returns single matching element or null if no matching element is found.

Parameters

  • selector any

Returns (Element | null)

element_content

Returns text content of an element and all its children. Returns null if no matching element is found.

Parameters

  • selector any

Returns (string | null)

element_value

Returns value of an element (should be form field). Form fields that can hold multiple values (e.g. multi select, checkboxes) return array of values. Returns null if no matching element is found.

Parameters

  • selector any

Returns (Array | string | null)

element_checked

Returns true if element is checked, otherwise false. Works only on CHECKBOX and RADIO inputs. All other matching elements will return null.

Parameters

  • selector any

Returns (boolean | null)

Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at riki@fczbkk.com.

License

Value Source is published under the MIT license.

About

Getter for values from various sources, with JSON-friendly config.

Resources

License

Stars

Watchers

Forks

Packages

No packages published