@fczbkk/value-source
v1.6.1
Published
Getter for values from various sources, with JSON-friendly config.
Downloads
16
Readme
Value Source
Getter for values from various sources, with JSON-friendly config.
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
- 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
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, defaultnull
)
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, defaultdefault_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
sources
SourcesList
Returns SourcesList
addSources
Adds sources to existing ones.
Parameters
sources
SourcesList
Returns SourcesList
removeSources
Removes specific sources by paths.
Parameters
sources
SourcePathList (optional, default[]
)
Returns SourcesList
getValue
Applies getValueFromSource
on currently set sources.
Parameters
config
GetValueConfig
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
attribute_name
string
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 [email protected].
License
Value Source is published under the MIT license.