npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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.

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 [email protected].

License

Value Source is published under the MIT license.