Gathering detailed insights and metrics for data-observer
Gathering detailed insights and metrics for data-observer
Gathering detailed insights and metrics for data-observer
Gathering detailed insights and metrics for data-observer
npm install data-observer
Typescript
Module System
Node Version
NPM Version
66
Supply Chain
96.5
Quality
75.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
6,474
Last Day
2
Last Week
4
Last Month
9
Last Year
162
MIT License
2 Stars
31 Commits
3 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Apr 15, 2022
Minified
Minified + Gzipped
Latest Version
0.0.3
Package Id
data-observer@0.0.3
Size
14.97 kB
NPM Version
2.5.1
Node Version
0.12.0
Published on
Jul 13, 2015
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
0%
4
Compared to previous week
Last Month
-30.8%
9
Compared to previous month
Last Year
47.3%
162
Compared to previous year
This library wraps Object.observe
and Array.observe
to make them more usable. Warning: This library has not yet been tested in production.
To observe an object, pass it to the observe
function to return a scalar observation.
1import observe from 'data-observer'; 2 3let object = {a: 1, b: 2, c: 3}; 4 5// observe a property 6let observation = observe(object, 'a') 7observation.getValue(); // => 1 8observation.onDidChangeValue((a) => { // called when `object.a` changes 9 console.log('a changed:', a) 10}); 11 12// observe multiple properties 13// pass a function to combine them into a single value 14let observation = observe(object, 'a', 'b', 'c', (a, b, c) => a + b + c) 15observation.getValue(); // => 6 16observation.onDidChangeValue((value) => { // called when `object.a`, `.b`, or `.c` change 17 console.log('a, b, or c changed:', value) 18}); 19 20// observe the whole object 21let observation = observe(object); 22observation.getValue() // object 23let disposable = observation.onDidChangeValue((object) => { // called when object changes 24 console.log('object changed', object); 25})
This library is much better than the raw Array.observe
API, because it efficiently coalesces the overlapping change events delivered by the raw API into a minimal list of non-overlapping changes. If you apply these changes to a copy of the original array in order, it will equal the current value of the observed array.
1let array = ['a', 'b', 'c', 'd', 'e']; 2let original = array.slice(); 3 4observe(array).onDidChangeValues(changes => { // called when array changes 5 for (let {index, removedCount, added} of changes) { 6 original.splice(index, removedCount, ...added); 7 } 8});
The observations are returned from the observe
function support a map
operation, which maps a transformation function over the current value(s) of the observation. Here we map value => value + 1
over an array observation.
1let array = [1, 2, 3]; 2let observation = observe(array); 3let mappedObservation = observation.map(value => value + 1); 4mappedObservation.getValues(); // => [2, 3, 4]
The transform will also be applied to all added
values passed to onDidChangeValues
listeners.
Mapping over scalar observations is similar:
let object = {a: 'hello'};
let observation = observe(object, 'a');
let mappedObservation = observation.map(value => value.toUpperCase());
mappedObservation.getValue(); // => 'HELLO';
You can chain calls to map
if you're so inclined:
1let doubleMapObservation = 2 observation.map(value => value + 1).map(value => value * 6);
You can also apply a map
operation by passing a transform function as the last argument to observe
:
1let observation = observe(array, value => value + 1);
observe(object, [...propertyNames], [transformFunction])
Observe an entire object or specific properties on an object.
object
The object to observe....propertyNames
The names of one or more properties to observe. If more than one property name is provided, you must supply a transformFunction
to combine them.transformationFunction
An optional function taking one or more observed values as arguments that returns a single combined value for the observation. This argument is required if more than one property name is supplied.Returns a scalar observation.
observe(array, [transformFunction])
Observe an array.
array
The array to observe.transformFunction
An optional function transforming the values of the observed array.Returns an array observation.
These are returned when observing an object or an object's fields. Scalar observations represent a single value that changes over time.
getValue()
Get the current value of the observation.
onDidChangeValue(fn)
Subscribe to changes to the observation's value.
fn
A function that will be called with the current value of the observation whenever it changes.Returns a Disposable
on which .dispose()
can be called to cancel the subscription.
map(transformFn)
Build a new observation based on applying the given transform function to the this observation's value.
transformFn
A function that transforms the observation's value.Returns a new scalar observation.
getValues()
Get the current values of the observed array.
onDidChangeValues(fn)
Subscribe to changes to the observed array. Applying the changes passed to the listener function to a copy of the array as it existed before being changed in order via splice
should update the copy to match the current state of the array.
fn
A function that will be called with an array of changes whenever the observed array changes.
changes
An Array
of change objects with the following keys:
index
Where the change starts.removedCount
The number of elements that were removed.added
The elements that were added.Returns a Disposable
on which .dispose()
can be called to cancel the subscription.
map(transformFn)
Build a new observation based on applying the given transform function to the this observation's values.
transformFn
A function that transforms the observation's values.Returns a new array observation.
I built this library so I can use it from a view framework that I haven't finished writing yet. It will combine virtual-DOM diffing with data-model observation. Stay tuned.
Array observations could be extended with relational methods, such as filter
, join
, flatMap
, etc. This would enable us to use the ideas of functional relational programming described in Out of the Tarpit (PDF) while still using simple JavaScript primitives. Scalar observations could similarly be extended with an API reminiscent of Rx.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-04-21
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More