Getter for values from various sources, with JSON-friendly config.
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']);
- SourcePath
- getValueFromSource
- SourcesList
- GetValueConfig
- SourcePathList
- constructSourceGetter
- getSources
- setSources
- addSources
- removeSources
- getValue
- default_sources
- direct
- global
- current_url
- frame_name
- frame_id
- frame_depth
- frame_element
- frame_attribute
- window_focus
- element
- element_content
- element_value
- element_checked
Dot separated path to a property inside SourcesList.
Type: string
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, defaultnull
)
Returns any
Object containing getter functions. The keys are names, the values are either functions or other SourcesList objects.
Type: Object
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.
Single path or list of paths to sources properties.
Type: (SourcePath | Array<SourcePath>)
Constructs object with methods for working with sources and getValue
method to execute getValueFromSource
on them.
Parameters
sources
SourcesList (optional, defaultdefault_sources
)
Returns {getSources: getSources, setSources: setSources, addSources: addSources, removeSources: removeSources, getValue: getValue}
Returns currently set sources list.
Returns SourcesList
Replaces existing sources with new ones.
Parameters
sources
SourcesList
Returns SourcesList
Adds sources to existing ones.
Parameters
sources
SourcesList
Returns SourcesList
Removes specific sources by paths.
Parameters
sources
SourcePathList (optional, default[]
)
Returns SourcesList
Applies getValueFromSource
on currently set sources.
Parameters
config
GetValueConfig
Returns any
Source of predefined source getters.
Directly returns unchanged value.
Parameters
value
any
Returns any
Finds value in global namespace.
Parameters
path
string Dot separated path to value in global namespace. (optional, default''
)parameters
Array (optional, default[]
)
Returns any
Returns full URL of current document.
Returns string
Returns frame name.
Returns string
Returns ID of parent frame element, or null
if ID is not set or there's no parent frame.
Returns (null | string)
Returns number of parent frames above current document.
Returns number
Returns reference to a FRAME or IFRAME element.
Returns Object
Returns value of frame element's attribute. Returns null
if it is not possible.
Parameters
attribute_name
string
Returns (string | null)
Returns true
if current window (or any of its frames) currently has focus.
Returns boolean
Returns single matching element or null
if no matching element is found.
Parameters
selector
any
Returns (Element | null)
Returns text content of an element and all its children. Returns null
if no matching element is found.
Parameters
selector
any
Returns (string | null)
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)
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)
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 [email protected].
Value Source is published under the MIT license.